# HG changeset patch # User Paul Boddie # Date 1616256331 -3600 # Node ID 7e00350a9add2f43a449f55c401878cc86f429c8 # Parent 28738a5f164887980b2aa74dc7e8b733fd817172 Changed the preallocated memory pool to obtain all its memory at once. Moved the allocate method from the base class to the incremental memory pool, eliminating the separate base class implementation file. diff -r 28738a5f1648 -r 7e00350a9add Makefile --- a/Makefile Sat Mar 20 16:53:21 2021 +0100 +++ b/Makefile Sat Mar 20 17:05:31 2021 +0100 @@ -49,7 +49,7 @@ PLAIN_SRC_CC_common_server = \ access_map.cc accessing.cc accessor.cc \ flexpage.cc file_pager.cc ipc.cc \ - memory.cc memory_incremental.cc memory_preallocated.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 28738a5f1648 -r 7e00350a9add memory.cc --- a/memory.cc Sat Mar 20 16:53:21 2021 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,31 +0,0 @@ -#include "memory.h" - -#include - - -/* Initialise the memory pool with an optional 'limit' in pages. */ - -Memory::Memory(offset_t region_size) -: _region_size(region_size) -{ -} - -/* Allocate a block of the given 'size'. */ - -Region *Memory::allocate(offset_t size) -{ - /* Attempt to allocate aligned memory. */ - - void *current; - - /* Make the size appropriate for the invocation. */ - - size = round_multiple(size, PAGE_SIZE); - - if (posix_memalign(¤t, size, size)) - return NULL; - - return new Region((offset_t) current, (offset_t) current + size); -} - -// vim: tabstop=4 expandtab shiftwidth=4 diff -r 28738a5f1648 -r 7e00350a9add memory.h --- a/memory.h Sat Mar 20 16:53:21 2021 +0100 +++ b/memory.h Sat Mar 20 17:05:31 2021 +0100 @@ -18,9 +18,10 @@ offset_t _region_size; public: - explicit Memory(offset_t region_size=PAGE_SIZE); - - virtual Region *allocate(offset_t size); + explicit Memory(offset_t region_size=PAGE_SIZE) + : _region_size(region_size) + { + } virtual Region *region() = 0; diff -r 28738a5f1648 -r 7e00350a9add memory_incremental.cc --- a/memory_incremental.cc Sat Mar 20 16:53:21 2021 +0100 +++ b/memory_incremental.cc Sat Mar 20 17:05:31 2021 +0100 @@ -1,5 +1,7 @@ #include "memory_incremental.h" +#include + /* Initialise the memory pool with an optional 'limit' in pages. */ MemoryIncremental::MemoryIncremental(unsigned int limit, offset_t region_size) @@ -14,6 +16,24 @@ _limited = false; } +/* Allocate a block of the given 'size'. */ + +Region *MemoryIncremental::allocate(offset_t size) +{ + /* Attempt to allocate aligned memory. */ + + void *current; + + /* Make the size appropriate for the invocation. */ + + size = round_multiple(size, PAGE_SIZE); + + if (posix_memalign(¤t, size, size)) + return NULL; + + return new Region((offset_t) current, (offset_t) current + size); +} + /* Allocate a new region of the configured size rounded to the nearest page. */ Region *MemoryIncremental::region() diff -r 28738a5f1648 -r 7e00350a9add memory_incremental.h --- a/memory_incremental.h Sat Mar 20 16:53:21 2021 +0100 +++ b/memory_incremental.h Sat Mar 20 17:05:31 2021 +0100 @@ -12,6 +12,8 @@ unsigned int _limit; bool _limited; + virtual Region *allocate(offset_t size); + public: explicit MemoryIncremental(unsigned int limit, offset_t region_size=PAGE_SIZE); diff -r 28738a5f1648 -r 7e00350a9add memory_preallocated.cc --- a/memory_preallocated.cc Sat Mar 20 16:53:21 2021 +0100 +++ b/memory_preallocated.cc Sat Mar 20 17:05:31 2021 +0100 @@ -1,23 +1,43 @@ #include "memory_preallocated.h" +#include + /* 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(); + /* Allocate all memory at once, rounding each unit to the nearest page + size. */ + + offset_t rounded_size = round_multiple(region_size, PAGE_SIZE); + offset_t total_size = limit * rounded_size; + offset_t current; - if (r != NULL) - _free.push_back(r); - else - break; + if (posix_memalign((void **) ¤t, total_size, total_size)) + return; + + /* Split the memory up into separate regions. */ + while (limit) + { + _free.push_back(new Region(current, current + rounded_size)); + current += rounded_size; limit--; } } +/* Free all allocated regions. */ + +MemoryPreallocated::~MemoryPreallocated() +{ + for (std::list::iterator it = _free.begin(); it != _free.end(); it++) + delete *it; + + for (std::set::iterator it = _used.begin(); it != _used.end(); it++) + delete *it; +} + /* Allocate a new region of the configured size rounded to the nearest page. */ Region *MemoryPreallocated::region() diff -r 28738a5f1648 -r 7e00350a9add memory_preallocated.h --- a/memory_preallocated.h Sat Mar 20 16:53:21 2021 +0100 +++ b/memory_preallocated.h Sat Mar 20 17:05:31 2021 +0100 @@ -19,6 +19,8 @@ public: explicit MemoryPreallocated(unsigned int limit, offset_t region_size=PAGE_SIZE); + virtual ~MemoryPreallocated(); + virtual Region *region(); virtual void release(Region *region);