diff --git a/h_malloc.c b/h_malloc.c index f03a74e..aed2078 100644 --- a/h_malloc.c +++ b/h_malloc.c @@ -194,7 +194,7 @@ static inline struct size_info get_size_info(size_t size) { return (struct size_info){0, 0}; } if (size <= 128) { - return (struct size_info){(size + 15) & ~15, ((size - 1) >> 4) + 1}; + return (struct size_info){align(size, 16), ((size - 1) >> 4) + 1}; } for (unsigned class = 9; class < N_SIZE_CLASSES; class++) { size_t real_size = size_classes[class]; @@ -217,7 +217,7 @@ static inline struct size_info get_size_info_align(size_t size, size_t alignment } static size_t get_slab_size(size_t slots, size_t size) { - return PAGE_CEILING(slots * size); + return page_align(slots * size); } struct __attribute__((aligned(CACHELINE_SIZE))) size_class { @@ -1202,9 +1202,9 @@ static size_t get_large_size_class(size_t size) { size = max(size, (size_t)PAGE_SIZE); size_t spacing_shift = 64 - __builtin_clzl(size - 1) - 3; size_t spacing_class = 1ULL << spacing_shift; - return (size + (spacing_class - 1)) & ~(spacing_class - 1); + return align(size, spacing_class); } - return PAGE_CEILING(size); + return page_align(size); } static size_t get_guard_size(struct random_state *state, size_t size) { @@ -1480,7 +1480,7 @@ EXPORT void *h_realloc(void *old, size_t size) { deallocate_pages(old, old_size, old_guard_size); } else { memory_unmap((char *)old - old_guard_size, old_guard_size); - memory_unmap((char *)old + PAGE_CEILING(old_size), old_guard_size); + memory_unmap((char *)old + page_align(old_size), old_guard_size); } thread_seal_metadata(); return new; @@ -1524,7 +1524,7 @@ EXPORT void *h_valloc(size_t size) { } EXPORT void *h_pvalloc(size_t size) { - size = PAGE_CEILING(size); + size = page_align(size); if (unlikely(!size)) { errno = ENOMEM; return NULL; diff --git a/pages.c b/pages.c index 505c884..27558de 100644 --- a/pages.c +++ b/pages.c @@ -9,10 +9,6 @@ static bool add_guards(size_t size, size_t guard_size, size_t *total_size) { __builtin_add_overflow(*total_size, guard_size, total_size); } -static uintptr_t alignment_ceiling(uintptr_t s, uintptr_t alignment) { - return ((s) + (alignment - 1)) & ((~alignment) + 1); -} - void *allocate_pages(size_t usable_size, size_t guard_size, bool unprotect, const char *name) { size_t real_size; if (unlikely(add_guards(usable_size, guard_size, &real_size))) { @@ -33,7 +29,7 @@ void *allocate_pages(size_t usable_size, size_t guard_size, bool unprotect, cons } void *allocate_pages_aligned(size_t usable_size, size_t alignment, size_t guard_size, const char *name) { - usable_size = PAGE_CEILING(usable_size); + usable_size = page_align(usable_size); if (unlikely(!usable_size)) { errno = ENOMEM; return NULL; @@ -59,7 +55,7 @@ void *allocate_pages_aligned(size_t usable_size, size_t alignment, size_t guard_ void *usable = (char *)real + guard_size; - size_t lead_size = alignment_ceiling((uintptr_t)usable, alignment) - (uintptr_t)usable; + size_t lead_size = align((uintptr_t)usable, alignment) - (uintptr_t)usable; size_t trail_size = alloc_size - lead_size - usable_size; void *base = (char *)usable + lead_size; diff --git a/pages.h b/pages.h index f0943cb..2874b48 100644 --- a/pages.h +++ b/pages.h @@ -5,11 +5,16 @@ #include #include +#include "util.h" + #define PAGE_SHIFT 12 #ifndef PAGE_SIZE #define PAGE_SIZE ((size_t)1 << PAGE_SHIFT) #endif -#define PAGE_CEILING(s) (((s) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)) + +static inline size_t page_align(size_t size) { + return align(size, PAGE_SIZE); +} void *allocate_pages(size_t usable_size, size_t guard_size, bool unprotect, const char *name); void *allocate_pages_aligned(size_t usable_size, size_t alignment, size_t guard_size, const char *name); diff --git a/util.h b/util.h index 4c7ac9f..6df2563 100644 --- a/util.h +++ b/util.h @@ -1,6 +1,7 @@ #ifndef UTIL_H #define UTIL_H +#include #include // C11 noreturn doesn't work in C++ @@ -32,6 +33,11 @@ static inline int ffzl(unsigned long x) { return __builtin_ffsl(~x); } +static inline size_t align(size_t size, size_t align) { + size_t mask = align - 1; + return (size + mask) & ~mask; +} + COLD noreturn void fatal_error(const char *s); typedef uint8_t u8;