ZFFramework
Loading...
Searching...
No Matches
ZFMemPool.h
Go to the documentation of this file.
1
5#ifndef _ZFI_ZFMemPool_h_
6#define _ZFI_ZFMemPool_h_
7
8#include "ZFCoreMutex.h"
9
11
15#ifndef ZF_ENV_ZFMEMPOOL_ENABLE
16 #if _ZFP_ZFMEM_LOG_DISABLE_MEMPOOL
17 #define ZF_ENV_ZFMEMPOOL_ENABLE 0
18 #else
19 #define ZF_ENV_ZFMEMPOOL_ENABLE 1
20 #endif
21#endif
22
23// ============================================================
34#if ZF_ENV_ZFMEMPOOL_ENABLE
35 #define zfpoolNew(T_Type, ...) zfnewPlacement((_ZFP_MP_Obj<T_Type >::pNew()), T_Type, ##__VA_ARGS__)
36 #define zfpoolDelete(obj) _ZFP_zfpoolDelete(obj)
37 #define zfpoolDeclareFriend() \
38 friend zfclassFwd _ZFP_MP_Obj<zfself>;
39#else
40 #define zfpoolNew(T_Type, ...) zfnew(T_Type, ##__VA_ARGS__)
41 #define zfpoolDelete(obj) zfdelete(obj)
42 #define zfpoolDeclareFriend() zfmemDeclareFriend()
43#endif
44
55#if ZF_ENV_ZFMEMPOOL_ENABLE
56 #define zfpoolMalloc(size) _ZFP_MP_malloc(size)
57 #define zfpoolRealloc(p, size) _ZFP_MP_realloc((p), (size))
58 #define zfpoolFree(p) _ZFP_MP_free(p)
59#else
60 #define zfpoolMalloc(size) zfmalloc(size)
61 #define zfpoolRealloc(p, size) zfrealloc((p), (size))
62 #define zfpoolFree(p) zffree(p)
63#endif
64
65#if ZF_ENV_ZFMEMPOOL_ENABLE
66template<int N>
67zfclassNotPOD _ZFP_MP_SA { // Size Align
68public:
69 enum {
70 _A = (N <= sizeof(void *) * 4
71 ? sizeof(void *) * 2
72 : N <= sizeof(void *) * 32
73 ? sizeof(void *) * 4
74 : sizeof(void *) * 32
75 ),
76 V = ((N % _A) == 0 ? N : ((N / _A) + 1) * _A),
77 M = (N <= sizeof(void *) * 4
78 ? 32
79 : N <= sizeof(void *) * 8
80 ? 16
81 : N <= sizeof(void *) * 32
82 ? 8
83 : N <= sizeof(void *) * 256
84 ? 4
85 : 0
86 ),
87 };
88};
89template<int N>
90union ZFLIB_ZFCore _ZFP_MP_B { // Block
91public:
92 zfbyte buf[N];
93 _ZFP_MP_B<N> *next;
94};
95template<int N>
96zfclassNotPOD _ZFP_MP_H { // Holder
97public:
98 static void *pNew(void) {
99 _ZFP_MP_H<N> &d = _instance();
100 if(d.available) {
101 _ZFP_MP_B<N> *t = d.available;
102 d.available = d.available->next;
103 --d.count;
104 return t;
105 }
106 else {
107 return zfmalloc(sizeof(_ZFP_MP_B<N>));
108 }
109 }
110 static void pDel(ZF_IN void *obj) {
111 _ZFP_MP_H<N> &d = _instance();
112 if(d.count >= _ZFP_MP_SA<N>::M) {
113 zffree(obj);
114 }
115 else {
116 ++d.count;
117 _ZFP_MP_B<N> *t = (_ZFP_MP_B<N> *)obj;
118 t->next = d.available;
119 d.available = t;
120 }
121 }
122private:
123 _ZFP_MP_H(void)
124 : available(zfnull)
125 , count(0)
126 {
127 }
128 ~_ZFP_MP_H(void) {
129 while(available) {
130 _ZFP_MP_B<N> *t = available;
131 available = available->next;
132 zffree(t);
133 }
134 }
135 static _ZFP_MP_H<N> &_instance(void) {
136 static _ZFP_MP_H<N> d;
137 return d;
138 }
139private:
140 _ZFP_MP_B<N> *available;
141 zfuint count;
142};
143
144template<typename T_Type>
145zfclassNotPOD _ZFP_MP_Obj {
146public:
147 static void *pNew(void) {
149 return _ZFP_MP_H<_ZFP_MP_SA<sizeof(T_Type)>::V>::pNew();
150 }
151 static void pDel(ZF_IN T_Type *obj) {
153 obj->~T_Type();
154 _ZFP_MP_H<_ZFP_MP_SA<sizeof(T_Type)>::V>::pDel(obj);
155 }
156};
157template<typename T_Type>
158inline void _ZFP_zfpoolDelete(ZF_IN T_Type *obj) {
159 if(obj) {
160 _ZFP_MP_Obj<T_Type>::pDel(obj);
161 }
162}
163
164// ============================================================
165template<int N>
166zfclassNotPOD _ZFP_MP_mallocSA {
167public:
168 enum {
169 V = _ZFP_MP_SA<_ZFP_MP_mallocSA<N - 1>::V + 1>::V,
170 };
171};
172template<>
173zfclassNotPOD _ZFP_MP_mallocSA<1> {
174public:
175 enum {
176 V = _ZFP_MP_SA<1>::V,
177 };
178};
179
180inline void *_ZFP_MP_mallocFix(ZF_IN void *p, ZF_IN zfindex size) {
181 if(p) {
182 *(zfindex *)p = size;
183 return (((zfbyte *)p) + sizeof(void *));
184 }
185 else {
186 return zfnull;
187 }
188}
189inline void *_ZFP_MP_malloc(ZF_IN zfindex size) {
191 if(zffalse){
192 }
193 else if(size <= _ZFP_MP_mallocSA<1>::V - sizeof(void *)) {
194 return _ZFP_MP_mallocFix(_ZFP_MP_H<_ZFP_MP_mallocSA<1>::V>::pNew(), size);
195 }
196 else if(size <= _ZFP_MP_mallocSA<2>::V - sizeof(void *)) {
197 return _ZFP_MP_mallocFix(_ZFP_MP_H<_ZFP_MP_mallocSA<2>::V>::pNew(), size);
198 }
199 else if(size <= _ZFP_MP_mallocSA<3>::V - sizeof(void *)) {
200 return _ZFP_MP_mallocFix(_ZFP_MP_H<_ZFP_MP_mallocSA<3>::V>::pNew(), size);
201 }
202 else if(size <= _ZFP_MP_mallocSA<4>::V - sizeof(void *)) {
203 return _ZFP_MP_mallocFix(_ZFP_MP_H<_ZFP_MP_mallocSA<4>::V>::pNew(), size);
204 }
205 else if(size <= _ZFP_MP_mallocSA<5>::V - sizeof(void *)) {
206 return _ZFP_MP_mallocFix(_ZFP_MP_H<_ZFP_MP_mallocSA<5>::V>::pNew(), size);
207 }
208 else {
209 return _ZFP_MP_mallocFix(zfmalloc(size + sizeof(void *)), size);
210 }
211}
212inline void _ZFP_MP_free(ZF_IN void *p) {
214 if(p == zfnull) {
215 return;
216 }
217 p = ((zfbyte *)p) - sizeof(void *);
218 zfindex size = *(zfindex *)p;
219 if(zffalse){
220 }
221 else if(size <= _ZFP_MP_mallocSA<1>::V - sizeof(void *)) {
222 _ZFP_MP_H<_ZFP_MP_mallocSA<1>::V>::pDel(p);
223 }
224 else if(size <= _ZFP_MP_mallocSA<2>::V - sizeof(void *)) {
225 _ZFP_MP_H<_ZFP_MP_mallocSA<2>::V>::pDel(p);
226 }
227 else if(size <= _ZFP_MP_mallocSA<3>::V - sizeof(void *)) {
228 _ZFP_MP_H<_ZFP_MP_mallocSA<3>::V>::pDel(p);
229 }
230 else if(size <= _ZFP_MP_mallocSA<4>::V - sizeof(void *)) {
231 _ZFP_MP_H<_ZFP_MP_mallocSA<4>::V>::pDel(p);
232 }
233 else if(size <= _ZFP_MP_mallocSA<5>::V - sizeof(void *)) {
234 _ZFP_MP_H<_ZFP_MP_mallocSA<5>::V>::pDel(p);
235 }
236 else {
237 zffree(p);
238 }
239}
240inline void *_ZFP_MP_realloc(ZF_IN void *p, ZF_IN zfindex size) {
242 if(p == zfnull) {
243 return _ZFP_MP_malloc(size);
244 }
245 zfindex sizeOld = *(zfindex *)(((zfbyte *)p) - sizeof(void *));
246 if(size <= sizeOld) {
247 return p;
248 }
249 else if(sizeOld <= _ZFP_MP_mallocSA<1>::V - sizeof(void *)) {
250 if(size <= _ZFP_MP_mallocSA<1>::V - sizeof(void *)) {
251 return p;
252 }
253 }
254 else if(sizeOld <= _ZFP_MP_mallocSA<2>::V - sizeof(void *)) {
255 if(size <= _ZFP_MP_mallocSA<2>::V - sizeof(void *)) {
256 return p;
257 }
258 }
259 else if(sizeOld <= _ZFP_MP_mallocSA<3>::V - sizeof(void *)) {
260 if(size <= _ZFP_MP_mallocSA<3>::V - sizeof(void *)) {
261 return p;
262 }
263 }
264 else if(sizeOld <= _ZFP_MP_mallocSA<4>::V - sizeof(void *)) {
265 if(size <= _ZFP_MP_mallocSA<4>::V - sizeof(void *)) {
266 return p;
267 }
268 }
269 else if(sizeOld <= _ZFP_MP_mallocSA<5>::V - sizeof(void *)) {
270 if(size <= _ZFP_MP_mallocSA<5>::V - sizeof(void *)) {
271 return p;
272 }
273 }
274 void *pNew = _ZFP_MP_malloc(size);
275 if(pNew == zfnull) {
276 return zfnull;
277 }
278 zfmemcpy(pNew, p, sizeOld);
279 _ZFP_MP_free(p);
280 return pNew;
281}
282#endif // #if ZF_ENV_ZFMEMPOOL_ENABLE
283
285
286#endif // #ifndef _ZFI_ZFMemPool_h_
287
#define ZFLIB_ZFCore
used to export symbols
Definition ZFCoreEnvDef.h:30
core mutex
#define ZFCoreMutexLocker()
util method to lock current block
Definition ZFCoreMutex.h:95
#define zffree(ptr)
same as free defined for future use, do nothing if ptr is NULL
Definition ZFCoreTypeDef_ClassType.h:104
#define ZF_IN
dummy macro that shows the param used as required input
Definition ZFCoreTypeDef_ClassType.h:191
#define zfclassNotPOD
shows the class is not a POD type, you should not memset it or declare it in stack or copy value by c...
Definition ZFCoreTypeDef_ClassType.h:48
void * zfmemcpy(void *dst, const void *src, zfindex size)
wrapper to memcpy
Definition ZFCoreTypeDef_ClassType.h:151
#define zfmalloc(size)
same as malloc defined for future use
Definition ZFCoreTypeDef_ClassType.h:90
_ZFT_t_zfindex zfindex
similar to size_t, used for index and size only
Definition ZFCoreTypeDef_CoreType.h:154
#define zffalse
bool false type
Definition ZFCoreTypeDef_CoreType.h:111
#define zfnull
same as NULL, defined for future use
Definition ZFCoreTypeDef_CoreType.h:88
_ZFT_t_zfbyte zfbyte
8-bit unsigned value, see zfindex
Definition ZFCoreTypeDef_CoreType.h:194
_ZFT_t_zfuint zfuint
same as unsigned int, see zfindex
Definition ZFCoreTypeDef_CoreType.h:169
#define ZF_NAMESPACE_GLOBAL_BEGIN
begin namespace ZFFramework
Definition ZFNamespace.h:97
#define ZF_NAMESPACE_GLOBAL_END
end namespace ZFFramework
Definition ZFNamespace.h:98