1 #include "memory_preallocated.h" 2 3 /* Initialise the memory pool with a mandatory 'limit' in pages. */ 4 5 MemoryPreallocated::MemoryPreallocated(unsigned int limit, offset_t region_size) 6 : Memory(region_size), _limit(limit) 7 { 8 while (limit > 0) 9 { 10 Region *r = region(); 11 12 if (r != NULL) 13 _free.push_back(r); 14 else 15 break; 16 17 limit--; 18 } 19 } 20 21 /* Allocate a new region of the configured size rounded to the nearest page. */ 22 23 Region *MemoryPreallocated::region() 24 { 25 std::lock_guard<std::mutex> guard(_lock); 26 27 std::list<Region *>::iterator it = _free.begin(); 28 29 if (it == _free.end()) 30 return NULL; 31 32 Region *r = *it; 33 34 _free.erase(it); 35 _used.insert(r); 36 } 37 38 /* Release the allocated 'region'. */ 39 40 void MemoryPreallocated::release(Region *region) 41 { 42 std::lock_guard<std::mutex> guard(_lock); 43 44 std::set<Region *>::iterator it = _used.find(region); 45 46 if (it != _used.end()) 47 { 48 _used.erase(it); 49 _free.push_back(region); 50 } 51 } 52 53 // vim: tabstop=4 expandtab shiftwidth=4