SDXFrameWork 0.13 SDXFrameWork

- Main Page
- Related Pages
- Classes

All Classes Namespaces Functions Variables Enumerations Enumerator Pages
MemoryPool.h
1 #ifndef _MEMORY_POOL_H_
2 #define _MEMORY_POOL_H_
3
4 #include <memory>
5 #include <cstdlib>
6
7 namespace MemoryPool {
8
9 static const int DEFAULT_EXPAND_SIZE = 10000;
10
11 template <class T, std::size_t Size = DEFAULT_EXPAND_SIZE>
12 class MemoryPool {
13 public:
14 MemoryPool() : next(0) {
15 expandFreeList(Size);
16 }
17
18 ~MemoryPool() {
19 MemoryPool<T, Size>* nextPtr = next;
20 for (nextPtr = next; nextPtr != 0; nextPtr = next) {
21 next = next->next;
22 std::free(nextPtr);
23 }
24 }
25
26 void* alloc(std::size_t) {
27 if (!next) expandFreeList(Size);
28 MemoryPool<T, Size>* head = next;
29 next = head->next;
30 return head;
31 }
32
33 void free(void* freePtr) {
34 MemoryPool<T, Size>* head = static_cast<MemoryPool<T, Size>*>(freePtr);
35 head->next = next;
36 next = head;
37 }
38
39 private:
40 void expandFreeList(int howMany) {
41 std::size_t size =
42 (sizeof(T) > sizeof(MemoryPool<T, Size>*)) ? sizeof(T) : sizeof(MemoryPool<T, Size>*);
43
44 MemoryPool<T, Size>* runner = reinterpret_cast<MemoryPool<T, Size>*>(std::malloc(size));
45 next = runner;
46
47 for (int i = 0; i < howMany; ++i) {
48 runner->next = reinterpret_cast<MemoryPool<T, Size>*>(std::malloc(size));
49 runner = runner->next;
50 }
51 runner->next = 0;
52 }
53
54 private:
55 MemoryPool<T, Size>* next;
56 };
57
58 template <class T, std::size_t Size = DEFAULT_EXPAND_SIZE>
59 class AppendMemoryPool {
60 public:
61 void* operator new(std::size_t size) { return pool->alloc(size); }
62 void operator delete(void* deletePtr, std::size_t) { pool->free(deletePtr); }
63 void* operator new[](std::size_t size) { return pool->alloc(size); }
64 void operator delete[](void* deletePtr, std::size_t) { pool->free(deletePtr); }
65
66 static void init() { pool.reset(new MemoryPool<T, Size>); }
67
68 private:
69 static std::auto_ptr<MemoryPool<T, Size> > pool;
70 };
71
72 template <class T, std::size_t Size = DEFAULT_EXPAND_SIZE>
73 class MakeMemoryPool {
74 public:
75 MakeMemoryPool() : pool(new MemoryPool<T, Size>) {}
76 ~MakeMemoryPool() {}
77
78 T* poolNew() {
79 T* allocPtr = static_cast<T*>(pool->alloc(sizeof(T)));
80 return static_cast<T*>(new(allocPtr) T());
81 }
82
83 void poolDelete(T* deletePtr) {
84 deletePtr->~T();
85 pool->free(deletePtr);
86 }
87
88 private:
89 std::auto_ptr<MemoryPool<T, Size> > pool;
90 };
91
92 struct DataPool16 { char data[16]; };
93 struct DataPool32 { char data[32]; };
94 struct DataPool64 { char data[64]; };
95 struct DataPool128 { char data[128]; };
96 struct DataPool256 { char data[256]; };
97
98 class GeneralMemoryPool {
99 public:
100 GeneralMemoryPool() {}
101 ~GeneralMemoryPool() {}
102
103 void* poolAlloc(std::size_t size) {
104 void* allocPtr = NULL;
105 if (size < 16) { allocPtr = mp16.alloc(size); }
106 else if (size < 32) { allocPtr = mp32.alloc(size); }
107 else if (size < 64) { allocPtr = mp64.alloc(size); }
108 else if (size < 128) { allocPtr = mp128.alloc(size); }
109 else if (size < 256) { allocPtr = mp256.alloc(size); }
110 return allocPtr;
111 }
112
113 void poolFree(void* freePtr, std::size_t size) {
114 if (size < 16) { mp16.free(freePtr); }
115 else if (size < 32) { mp32.free(freePtr); }
116 else if (size < 64) { mp64.free(freePtr); }
117 else if (size < 128) { mp128.free(freePtr); }
118 else if (size < 256) { mp256.free(freePtr); }
119 }
120
121 private:
122 MemoryPool<DataPool16> mp16;
123 MemoryPool<DataPool32> mp32;
124 MemoryPool<DataPool64> mp64;
125 MemoryPool<DataPool128> mp128;
126 MemoryPool<DataPool256> mp256;
127 };
128
129 template <class T, std::size_t Size>
130 std::auto_ptr<MemoryPool<T, Size> > AppendMemoryPool<T, Size>::pool;
131
132 } /* end of namespace MemoryPool */
133
134 #endif
MemoryPool::GeneralMemoryPool
Definition: MemoryPool.h:98
MemoryPool::AppendMemoryPool
Definition: MemoryPool.h:59
MemoryPool::DataPool128
Definition: MemoryPool.h:95
MemoryPool::MakeMemoryPool
Definition: MemoryPool.h:73
MemoryPool::DataPool256
Definition: MemoryPool.h:96
MemoryPool::DataPool16
Definition: MemoryPool.h:92
MemoryPool::MemoryPool
Definition: MemoryPool.h:12
MemoryPool::DataPool64
Definition: MemoryPool.h:94
MemoryPool::DataPool32
Definition: MemoryPool.h:93