1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/memory/memory_preallocated.cc Sat Mar 27 22:12:12 2021 +0100
1.3 @@ -0,0 +1,46 @@
1.4 +#include "memory_preallocated.h"
1.5 +
1.6 +
1.7 +
1.8 +/* Initialise the memory pool with the given amount in bytes. */
1.9 +
1.10 +MemoryPreallocated::MemoryPreallocated(Memory *memory, offset_t amount)
1.11 +: _memory(memory), _amount(amount)
1.12 +{
1.13 + // NOTE: Handle allocation failure.
1.14 +
1.15 + offset_t remaining = amount;
1.16 +
1.17 + while (remaining)
1.18 + {
1.19 + _regions.push_back(memory->region());
1.20 +
1.21 + if (memory->region_size() >= remaining)
1.22 + break;
1.23 +
1.24 + remaining -= memory->region_size();
1.25 + }
1.26 +}
1.27 +
1.28 +/* Obtain an allocated region. */
1.29 +
1.30 +Region *MemoryPreallocated::region()
1.31 +{
1.32 + std::lock_guard<std::mutex> guard(_lock);
1.33 +
1.34 + Region *region = _regions.front();
1.35 +
1.36 + _regions.pop_front();
1.37 + return region;
1.38 +}
1.39 +
1.40 +/* Release the allocated 'region'. */
1.41 +
1.42 +void MemoryPreallocated::release(Region *region)
1.43 +{
1.44 + std::lock_guard<std::mutex> guard(_lock);
1.45 +
1.46 + _regions.push_back(region);
1.47 +}
1.48 +
1.49 +// vim: tabstop=4 expandtab shiftwidth=4