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// ============================================================
28#if ZF_ENV_ZFMEMPOOL_ENABLE
29 #define zfpoolDeclareFriend() \
30 friend zfclassFwd _ZFP_MP_Obj<zfself>;
31#else
32 #define zfpoolDeclareFriend() zfmemDeclareFriend()
33#endif
34
35// ============================================================
43#if ZF_ENV_ZFMEMPOOL_ENABLE
44 #define zfunsafe_zfpoolNew(T_Type, ...) zfnewPlacement((_ZFP_MP_Obj<T_Type >::pNew()), T_Type, ##__VA_ARGS__)
45 #define zfunsafe_zfpoolDelete(obj) _ZFP_zfpoolDelete(obj)
46#else
47 #define zfunsafe_zfpoolNew(T_Type, ...) zfnew(T_Type, ##__VA_ARGS__)
48 #define zfunsafe_zfpoolDelete(obj) zfdelete(obj)
49#endif
50
61#if ZF_ENV_ZFMEMPOOL_ENABLE
62 #define zfunsafe_zfpoolMalloc(size) _ZFP_MP_malloc(size)
63 #define zfunsafe_zfpoolRealloc(p, size) _ZFP_MP_realloc((p), (size))
64 #define zfunsafe_zfpoolFree(p) _ZFP_MP_free(p)
65#else
66 #define zfunsafe_zfpoolMalloc(size) zfmalloc(size)
67 #define zfunsafe_zfpoolRealloc(p, size) zfrealloc((p), (size))
68 #define zfunsafe_zfpoolFree(p) zffree(p)
69#endif
70
71// ============================================================
79#if ZF_ENV_ZFMEMPOOL_ENABLE
80 #define zfpoolNew(T_Type, ...) (ZFCoreMutexLockerHolder(), zfunsafe_zfpoolNew(T_Type, ##__VA_ARGS__))
81 #define zfpoolDelete(obj) (ZFCoreMutexLockerHolder(), zfunsafe_zfpoolDelete(obj))
82#else
83 #define zfpoolNew(T_Type, ...) zfnew(T_Type, ##__VA_ARGS__)
84 #define zfpoolDelete(obj) zfdelete(obj)
85#endif
86
97#if ZF_ENV_ZFMEMPOOL_ENABLE
98 #define zfpoolMalloc(size) (ZFCoreMutexLockerHolder(), zfunsafe_zfpoolMalloc(size))
99 #define zfpoolRealloc(p, size) (ZFCoreMutexLockerHolder(), zfunsafe_zfpoolRealloc((p), (size)))
100 #define zfpoolFree(p) (ZFCoreMutexLockerHolder(), zfunsafe_zfpoolFree(p))
101#else
102 #define zfpoolMalloc(size) zfmalloc(size)
103 #define zfpoolRealloc(p, size) zfrealloc((p), (size))
104 #define zfpoolFree(p) zffree(p)
105#endif
106
107// ============================================================
108zfclassPOD ZFLIB_ZFCore _ZFP_MP_D {
109public:
110 void *available;
111 zfuint count;
112};
113extern ZFLIB_ZFCore _ZFP_MP_D &_ZFP_MP_A(ZF_IN zfuint size);
114#if ZF_ENV_ZFMEMPOOL_ENABLE
115template<int N>
116zfclassNotPOD _ZFP_MP_SA { // Size Align
117public:
118 enum {
119 _A = (N <= sizeof(void *) * 4
120 ? sizeof(void *) * 2
121 : N <= sizeof(void *) * 32
122 ? sizeof(void *) * 4
123 : sizeof(void *) * 32
124 ),
125 V = ((N % _A) == 0 ? N : ((N / _A) + 1) * _A),
126 M = (N <= sizeof(void *) * 4
127 ? 32
128 : N <= sizeof(void *) * 8
129 ? 16
130 : N <= sizeof(void *) * 32
131 ? 8
132 : N <= sizeof(void *) * 256
133 ? 4
134 : 0
135 ),
136 };
137};
138template<int N>
139union ZFLIB_ZFCore _ZFP_MP_B { // Block
140public:
141 zfbyte buf[N];
142 _ZFP_MP_B<N> *next;
143};
144template<int N>
145zfclassNotPOD _ZFP_MP_H { // Holder
146public:
147 static void *pNew(void) {
148 _ZFP_MP_D &d = _instance();
149 if(d.available) {
150 _ZFP_MP_B<N> *t = (_ZFP_MP_B<N> *)d.available;
151 d.available = t->next;
152 --d.count;
153 return t;
154 }
155 else {
156 return zfmalloc(sizeof(_ZFP_MP_B<N>));
157 }
158 }
159 static void pDel(ZF_IN void *obj) {
160 _ZFP_MP_D &d = _instance();
161 if(d.count >= _ZFP_MP_SA<N>::M) {
162 zffree(obj);
163 }
164 else {
165 ++d.count;
166 _ZFP_MP_B<N> *t = (_ZFP_MP_B<N> *)obj;
167 t->next = (_ZFP_MP_B<N> *)d.available;
168 d.available = t;
169 }
170 }
171private:
172 static _ZFP_MP_D &_instance(void) {
173 static _ZFP_MP_D &d = _ZFP_MP_A(N);
174 return d;
175 }
176};
177
178template<typename T_Type>
179zfclassNotPOD _ZFP_MP_Obj {
180public:
181 static void *pNew(void) {
182 return _ZFP_MP_H<_ZFP_MP_SA<sizeof(T_Type)>::V>::pNew();
183 }
184 static void pDel(ZF_IN T_Type *obj) {
185 obj->~T_Type();
186 _ZFP_MP_H<_ZFP_MP_SA<sizeof(T_Type)>::V>::pDel(obj);
187 }
188};
189template<typename T_Type>
190inline void _ZFP_zfpoolDelete(ZF_IN T_Type *obj) {
191 if(obj) {
192 _ZFP_MP_Obj<T_Type>::pDel(obj);
193 }
194}
195
196// ============================================================
197template<int N>
198zfclassNotPOD _ZFP_MP_mallocSA {
199public:
200 enum {
201 V = _ZFP_MP_SA<_ZFP_MP_mallocSA<N - 1>::V + 1>::V,
202 };
203};
204template<>
205zfclassNotPOD _ZFP_MP_mallocSA<1> {
206public:
207 enum {
208 V = _ZFP_MP_SA<1>::V,
209 };
210};
211
212inline void *_ZFP_MP_mallocFix(ZF_IN void *p, ZF_IN zfindex size) {
213 if(p) {
214 *(zfindex *)p = size;
215 return (((zfbyte *)p) + sizeof(void *));
216 }
217 else {
218 return zfnull;
219 }
220}
221inline void *_ZFP_MP_malloc(ZF_IN zfindex size) {
222 if(zffalse){
223 }
224 else if(size <= _ZFP_MP_mallocSA<1>::V - sizeof(void *)) {
225 return _ZFP_MP_mallocFix(_ZFP_MP_H<_ZFP_MP_mallocSA<1>::V>::pNew(), size);
226 }
227 else if(size <= _ZFP_MP_mallocSA<2>::V - sizeof(void *)) {
228 return _ZFP_MP_mallocFix(_ZFP_MP_H<_ZFP_MP_mallocSA<2>::V>::pNew(), size);
229 }
230 else if(size <= _ZFP_MP_mallocSA<3>::V - sizeof(void *)) {
231 return _ZFP_MP_mallocFix(_ZFP_MP_H<_ZFP_MP_mallocSA<3>::V>::pNew(), size);
232 }
233 else if(size <= _ZFP_MP_mallocSA<4>::V - sizeof(void *)) {
234 return _ZFP_MP_mallocFix(_ZFP_MP_H<_ZFP_MP_mallocSA<4>::V>::pNew(), size);
235 }
236 else if(size <= _ZFP_MP_mallocSA<5>::V - sizeof(void *)) {
237 return _ZFP_MP_mallocFix(_ZFP_MP_H<_ZFP_MP_mallocSA<5>::V>::pNew(), size);
238 }
239 else {
240 return _ZFP_MP_mallocFix(zfmalloc(size + sizeof(void *)), size);
241 }
242}
243inline void _ZFP_MP_free(ZF_IN void *p) {
244 if(p == zfnull) {
245 return;
246 }
247 p = ((zfbyte *)p) - sizeof(void *);
248 zfindex size = *(zfindex *)p;
249 if(zffalse){
250 }
251 else if(size <= _ZFP_MP_mallocSA<1>::V - sizeof(void *)) {
252 _ZFP_MP_H<_ZFP_MP_mallocSA<1>::V>::pDel(p);
253 }
254 else if(size <= _ZFP_MP_mallocSA<2>::V - sizeof(void *)) {
255 _ZFP_MP_H<_ZFP_MP_mallocSA<2>::V>::pDel(p);
256 }
257 else if(size <= _ZFP_MP_mallocSA<3>::V - sizeof(void *)) {
258 _ZFP_MP_H<_ZFP_MP_mallocSA<3>::V>::pDel(p);
259 }
260 else if(size <= _ZFP_MP_mallocSA<4>::V - sizeof(void *)) {
261 _ZFP_MP_H<_ZFP_MP_mallocSA<4>::V>::pDel(p);
262 }
263 else if(size <= _ZFP_MP_mallocSA<5>::V - sizeof(void *)) {
264 _ZFP_MP_H<_ZFP_MP_mallocSA<5>::V>::pDel(p);
265 }
266 else {
267 zffree(p);
268 }
269}
270inline void *_ZFP_MP_realloc(ZF_IN void *p, ZF_IN zfindex size) {
271 if(p == zfnull) {
272 return _ZFP_MP_malloc(size);
273 }
274 zfindex sizeOld = *(zfindex *)(((zfbyte *)p) - sizeof(void *));
275 if(size <= sizeOld) {
276 return p;
277 }
278 else if(sizeOld <= _ZFP_MP_mallocSA<1>::V - sizeof(void *)) {
279 if(size <= _ZFP_MP_mallocSA<1>::V - sizeof(void *)) {
280 return p;
281 }
282 }
283 else if(sizeOld <= _ZFP_MP_mallocSA<2>::V - sizeof(void *)) {
284 if(size <= _ZFP_MP_mallocSA<2>::V - sizeof(void *)) {
285 return p;
286 }
287 }
288 else if(sizeOld <= _ZFP_MP_mallocSA<3>::V - sizeof(void *)) {
289 if(size <= _ZFP_MP_mallocSA<3>::V - sizeof(void *)) {
290 return p;
291 }
292 }
293 else if(sizeOld <= _ZFP_MP_mallocSA<4>::V - sizeof(void *)) {
294 if(size <= _ZFP_MP_mallocSA<4>::V - sizeof(void *)) {
295 return p;
296 }
297 }
298 else if(sizeOld <= _ZFP_MP_mallocSA<5>::V - sizeof(void *)) {
299 if(size <= _ZFP_MP_mallocSA<5>::V - sizeof(void *)) {
300 return p;
301 }
302 }
303 void *pNew = _ZFP_MP_malloc(size);
304 if(pNew == zfnull) {
305 return zfnull;
306 }
307 zfmemcpy(pNew, p, sizeOld);
308 _ZFP_MP_free(p);
309 return pNew;
310}
311#endif // #if ZF_ENV_ZFMEMPOOL_ENABLE
312
314
315#endif // #ifndef _ZFI_ZFMemPool_h_
316
#define ZFLIB_ZFCore
used to export symbols
Definition ZFCoreEnvDef.h:30
core mutex
#define zffree(ptr)
same as free defined for future use, see zfnew for more info
Definition ZFCoreTypeDef_ClassType.h:98
#define ZF_IN
dummy macro that shows the param used as required input
Definition ZFCoreTypeDef_ClassType.h:196
#define zfclassPOD
shows the class is a POD type
Definition ZFCoreTypeDef_ClassType.h:35
#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:156
#define zfmalloc(size)
same as malloc defined for future use, see zfnew for more info
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