alloc: optimize

This commit is contained in:
Matheus
2024-12-28 22:50:08 -03:00
parent d1ba5a94f4
commit 4ee5fa42ae
+14 -13
View File
@@ -16,7 +16,7 @@
#define CAPACITY_SIZE(cache) cache->size - sizeof(size_t) * 2 - USE_POISON * sizeof(size_t) #define CAPACITY_SIZE(cache) cache->size - sizeof(size_t) * 2 - USE_POISON * sizeof(size_t)
static size_t allocsizes[CACHE_COUNT] = {32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536}; static size_t allocsizes[CACHE_COUNT] = {32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536};
static scache_t *caches[CACHE_COUNT]; static scache_t *caches[CACHE_COUNT + 5];
static void initarea(scache_t *cache, void *obj) { static void initarea(scache_t *cache, void *obj) {
size_t *ptr = obj; size_t *ptr = obj;
@@ -35,15 +35,11 @@ static void dtor(scache_t *cache, void *obj) {
} }
static scache_t *getcachefromsize(size_t size) { static scache_t *getcachefromsize(size_t size) {
scache_t *cache = NULL; if (size == 0)
for (int i = 0; i < CACHE_COUNT; ++i) { return caches[0];
if (size <= allocsizes[i]) {
cache = caches[i]; size_t i = 64 - __builtin_clzll(size - 1);
break; return caches[i];
}
}
__assert(cache);
return cache;
} }
void *alloc(size_t size) { void *alloc(size_t size) {
@@ -51,7 +47,9 @@ void *alloc(size_t size) {
size_t *ret = slab_allocate(cache); size_t *ret = slab_allocate(cache);
if (ret == NULL) if (ret == NULL)
return NULL; return NULL;
__assert(*ret == CAPACITY_SIZE(cache)); #if USE_POISON == 1
__assert(*ret == CAPACITY_SIZE(cache));
#endif
*(ret + 1) = size; *(ret + 1) = size;
return ret + 2; return ret + 2;
} }
@@ -97,7 +95,10 @@ void *realloc(void *ptr, size_t size) {
void alloc_init() { void alloc_init() {
for (int i = 0; i < CACHE_COUNT; ++i) { for (int i = 0; i < CACHE_COUNT; ++i) {
caches[i] = slab_newcache(allocsizes[i] + sizeof(size_t) * 2 + sizeof(size_t) * USE_POISON, 0, initarea, dtor); caches[i + 5] = slab_newcache(allocsizes[i] + sizeof(size_t) * 2 + sizeof(size_t) * USE_POISON, 0, initarea, dtor);
__assert(caches[i]); __assert(caches[i + 5]);
} }
for (int i = 0; i < 5; ++i)
caches[i] = caches[5];
} }