Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/spacestationentry/src/libraries/util/SmallObjectAllocator.cc @ 9903

Last change on this file since 9903 was 7401, checked in by landauf, 14 years ago

merged doc branch back to trunk

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
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
29/**
30    @file
31    @brief Implementation of SmallObjectAllocator
32*/
33
34#include "SmallObjectAllocator.h"
35
36namespace orxonox
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    */
43    SmallObjectAllocator::SmallObjectAllocator(size_t objectSize, size_t numObjects)
44    {
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;
47        this->first_ = 0;
48    }
49
50    /**
51        @brief Destructor: deletes the allocated memory blocks.
52    */
53    SmallObjectAllocator::~SmallObjectAllocator()
54    {
55        for (std::vector<char*>::iterator it = this->blocks_.begin(); it != this->blocks_.end(); ++it)
56            delete[] *it;
57    }
58
59    /**
60        @brief Helper function, used to set the next_ pointer of a Chunk.
61    */
62    /* static */ void SmallObjectAllocator::setNext(void* chunk, void* next)
63    {
64        static_cast<Chunk*>(chunk)->next_ = static_cast<Chunk*>(next);
65    }
66
67    /**
68        @brief Helper function, returns the next_ pointer of a Chunk
69    */
70    /* static */ void* SmallObjectAllocator::getNext(void* chunk)
71    {
72        return static_cast<Chunk*>(chunk)->next_;
73    }
74
75    /**
76        @brief Returns the first free memory chunk or allocates a new block of memory.
77    */
78    void* SmallObjectAllocator::alloc()
79    {
80        // get the first free chunk
81        void* chunk = this->first_;
82
83        // check if the chunk exists
84        if (chunk)
85        {
86            // yes it does - the first_ pointer now points to the second element in the list
87            this->first_ = getNext(chunk);
88        }
89        else
90        {
91            // no it doesnt - allocate a new block of memory
92            char* block = new char[this->chunkSize_ * this->numChunksPerBlock_];
93            this->blocks_.push_back(block);
94
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_);
98
99            // the next_ pointer of the last chunk must point to NULL
100            setNext(block + (this->numChunksPerBlock_ - 1) * this->chunkSize_, 0);
101
102            // The second chunk in the block is assigned to the first_ pointer
103            this->first_ = block + this->chunkSize_;
104
105            // The first chunk in the block is returned
106            chunk = block;
107        }
108
109        // return the pointer to the chunk
110        return chunk;
111    }
112
113    /**
114        @brief Puts the memory chunk back on the list of free memory.
115    */
116    void SmallObjectAllocator::free(void* chunk)
117    {
118        // The first_ pointer points to the freed chunk, its next_ pointer points to the rest of the list
119        setNext(chunk, this->first_);
120        this->first_ = chunk;
121    }
122}
Note: See TracBrowser for help on using the repository browser.