5#ifndef _ZFI_ZFMemPool_h_
6#define _ZFI_ZFMemPool_h_
15#ifndef ZF_ENV_ZFMEMPOOL_ENABLE
16 #if _ZFP_ZFMEM_LOG_DISABLE_MEMPOOL
17 #define ZF_ENV_ZFMEMPOOL_ENABLE 0
19 #define ZF_ENV_ZFMEMPOOL_ENABLE 1
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>;
40 #define zfpoolNew(T_Type, ...) zfnew(T_Type, ##__VA_ARGS__)
41 #define zfpoolDelete(obj) zfdelete(obj)
42 #define zfpoolDeclareFriend() zfmemDeclareFriend()
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)
60 #define zfpoolMalloc(size) zfmalloc(size)
61 #define zfpoolRealloc(p, size) zfrealloc((p), (size))
62 #define zfpoolFree(p) zffree(p)
65#if ZF_ENV_ZFMEMPOOL_ENABLE
70 _A = (N <=
sizeof(
void *) * 4
72 : N <=
sizeof(
void *) * 32
76 V = ((N % _A) == 0 ? N : ((N / _A) + 1) * _A),
77 M = (N <=
sizeof(
void *) * 4
79 : N <=
sizeof(
void *) * 8
81 : N <=
sizeof(
void *) * 32
83 : N <=
sizeof(
void *) * 256
98 static void *pNew(
void) {
99 _ZFP_MP_H<N> &d = _instance();
101 _ZFP_MP_B<N> *t = d.available;
102 d.available = d.available->next;
107 return zfmalloc(
sizeof(_ZFP_MP_B<N>));
110 static void pDel(
ZF_IN void *obj) {
111 _ZFP_MP_H<N> &d = _instance();
112 if(d.count >= _ZFP_MP_SA<N>::M) {
117 _ZFP_MP_B<N> *t = (_ZFP_MP_B<N> *)obj;
118 t->next = d.available;
130 _ZFP_MP_B<N> *t = available;
131 available = available->next;
135 static _ZFP_MP_H<N> &_instance(
void) {
136 static _ZFP_MP_H<N> d;
140 _ZFP_MP_B<N> *available;
144template<
typename T_Type>
147 static void *pNew(
void) {
149 return _ZFP_MP_H<_ZFP_MP_SA<
sizeof(T_Type)>::V>::pNew();
151 static void pDel(
ZF_IN T_Type *obj) {
154 _ZFP_MP_H<_ZFP_MP_SA<
sizeof(T_Type)>::V>::pDel(obj);
157template<
typename T_Type>
158inline void _ZFP_zfpoolDelete(
ZF_IN T_Type *obj) {
160 _ZFP_MP_Obj<T_Type>::pDel(obj);
169 V = _ZFP_MP_SA<_ZFP_MP_mallocSA<N - 1>::V + 1>::V,
176 V = _ZFP_MP_SA<1>::V,
183 return (((
zfbyte *)p) +
sizeof(
void *));
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);
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);
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);
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);
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);
209 return _ZFP_MP_mallocFix(
zfmalloc(size +
sizeof(
void *)), size);
212inline void _ZFP_MP_free(
ZF_IN void *p) {
217 p = ((
zfbyte *)p) -
sizeof(
void *);
221 else if(size <= _ZFP_MP_mallocSA<1>::V -
sizeof(
void *)) {
222 _ZFP_MP_H<_ZFP_MP_mallocSA<1>::V>::pDel(p);
224 else if(size <= _ZFP_MP_mallocSA<2>::V -
sizeof(
void *)) {
225 _ZFP_MP_H<_ZFP_MP_mallocSA<2>::V>::pDel(p);
227 else if(size <= _ZFP_MP_mallocSA<3>::V -
sizeof(
void *)) {
228 _ZFP_MP_H<_ZFP_MP_mallocSA<3>::V>::pDel(p);
230 else if(size <= _ZFP_MP_mallocSA<4>::V -
sizeof(
void *)) {
231 _ZFP_MP_H<_ZFP_MP_mallocSA<4>::V>::pDel(p);
233 else if(size <= _ZFP_MP_mallocSA<5>::V -
sizeof(
void *)) {
234 _ZFP_MP_H<_ZFP_MP_mallocSA<5>::V>::pDel(p);
243 return _ZFP_MP_malloc(size);
246 if(size <= sizeOld) {
249 else if(sizeOld <= _ZFP_MP_mallocSA<1>::V -
sizeof(
void *)) {
250 if(size <= _ZFP_MP_mallocSA<1>::V -
sizeof(
void *)) {
254 else if(sizeOld <= _ZFP_MP_mallocSA<2>::V -
sizeof(
void *)) {
255 if(size <= _ZFP_MP_mallocSA<2>::V -
sizeof(
void *)) {
259 else if(sizeOld <= _ZFP_MP_mallocSA<3>::V -
sizeof(
void *)) {
260 if(size <= _ZFP_MP_mallocSA<3>::V -
sizeof(
void *)) {
264 else if(sizeOld <= _ZFP_MP_mallocSA<4>::V -
sizeof(
void *)) {
265 if(size <= _ZFP_MP_mallocSA<4>::V -
sizeof(
void *)) {
269 else if(sizeOld <= _ZFP_MP_mallocSA<5>::V -
sizeof(
void *)) {
270 if(size <= _ZFP_MP_mallocSA<5>::V -
sizeof(
void *)) {
274 void *pNew = _ZFP_MP_malloc(size);
#define ZFLIB_ZFCore
used to export symbols
Definition ZFCoreEnvDef.h:30
#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