mirror of
https://github.com/GrapheneOS/hardened_malloc.git
synced 2024-10-01 01:36:01 -04:00
use consistent wrappers around clz/ffs
This commit is contained in:
parent
86f9c739ee
commit
d8cb2d9f7a
10
h_malloc.c
10
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;
|
||||
|
20
util.h
20
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
|
||||
|
Loading…
Reference in New Issue
Block a user