calculate slab size class instead of array loop
This commit is contained in:
parent
d8cb2d9f7a
commit
81cf2f27a0
22
h_malloc.c
22
h_malloc.c
@ -196,13 +196,16 @@ static inline struct size_info get_size_info(size_t size) {
|
|||||||
if (size <= 128) {
|
if (size <= 128) {
|
||||||
return (struct size_info){align(size, 16), ((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];
|
const size_t initial_spacing_multiplier = 5;
|
||||||
if (size <= real_size) {
|
const size_t special_small_sizes = 5; // 0, 16, 32, 48, 64
|
||||||
return (struct size_info){real_size, class};
|
|
||||||
}
|
size_t spacing_class_shift = log2u64(size - 1) - 2;
|
||||||
}
|
size_t spacing_class = 1ULL << spacing_class_shift;
|
||||||
fatal_error("invalid size for slabs");
|
size_t real_size = align(size, spacing_class);
|
||||||
|
size_t spacing_class_index = (real_size >> spacing_class_shift) - initial_spacing_multiplier;
|
||||||
|
size_t index = (spacing_class_shift - 4) * 4 + special_small_sizes + spacing_class_index;
|
||||||
|
return (struct size_info){real_size, index};
|
||||||
}
|
}
|
||||||
|
|
||||||
// alignment must be a power of 2 <= PAGE_SIZE since slabs are only page aligned
|
// alignment must be a power of 2 <= PAGE_SIZE since slabs are only page aligned
|
||||||
@ -1199,10 +1202,7 @@ static size_t get_large_size_class(size_t size) {
|
|||||||
// 512 KiB [2560 KiB, 3 MiB, 3584 KiB, 4 MiB]
|
// 512 KiB [2560 KiB, 3 MiB, 3584 KiB, 4 MiB]
|
||||||
// 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);
|
return get_size_info(max(size, (size_t)PAGE_SIZE)).size;
|
||||||
size_t spacing_shift = U64_WIDTH - clz64(size - 1) - 3;
|
|
||||||
size_t spacing_class = 1ULL << spacing_shift;
|
|
||||||
return align(size, spacing_class);
|
|
||||||
}
|
}
|
||||||
return page_align(size);
|
return page_align(size);
|
||||||
}
|
}
|
||||||
|
8
util.h
8
util.h
@ -35,6 +35,8 @@ typedef uint32_t u32;
|
|||||||
typedef uint64_t u64;
|
typedef uint64_t u64;
|
||||||
typedef unsigned __int128 u128;
|
typedef unsigned __int128 u128;
|
||||||
|
|
||||||
|
#define U64_WIDTH 64
|
||||||
|
|
||||||
static inline int clz64(u64 x) {
|
static inline int clz64(u64 x) {
|
||||||
return __builtin_clzll(x);
|
return __builtin_clzll(x);
|
||||||
}
|
}
|
||||||
@ -43,6 +45,10 @@ static inline int ffz64(u64 x) {
|
|||||||
return __builtin_ffsll(~x);
|
return __builtin_ffsll(~x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline u64 log2u64(u64 x) {
|
||||||
|
return U64_WIDTH - clz64(x) - 1;
|
||||||
|
}
|
||||||
|
|
||||||
static inline size_t align(size_t size, size_t align) {
|
static inline size_t align(size_t size, size_t align) {
|
||||||
size_t mask = align - 1;
|
size_t mask = align - 1;
|
||||||
return (size + mask) & ~mask;
|
return (size + mask) & ~mask;
|
||||||
@ -50,8 +56,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);
|
||||||
|
|
||||||
#define U64_WIDTH 64
|
|
||||||
|
|
||||||
#if CONFIG_SEAL_METADATA
|
#if CONFIG_SEAL_METADATA
|
||||||
|
|
||||||
#ifdef __GLIBC__
|
#ifdef __GLIBC__
|
||||||
|
Loading…
Reference in New Issue
Block a user