# HG changeset patch # User Paul Boddie # Date 1616200903 -3600 # Node ID 57203e878c7c72b9d70a046920f05f1301f68ed5 # Parent 9314408dbd26bd2c075e9bfbcc1e449ee16319f7 Introduced separate incremental and preallocated memory pool abstractions. diff -r 9314408dbd26 -r 57203e878c7c Makefile --- a/Makefile Sat Mar 20 01:40:39 2021 +0100 +++ b/Makefile Sat Mar 20 01:41:43 2021 +0100 @@ -48,7 +48,8 @@ PLAIN_SRC_CC_common_server = \ access_map.cc accessing.cc accessor.cc \ - flexpage.cc file_pager.cc ipc.cc memory.cc \ + flexpage.cc file_pager.cc ipc.cc \ + memory.cc memory_incremental.cc memory_preallocated.cc \ opener_resource.cc opener_context_resource.cc \ page_mapper.cc page_queue.cc pager.cc pages.cc paging.cc \ region.cc resource_server.cc simple_pager.cc diff -r 9314408dbd26 -r 57203e878c7c dstest_block_server.cc --- a/dstest_block_server.cc Sat Mar 20 01:40:39 2021 +0100 +++ b/dstest_block_server.cc Sat Mar 20 01:41:43 2021 +0100 @@ -27,7 +27,7 @@ #include #include "accessing.h" -#include "memory.h" +#include "memory_incremental.h" #include "memory_utils.h" #include "pages.h" #include "paging.h" @@ -42,7 +42,7 @@ { /* Some memory plus infrastructure. */ - Memory mem(MEMORY_PAGES); + MemoryIncremental mem(MEMORY_PAGES); Accessing accessing; Paging paging; Pages pages(&mem); diff -r 9314408dbd26 -r 57203e878c7c dstest_host_server.cc --- a/dstest_host_server.cc Sat Mar 20 01:40:39 2021 +0100 +++ b/dstest_host_server.cc Sat Mar 20 01:41:43 2021 +0100 @@ -27,7 +27,7 @@ #include #include "accessing.h" -#include "memory.h" +#include "memory_incremental.h" #include "memory_utils.h" #include "pages.h" #include "paging.h" @@ -42,7 +42,7 @@ { /* Some memory plus infrastructure. */ - Memory mem(MEMORY_PAGES); + MemoryIncremental mem(MEMORY_PAGES); Accessing accessing; Paging paging; Pages pages(&mem); diff -r 9314408dbd26 -r 57203e878c7c dstest_test_server.cc --- a/dstest_test_server.cc Sat Mar 20 01:40:39 2021 +0100 +++ b/dstest_test_server.cc Sat Mar 20 01:41:43 2021 +0100 @@ -29,7 +29,7 @@ #include #include "accessing.h" -#include "memory.h" +#include "memory_incremental.h" #include "memory_utils.h" #include "pages.h" #include "paging.h" @@ -65,7 +65,7 @@ /* Some memory plus infrastructure. */ - Memory mem(memory_pages, page(REGION_PAGES)); + MemoryIncremental mem(memory_pages, page(REGION_PAGES)); Accessing accessing; Paging paging; Pages pages(&mem); diff -r 9314408dbd26 -r 57203e878c7c memory.cc --- a/memory.cc Sat Mar 20 01:40:39 2021 +0100 +++ b/memory.cc Sat Mar 20 01:41:43 2021 +0100 @@ -5,16 +5,9 @@ /* Initialise the memory pool with an optional 'limit' in pages. */ -Memory::Memory(unsigned int limit, offset_t region_size) -: _limit(limit), _region_size(region_size) +Memory::Memory(offset_t region_size) +: _region_size(region_size) { - _limited = true; -} - -Memory::Memory() -: _region_size(PAGE_SIZE) -{ - _limited = false; } /* Allocate a block of the given 'size'. */ @@ -35,56 +28,4 @@ return new Region((offset_t) current, (offset_t) current + size); } -/* Allocate a new region of the given 'size' rounded to the nearest page. */ - -Region *Memory::region(offset_t size) -{ - std::lock_guard guard(_lock); - - offset_t rounded = round(size, PAGE_SIZE); - offset_t pages = rounded / PAGE_SIZE; - - /* Check for sufficient pages. */ - - if (!_limited || (_limit >= pages)) - { - /* Attempt to allocate aligned memory. */ - - Region *region = allocate(rounded); - - if (region == NULL) - return NULL; - - if (_limited) - _limit -= pages; - - return region; - } - - /* Return no region without sufficient pages. */ - - else - return NULL; -} - -Region *Memory::region() -{ - return region(_region_size); -} - -/* Release the allocated 'region'. */ - -void Memory::release(Region *region) -{ - std::lock_guard guard(_lock); - - offset_t rounded = round(region->size(), PAGE_SIZE); - offset_t pages = rounded / PAGE_SIZE; - - if (_limited) - _limit += pages; - - delete region; -} - // vim: tabstop=4 expandtab shiftwidth=4 diff -r 9314408dbd26 -r 57203e878c7c memory.h --- a/memory.h Sat Mar 20 01:40:39 2021 +0100 +++ b/memory.h Sat Mar 20 01:41:43 2021 +0100 @@ -2,7 +2,6 @@ #include -#include #include #include "memory_utils.h" @@ -16,23 +15,16 @@ { protected: std::mutex _lock; - - unsigned int _limit; offset_t _region_size; - bool _limited; public: - explicit Memory(unsigned int limit, offset_t region_size=PAGE_SIZE); + explicit Memory(offset_t region_size=PAGE_SIZE); - explicit Memory(); - - Region *allocate(offset_t size); + virtual Region *allocate(offset_t size); - Region *region(offset_t size); + virtual Region *region() = 0; - Region *region(); - - void release(Region *region); + virtual void release(Region *region) = 0; }; // vim: tabstop=4 expandtab shiftwidth=4 diff -r 9314408dbd26 -r 57203e878c7c memory_incremental.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/memory_incremental.cc Sat Mar 20 01:41:43 2021 +0100 @@ -0,0 +1,71 @@ +#include "memory_incremental.h" + +/* Initialise the memory pool with an optional 'limit' in pages. */ + +MemoryIncremental::MemoryIncremental(unsigned int limit, offset_t region_size) +: Memory(region_size), _limit(limit) +{ + _limited = true; +} + +MemoryIncremental::MemoryIncremental() +: Memory() +{ + _limited = false; +} + +/* Allocate a new region of the configured size rounded to the nearest page. */ + +Region *MemoryIncremental::region() +{ + return region(_region_size); +} + +/* Allocate a new region of the given 'size' rounded to the nearest page. */ + +Region *MemoryIncremental::region(offset_t size) +{ + std::lock_guard guard(_lock); + + offset_t rounded = round(size, PAGE_SIZE); + offset_t pages = rounded / PAGE_SIZE; + + /* Check for sufficient pages. */ + + if (!_limited || (_limit >= pages)) + { + /* Attempt to allocate aligned memory. */ + + Region *region = allocate(rounded); + + if (region == NULL) + return NULL; + + if (_limited) + _limit -= pages; + + return region; + } + + /* Return no region without sufficient pages. */ + + else + return NULL; +} + +/* Release the allocated 'region'. */ + +void MemoryIncremental::release(Region *region) +{ + std::lock_guard guard(_lock); + + offset_t rounded = round(region->size(), PAGE_SIZE); + offset_t pages = rounded / PAGE_SIZE; + + if (_limited) + _limit += pages; + + delete region; +} + +// vim: tabstop=4 expandtab shiftwidth=4 diff -r 9314408dbd26 -r 57203e878c7c memory_incremental.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/memory_incremental.h Sat Mar 20 01:41:43 2021 +0100 @@ -0,0 +1,27 @@ +#pragma once + +#include "memory.h" + + + +/* A memory pool incrementally requesting new regions. */ + +class MemoryIncremental : public Memory +{ +protected: + unsigned int _limit; + bool _limited; + +public: + explicit MemoryIncremental(unsigned int limit, offset_t region_size=PAGE_SIZE); + + explicit MemoryIncremental(); + + virtual Region *region(); + + virtual Region *region(offset_t size); + + virtual void release(Region *region); +}; + +// vim: tabstop=4 expandtab shiftwidth=4 diff -r 9314408dbd26 -r 57203e878c7c memory_preallocated.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/memory_preallocated.cc Sat Mar 20 01:41:43 2021 +0100 @@ -0,0 +1,53 @@ +#include "memory_preallocated.h" + +/* Initialise the memory pool with a mandatory 'limit' in pages. */ + +MemoryPreallocated::MemoryPreallocated(unsigned int limit, offset_t region_size) +: Memory(region_size), _limit(limit) +{ + while (limit > 0) + { + Region *r = region(); + + if (r != NULL) + _free.push_back(r); + else + break; + + limit--; + } +} + +/* Allocate a new region of the configured size rounded to the nearest page. */ + +Region *MemoryPreallocated::region() +{ + std::lock_guard guard(_lock); + + std::list::iterator it = _free.begin(); + + if (it == _free.end()) + return NULL; + + Region *r = *it; + + _free.erase(it); + _used.insert(r); +} + +/* Release the allocated 'region'. */ + +void MemoryPreallocated::release(Region *region) +{ + std::lock_guard guard(_lock); + + std::set::iterator it = _used.find(region); + + if (it != _used.end()) + { + _used.erase(it); + _free.push_back(region); + } +} + +// vim: tabstop=4 expandtab shiftwidth=4 diff -r 9314408dbd26 -r 57203e878c7c memory_preallocated.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/memory_preallocated.h Sat Mar 20 01:41:43 2021 +0100 @@ -0,0 +1,27 @@ +#pragma once + +#include +#include + +#include "memory.h" + + + +/* A memory pool preallocating its memory. */ + +class MemoryPreallocated : public Memory +{ +protected: + unsigned int _limit; + std::list _free; + std::set _used; + +public: + explicit MemoryPreallocated(unsigned int limit, offset_t region_size=PAGE_SIZE); + + virtual Region *region(); + + virtual void release(Region *region); +}; + +// vim: tabstop=4 expandtab shiftwidth=4 diff -r 9314408dbd26 -r 57203e878c7c pages.cc --- a/pages.cc Sat Mar 20 01:40:39 2021 +0100 +++ b/pages.cc Sat Mar 20 01:41:43 2021 +0100 @@ -1,3 +1,4 @@ +#include "memory_incremental.h" #include "pages.h" Pages::Pages(Memory *memory) @@ -7,7 +8,7 @@ Pages::Pages() { - _memory = new Memory(); + _memory = new MemoryIncremental(); } /* Remove the first flexpage in the queue. If its owner exists, remove it from diff -r 9314408dbd26 -r 57203e878c7c simple_pager.cc --- a/simple_pager.cc Sat Mar 20 01:40:39 2021 +0100 +++ b/simple_pager.cc Sat Mar 20 01:41:43 2021 +0100 @@ -2,6 +2,7 @@ #include "dataspace_server.h" #include "ipc.h" +#include "memory_incremental.h" #include "simple_pager.h" @@ -9,7 +10,7 @@ SimplePager::SimplePager(Memory *memory) { if (memory == NULL) - _memory = new Memory(); + _memory = new MemoryIncremental(); else _memory = memory;