1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:37:44 +00:00

LibC: Implement posix_memalign(3) and aligned_alloc(3)

Some ports linked against posix_memalign, but didn't use it, and others
used it if it was Available. So I decided to implement posix_memalign.

My implementation adds almost no overhead to regular mallocs. However,
if an alignment is specified, it will use the smallest ChunkedBlock, for
which aligned chunks exist, and simply use one of the chunks that is
aligned. If it cannot use a ChunkedBlock, for size or alignment reasons,
it will use a BigAllocationBlock, and return a pointer to the first
aligned address past the start of the block. This implementation
supports alignments up to 32768, due to the limitations of the
BigAllocationBlock technique.
This commit is contained in:
Peter Elliott 2022-05-04 23:08:57 -06:00 committed by Andreas Kling
parent 830f5c610d
commit 4e0adb638d
4 changed files with 172 additions and 60 deletions

View file

@ -101,11 +101,7 @@ int posix_openpt(int flags);
int grantpt(int fd);
int unlockpt(int fd);
// FIXME: Remove the ifdef once we have a working memalign implementation.
// This is hidden by default until then because many applications prefer
// `posix_memalign` over other implementations of aligned memory.
#ifdef SERENITY_LIBC_SHOW_POSIX_MEMALIGN
int posix_memalign(void**, size_t alignment, size_t size);
#endif
__attribute__((malloc, alloc_size(2), alloc_align(1))) void* aligned_alloc(size_t alignment, size_t size);
__END_DECLS