20 #include <winpr/config.h>
22 #include <winpr/crt.h>
23 #include <winpr/assert.h>
25 #include <winpr/collections.h>
46 static void ObjectPool_Lock(wObjectPool* pool)
49 if (pool->synchronized)
50 EnterCriticalSection(&pool->lock);
53 static void ObjectPool_Unlock(wObjectPool* pool)
56 if (pool->synchronized)
57 LeaveCriticalSection(&pool->lock);
64 void* ObjectPool_Take(wObjectPool* pool)
68 ObjectPool_Lock(pool);
71 obj = pool->array[--(pool->size)];
75 if (pool->object.fnObjectNew)
76 obj = pool->object.fnObjectNew(NULL);
79 if (pool->object.fnObjectInit)
80 pool->object.fnObjectInit(obj);
82 ObjectPool_Unlock(pool);
91 void ObjectPool_Return(wObjectPool* pool,
void* obj)
93 ObjectPool_Lock(pool);
95 if ((pool->size + 1) >= pool->capacity)
98 void** new_arr = NULL;
100 new_cap = pool->capacity * 2;
101 new_arr = (
void**)realloc(pool->array,
sizeof(
void*) * new_cap);
105 pool->array = new_arr;
106 pool->capacity = new_cap;
109 pool->array[(pool->size)++] = obj;
111 if (pool->object.fnObjectUninit)
112 pool->object.fnObjectUninit(obj);
115 ObjectPool_Unlock(pool);
118 wObject* ObjectPool_Object(wObjectPool* pool)
121 return &pool->object;
128 void ObjectPool_Clear(wObjectPool* pool)
130 ObjectPool_Lock(pool);
132 while (pool->size > 0)
136 if (pool->object.fnObjectFree)
137 pool->object.fnObjectFree(pool->array[pool->size]);
140 ObjectPool_Unlock(pool);
147 wObjectPool* ObjectPool_New(BOOL
synchronized)
149 wObjectPool* pool = NULL;
151 pool = (wObjectPool*)calloc(1,
sizeof(wObjectPool));
157 pool->array = (
void**)calloc(pool->capacity,
sizeof(
void*));
163 pool->synchronized =
synchronized;
165 if (pool->synchronized)
166 InitializeCriticalSectionAndSpinCount(&pool->lock, 4000);
172 void ObjectPool_Free(wObjectPool* pool)
176 ObjectPool_Clear(pool);
178 if (pool->synchronized)
179 DeleteCriticalSection(&pool->lock);
This struct contains function pointer to initialize/free objects.