aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2017-05-31 11:23:59 +0200
committerAlex Auvolat <alex@adnab.me>2017-05-31 11:23:59 +0200
commit6990cc1b54e6fd0a1a3ae71edb65ea53db3bd9a1 (patch)
treee5cd9f38a7fa62cd7e20055633d8e2e2902156ff
parentb74a9f0c4b73bc3b98ec6c4345d9aaf4b6279f7a (diff)
downloadkogata-6990cc1b54e6fd0a1a3ae71edb65ea53db3bd9a1.tar.gz
kogata-6990cc1b54e6fd0a1a3ae71edb65ea53db3bd9a1.zip
Make memory allocator a bit faster ?
-rw-r--r--src/common/libkogata/slab_alloc.c20
-rw-r--r--src/lib/libc/malloc.c22
2 files changed, 30 insertions, 12 deletions
diff --git a/src/common/libkogata/slab_alloc.c b/src/common/libkogata/slab_alloc.c
index 2f6cef1..729bdc3 100644
--- a/src/common/libkogata/slab_alloc.c
+++ b/src/common/libkogata/slab_alloc.c
@@ -251,7 +251,7 @@ void slab_free(mem_allocator_t* a, void* addr) {
r->n_free_objs++;
if (r->n_free_objs == region_size / a->types[i].obj_size) {
- // region is completely unused, free it.
+ // region is completely unused, free it if we have two that are unused.
if (a->slabs[i].first_cache == r) {
a->slabs[i].first_cache = r->next_cache;
} else {
@@ -262,8 +262,22 @@ void slab_free(mem_allocator_t* a, void* addr) {
}
}
}
- a->free_fun(r->region_addr);
- add_free_descriptor(a, (descriptor_t*)r);
+
+ cache_t *last_cache = a->slabs[i].first_cache;
+ while (last_cache && last_cache->next_cache) last_cache = last_cache->next_cache;
+
+ if (last_cache == 0) {
+ ASSERT(a->slabs[i].first_cache == 0);
+ a->slabs[i].first_cache = r;
+ r->next_cache = 0;
+ } else if (last_cache->n_free_objs == region_size / a->types[i].obj_size) {
+ a->free_fun(r->region_addr);
+ add_free_descriptor(a, (descriptor_t*)r);
+ } else {
+ ASSERT(last_cache->next_cache == 0);
+ last_cache->next_cache = r;
+ r->next_cache = 0;
+ }
}
return;
}
diff --git a/src/lib/libc/malloc.c b/src/lib/libc/malloc.c
index 6498273..e3df637 100644
--- a/src/lib/libc/malloc.c
+++ b/src/lib/libc/malloc.c
@@ -27,15 +27,19 @@ static void heap_free_pages(void* addr) {
static mem_allocator_t *mem_allocator;
static slab_type_t slab_sizes[] = {
{ "8B malloc objects", 8, 2 },
- { "16B malloc objects", 16, 4 },
- { "32B malloc objects", 32, 4 },
- { "64B malloc objects", 64, 4 },
- { "128B malloc objects", 128, 8 },
- { "256B malloc objects", 256, 8 },
- { "512B malloc objects", 512, 8 },
- { "1KB malloc objects", 1024, 16 },
- { "2KB malloc objects", 2048, 16 },
- { "4KB malloc objects", 4096, 16 },
+ { "16B malloc objects", 0x10, 4 },
+ { "32B malloc objects", 0x20, 4 },
+ { "64B malloc objects", 0x40, 4 },
+ { "128B malloc objects", 0x80, 8 },
+ { "256B malloc objects", 0x100, 8 },
+ { "512B malloc objects", 0x200, 8 },
+ { "1KB malloc objects", 0x400, 16 },
+ { "2KB malloc objects", 0x800, 16 },
+ { "4KB malloc objects", 0x1000, 16 },
+ { "8KB malloc objects", 0x2000, 16 },
+ { "16KB malloc objects", 0x4000, 16 },
+ { "32KB malloc objects", 0x8000, 32 },
+ { "64KB malloc objects", 0x10000, 64 },
{ 0, 0, 0 }
};