From 025b183b5a47814d58a320c66e80f18690ff6416 Mon Sep 17 00:00:00 2001 From: Olaf Barthel Date: Thu, 17 Nov 2016 13:14:13 +0100 Subject: [PATCH] Unused slabs are getting purged after adding a new slab If a new slab is allocated, or an empty slab is reused, the list of empty slabs is now purged, if possible. Empty slabs which cannot be immediately purged "decay" and will be collected the next time a new slab is allocated or an empty slab is reused. If a chunk is allocated and the slab which the chunk came from is fully utilized, the slab is moved to the end of its list. This should avoid searching this slab for free space before all other slabs of the same chunk size have been tested first. Slabs with free space are always placed closer to the head of the list. --- library/stdlib_malloc.c | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/library/stdlib_malloc.c b/library/stdlib_malloc.c index 9d09d45..94aa261 100644 --- a/library/stdlib_malloc.c +++ b/library/stdlib_malloc.c @@ -273,6 +273,19 @@ __allocate_memory(size_t size,BOOL never_free,const char * UNUSED unused_file,in } sn->sn_UseCount++; + + /* Is this slab now fully utilized? Move it to the + * end of the queue so that it will not be checked + * before other slabs of the same size have been + * tested. Those at the front of the queue should + * still have room left. + */ + if(sn->sn_UseCount == sn->sn_Count && sn != (struct SlabNode *)slab_list->mlh_TailPred) + { + Remove((struct Node *)sn); + AddTail((struct List *)slab_list, (struct Node *)sn); + } + break; } } @@ -367,6 +380,38 @@ __allocate_memory(size_t size,BOOL never_free,const char * UNUSED unused_file,in AddHead((struct List *)slab_list,(struct Node *)&new_sn); } + + /* Mark unused slabs for purging, and purge those which + * are ready to be purged. + */ + for(free_node = (struct MinNode *)__slab_data.sd_EmptySlabs.mlh_Head ; + free_node->mln_Succ != NULL ; + free_node = free_node_next) + { + free_node_next = (struct MinNode *)free_node->mln_Succ; + + /* free_node points to SlabNode.sn_EmptyLink, which + * directly follows the SlabNode.sn_MinNode. + */ + sn = (struct SlabNode *)&free_node[-1]; + + /* Is this empty slab ready to be purged? */ + if(sn->sn_EmptyDecay == 0) + { + /* Unlink from list of empty slabs. */ + Remove((struct Node *)free_node); + + /* Unlink from list of slabs of the same size. */ + Remove((struct Node *)sn); + + FreeVec(sn); + } + /* Give it another chance. */ + else + { + sn->sn_EmptyDecay--; + } + } } } }