From 1e84b46c3f9d4750e6f5facbd166fcbb33c7dc39 Mon Sep 17 00:00:00 2001 From: Alexander Boettcher Date: Tue, 9 Feb 2021 23:06:16 +0100 Subject: [PATCH] slab: add option to free empty blocks explictly by a method Issue #4014 --- repos/base/include/base/allocator_avl.h | 6 +++++- repos/base/include/base/slab.h | 6 ++++++ repos/base/lib/symbols/ld | 1 + repos/base/src/lib/base/slab.cc | 11 +++++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/repos/base/include/base/allocator_avl.h b/repos/base/include/base/allocator_avl.h index 9e24e67125..9accdfc27e 100644 --- a/repos/base/include/base/allocator_avl.h +++ b/repos/base/include/base/allocator_avl.h @@ -340,7 +340,11 @@ class Genode::Allocator_avl_tpl : public Allocator_avl_base _metadata((metadata_chunk_alloc) ? metadata_chunk_alloc : this, (Block *)&_initial_md_block) { } - ~Allocator_avl_tpl() { _revert_allocations_and_ranges(); } + ~Allocator_avl_tpl() + { + _metadata.free_empty_blocks(); + _revert_allocations_and_ranges(); + } /** * Return size of slab blocks used for meta data diff --git a/repos/base/include/base/slab.h b/repos/base/include/base/slab.h index e2beffb5c6..3cc477af9e 100644 --- a/repos/base/include/base/slab.h +++ b/repos/base/include/base/slab.h @@ -138,6 +138,12 @@ class Genode::Slab : public Allocator Allocator *backing_store() { return _backing_store; } + /** + * Free memory of empty slab blocks + */ + void free_empty_blocks(); + + /************************* ** Allocator interface ** *************************/ diff --git a/repos/base/lib/symbols/ld b/repos/base/lib/symbols/ld index b8a8d91882..7e8aa86adc 100644 --- a/repos/base/lib/symbols/ld +++ b/repos/base/lib/symbols/ld @@ -208,6 +208,7 @@ _ZN6Genode4Lock4lockEv T _ZN6Genode4Lock6unlockEv T _ZN6Genode4LockC1ENS0_5StateE T _ZN6Genode4Slab13any_used_elemEv T +_ZN6Genode4Slab17free_empty_blocksEv T _ZN6Genode4Slab5Block11_slab_entryEi T _ZN6Genode4Slab5Block14any_used_entryEv T _ZN6Genode4Slab5Block5allocEv T diff --git a/repos/base/src/lib/base/slab.cc b/repos/base/src/lib/base/slab.cc index 1e4bc0ba77..c8e0b7fbde 100644 --- a/repos/base/src/lib/base/slab.cc +++ b/repos/base/src/lib/base/slab.cc @@ -416,6 +416,17 @@ void Slab::_free(void *addr) } +void Slab::free_empty_blocks() +{ + for (size_t blocks = _num_blocks; blocks > 0; blocks--) { + if (_curr_sb != _initial_sb && _curr_sb->avail() == _entries_per_block) + _free_curr_sb(); + else + _curr_sb = _curr_sb->next; + } +} + + void *Slab::any_used_elem() { if (_total_avail == _num_blocks*_entries_per_block)