DART 6.13.2
Loading...
Searching...
No Matches
PoolAllocator.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2022, The DART development contributors
3 * All rights reserved.
4 *
5 * The list of contributors can be found at:
6 * https://github.com/dartsim/dart/blob/master/LICENSE
7 *
8 * This file is provided under the following "BSD-style" License:
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
11 * conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifndef DART_COMMON_POOLALLOCATOR_HPP_
34#define DART_COMMON_POOLALLOCATOR_HPP_
35
36#include <array>
37#include <mutex>
38
41
42namespace dart::common {
43
47{
48public:
50
55 explicit PoolAllocator(
57
59 ~PoolAllocator() override;
60
62
64 [[nodiscard]] const MemoryAllocator& getBaseAllocator() const;
65
67 [[nodiscard]] MemoryAllocator& getBaseAllocator();
68
70 [[nodiscard]] int getNumAllocatedMemoryBlocks() const;
71
72 // Documentation inherited
73 [[nodiscard]] void* allocate(size_t bytes) noexcept override;
74
75 // Documentation inherited
76 void deallocate(void* pointer, size_t bytes) override;
77
78 // Documentation inherited
79 void print(std::ostream& os = std::cout, int indent = 0) const override;
80
81private:
83 {
86 };
87
93
94 inline static constexpr int HEAP_COUNT = 128;
95
96 inline static constexpr size_t MAX_UNIT_SIZE = 1024;
97
98 inline static constexpr size_t BLOCK_SIZE = 16 * MAX_UNIT_SIZE;
99
100 inline static std::array<size_t, HEAP_COUNT> mUnitSizes;
101
102 inline static std::array<int, MAX_UNIT_SIZE + 1> mMapSizeToHeapIndex;
103
104 inline static bool mInitialized = false;
105
108
111 mutable std::mutex mMutex;
112
118
124
127
136 std::array<MemoryUnit*, HEAP_COUNT> mFreeMemoryUnits;
137};
138
139} // namespace dart::common
140
141#endif // DART_COMMON_POOLALLOCATOR_HPP_
#define DART_STRING_TYPE(type_name)
Definition Castable.hpp:38
Base class for std::allocator compatible allocators.
Definition MemoryAllocator.hpp:46
static MemoryAllocator & GetDefault()
Returns the default memory allocator.
Definition MemoryAllocator.cpp:41
Memory allocator optimized for allocating many objects of the same or similar sizes.
Definition PoolAllocator.hpp:47
std::mutex mMutex
Mutex for for mNumAllocatedMemoryBlocks, mNumMemoryBlocks, mFreeMemoryUnits, and mAllocatedMemoryBloc...
Definition PoolAllocator.hpp:111
MemoryBlock * mMemoryBlocks
The array of memory blocks.
Definition PoolAllocator.hpp:117
MemoryAllocator & mBaseAllocator
The base allocator to allocate memory chunk.
Definition PoolAllocator.hpp:107
void deallocate(void *pointer, size_t bytes) override
Deallocates the storage referenced by the pointer p, which must be a pointer obtained by an earlier c...
Definition PoolAllocator.cpp:193
int mMemoryBlocksSize
The size of mMemoryBlocks.
Definition PoolAllocator.hpp:123
int getNumAllocatedMemoryBlocks() const
Returns the count of allocated memory blocks.
Definition PoolAllocator.cpp:114
static std::array< size_t, HEAP_COUNT > mUnitSizes
Definition PoolAllocator.hpp:100
~PoolAllocator() override
Destructor.
Definition PoolAllocator.cpp:88
void * allocate(size_t bytes) noexcept override
Allocates size bytes of uninitialized storage.
Definition PoolAllocator.cpp:120
const MemoryAllocator & getBaseAllocator() const
Returns the base allocator.
Definition PoolAllocator.cpp:102
int mCurrentMemoryBlockIndex
The count of the allocated memory blocks in use.
Definition PoolAllocator.hpp:126
static constexpr int HEAP_COUNT
Definition PoolAllocator.hpp:94
static bool mInitialized
Definition PoolAllocator.hpp:104
static constexpr size_t MAX_UNIT_SIZE
Definition PoolAllocator.hpp:96
void print(std::ostream &os=std::cout, int indent=0) const override
Prints state of the memory allocator.
Definition PoolAllocator.cpp:217
std::array< MemoryUnit *, HEAP_COUNT > mFreeMemoryUnits
List of free memory units.
Definition PoolAllocator.hpp:136
static constexpr size_t BLOCK_SIZE
Definition PoolAllocator.hpp:98
static std::array< int, MAX_UNIT_SIZE+1 > mMapSizeToHeapIndex
Definition PoolAllocator.hpp:102
Definition Aspect.cpp:42
Definition PoolAllocator.hpp:89
MemoryUnit * mMemoryUnits
Pointer to the first memory unit.
Definition PoolAllocator.hpp:91
Definition PoolAllocator.hpp:83
MemoryUnit * mNext
Pointer to next memory block.
Definition PoolAllocator.hpp:85