From 4303ae75205bacb099348b0f108b81e19c841498 Mon Sep 17 00:00:00 2001 From: Michael Poole Date: Sat, 5 Feb 2005 03:52:51 +0000 Subject: [PATCH] Even more slab allocator updates. src/slab-alloc.c (SLAB_DEBUG): Default to on. (SMALL_CUTOFF): Fix default value (must be a multiple of 4). (slab_unalloc): Fix slab counting. When SLAB_RESERVE, allocate the set of pages in a burst, rather than supplementing them as we unmap. (slab_realloc): Fix a rather embarassing (and LARGE) memory leak. git-archimport-id: srvx@srvx.net--2005-srvx/srvx--devo--1.3--patch-14 --- ChangeLog | 17 +++++++++++++++++ src/alloc-slab.c | 37 +++++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/ChangeLog b/ChangeLog index d65235b..8a558c9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,23 @@ # arch-tag: automatic-ChangeLog--srvx@srvx.net--2005-srvx/srvx--devo--1.3 # +2005-02-05 03:52:51 GMT Michael Poole patch-14 + + Summary: + Even more slab allocator updates. + Revision: + srvx--devo--1.3--patch-14 + + src/slab-alloc.c (SLAB_DEBUG): Default to on. + (SMALL_CUTOFF): Fix default value (must be a multiple of 4). + (slab_unalloc): Fix slab counting. When SLAB_RESERVE, allocate the + set of pages in a burst, rather than supplementing them as we unmap. + (slab_realloc): Fix a rather embarassing (and LARGE) memory leak. + + modified files: + ChangeLog src/alloc-slab.c + + 2005-02-04 16:36:40 GMT Michael Poole patch-13 Summary: diff --git a/src/alloc-slab.c b/src/alloc-slab.c index dd07ecd..b2b99a4 100644 --- a/src/alloc-slab.c +++ b/src/alloc-slab.c @@ -25,7 +25,7 @@ # error The slab allocator requires that your system have the mmap() system call. #endif -#define SLAB_DEBUG 0 +#define SLAB_DEBUG 1 #define SLAB_RESERVE 1024 #if SLAB_DEBUG @@ -97,7 +97,7 @@ struct slabset { #define SLAB_MIN (2 * sizeof(void*)) #define SLAB_GRAIN sizeof(void*) #define SLAB_ALIGN SLAB_GRAIN -#define SMALL_CUTOFF 582 +#define SMALL_CUTOFF 580 /* Element size < SMALL_CUTOFF -> use small slabs. * Larger elements are allocated directly using mmap(). The largest * regularly allocated struct in srvx 1.x is smaller than @@ -293,7 +293,6 @@ slab_unalloc(void *ptr, size_t size) /* log_module(MAIN_LOG, LOG_DEBUG, "%u-slab %p became full.", slab->parent->size, slab); */ /* Unlink slab from its parent. */ slab->parent->nslabs--; - slab_count--; if (slab->prev) slab->prev->next = slab->next; if (slab->next) @@ -307,18 +306,20 @@ slab_unalloc(void *ptr, size_t size) } #if SLAB_RESERVE - /* Make sure we have enough free slab pages. */ - while (free_slab_count < SLAB_RESERVE) { - struct slab *tslab; - item = slab_map(slab_pagesize()); - tslab = (struct slab*)((char*)item + slab_pagesize() - sizeof(*slab)); - tslab->base = item; - tslab->prev = free_slab_tail; - free_slab_tail = tslab; - if (!free_slab_head) - free_slab_head = tslab; - free_slab_count++; - slab_count++; + if (!free_slab_count) { + /* Make sure we have enough free slab pages. */ + while (free_slab_count < SLAB_RESERVE) { + struct slab *tslab; + item = slab_map(slab_pagesize()); + tslab = (struct slab*)((char*)item + slab_pagesize() - sizeof(*slab)); + tslab->base = item; + tslab->prev = free_slab_tail; + free_slab_tail = tslab; + if (!free_slab_head) + free_slab_head = tslab; + free_slab_count++; + slab_count++; + } } /* Unmap old slab, so accesses to stale pointers will fault. */ @@ -387,6 +388,7 @@ slab_realloc(const char *file, unsigned int line, void *ptr, size_t size) return ptr; newblock = slab_malloc(file, line, size); memcpy(newblock, ptr, osize); + slab_free(file, line, ptr); return newblock; } @@ -403,7 +405,7 @@ slab_strdup(const char *file, unsigned int line, const char *src) } void -slab_free(UNUSED_ARG(const char *file), UNUSED_ARG(unsigned int line), void *ptr) +slab_free(const char *file, unsigned int line, void *ptr) { alloc_header_t *hdr; size_t real; @@ -413,10 +415,13 @@ slab_free(UNUSED_ARG(const char *file), UNUSED_ARG(unsigned int line), void *ptr verify(ptr); hdr = (alloc_header_t*)ptr - 1; #if SLAB_DEBUG + hdr->file_id = get_file_id(file); + hdr->line = line; hdr->magic = FREE_MAGIC; real = hdr->size + sizeof(*hdr); #else real = *hdr + sizeof(*hdr); + (void)file; (void)line; #endif real = (real + SLAB_GRAIN - 1) & ~(SLAB_GRAIN - 1); if (real < SMALL_CUTOFF) { -- 2.20.1