[7264] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[7401] | 29 | /** |
---|
| 30 | @file |
---|
| 31 | @brief Implementation of SmallObjectAllocator |
---|
| 32 | */ |
---|
| 33 | |
---|
[7264] | 34 | #include "SmallObjectAllocator.h" |
---|
| 35 | |
---|
| 36 | namespace orxonox |
---|
| 37 | { |
---|
[7401] | 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 | */ |
---|
[7264] | 43 | SmallObjectAllocator::SmallObjectAllocator(size_t objectSize, size_t numObjects) |
---|
| 44 | { |
---|
[7401] | 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; |
---|
[10768] | 47 | this->first_ = nullptr; |
---|
[7264] | 48 | } |
---|
| 49 | |
---|
[7401] | 50 | /** |
---|
| 51 | @brief Destructor: deletes the allocated memory blocks. |
---|
| 52 | */ |
---|
[7264] | 53 | SmallObjectAllocator::~SmallObjectAllocator() |
---|
| 54 | { |
---|
[10916] | 55 | for (char* block : this->blocks_) |
---|
| 56 | delete[] block; |
---|
[7264] | 57 | } |
---|
| 58 | |
---|
[7401] | 59 | /** |
---|
| 60 | @brief Helper function, used to set the next_ pointer of a Chunk. |
---|
| 61 | */ |
---|
[7264] | 62 | /* static */ void SmallObjectAllocator::setNext(void* chunk, void* next) |
---|
| 63 | { |
---|
| 64 | static_cast<Chunk*>(chunk)->next_ = static_cast<Chunk*>(next); |
---|
| 65 | } |
---|
| 66 | |
---|
[7401] | 67 | /** |
---|
| 68 | @brief Helper function, returns the next_ pointer of a Chunk |
---|
| 69 | */ |
---|
[7264] | 70 | /* static */ void* SmallObjectAllocator::getNext(void* chunk) |
---|
| 71 | { |
---|
| 72 | return static_cast<Chunk*>(chunk)->next_; |
---|
| 73 | } |
---|
| 74 | |
---|
[7401] | 75 | /** |
---|
| 76 | @brief Returns the first free memory chunk or allocates a new block of memory. |
---|
| 77 | */ |
---|
[7264] | 78 | void* SmallObjectAllocator::alloc() |
---|
| 79 | { |
---|
[7401] | 80 | // get the first free chunk |
---|
[7264] | 81 | void* chunk = this->first_; |
---|
| 82 | |
---|
[7401] | 83 | // check if the chunk exists |
---|
[7264] | 84 | if (chunk) |
---|
| 85 | { |
---|
[7401] | 86 | // yes it does - the first_ pointer now points to the second element in the list |
---|
[7264] | 87 | this->first_ = getNext(chunk); |
---|
| 88 | } |
---|
| 89 | else |
---|
| 90 | { |
---|
[7401] | 91 | // no it doesnt - allocate a new block of memory |
---|
| 92 | char* block = new char[this->chunkSize_ * this->numChunksPerBlock_]; |
---|
[7264] | 93 | this->blocks_.push_back(block); |
---|
| 94 | |
---|
[7401] | 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_); |
---|
[7264] | 98 | |
---|
[10765] | 99 | // the next_ pointer of the last chunk must point to nullptr |
---|
[10768] | 100 | setNext(block + (this->numChunksPerBlock_ - 1) * this->chunkSize_, nullptr); |
---|
[7264] | 101 | |
---|
[7401] | 102 | // The second chunk in the block is assigned to the first_ pointer |
---|
| 103 | this->first_ = block + this->chunkSize_; |
---|
[7264] | 104 | |
---|
[7401] | 105 | // The first chunk in the block is returned |
---|
[7264] | 106 | chunk = block; |
---|
| 107 | } |
---|
| 108 | |
---|
[7401] | 109 | // return the pointer to the chunk |
---|
[7264] | 110 | return chunk; |
---|
| 111 | } |
---|
| 112 | |
---|
[7401] | 113 | /** |
---|
| 114 | @brief Puts the memory chunk back on the list of free memory. |
---|
| 115 | */ |
---|
[7264] | 116 | void SmallObjectAllocator::free(void* chunk) |
---|
| 117 | { |
---|
[7401] | 118 | // The first_ pointer points to the freed chunk, its next_ pointer points to the rest of the list |
---|
[7264] | 119 | setNext(chunk, this->first_); |
---|
| 120 | this->first_ = chunk; |
---|
| 121 | } |
---|
| 122 | } |
---|