1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2020-2022 The Defold Foundation
// Copyright 2014-2020 King
// Copyright 2009-2014 Ragnar Svensson, Christian Murray
// Licensed under the Defold License version 1.0 (the "License"); you may not use
// this file except in compliance with the License.
//
// You may obtain a copy of the License, together with FAQs at
// https://www.defold.com/license
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#ifndef DMSDK_OBJECT_POOL_H
#define DMSDK_OBJECT_POOL_H
#include <stdint.h>
#include <dmsdk/dlib/array.h>
/*# SDK Object Pool API documentation
*
* @document
* @name ObjectPool
* @path engine/dlib/src/dmsdk/dlib/object_pool.h
*/
/*#
* Object pool data-structure with the following properties
* - Mapping from logical index to physical index
* - Logical index does not changes
* - Allocated objects are contiguously laid out in memory
* Loop of m_Objects [0..Size()-1] times to iterate all objects
* - Internal physical order is not preserved and a direct consequence of the
* contiguous property
*
* @struct
* @name dmObjectPool
*/
template <typename T>
class dmObjectPool
{
private:
struct Entry
{
uint32_t m_Physical;
uint32_t m_Next;
};
dmArray<T> m_Objects; // All objects [0..Size()]
dmArray<Entry> m_Entries;
dmArray<uint32_t> m_ToLogical;
uint32_t m_FirstFree;
public:
/*#
* Constructor
* @name dmObjectPool
* @param capacity
*/
dmObjectPool()
{
m_FirstFree = 0xffffffff;
}
/*#
* Set capacity. New capacity must be >= old_capacity
* @name SetCapacity
* @param capacity [type: uint32_t] max number of objects to store
*/
void SetCapacity(uint32_t capacity)
{
assert(capacity >= m_Objects.Capacity());
m_Entries.SetCapacity(capacity);
m_Objects.SetCapacity(capacity);
m_ToLogical.SetCapacity(capacity);
m_ToLogical.SetSize(capacity);
}
/*#
* Allocate a new object
* @name Alloc
* @return index [type:uint32_t] logical index
*/
uint32_t Alloc()
{
uint32_t size = m_Objects.Size();
Entry* e = 0;
if (m_FirstFree != 0xffffffff) {
e = &m_Entries[m_FirstFree];
m_FirstFree = e->m_Next;
} else {
m_Entries.SetSize(size + 1);
e = &m_Entries[size];
}
e->m_Next = 0xffffffff;
e->m_Physical = size;
m_Objects.SetSize(size + 1);
uint32_t index = e - m_Entries.Begin();
m_ToLogical[size] = index;
return index;
}
/*# get size
* Get number of objects currently stored
* @name Size
* @return size [type: uint32_t] returns the number of objects currently stored
*/
uint32_t Size()
{
return m_Objects.Size();
}
/*#
* Checks if the pool is full
* @name Full
* @return result [type: bool] returns true if the pool is full
*/
bool Full()
{
return m_Objects.Remaining() == 0;
}
/*#
* @name Capacity
* @return capacity [type: uint32_t] maximum number of objects
*/
uint32_t Capacity()
{
return m_Objects.Capacity();
}
/*#
* Returns object index to the object pool
*
* @name Free
* @param index [type: uint32_t] index of object
* @param clear [type: bool] If set, memset's the object memory
*/
void Free(uint32_t index, bool clear)
{
uint32_t size = m_Objects.Size();
Entry* e = &m_Entries[index];
uint32_t last = m_ToLogical[size - 1];
assert(e->m_Physical < size);
T* o = &m_Objects[e->m_Physical];
if (clear) {
memset(o, 0, sizeof(T));
}
// Remap logical/physical
m_Entries[last].m_Physical = e->m_Physical;
m_ToLogical[e->m_Physical] = last;
// Remove
m_Objects.EraseSwap(e->m_Physical);
// Put in free list
e->m_Next = m_FirstFree;
m_FirstFree = e - m_Entries.Begin();
}
/*#
* Get the array of currently active objects
* @note The order of objects in this array may change if Alloc() or Free() has been called
* @name GetRawObjects
* @return object [type: dmArray<T>&] a reference to the array of objects
*/
dmArray<T>& GetRawObjects()
{
return m_Objects;
}
/*#
* Get object from logical index
* @name Get
* @param index [type: uint32_t] index of the object
* @return object [type: T&] a reference to the object
*/
T& Get(uint32_t index)
{
Entry* e = &m_Entries[index];
T& o = m_Objects[e->m_Physical];
return o;
}
/*#
* Set object from logical index
* @name Set
* @param index [type: uint32_t] index of object
* @param object [type: T&] reference ot object. THe object stored is copied by value.
*/
void Set(uint32_t index, T& object)
{
Entry* e = &m_Entries[index];
m_Objects[e->m_Physical] = object;
}
};
#endif /* DMSDK_OBJECT_POOL_H */