#include <MemoryPool.h>
Collaboration diagram for OpenDDS::DCPS::FreeIndex:
Public Member Functions | |
FreeIndex (FreeHeader *&largest_free) | |
void | init (FreeHeader *init_free_block) |
void | add (FreeHeader *free_block) |
void | remove (FreeHeader *free_block, FreeHeader *next_largest) |
FreeHeader * | find (size_t size, unsigned char *base) |
Static Public Member Functions | |
static unsigned int | node_index (size_t size) |
Private Types | |
min_index_pow = 3 | |
max_index_pow = 12 | |
min_index = 8 | |
max_index = 4096 | |
enum | { min_index_pow = 3, max_index_pow = 12 } |
enum | { min_index = 8, max_index = 4096 } |
Private Attributes | |
size_t | size_ |
Number of index nodes. | |
FreeHeader *& | largest_free_ |
Memory pool's pointer to largest free block. | |
FreeIndexNode | nodes_ [max_index_pow-min_index_pow+1] |
Index nodes. | |
Friends | |
class | ::MemoryPoolTest |
class | ::FreeIndexTest |
Definition at line 115 of file MemoryPool.h.
anonymous enum [private] |
Definition at line 140 of file MemoryPool.h.
00140 { 00141 min_index_pow = 3, 00142 max_index_pow = 12 00143 };
anonymous enum [private] |
OpenDDS::DCPS::FreeIndex::FreeIndex | ( | FreeHeader *& | largest_free | ) | [explicit] |
Definition at line 159 of file MemoryPool.cpp.
00160 : size_(0) 00161 , largest_free_(largest_free) 00162 { 00163 }
void OpenDDS::DCPS::FreeIndex::add | ( | FreeHeader * | free_block | ) |
Add free block to index
Definition at line 166 of file MemoryPool.cpp.
References node_index(), nodes_, OpenDDS::DCPS::FreeIndexNode::set_ptr(), and OpenDDS::DCPS::AllocHeader::size().
Referenced by init(), and OpenDDS::DCPS::MemoryPool::insert_free_alloc().
00167 { 00168 unsigned int index = node_index(freed->size()); 00169 FreeIndexNode* node = nodes_ + index; 00170 00171 // If the node is empty, or if freed is smaller or equal to the node's alloc 00172 if ((node->ptr() == NULL) || (node->ptr()->size() >= freed->size())) { 00173 // Use this alloc in the index 00174 node->set_ptr(freed); 00175 } 00176 }
FreeHeader * OpenDDS::DCPS::FreeIndex::find | ( | size_t | size, | |
unsigned char * | base | |||
) |
Find smallest free block of size or larger
Definition at line 207 of file MemoryPool.cpp.
References largest_free_, node_index(), nodes_, OpenDDS::DCPS::FreeIndexNode::ptr(), OpenDDS::DCPS::AllocHeader::size(), size_, and OpenDDS::DCPS::FreeHeader::smaller_free().
Referenced by OpenDDS::DCPS::MemoryPool::insert_free_alloc(), and OpenDDS::DCPS::MemoryPool::pool_alloc().
00208 { 00209 unsigned int index = node_index(search_size); 00210 FreeIndexNode* index_node = nodes_ + index; 00211 00212 // Larger or equal to search_size 00213 FreeHeader* result = NULL; 00214 if (largest_free_ && (largest_free_->size() >= search_size)) { 00215 result = largest_free_; 00216 00217 // Look from here and larger 00218 while (index_node < nodes_ + size_) { 00219 if (index_node->ptr() && index_node->ptr()->size() >= search_size) { 00220 result = index_node->ptr(); 00221 break; 00222 } 00223 ++index_node; 00224 } 00225 } 00226 00227 // Now traverse, searching for smaller than result 00228 while (result) { 00229 FreeHeader* smaller = result->smaller_free(pool_base); 00230 if (smaller && smaller->size() >= search_size) { 00231 result = smaller; 00232 } else { 00233 break; 00234 } 00235 } 00236 00237 return result; 00238 }
void OpenDDS::DCPS::FreeIndex::init | ( | FreeHeader * | init_free_block | ) |
Initialize index with initial free block
Definition at line 196 of file MemoryPool.cpp.
References add(), max_index, min_index, nodes_, and size_.
Referenced by OpenDDS::DCPS::MemoryPool::MemoryPool().
00197 { 00198 size_t max = std::numeric_limits<size_t>::max(); 00199 for (size_t size = min_index; size <= max_index; size *= 2) { 00200 nodes_[size_].set_sizes(size, (size == max_index) ? max : size*2); 00201 ++size_; 00202 } 00203 add(init_free_block); 00204 }
unsigned int OpenDDS::DCPS::FreeIndex::node_index | ( | size_t | size | ) | [static] |
Calculate index of node corresponding to a size
Definition at line 241 of file MemoryPool.cpp.
References max_index_pow, and min_index_pow.
Referenced by add(), find(), and remove().
00242 { 00243 // Use shifting to perform log base 2 of size 00244 // start by using min + 1 (+1 because min is a power of 2 whch is already 00245 // one bit) 00246 size_t size_copy = size >> (min_index_pow + 1); 00247 unsigned int index = 0; 00248 unsigned int max_idx = max_index_pow - min_index_pow; 00249 while (size_copy && (index < max_idx)) { 00250 ++index; 00251 size_copy = size_copy >> 1; 00252 } 00253 return index; 00254 }
void OpenDDS::DCPS::FreeIndex::remove | ( | FreeHeader * | free_block, | |
FreeHeader * | next_largest | |||
) |
Remove free block from index
Definition at line 179 of file MemoryPool.cpp.
References node_index(), nodes_, OpenDDS::DCPS::FreeIndexNode::set_ptr(), and OpenDDS::DCPS::AllocHeader::size().
Referenced by OpenDDS::DCPS::MemoryPool::remove_free_alloc().
00180 { 00181 unsigned int index = node_index(free_block->size()); 00182 FreeIndexNode* node = nodes_ + index; 00183 00184 // If the node points to the free block 00185 if (node->ptr() == free_block) { 00186 // If the larger can be used by this node 00187 if (larger && node->contains(larger->size())) { 00188 node->set_ptr(larger); 00189 } else { 00190 node->set_ptr(NULL); 00191 } 00192 } 00193 }
friend class ::FreeIndexTest [friend] |
Definition at line 117 of file MemoryPool.h.
friend class ::MemoryPoolTest [friend] |
Definition at line 116 of file MemoryPool.h.
FreeHeader*& OpenDDS::DCPS::FreeIndex::largest_free_ [private] |
Memory pool's pointer to largest free block.
Definition at line 150 of file MemoryPool.h.
Referenced by find().
FreeIndexNode OpenDDS::DCPS::FreeIndex::nodes_[max_index_pow-min_index_pow+1] [private] |
size_t OpenDDS::DCPS::FreeIndex::size_ [private] |