- Timestamp:
- Sep 3, 2010, 12:19:53 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/doc/src/libraries/util/SmallObjectAllocator.cc
r7284 r7327 27 27 */ 28 28 29 /** 30 @file 31 @brief Implementation of SmallObjectAllocator 32 */ 33 29 34 #include "SmallObjectAllocator.h" 30 35 31 36 namespace orxonox 32 37 { 38 /** 39 @brief Constructor: initializes the allocator and its values. 40 @param objectSize The size in bytes (returned by sizeof()) of the allocated objects 41 @param numObjects The number of objects that are allocated in one block of memory 42 */ 33 43 SmallObjectAllocator::SmallObjectAllocator(size_t objectSize, size_t numObjects) 34 44 { 35 this-> objectSize_ = std::max(objectSize, sizeof(Chunk));36 this->num Objects_ = numObjects;45 this->chunkSize_ = std::max(objectSize, sizeof(Chunk)); // the chunk's size will be the maximum of the object's size and the size of a Chunk object itself 46 this->numChunksPerBlock_ = numObjects; 37 47 this->first_ = 0; 38 48 } 39 49 50 /** 51 @brief Destructor: deletes the allocated memory blocks. 52 */ 40 53 SmallObjectAllocator::~SmallObjectAllocator() 41 54 { … … 44 57 } 45 58 59 /** 60 @brief Helper function, used to set the next_ pointer of a Chunk. 61 */ 46 62 /* static */ void SmallObjectAllocator::setNext(void* chunk, void* next) 47 63 { … … 49 65 } 50 66 67 /** 68 @brief Helper function, returns the next_ pointer of a Chunk 69 */ 51 70 /* static */ void* SmallObjectAllocator::getNext(void* chunk) 52 71 { … … 54 73 } 55 74 75 /** 76 @brief Returns the first free memory chunk or allocates a new block of memory. 77 */ 56 78 void* SmallObjectAllocator::alloc() 57 79 { 80 // get the first free chunk 58 81 void* chunk = this->first_; 59 82 83 // check if the chunk exists 60 84 if (chunk) 61 85 { 86 // yes it does - the first_ pointer now points to the second element in the list 62 87 this->first_ = getNext(chunk); 63 88 } 64 89 else 65 90 { 66 char* block = new char[this->objectSize_ * this->numObjects_]; 91 // no it doesnt - allocate a new block of memory 92 char* block = new char[this->chunkSize_ * this->numChunksPerBlock_]; 67 93 this->blocks_.push_back(block); 68 94 69 for (size_t i = 1; i < this->numObjects_ - 1; ++i) 70 setNext(block + i * this->objectSize_, block + (i + 1) * this->objectSize_); 95 // iterate through the chunks in the new memory block and link them together to a single linked list 96 for (size_t i = 1; i < this->numChunksPerBlock_ - 1; ++i) 97 setNext(block + i * this->chunkSize_, block + (i + 1) * this->chunkSize_); 71 98 72 setNext(block + (this->numObjects_ - 1) * this->objectSize_, 0); 99 // the next_ pointer of the last chunk must point to NULL 100 setNext(block + (this->numChunksPerBlock_ - 1) * this->chunkSize_, 0); 73 101 74 this->first_ = block + this->objectSize_; 102 // The second chunk in the block is assigned to the first_ pointer 103 this->first_ = block + this->chunkSize_; 75 104 105 // The first chunk in the block is returned 76 106 chunk = block; 77 107 } 78 108 109 // return the pointer to the chunk 79 110 return chunk; 80 111 } 81 112 113 /** 114 @brief Puts the memory chunk back on the list of free memory. 115 */ 82 116 void SmallObjectAllocator::free(void* chunk) 83 117 { 118 // The first_ pointer points to the freed chunk, its next_ pointer points to the rest of the list 84 119 setNext(chunk, this->first_); 85 120 this->first_ = chunk;
Note: See TracChangeset
for help on using the changeset viewer.