0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-18 09:58:22 +02:00

ircd::fs: Support more exotic alignments for memory mapped files.

This commit is contained in:
Jason Volk 2022-02-23 17:10:20 -08:00
parent b3dc9a9ac3
commit 6843d7a98d
3 changed files with 38 additions and 7 deletions

View file

@ -1189,6 +1189,7 @@ AC_CHECK_FUNCS([ \
mlock2 \ mlock2 \
mmap \ mmap \
mprotect \ mprotect \
mremap \
msync \ msync \
posix_fadvise \ posix_fadvise \
posix_madvise \ posix_madvise \

View file

@ -46,6 +46,7 @@ struct ircd::fs::map
struct ircd::fs::map::opts struct ircd::fs::map::opts
:fd::opts :fd::opts
{ {
uint alignment {0};
bool execute {false}; bool execute {false};
bool shared {false}; bool shared {false};
bool reserve {false}; bool reserve {false};

View file

@ -2047,25 +2047,51 @@ ircd::fs::map::default_opts;
ircd::fs::map::map(const fd &fd, ircd::fs::map::map(const fd &fd,
const opts &opts, const opts &opts,
const size_t &size) const size_t &size)
#if defined(HAVE_MMAP)
{ {
const auto map_size const auto map_size
{ {
size?: fs::size(fd) size?: fs::size(fd)
}; };
#ifdef HAVE_MMAP void *ptr
void *const &ptr
{ {
::mmap(nullptr, map_size, prot(opts), flags(opts), int(fd), opts.offset) ::mmap
(
nullptr,
map_size,
prot(opts),
flags(opts),
int(fd),
opts.offset
)
}; };
#else
#error "Missing ::mmap(2) on this platform."
#endif
if(unlikely(ptr == MAP_FAILED)) if(unlikely(ptr == MAP_FAILED))
throw_system_error(errno); throw_system_error(errno);
static_cast<mutable_buffer &>(*this) = mutable_buffer #if defined(HAVE_MREMAP)
if(opts.alignment && !aligned(ptr, opts.alignment))
{
assert(opts.alignment > 1);
assert(opts.alignment > info::page_size);
ptr = ::mremap
(
ptr,
map_size,
map_size,
MREMAP_FIXED | MREMAP_MAYMOVE,
align_up(ptr, opts.alignment) + pad_to(map_size, opts.alignment)
);
if(unlikely(ptr == MAP_FAILED))
throw_system_error(errno);
}
#endif
assert(aligned(ptr, opts.alignment));
assert(padded(map_size, opts.alignment));
static_cast<mutable_buffer &>(*this) =
{ {
reinterpret_cast<char *>(ptr), reinterpret_cast<char *>(ptr),
map_size map_size
@ -2084,6 +2110,9 @@ ircd::fs::map::map(const fd &fd,
if(advise) if(advise)
fs::advise(*this, advise, map_size); fs::advise(*this, advise, map_size);
} }
#else
#error "Missing mmap(2) on this platform."
#endif
ircd::fs::map::~map() ircd::fs::map::~map()
noexcept try noexcept try