DART 6.13.2
Loading...
Searching...
No Matches
FreeListAllocator.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_FREELISTALLOCATOR_HPP_
34#define DART_COMMON_FREELISTALLOCATOR_HPP_
35
36#include <mutex>
37
40
41namespace dart::common {
42
57{
58public:
60
65 explicit FreeListAllocator(
67 size_t initialAllocation = 1048576 /* 1 MB */);
68
70 ~FreeListAllocator() override;
71
73
75 [[nodiscard]] const MemoryAllocator& getBaseAllocator() const;
76
78 [[nodiscard]] MemoryAllocator& getBaseAllocator();
79
80 // Documentation inherited
81 [[nodiscard]] void* allocate(size_t bytes) noexcept override;
82
83 // Documentation inherited
84 void deallocate(void* pointer, size_t bytes) override;
85
86 // Documentation inherited
87 void print(std::ostream& os = std::cout, int indent = 0) const override;
88
89private:
91 {
93 size_t mSize;
94
97
100
103
106
108 explicit MemoryBlockHeader(
109 size_t size,
110 MemoryBlockHeader* prev,
111 MemoryBlockHeader* next,
112 bool isNextContiguous);
113
115 size_t asSizeT() const;
116
118 unsigned char* asCharPtr();
119
121 const unsigned char* asCharPtr() const;
122
124 void split(size_t sizeToSplit);
125
127 void merge(MemoryBlockHeader* other);
128
129#ifndef NDEBUG
131 bool isValid() const;
132#endif
133 };
134
139 bool allocateMemoryBlock(size_t sizeToAllocate);
140
143
145 mutable std::mutex mMutex;
146
149
152
155
158};
159
160} // namespace dart::common
161
162#endif // DART_COMMON_FREELISTALLOCATOR_HPP_
#define DART_STRING_TYPE(type_name)
Definition Castable.hpp:38
Most general heap memory allocator for allocating memory of various sizes.
Definition FreeListAllocator.hpp:57
bool allocateMemoryBlock(size_t sizeToAllocate)
Allocates a new memory block for sizeToAllocate bytes.
Definition FreeListAllocator.cpp:240
MemoryBlockHeader * mFirstMemoryBlock
Pointer to the first memory block.
Definition FreeListAllocator.hpp:148
MemoryAllocator & mBaseAllocator
The base allocator.
Definition FreeListAllocator.hpp:142
const MemoryAllocator & getBaseAllocator() const
Returns the base allocator.
Definition FreeListAllocator.cpp:108
size_t mTotalAllocatedSize
The total allocated size in bytes.
Definition FreeListAllocator.hpp:157
std::mutex mMutex
Mutex for private variables except the base allocator.
Definition FreeListAllocator.hpp:145
void print(std::ostream &os=std::cout, int indent=0) const override
Prints state of the memory allocator.
Definition FreeListAllocator.cpp:270
MemoryBlockHeader * mFreeBlock
Pointer to the current free memory block.
Definition FreeListAllocator.hpp:151
size_t mTotalAllocatedBlockSize
The total allocated block size in bytes.
Definition FreeListAllocator.hpp:154
void * allocate(size_t bytes) noexcept override
Allocates size bytes of uninitialized storage.
Definition FreeListAllocator.cpp:120
~FreeListAllocator() override
Destructor.
Definition FreeListAllocator.cpp:50
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 FreeListAllocator.cpp:198
Base class for std::allocator compatible allocators.
Definition MemoryAllocator.hpp:46
static MemoryAllocator & GetDefault()
Returns the default memory allocator.
Definition MemoryAllocator.cpp:41
Definition Aspect.cpp:42
Definition FreeListAllocator.hpp:91
MemoryBlockHeader * mPrev
Pointer to previous memory block.
Definition FreeListAllocator.hpp:96
bool mIsAllocated
Whether this block is used.
Definition FreeListAllocator.hpp:102
void merge(MemoryBlockHeader *other)
Merges this memory block with the given memory block.
Definition FreeListAllocator.cpp:375
size_t mSize
Memory block size in bytes.
Definition FreeListAllocator.hpp:93
unsigned char * asCharPtr()
Casts to unsigned char*.
Definition FreeListAllocator.cpp:328
bool isValid() const
[Debug only] Returns whether this memory block is valid
Definition FreeListAllocator.cpp:399
bool mIsNextContiguous
Whether the next memory block is contiguous.
Definition FreeListAllocator.hpp:105
void split(size_t sizeToSplit)
Splits the memory block.
Definition FreeListAllocator.cpp:340
size_t asSizeT() const
Casts to size_t.
Definition FreeListAllocator.cpp:322
MemoryBlockHeader * mNext
Pointer to next memory block.
Definition FreeListAllocator.hpp:99