- Timestamp:
- Sep 3, 2010, 12:19:53 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/doc/src/libraries/util/SmallObjectAllocator.h
r7284 r7327 27 27 */ 28 28 29 /** 30 @defgroup SmallObjectAllocator SmallObjectAllocator 31 @ingroup Util Object 32 */ 33 34 /** 35 @file 36 @ingroup Util 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 29 74 #ifndef _SmallObjectAllocator_H__ 30 75 #define _SmallObjectAllocator_H__ … … 35 80 namespace orxonox 36 81 { 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 */ 37 89 class _UtilExport SmallObjectAllocator 38 90 { 91 /// The memory chunk is at the same time an element of a single linked list. 39 92 struct Chunk 40 93 { 41 Chunk* next_; 94 Chunk* next_; ///< A pointer to the next chunk in the list 42 95 }; 43 96 … … 53 106 static void* getNext(void* chunk); 54 107 55 void* first_; 56 size_t objectSize_;57 size_t num Objects_;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 58 111 59 std::vector<char*> blocks_; 112 std::vector<char*> blocks_; ///< A list of all allocated memory blocks (used to destroy them again) 60 113 }; 61 114 }
Note: See TracChangeset
for help on using the changeset viewer.