1.1 --- a/memory_preallocated.cc Sat Mar 20 16:53:21 2021 +0100
1.2 +++ b/memory_preallocated.cc Sat Mar 20 17:05:31 2021 +0100
1.3 @@ -1,23 +1,43 @@
1.4 #include "memory_preallocated.h"
1.5
1.6 +#include <stdlib.h>
1.7 +
1.8 /* Initialise the memory pool with a mandatory 'limit' in pages. */
1.9
1.10 MemoryPreallocated::MemoryPreallocated(unsigned int limit, offset_t region_size)
1.11 : Memory(region_size), _limit(limit)
1.12 {
1.13 - while (limit > 0)
1.14 - {
1.15 - Region *r = region();
1.16 + /* Allocate all memory at once, rounding each unit to the nearest page
1.17 + size. */
1.18 +
1.19 + offset_t rounded_size = round_multiple(region_size, PAGE_SIZE);
1.20 + offset_t total_size = limit * rounded_size;
1.21 + offset_t current;
1.22
1.23 - if (r != NULL)
1.24 - _free.push_back(r);
1.25 - else
1.26 - break;
1.27 + if (posix_memalign((void **) ¤t, total_size, total_size))
1.28 + return;
1.29 +
1.30 + /* Split the memory up into separate regions. */
1.31
1.32 + while (limit)
1.33 + {
1.34 + _free.push_back(new Region(current, current + rounded_size));
1.35 + current += rounded_size;
1.36 limit--;
1.37 }
1.38 }
1.39
1.40 +/* Free all allocated regions. */
1.41 +
1.42 +MemoryPreallocated::~MemoryPreallocated()
1.43 +{
1.44 + for (std::list<Region *>::iterator it = _free.begin(); it != _free.end(); it++)
1.45 + delete *it;
1.46 +
1.47 + for (std::set<Region *>::iterator it = _used.begin(); it != _used.end(); it++)
1.48 + delete *it;
1.49 +}
1.50 +
1.51 /* Allocate a new region of the configured size rounded to the nearest page. */
1.52
1.53 Region *MemoryPreallocated::region()