1 #include "memory_preallocated.h" 2 3 4 5 /* Initialise the memory pool with the given amount in bytes. */ 6 7 MemoryPreallocated::MemoryPreallocated(Memory *memory, offset_t amount) 8 : _memory(memory), _amount(amount) 9 { 10 // NOTE: Handle allocation failure. 11 12 offset_t remaining = amount; 13 14 while (remaining) 15 { 16 _regions.push_back(memory->region()); 17 18 if (memory->region_size() >= remaining) 19 break; 20 21 remaining -= memory->region_size(); 22 } 23 } 24 25 /* Obtain an allocated region. */ 26 27 Region *MemoryPreallocated::region() 28 { 29 std::lock_guard<std::mutex> guard(_lock); 30 31 Region *region = _regions.front(); 32 33 _regions.pop_front(); 34 return region; 35 } 36 37 /* Release the allocated 'region'. */ 38 39 void MemoryPreallocated::release(Region *region) 40 { 41 std::lock_guard<std::mutex> guard(_lock); 42 43 _regions.push_back(region); 44 } 45 46 // vim: tabstop=4 expandtab shiftwidth=4