2013-02-15 05:39:56 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Object Pool
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
|
|
|
#include <winpr/collections.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* C Object Pool similar to C# BufferManager Class:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/ms405814.aspx
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an object from the pool.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void* ObjectPool_Take(wObjectPool* pool)
|
|
|
|
{
|
|
|
|
void* obj = NULL;
|
|
|
|
|
|
|
|
if (pool->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
EnterCriticalSection(&pool->lock);
|
2013-02-15 05:39:56 +04:00
|
|
|
|
|
|
|
if (pool->size > 0)
|
|
|
|
obj = pool->array[--(pool->size)];
|
|
|
|
|
|
|
|
if (!obj)
|
|
|
|
{
|
|
|
|
if (pool->object.fnObjectNew)
|
2018-10-19 12:30:01 +03:00
|
|
|
obj = pool->object.fnObjectNew(NULL);
|
2013-02-15 05:39:56 +04:00
|
|
|
}
|
|
|
|
|
2013-08-13 17:34:15 +04:00
|
|
|
if (pool->object.fnObjectInit)
|
|
|
|
pool->object.fnObjectInit(obj);
|
|
|
|
|
2013-02-15 05:39:56 +04:00
|
|
|
if (pool->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
LeaveCriticalSection(&pool->lock);
|
2013-02-15 05:39:56 +04:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an object to the pool.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void ObjectPool_Return(wObjectPool* pool, void* obj)
|
|
|
|
{
|
|
|
|
if (pool->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
EnterCriticalSection(&pool->lock);
|
2013-02-15 05:39:56 +04:00
|
|
|
|
|
|
|
if ((pool->size + 1) >= pool->capacity)
|
|
|
|
{
|
2019-07-11 22:12:50 +03:00
|
|
|
size_t new_cap;
|
2019-11-06 17:24:51 +03:00
|
|
|
void** new_arr;
|
2015-03-11 09:59:56 +03:00
|
|
|
|
|
|
|
new_cap = pool->capacity * 2;
|
2019-11-06 17:24:51 +03:00
|
|
|
new_arr = (void**)realloc(pool->array, sizeof(void*) * new_cap);
|
2015-03-11 09:59:56 +03:00
|
|
|
if (!new_arr)
|
2019-07-11 22:13:19 +03:00
|
|
|
goto out;
|
|
|
|
|
2015-03-11 09:59:56 +03:00
|
|
|
pool->array = new_arr;
|
|
|
|
pool->capacity = new_cap;
|
2013-02-15 05:39:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pool->array[(pool->size)++] = obj;
|
|
|
|
|
2013-08-13 17:34:15 +04:00
|
|
|
if (pool->object.fnObjectUninit)
|
|
|
|
pool->object.fnObjectUninit(obj);
|
|
|
|
|
2019-07-11 22:13:19 +03:00
|
|
|
out:
|
2013-02-15 05:39:56 +04:00
|
|
|
if (pool->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
LeaveCriticalSection(&pool->lock);
|
2013-02-15 05:39:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Releases the buffers currently cached in the pool.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void ObjectPool_Clear(wObjectPool* pool)
|
|
|
|
{
|
|
|
|
if (pool->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
EnterCriticalSection(&pool->lock);
|
2013-02-15 05:39:56 +04:00
|
|
|
|
|
|
|
while (pool->size > 0)
|
|
|
|
{
|
|
|
|
(pool->size)--;
|
|
|
|
|
|
|
|
if (pool->object.fnObjectFree)
|
|
|
|
pool->object.fnObjectFree(pool->array[pool->size]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pool->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
LeaveCriticalSection(&pool->lock);
|
2013-02-15 05:39:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construction, Destruction
|
|
|
|
*/
|
|
|
|
|
|
|
|
wObjectPool* ObjectPool_New(BOOL synchronized)
|
|
|
|
{
|
|
|
|
wObjectPool* pool = NULL;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
pool = (wObjectPool*)calloc(1, sizeof(wObjectPool));
|
2013-02-15 05:39:56 +04:00
|
|
|
|
|
|
|
if (pool)
|
|
|
|
{
|
2015-04-14 14:47:23 +03:00
|
|
|
pool->capacity = 32;
|
|
|
|
pool->size = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
pool->array = (void**)calloc(pool->capacity, sizeof(void*));
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!pool->array)
|
|
|
|
{
|
|
|
|
free(pool);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-02-15 05:39:56 +04:00
|
|
|
pool->synchronized = synchronized;
|
|
|
|
|
|
|
|
if (pool->synchronized)
|
2013-08-07 19:10:43 +04:00
|
|
|
InitializeCriticalSectionAndSpinCount(&pool->lock, 4000);
|
2013-02-15 05:39:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return pool;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectPool_Free(wObjectPool* pool)
|
|
|
|
{
|
|
|
|
if (pool)
|
|
|
|
{
|
|
|
|
ObjectPool_Clear(pool);
|
|
|
|
|
|
|
|
if (pool->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
DeleteCriticalSection(&pool->lock);
|
2013-02-15 05:39:56 +04:00
|
|
|
|
|
|
|
free(pool->array);
|
|
|
|
|
|
|
|
free(pool);
|
|
|
|
}
|
|
|
|
}
|