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
|
#ifndef NW4HBM_SND_INSTANCE_POOL_H
#define NW4HBM_SND_INSTANCE_POOL_H
#include <new>
#include <revolution/types.h>
namespace nw4hbm {
namespace snd {
namespace detail {
class PoolImpl {
public:
PoolImpl() : mNext(NULL) {}
protected:
u32 CreateImpl(void* buffer, u32 size, u32 objSize);
void DestroyImpl(void* buffer, u32 size);
int CountImpl() const;
void* AllocImpl();
void FreeImpl(void* ptr);
private:
/* 0x00 */ PoolImpl* mNext;
};
template <typename T>
class InstancePool : private PoolImpl {
public:
u32 Create(void* buffer, u32 size) { return CreateImpl(buffer, size, sizeof(T)); }
void Destroy(void* buffer, u32 size) { DestroyImpl(buffer, size); }
int Count() const { return CountImpl(); }
T* Alloc() {
void* ptr = AllocImpl();
if (ptr == NULL) {
return NULL;
}
return new (ptr) T;
}
void Free(T* obj) {
if (obj != NULL) {
obj->~T();
FreeImpl(obj);
}
}
};
template <typename T>
class MemoryPool : private PoolImpl {
public:
u32 Create(void* buffer, u32 size) { return CreateImpl(buffer, size, sizeof(T)); }
void Destroy(void* buffer, u32 size) { DestroyImpl(buffer, size); }
int Count() const { return CountImpl(); }
T* Alloc() { return static_cast<T*>(AllocImpl()); }
void Free(T* obj) { FreeImpl(obj); }
};
} // namespace detail
} // namespace snd
} // namespace nw4hbm
#endif
|