use consistent wrappers around clz/ffs

pull/187/head
Daniel Micay 2022-01-16 15:39:59 -05:00
parent 86f9c739ee
commit d8cb2d9f7a
2 changed files with 17 additions and 13 deletions

View File

@ -374,7 +374,7 @@ static size_t get_free_slot(struct random_state *rng, size_t slots, const struct
} }
if (masked != ~0UL) { 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; 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) { 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); 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 #if SLAB_QUARANTINE_RANDOM_LENGTH > 0
size_t slab_quarantine_random_length = SLAB_QUARANTINE_RANDOM_LENGTH << quarantine_shift; 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] // 1 MiB [5 MiB, 6 MiB, 7 MiB, 8 MiB]
// etc. // etc.
size = max(size, (size_t)PAGE_SIZE); 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; size_t spacing_class = 1ULL << spacing_shift;
return align(size, spacing_class); 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 SLAB_QUARANTINE && CONFIG_EXTENDED_SIZE_CLASSES
if (size >= min_extended_size_class) { 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 #if SLAB_QUARANTINE_RANDOM_LENGTH > 0
size_t slab_quarantine_random_length = SLAB_QUARANTINE_RANDOM_LENGTH << quarantine_shift; size_t slab_quarantine_random_length = SLAB_QUARANTINE_RANDOM_LENGTH << quarantine_shift;

20
util.h
View File

@ -29,8 +29,18 @@
#define STRINGIFY(s) #s #define STRINGIFY(s) #s
#define ALIAS(f) __attribute__((alias(STRINGIFY(f)))) #define ALIAS(f) __attribute__((alias(STRINGIFY(f))))
static inline int ffzl(unsigned long x) { typedef uint8_t u8;
return __builtin_ffsl(~x); 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) { 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); 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 #define U64_WIDTH 64
#if CONFIG_SEAL_METADATA #if CONFIG_SEAL_METADATA