forked from xNVSE/NVSE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIObjectPool.h
More file actions
68 lines (54 loc) · 1.31 KB
/
Copy pathIObjectPool.h
File metadata and controls
68 lines (54 loc) · 1.31 KB
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
#pragma once
#include "common/ITypes.h"
#include "common/IErrors.h"
#pragma warning(push)
#pragma warning(disable: 4804)
/**
* A memory pool of statically allocated objects
*/
template <class T, UInt32 numObjects>
class IObjectPool
{
public:
IObjectPool() { lastFreed = 0; ASSERT_STR(numObjects > 0, "IObjectPool: bad numObjects"); }
~IObjectPool() { }
//! Get an object from the pool
T & Alloc(void)
{
UInt32 traverse = lastFreed;
for(UInt32 i = 0; i < numObjects; i++)
{
if(!pool[traverse].allocated)
return pool[traverse].data;
traverse++;
if(traverse > numObjects)
traverse = 0;
}
HALT("IObjectPool::Alloc: couldn't find free entry");
return pool[0].data;
}
//! Release an object back to the pool
void Free(T & in)
{
for(UInt32 i = 0; i < numObjects; i++)
{
if(pool[i].allocated && (&in == &pool[i].data))
{
pool[i].allocated = 0;
lastFreed = i;
return;
}
}
HALT("IObjectPool::Free: object not in list");
}
private:
//! Object storage with an "allocated" flag
struct Pair
{
T data; //!< the object
UInt32 allocated; //!< is this object allocated?
};
Pair pool[numObjects]; //!< the object pool
UInt32 lastFreed; //!< the last freed object
};
#pragma warning(pop)