From d8cb2d9f7a1f132e1c45d6578f3f4d124ec0ebef Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Sun, 16 Jan 2022 15:39:59 -0500 Subject: [PATCH] use consistent wrappers around clz/ffs --- h_malloc.c | 10 +++++----- util.h | 20 ++++++++++++-------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/h_malloc.c b/h_malloc.c index 865c73d..1e303d9 100644 --- a/h_malloc.c +++ b/h_malloc.c @@ -374,7 +374,7 @@ static size_t get_free_slot(struct random_state *rng, size_t slots, const struct } if (masked != ~0UL) { - return ffzl(masked) - 1 + i * U64_WIDTH; + return ffz64(masked) - 1 + i * U64_WIDTH; } i = i == (slots - 1) / U64_WIDTH ? 0 : i + 1; @@ -388,7 +388,7 @@ static size_t get_free_slot(struct random_state *rng, size_t slots, const struct } if (masked != ~0UL) { - return ffzl(masked) - 1 + i * U64_WIDTH; + return ffz64(masked) - 1 + i * U64_WIDTH; } } } @@ -703,7 +703,7 @@ static inline void deallocate_small(void *p, const size_t *expected_size) { set_quarantine_slot(metadata, slot); - size_t quarantine_shift = __builtin_clzl(size) - (63 - MAX_SLAB_SIZE_CLASS_SHIFT); + size_t quarantine_shift = clz64(size) - (63 - MAX_SLAB_SIZE_CLASS_SHIFT); #if SLAB_QUARANTINE_RANDOM_LENGTH > 0 size_t slab_quarantine_random_length = SLAB_QUARANTINE_RANDOM_LENGTH << quarantine_shift; @@ -1200,7 +1200,7 @@ static size_t get_large_size_class(size_t size) { // 1 MiB [5 MiB, 6 MiB, 7 MiB, 8 MiB] // etc. size = max(size, (size_t)PAGE_SIZE); - size_t spacing_shift = U64_WIDTH - __builtin_clzl(size - 1) - 3; + size_t spacing_shift = U64_WIDTH - clz64(size - 1) - 3; size_t spacing_class = 1ULL << spacing_shift; return align(size, spacing_class); } @@ -1766,7 +1766,7 @@ EXPORT int h_malloc_trim(UNUSED size_t pad) { #if SLAB_QUARANTINE && CONFIG_EXTENDED_SIZE_CLASSES if (size >= min_extended_size_class) { - size_t quarantine_shift = __builtin_clzl(size) - (63 - MAX_SLAB_SIZE_CLASS_SHIFT); + size_t quarantine_shift = clz64(size) - (63 - MAX_SLAB_SIZE_CLASS_SHIFT); #if SLAB_QUARANTINE_RANDOM_LENGTH > 0 size_t slab_quarantine_random_length = SLAB_QUARANTINE_RANDOM_LENGTH << quarantine_shift; diff --git a/util.h b/util.h index f0f2058..7ec6bf3 100644 --- a/util.h +++ b/util.h @@ -29,8 +29,18 @@ #define STRINGIFY(s) #s #define ALIAS(f) __attribute__((alias(STRINGIFY(f)))) -static inline int ffzl(unsigned long x) { - return __builtin_ffsl(~x); +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; +typedef unsigned __int128 u128; + +static inline int clz64(u64 x) { + return __builtin_clzll(x); +} + +static inline int ffz64(u64 x) { + return __builtin_ffsll(~x); } static inline size_t align(size_t size, size_t align) { @@ -40,12 +50,6 @@ static inline size_t align(size_t size, size_t align) { COLD noreturn void fatal_error(const char *s); -typedef uint8_t u8; -typedef uint16_t u16; -typedef uint32_t u32; -typedef uint64_t u64; -typedef unsigned __int128 u128; - #define U64_WIDTH 64 #if CONFIG_SEAL_METADATA