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
36#if ZF_ENV_ZFMEMPOOL_ENABLE
37 #define zfpoolNew(T_Type, ...) zfnewPlacement((_ZFP_MP_Obj<T_Type >::pNew()), T_Type, ##__VA_ARGS__)
38 #define zfpoolDelete(obj) _ZFP_zfpoolDelete(obj)
39 #define zfpoolDeclareFriend() \
40 friend zfclassFwd _ZFP_MP_Obj<zfself>;
42 #define zfpoolNew(T_Type, ...) zfnew(T_Type, ##__VA_ARGS__)
43 #define zfpoolDelete(obj) zfdelete(obj)
44 #define zfpoolDeclareFriend()
47#if ZF_ENV_ZFMEMPOOL_ENABLE
54 _A = (N <=
sizeof(
const void *) * 4
55 ?
sizeof(
const void *)
56 : N <=
sizeof(
const void *) * 32
57 ?
sizeof(
const void *) * 4
58 :
sizeof(
const void *) * 32
60 V = ((N % _A) == 0 ? N : ((N / _A) + 1) * _A),
61 M = (N <=
sizeof(
const void *) * 4
63 : N <=
sizeof(
const void *) * 8
65 : N <=
sizeof(
const void *) * 32
67 : N <=
sizeof(
const void *) * 256
82 static void *pNew(
void) {
83 _ZFP_MP_H<N> &d = _instance();
85 _ZFP_MP_B<N> *t = d.available;
86 d.available = d.available->next;
91 return zfmalloc(
sizeof(_ZFP_MP_B<N>));
94 static void pDel(
ZF_IN void *obj) {
95 _ZFP_MP_H<N> &d = _instance();
96 if(d.count >= _ZFP_MP_SA<N>::M) {
101 _ZFP_MP_B<N> *t = (_ZFP_MP_B<N> *)obj;
102 t->next = d.available;
114 _ZFP_MP_B<N> *t = available;
115 available = available->next;
119 static _ZFP_MP_H<N> &_instance(
void) {
120 static _ZFP_MP_H<N> d;
124 _ZFP_MP_B<N> *available;
128template<
typename T_Type>
131 static void *pNew(
void) {
133 return _ZFP_MP_H<_ZFP_MP_SA<
sizeof(T_Type)>::V>::pNew();
135 static void pDel(
ZF_IN T_Type *obj) {
137 _ZFP_MP_H<_ZFP_MP_SA<
sizeof(T_Type)>::V>::pDel(obj);
140template<
typename T_Type>
141inline void _ZFP_zfpoolDelete(
ZF_IN T_Type *obj) {
144 _ZFP_MP_Obj<T_Type>::pDel(obj);
150#if _ZFP_ZFMEMPOOL_DEBUG
158 static _ZFP_MP_State *&d(
void) {
159 static _ZFP_MP_State *d =
zfnull;
167 template<
typename T_Type>
170 static zfbool reg(
const char *name) {
176 _ZFP_MP_State *&d = _ZFP_MP_State::d();
177 zfindex &c = _ZFP_MP_State::c();
179 d = (_ZFP_MP_State *)
zfrealloc(d,
sizeof(_ZFP_MP_State) * c);
180 _ZFP_MP_State &p = d[c - 1];
182 p.countGetter = countGetter;
183 p.sizeGetter = sizeGetter;
188 static T_Type *a(T_Type *obj) {
192 Item *&pEnd_ = pEnd();
193 Item *&cEnd_ = cEnd();
196 zfindex capacity = count ? count * 2 : 8;
197 p_ = (Item *)
zfrealloc(p_, capacity *
sizeof(Item));
199 cEnd_ = p_ + capacity;
206 static T_Type *d(T_Type *obj) {
209 Item *&pEnd_ = pEnd();
210 for(Item *t = p_; t != pEnd_; ++t) {
212 zfmemmove(t, t + 1, (pEnd_ - (t + 1)) *
sizeof(Item));
221 typedef T_Type *Item;
222 static Item *&p(
void) {
226 static Item *&pEnd(
void) {
230 static Item *&cEnd(
void) {
235 static zfindex sizeGetter(
void) {
236 return (
zfindex)
sizeof(T_Type);
238 static zfindex countGetter(
void) {
242 template<
typename T_Type>
243 inline void _ZFP_MP_ObjDebugDelete(
ZF_IN T_Type *obj) {
246 _ZFP_MP_ObjDebug<T_Type>::d(obj);
247 _ZFP_MP_Obj<T_Type>::pDel(obj);
254 #define zfpoolNew(T_Type, ...) _ZFP_MP_ObjDebug<T_Type >::a(( \
255 _ZFP_MP_ObjDebug<T_Type>::reg(#T_Type) \
256 , zfnewPlacement((_ZFP_MP_Obj<T_Type >::pNew()), T_Type, ##__VA_ARGS__) \
258 #define zfpoolDelete(obj) _ZFP_MP_ObjDebugDelete(obj)
260 inline void _ZFP_MP_statePrint(
void) {
261 _ZFP_MP_State *d = _ZFP_MP_State::d();
262 _ZFP_MP_State *dEnd = d + _ZFP_MP_State::c();
263 for( ; d < dEnd; ++d) {
264 printf(
"%4d %4d %s\n"
265 , (
int)d->sizeGetter()
266 , (
int)d->countGetter()
#define ZFLIB_ZFCore
used to export symbols
Definition ZFCoreEnvDef.h:30
#define ZFCoreMutexLocker()
util method to lock current block
Definition ZFCoreMutex.h:95
#define ZFCoreMutexUnlock()
see ZFCoreMutexLock
Definition ZFCoreMutex.h:58
#define ZFCoreMutexLock()
internal use only
Definition ZFCoreMutex.h:51
#define zffree(ptr)
same as free defined for future use, do nothing if ptr is NULL
Definition ZFCoreTypeDef_ClassType.h:112
#define ZF_IN
dummy macro that shows the param used as required input
Definition ZFCoreTypeDef_ClassType.h:180
void * zfmemmove(void *dst, const void *src, zfindex size)
wrapper to memmove
Definition ZFCoreTypeDef_ClassType.h:142
#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
#define zfrealloc(oldPtr, newSize)
same as realloc defined for future use
Definition ZFCoreTypeDef_ClassType.h:106
#define zfmalloc(size)
same as malloc defined for future use
Definition ZFCoreTypeDef_ClassType.h:100
_ZFT_t_zfbool zfbool
bool type
Definition ZFCoreTypeDef_CoreType.h:103
_ZFT_t_zfindex zfindex
similar to size_t, used for index and size only
Definition ZFCoreTypeDef_CoreType.h:154
#define zftrue
bool true type
Definition ZFCoreTypeDef_CoreType.h:107
#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