Toggle navigation
PlayRho
0.9.0
GitHub
Main Page
Modules
Classes
Files
Examples
File List
File Members
Common
BlockAllocator.hpp
Go to the documentation of this file.
1
/*
2
* Original work Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
3
* Modified work Copyright (c) 2017 Louis Langholtz https://github.com/louis-langholtz/PlayRho
4
*
5
* This software is provided 'as-is', without any express or implied
6
* warranty. In no event will the authors be held liable for any damages
7
* arising from the use of this software.
8
* Permission is granted to anyone to use this software for any purpose,
9
* including commercial applications, and to alter it and redistribute it
10
* freely, subject to the following restrictions:
11
* 1. The origin of this software must not be misrepresented; you must not
12
* claim that you wrote the original software. If you use this software
13
* in a product, an acknowledgment in the product documentation would be
14
* appreciated but is not required.
15
* 2. Altered source versions must be plainly marked as such, and must not be
16
* misrepresented as being the original software.
17
* 3. This notice may not be removed or altered from any source distribution.
18
*/
19
20
#ifndef PLAYRHO_COMMON_BLOCKALLOCATOR_HPP
21
#define PLAYRHO_COMMON_BLOCKALLOCATOR_HPP
22
23
#include <
PlayRho/Common/Settings.hpp
>
24
25
namespace
playrho
{
26
28
PLAYRHO_CONSTEXPR
const
std::size_t
AllocatorBlockSizes
[] =
29
{
30
16, 32, 64, 96, 128, 160, 192, 224, 256, 320, 384, 448, 512, 640,
31
};
32
40
class
BlockAllocator
41
{
42
public
:
43
45
using
size_type
= std::size_t;
46
48
static
PLAYRHO_CONSTEXPR
const
auto
ChunkSize
=
size_type
{16 * 1024};
49
51
static
PLAYRHO_CONSTEXPR
size_type
GetMaxBlockSize
() noexcept
52
{
53
return
AllocatorBlockSizes
[
size
(
AllocatorBlockSizes
) - 1];
54
}
55
57
static
PLAYRHO_CONSTEXPR
size_type
GetChunkArrayIncrement
() noexcept
58
{
59
return
size_type
{128};
60
}
61
62
BlockAllocator
();
63
64
BlockAllocator
(
const
BlockAllocator
& other) =
delete
;
65
66
BlockAllocator
(
BlockAllocator
&& other) =
delete
;
67
68
~BlockAllocator
() noexcept;
69
70
BlockAllocator
& operator= (const
BlockAllocator
& other) = delete;
71
72
BlockAllocator
& operator= (
BlockAllocator
&& other) = delete;
73
81
void
*
Allocate
(
size_type
n);
82
84
template <typename T>
85
T*
AllocateArray
(
size_type
n)
86
{
87
return
static_cast<
T*
>
(
Allocate
(n *
sizeof
(T)));
88
}
89
92
void
Free
(
void
* p,
size_type
n);
93
96
void
Clear
();
97
99
auto
GetChunkCount
() const noexcept
100
{
101
return
m_chunkCount;
102
}
103
104
private
:
105
struct
Chunk;
106
struct
Block;
107
108
size_type
m_chunkCount = 0;
109
size_type
m_chunkSpace =
GetChunkArrayIncrement
();
110
Chunk* m_chunks;
111
Block* m_freeLists[
size
(
AllocatorBlockSizes
)];
112
};
113
116
template
<
typename
T>
117
inline
void
Delete
(
const
T* p,
BlockAllocator
& allocator)
118
{
119
p->~T();
120
allocator.
Free
(
const_cast<
T*
>
(p),
sizeof
(T));
121
}
122
124
struct
BlockDeallocator
125
{
127
using
size_type
=
BlockAllocator::size_type
;
128
129
BlockDeallocator
() =
default
;
130
132
PLAYRHO_CONSTEXPR
inline
BlockDeallocator
(
BlockAllocator
* a,
size_type
n) noexcept:
133
allocator
{a},
nelem
{n}
134
{
135
// Intentionally empty.
136
}
137
139
void
operator()
(
void
*p) noexcept
140
{
141
allocator
->
Free
(p,
nelem
);
142
}
143
144
BlockAllocator
*
allocator
;
145
size_type
nelem
;
146
};
147
149
inline
bool
operator==
(
const
BlockAllocator
& a,
const
BlockAllocator
& b)
150
{
151
return
&a == &b;
152
}
153
155
inline
bool
operator!=
(
const
BlockAllocator
& a,
const
BlockAllocator
& b)
156
{
157
return
&a != &b;
158
}
159
160
}
// namespace playrho
161
162
#endif // PLAYRHO_COMMON_BLOCKALLOCATOR_HPP