mirror of
https://github.com/matrix-construct/construct
synced 2024-11-29 10:12:39 +01:00
ircd::buffer: Move abstract align/padding tools to ircd::util.
This commit is contained in:
parent
c92481475f
commit
2604f1370a
5 changed files with 148 additions and 135 deletions
|
@ -58,21 +58,6 @@ namespace ircd::buffer
|
|||
extern const mutable_buffer null_buffer;
|
||||
extern const ilist<mutable_buffer> null_buffers;
|
||||
|
||||
// Alignment constant expressions
|
||||
constexpr bool padded(const size_t, size_t alignment);
|
||||
constexpr bool aligned(const uintptr_t, size_t alignment);
|
||||
constexpr size_t padding(const size_t size, size_t alignment);
|
||||
constexpr size_t pad_to(const size_t size, const size_t alignment);
|
||||
constexpr uintptr_t align(uintptr_t, size_t alignment);
|
||||
constexpr uintptr_t align_up(uintptr_t, size_t alignment);
|
||||
|
||||
// Alignment inline tools
|
||||
bool aligned(const void *const &, const size_t &alignment);
|
||||
template<class T = char> const T *align(const void *const &, const size_t &alignment);
|
||||
template<class T = char> T *align(void *const &, const size_t &alignment);
|
||||
template<class T = char> const T *align_up(const void *const &, const size_t &alignment);
|
||||
template<class T = char> T *align_up(void *const &, const size_t &alignment);
|
||||
|
||||
// Single buffer iteration of contents
|
||||
template<class it> const it &begin(const buffer<it> &buffer);
|
||||
template<class it> const it &end(const buffer<it> &buffer);
|
||||
|
@ -140,13 +125,6 @@ namespace ircd
|
|||
using buffer::const_buffers;
|
||||
using buffer::mutable_buffers;
|
||||
|
||||
using buffer::aligned;
|
||||
using buffer::align;
|
||||
using buffer::align_up;
|
||||
using buffer::padded;
|
||||
using buffer::padding;
|
||||
using buffer::pad_to;
|
||||
|
||||
using buffer::size;
|
||||
using buffer::data;
|
||||
using buffer::copy;
|
||||
|
@ -154,6 +132,9 @@ namespace ircd
|
|||
using buffer::consume;
|
||||
using buffer::begin;
|
||||
using buffer::end;
|
||||
|
||||
using buffer::aligned;
|
||||
using buffer::padded;
|
||||
}
|
||||
|
||||
template<class it>
|
||||
|
@ -266,7 +247,7 @@ ircd::buffer::aligned(const buffer<it> &buffer,
|
|||
const size_t &a)
|
||||
{
|
||||
return likely(a)?
|
||||
aligned(data(buffer), a) && padded(buffer, a):
|
||||
ircd::aligned(data(buffer), a) && ircd::padded(buffer, a):
|
||||
true;
|
||||
}
|
||||
|
||||
|
@ -370,108 +351,3 @@ ircd::buffer::begin(const buffer<it> &buffer)
|
|||
{
|
||||
return get<0>(buffer);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
[[gnu::always_inline]]
|
||||
inline T *
|
||||
ircd::buffer::align(void *const &ptr,
|
||||
const size_t &alignment)
|
||||
{
|
||||
return reinterpret_cast<T *>
|
||||
(
|
||||
align(uintptr_t(ptr), alignment)
|
||||
);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
[[gnu::always_inline]]
|
||||
inline const T *
|
||||
ircd::buffer::align(const void *const &ptr,
|
||||
const size_t &alignment)
|
||||
{
|
||||
return reinterpret_cast<const T *>
|
||||
(
|
||||
align(uintptr_t(ptr), alignment)
|
||||
);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
[[gnu::always_inline]]
|
||||
inline T *
|
||||
ircd::buffer::align_up(void *const &ptr,
|
||||
const size_t &alignment)
|
||||
{
|
||||
return reinterpret_cast<T *>
|
||||
(
|
||||
align_up(uintptr_t(ptr), alignment)
|
||||
);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
[[gnu::always_inline]]
|
||||
inline const T *
|
||||
ircd::buffer::align_up(const void *const &ptr,
|
||||
const size_t &alignment)
|
||||
{
|
||||
return reinterpret_cast<const T *>
|
||||
(
|
||||
align_up(uintptr_t(ptr), alignment)
|
||||
);
|
||||
}
|
||||
|
||||
[[gnu::always_inline]]
|
||||
inline bool
|
||||
ircd::buffer::aligned(const void *const &ptr,
|
||||
const size_t &alignment)
|
||||
{
|
||||
return aligned(uintptr_t(ptr), alignment);
|
||||
}
|
||||
|
||||
constexpr uintptr_t
|
||||
ircd::buffer::align_up(uintptr_t ptr,
|
||||
size_t alignment)
|
||||
{
|
||||
alignment = std::max(alignment, 1UL);
|
||||
ptr += (alignment - (ptr % alignment)) % alignment;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
constexpr uintptr_t
|
||||
ircd::buffer::align(uintptr_t ptr,
|
||||
size_t alignment)
|
||||
{
|
||||
alignment = std::max(alignment, 1UL);
|
||||
ptr -= (ptr % alignment);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
constexpr size_t
|
||||
ircd::buffer::pad_to(const size_t size,
|
||||
const size_t alignment)
|
||||
{
|
||||
return size + padding(size, alignment);
|
||||
}
|
||||
|
||||
constexpr size_t
|
||||
ircd::buffer::padding(const size_t size,
|
||||
size_t alignment)
|
||||
{
|
||||
alignment = std::max(alignment, 1UL);
|
||||
return (alignment - (size % alignment)) % alignment;
|
||||
}
|
||||
|
||||
constexpr bool
|
||||
ircd::buffer::aligned(const uintptr_t ptr,
|
||||
size_t alignment)
|
||||
{
|
||||
alignment = std::max(alignment, 1UL);
|
||||
return ptr % alignment == 0;
|
||||
}
|
||||
|
||||
constexpr bool
|
||||
ircd::buffer::padded(const size_t size,
|
||||
size_t alignment)
|
||||
{
|
||||
alignment = std::max(alignment, 1UL);
|
||||
return size % alignment == 0;
|
||||
}
|
||||
|
|
|
@ -42,6 +42,7 @@
|
|||
|
||||
#include "util/assume.h"
|
||||
#include "util/mask.h"
|
||||
#include "util/align.h"
|
||||
#include "string_view.h"
|
||||
#include "vector_view.h"
|
||||
#include "byte_view.h"
|
||||
|
|
136
include/ircd/util/align.h
Normal file
136
include/ircd/util/align.h
Normal file
|
@ -0,0 +1,136 @@
|
|||
// Matrix Construct
|
||||
//
|
||||
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
||||
// Copyright (C) 2016-2022 Jason Volk <jason@zemos.net>
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice is present in all copies. The
|
||||
// full license for this software is available in the LICENSE file.
|
||||
|
||||
#pragma once
|
||||
#define HAVE_IRCD_UTIL_ALIGN_H
|
||||
|
||||
namespace ircd {
|
||||
inline namespace util
|
||||
{
|
||||
// Alignment constant expressions
|
||||
constexpr bool padded(const size_t, size_t alignment);
|
||||
constexpr bool aligned(const uintptr_t, size_t alignment);
|
||||
constexpr size_t padding(const size_t size, size_t alignment);
|
||||
constexpr size_t pad_to(const size_t size, const size_t alignment);
|
||||
constexpr uintptr_t align(uintptr_t, size_t alignment);
|
||||
constexpr uintptr_t align_up(uintptr_t, size_t alignment);
|
||||
|
||||
// Alignment inline tools
|
||||
bool aligned(const void *const &, const size_t &alignment);
|
||||
template<class T = char> const T *align(const void *const &, const size_t &alignment);
|
||||
template<class T = char> T *align(void *const &, const size_t &alignment);
|
||||
template<class T = char> const T *align_up(const void *const &, const size_t &alignment);
|
||||
template<class T = char> T *align_up(void *const &, const size_t &alignment);
|
||||
}}
|
||||
|
||||
template<class T>
|
||||
[[gnu::always_inline]]
|
||||
inline T *
|
||||
ircd::util::align(void *const &ptr,
|
||||
const size_t &alignment)
|
||||
{
|
||||
return reinterpret_cast<T *>
|
||||
(
|
||||
align(uintptr_t(ptr), alignment)
|
||||
);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
[[gnu::always_inline]]
|
||||
inline const T *
|
||||
ircd::util::align(const void *const &ptr,
|
||||
const size_t &alignment)
|
||||
{
|
||||
return reinterpret_cast<const T *>
|
||||
(
|
||||
align(uintptr_t(ptr), alignment)
|
||||
);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
[[gnu::always_inline]]
|
||||
inline T *
|
||||
ircd::util::align_up(void *const &ptr,
|
||||
const size_t &alignment)
|
||||
{
|
||||
return reinterpret_cast<T *>
|
||||
(
|
||||
align_up(uintptr_t(ptr), alignment)
|
||||
);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
[[gnu::always_inline]]
|
||||
inline const T *
|
||||
ircd::util::align_up(const void *const &ptr,
|
||||
const size_t &alignment)
|
||||
{
|
||||
return reinterpret_cast<const T *>
|
||||
(
|
||||
align_up(uintptr_t(ptr), alignment)
|
||||
);
|
||||
}
|
||||
|
||||
[[gnu::always_inline]]
|
||||
inline bool
|
||||
ircd::util::aligned(const void *const &ptr,
|
||||
const size_t &alignment)
|
||||
{
|
||||
return aligned(uintptr_t(ptr), alignment);
|
||||
}
|
||||
|
||||
constexpr uintptr_t
|
||||
ircd::util::align_up(uintptr_t ptr,
|
||||
size_t alignment)
|
||||
{
|
||||
alignment = std::max(alignment, 1UL);
|
||||
ptr += (alignment - (ptr % alignment)) % alignment;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
constexpr uintptr_t
|
||||
ircd::util::align(uintptr_t ptr,
|
||||
size_t alignment)
|
||||
{
|
||||
alignment = std::max(alignment, 1UL);
|
||||
ptr -= (ptr % alignment);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
constexpr size_t
|
||||
ircd::util::pad_to(const size_t size,
|
||||
const size_t alignment)
|
||||
{
|
||||
return size + padding(size, alignment);
|
||||
}
|
||||
|
||||
constexpr size_t
|
||||
ircd::util::padding(const size_t size,
|
||||
size_t alignment)
|
||||
{
|
||||
alignment = std::max(alignment, 1UL);
|
||||
return (alignment - (size % alignment)) % alignment;
|
||||
}
|
||||
|
||||
constexpr bool
|
||||
ircd::util::aligned(const uintptr_t ptr,
|
||||
size_t alignment)
|
||||
{
|
||||
alignment = std::max(alignment, 1UL);
|
||||
return ptr % alignment == 0;
|
||||
}
|
||||
|
||||
constexpr bool
|
||||
ircd::util::padded(const size_t size,
|
||||
size_t alignment)
|
||||
{
|
||||
alignment = std::max(alignment, 1UL);
|
||||
return size % alignment == 0;
|
||||
}
|
|
@ -300,12 +300,12 @@ ircd::allocator::incore(const const_buffer &buf)
|
|||
{
|
||||
const auto base
|
||||
{
|
||||
buffer::align(begin(buf), info::page_size)
|
||||
align(begin(buf), info::page_size)
|
||||
};
|
||||
|
||||
const auto top
|
||||
{
|
||||
buffer::align_up(end(buf), info::page_size)
|
||||
align_up(end(buf), info::page_size)
|
||||
};
|
||||
|
||||
assert(base <= data(buf));
|
||||
|
|
10
ircd/fs.cc
10
ircd/fs.cc
|
@ -828,7 +828,7 @@ ircd::fs::incore(const fd &fd,
|
|||
const read_opts &opts)
|
||||
{
|
||||
fs::map::opts map_opts;
|
||||
map_opts.offset = buffer::align(opts.offset, info::page_size);
|
||||
map_opts.offset = align(opts.offset, info::page_size);
|
||||
map_opts.blocking = false;
|
||||
const size_t &map_size
|
||||
{
|
||||
|
@ -1953,7 +1953,7 @@ ircd::fs::sync(const map &map,
|
|||
|
||||
const size_t offset
|
||||
{
|
||||
buffer::align(opts.offset, info::page_size)
|
||||
align(opts.offset, info::page_size)
|
||||
};
|
||||
|
||||
const mutable_buffer buf
|
||||
|
@ -1976,7 +1976,7 @@ ircd::fs::flush(const map &map,
|
|||
|
||||
const size_t offset
|
||||
{
|
||||
buffer::align(opts.offset, info::page_size)
|
||||
align(opts.offset, info::page_size)
|
||||
};
|
||||
|
||||
const mutable_buffer buf
|
||||
|
@ -1994,7 +1994,7 @@ ircd::fs::evict(const map &map,
|
|||
{
|
||||
const size_t offset
|
||||
{
|
||||
buffer::align(opts.offset, info::page_size)
|
||||
align(opts.offset, info::page_size)
|
||||
};
|
||||
|
||||
const mutable_buffer buf
|
||||
|
@ -2012,7 +2012,7 @@ ircd::fs::prefetch(const map &map,
|
|||
{
|
||||
const size_t offset
|
||||
{
|
||||
buffer::align(opts.offset, info::page_size)
|
||||
align(opts.offset, info::page_size)
|
||||
};
|
||||
|
||||
const mutable_buffer buf
|
||||
|
|
Loading…
Reference in a new issue