00001 00002 #include <list> 00003 00014 class Memory_Pool { 00015 public: 00016 00023 Memory_Pool (std::size_t block_size, std::size_t num_blocks) 00024 : memory_block_ (::operator new (block_size * num_blocks)), 00025 block_size_ (block_size), 00026 num_blocks_ (num_blocks) 00027 { 00028 for (std::size_t i = 0; i < num_blocks; i++) 00029 { 00030 void *block = static_cast<char*>(memory_block_) + i*block_size; 00031 free_list_.push_back (block); 00032 } 00033 } 00034 00041 void *acquire (size_t size) 00042 { 00043 if (size > block_size_) 00044 { 00045 // if blocks larger than the supported size 00046 // are tried to acquire, throw bad_alloc 00047 throw std::bad_alloc (); 00048 } 00049 else 00050 { 00051 void *acquired_block = free_list_.front (); 00052 free_list_.pop_front (); 00053 return acquired_block; 00054 } 00055 } 00056 00063 void release (void *block) 00064 { 00065 free_list_.push_back (block); 00066 } 00067 00068 00069 private: 00070 void *memory_block_; 00071 std::size_t block_size_; 00072 std::size_t num_blocks_; 00073 std::list<void *> free_list_; 00074 };
These pages document the source code of the patterns Lookup, Eager Acquisition, and Leasing.
Copyright 2004 John Wiley and Sons. All Rights Reserved.