DART 6.13.2
Loading...
Searching...
No Matches
MemoryManager.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_MEMORYMANAGER_HPP_
34#define DART_COMMON_MEMORYMANAGER_HPP_
35
36#ifndef NDEBUG
37 #include <mutex>
38#endif
39#include <iostream>
40
43
44namespace dart::common {
45
48class MemoryManager final
49{
50public:
52 enum class Type
53 {
54 Base,
55 Free,
56 Pool,
57 };
58
60 [[nodiscard]] static MemoryManager& GetDefault();
61
66 explicit MemoryManager(
68
71
73 [[nodiscard]] MemoryAllocator& getBaseAllocator();
74
77
79 [[nodiscard]] PoolAllocator& getPoolAllocator();
80
88 [[nodiscard]] void* allocate(Type type, size_t bytes);
89
96 [[nodiscard]] void* allocateUsingFree(size_t bytes);
97
104 [[nodiscard]] void* allocateUsingPool(size_t bytes);
105
112 void deallocate(Type type, void* pointer, size_t bytes);
113 // TODO(JS): Make this constexpr once migrated to C++20
114
115 void deallocateUsingFree(void* pointer, size_t bytes);
116
117 void deallocateUsingPool(void* pointer, size_t bytes);
118
127 template <typename T, typename... Args>
128 [[nodiscard]] T* construct(Type type, Args&&... args) noexcept;
129
132 template <typename T, typename... Args>
133 [[nodiscard]] T* constructUsingFree(Args&&... args) noexcept;
134
137 template <typename T, typename... Args>
138 [[nodiscard]] T* constructUsingPool(Args&&... args) noexcept;
139
141 template <typename T>
142 void destroy(Type type, T* object) noexcept;
143
146 template <typename T>
147 void destroyUsingFree(T* pointer) noexcept;
148
151 template <typename T>
152 void destroyUsingPool(T* pointer) noexcept;
153
154#ifndef NDEBUG
156 [[nodiscard]] bool hasAllocated(void* pointer, size_t size) const noexcept;
157#endif
158
160 void print(std::ostream& os = std::cout, int indent = 0) const;
161
163 friend std::ostream& operator<<(
164 std::ostream& os, const MemoryManager& memoryManager);
165
166private:
169
170#ifdef NDEBUG
173
176#else
179
182#endif
183};
184
185} // namespace dart::common
186
188
189#endif // DART_COMMON_MEMORYMANAGER_HPP_
std::string type
Definition SdfParser.cpp:82
Most general heap memory allocator for allocating memory of various sizes.
Definition FreeListAllocator.hpp:57
Base class for std::allocator compatible allocators.
Definition MemoryAllocator.hpp:46
static MemoryAllocator & GetDefault()
Returns the default memory allocator.
Definition MemoryAllocator.cpp:41
A composite memory allocator that contains various memory allocators that are optimized for different...
Definition MemoryManager.hpp:49
bool hasAllocated(void *pointer, size_t size) const noexcept
Returns true if a pointer is allocated by the internal allocator.
Definition MemoryManager.cpp:151
~MemoryManager()
Destructor.
Definition MemoryManager.cpp:62
void * allocateUsingFree(size_t bytes)
Allocates size bytes of uninitialized storage using FreeListAllocator.
Definition MemoryManager.cpp:109
static MemoryManager & GetDefault()
Returns the default memory manager.
Definition MemoryManager.cpp:42
T * constructUsingPool(Args &&... args) noexcept
Allocates uninitialized storage using PoolAllocator and constructs an object of type T to the allocat...
Definition MemoryManager-impl.hpp:74
void destroyUsingPool(T *pointer) noexcept
Calls the destructor of the object and deallocate the storage using PoolAllocator.
Definition MemoryManager-impl.hpp:100
MemoryAllocator & getBaseAllocator()
Returns the base allocator.
Definition MemoryManager.cpp:68
FreeListAllocator & getFreeListAllocator()
Returns the free list allocator.
Definition MemoryManager.cpp:74
void * allocate(Type type, size_t bytes)
Allocates size bytes of uninitialized storage.
Definition MemoryManager.cpp:94
PoolAllocator & getPoolAllocator()
Returns the pool allocator.
Definition MemoryManager.cpp:84
MemoryAllocator & mBaseAllocator
The base allocator to allocate memory chunck.
Definition MemoryManager.hpp:168
void print(std::ostream &os=std::cout, int indent=0) const
Prints state of the memory manager.
Definition MemoryManager.cpp:164
FreeListAllocator::Debug mFreeListAllocator
The free list allocator.
Definition MemoryManager.hpp:178
void * allocateUsingPool(size_t bytes)
Allocates size bytes of uninitialized storage using PoolAllocator.
Definition MemoryManager.cpp:115
T * construct(Type type, Args &&... args) noexcept
Allocates uninitialized storage and constructs an object of type T to the allocated storage.
Definition MemoryManager-impl.hpp:42
void deallocate(Type type, void *pointer, size_t bytes)
Deallocates the storage referenced by the pointer p, which must be a pointer obtained by an earlier c...
Definition MemoryManager.cpp:121
Type
Type of the memory allocators.
Definition MemoryManager.hpp:53
PoolAllocator::Debug mPoolAllocator
The pool allocator.
Definition MemoryManager.hpp:181
friend std::ostream & operator<<(std::ostream &os, const MemoryManager &memoryManager)
Prints state of the memory manager.
Definition MemoryManager.cpp:180
void deallocateUsingPool(void *pointer, size_t bytes)
Definition MemoryManager.cpp:144
void destroy(Type type, T *object) noexcept
Calls the destructor of the object and deallocate the storage.
Definition MemoryManager-impl.hpp:81
void destroyUsingFree(T *pointer) noexcept
Calls the destructor of the object and deallocate the storage using FreeListAllocator.
Definition MemoryManager-impl.hpp:93
T * constructUsingFree(Args &&... args) noexcept
Allocates uninitialized storage using FreeListAllocator and constructs an object of type T to the all...
Definition MemoryManager-impl.hpp:67
void deallocateUsingFree(void *pointer, size_t bytes)
Definition MemoryManager.cpp:138
Memory allocator optimized for allocating many objects of the same or similar sizes.
Definition PoolAllocator.hpp:47
Definition Aspect.cpp:42