Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Sep 11, 2010, 12:34:00 AM (14 years ago)
Author:
landauf
Message:

merged doc branch back to trunk

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/libraries/util/SmallObjectAllocator.cc

    r7284 r7401  
    2727 */
    2828
     29/**
     30    @file
     31    @brief Implementation of SmallObjectAllocator
     32*/
     33
    2934#include "SmallObjectAllocator.h"
    3035
    3136namespace orxonox
    3237{
     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    */
    3343    SmallObjectAllocator::SmallObjectAllocator(size_t objectSize, size_t numObjects)
    3444    {
    35         this->objectSize_ = std::max(objectSize, sizeof(Chunk));
    36         this->numObjects_ = 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;
    3747        this->first_ = 0;
    3848    }
    3949
     50    /**
     51        @brief Destructor: deletes the allocated memory blocks.
     52    */
    4053    SmallObjectAllocator::~SmallObjectAllocator()
    4154    {
     
    4457    }
    4558
     59    /**
     60        @brief Helper function, used to set the next_ pointer of a Chunk.
     61    */
    4662    /* static */ void SmallObjectAllocator::setNext(void* chunk, void* next)
    4763    {
     
    4965    }
    5066
     67    /**
     68        @brief Helper function, returns the next_ pointer of a Chunk
     69    */
    5170    /* static */ void* SmallObjectAllocator::getNext(void* chunk)
    5271    {
     
    5473    }
    5574
     75    /**
     76        @brief Returns the first free memory chunk or allocates a new block of memory.
     77    */
    5678    void* SmallObjectAllocator::alloc()
    5779    {
     80        // get the first free chunk
    5881        void* chunk = this->first_;
    5982
     83        // check if the chunk exists
    6084        if (chunk)
    6185        {
     86            // yes it does - the first_ pointer now points to the second element in the list
    6287            this->first_ = getNext(chunk);
    6388        }
    6489        else
    6590        {
    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_];
    6793            this->blocks_.push_back(block);
    6894
    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_);
    7198
    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);
    73101
    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_;
    75104
     105            // The first chunk in the block is returned
    76106            chunk = block;
    77107        }
    78108
     109        // return the pointer to the chunk
    79110        return chunk;
    80111    }
    81112
     113    /**
     114        @brief Puts the memory chunk back on the list of free memory.
     115    */
    82116    void SmallObjectAllocator::free(void* chunk)
    83117    {
     118        // The first_ pointer points to the freed chunk, its next_ pointer points to the rest of the list
    84119        setNext(chunk, this->first_);
    85120        this->first_ = chunk;
Note: See TracChangeset for help on using the changeset viewer.