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.h

    r7284 r7401  
    2727 */
    2828
     29/**
     30    @defgroup SmallObjectAllocator SmallObjectAllocator
     31    @ingroup Util
     32*/
     33
     34/**
     35    @file
     36    @ingroup SmallObjectAllocator
     37    @brief Declaration of SmallObjectAllocator
     38
     39    @anchor SmallObjectAllocatorExample
     40
     41    The default implementations of new and delete are designed to work with objects of
     42    arbitrary size. They are thus not optimal for small objects.
     43    @ref orxonox::SmallObjectAllocator "SmallObjectAllocator" allocates a large memory
     44    block and divides it into small chunks. These chunks are returned by the function
     45    @ref orxonox::SmallObjectAllocator::alloc() "alloc()" and can be used to create a
     46    new object using the placement new operator. Instead of delete, the function
     47    @ref orxonox::SmallObjectAllocator::free() "free()" is used to give the memory
     48    back to SmallObjectAllocator.
     49
     50    Example:
     51    @code
     52    SmallObjectAllocator allocator(sizeof(MySmallObject));  // Create an allocator. The size of the memory chunks must equal the size of the desired class
     53
     54    void* chunk = allocator.alloc();                        // Allocate a memory chunk
     55    MySmallObject* object = new (chunk) MySmallObject();    // Call the placement new operator
     56
     57    object->someFunction();                                 // Do something with the object
     58
     59    object->~MySmallObject();                               // Call the destructor
     60    allocator.free(object);                                 // Free the allocated memory
     61    @endcode
     62
     63    @b Important: You have to call the destructor of the object manually, because this
     64    is not automatically done by the allocator nor free().
     65
     66    @note The destructor can be ignored if it is empty or not implemented. This saves
     67    another amount of time.
     68
     69    @remarks For a distributed usage of SmallObjectAllocator it may be a good idea to
     70    create a static function that returns an instance to it. The allocator then works
     71    like a singleton and can be accesses from everywhere.
     72*/
     73
    2974#ifndef _SmallObjectAllocator_H__
    3075#define _SmallObjectAllocator_H__
     
    3580namespace orxonox
    3681{
     82    /**
     83        @brief This class is used to allocate and free small objects (usually not polymorphic).
     84
     85        SmallObjectAllocator provides a fast alternative to new and delete for small objects.
     86
     87        @see See @ref SmallObjectAllocatorExample "this description" for more information and an example.
     88    */
    3789    class _UtilExport SmallObjectAllocator
    3890    {
     91        /// The memory chunk is at the same time an element of a single linked list.
    3992        struct Chunk
    4093        {
    41             Chunk* next_;
     94            Chunk* next_;   ///< A pointer to the next chunk in the list
    4295        };
    4396
     
    53106            static void* getNext(void* chunk);
    54107
    55             void* first_;
    56             size_t objectSize_;
    57             size_t numObjects_;
     108            void* first_;                   ///< A pointer to the first free memory chunk
     109            size_t chunkSize_;              ///< The size of each chunk (and usually also the size of the created objects)
     110            size_t numChunksPerBlock_;      ///< The number of chunks per memory block
    58111
    59             std::vector<char*> blocks_;
     112            std::vector<char*> blocks_;     ///< A list of all allocated memory blocks (used to destroy them again)
    60113    };
    61114}
Note: See TracChangeset for help on using the changeset viewer.