Files
FreeRDP/winpr/libwinpr/utils/collections/ObjectPool.c

195 lines
3.5 KiB
C
Raw Normal View History

2013-02-14 20:39:56 -05: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.
*/
2022-02-16 10:08:00 +01:00
#include <winpr/config.h>
2013-02-14 20:39:56 -05:00
#include <winpr/crt.h>
2021-06-16 10:40:53 +02:00
#include <winpr/assert.h>
2013-02-14 20:39:56 -05:00
#include <winpr/collections.h>
struct s_wObjectPool
{
size_t size;
size_t capacity;
void** array;
CRITICAL_SECTION lock;
wObject object;
BOOL synchronized;
};
2013-02-14 20:39:56 -05:00
/**
* C Object Pool similar to C# BufferManager Class:
* http://msdn.microsoft.com/en-us/library/ms405814.aspx
*/
/**
* Methods
*/
static void ObjectPool_Lock(wObjectPool* pool)
{
2021-06-16 10:40:53 +02:00
WINPR_ASSERT(pool);
if (pool->synchronized)
EnterCriticalSection(&pool->lock);
}
static void ObjectPool_Unlock(wObjectPool* pool)
{
2021-06-16 10:40:53 +02:00
WINPR_ASSERT(pool);
if (pool->synchronized)
LeaveCriticalSection(&pool->lock);
}
2013-02-14 20:39:56 -05:00
/**
* Gets an object from the pool.
*/
void* ObjectPool_Take(wObjectPool* pool)
{
2026-02-26 14:32:50 +01:00
void* obj = nullptr;
2013-02-14 20:39:56 -05:00
ObjectPool_Lock(pool);
2013-02-14 20:39:56 -05:00
if (pool->size > 0)
obj = pool->array[--(pool->size)];
if (!obj)
{
if (pool->object.fnObjectNew)
2026-02-26 14:32:50 +01:00
obj = pool->object.fnObjectNew(nullptr);
2013-02-14 20:39:56 -05:00
}
if (pool->object.fnObjectInit)
pool->object.fnObjectInit(obj);
ObjectPool_Unlock(pool);
2013-02-14 20:39:56 -05:00
return obj;
}
static BOOL ObjectPool_EnsureCapacity(wObjectPool* pool, size_t add)
2013-02-14 20:39:56 -05:00
{
WINPR_ASSERT(pool->size < SIZE_MAX - add);
2013-02-14 20:39:56 -05:00
const size_t blocksize = 128ull;
const size_t required = pool->size + add;
if (required >= pool->capacity)
2013-02-14 20:39:56 -05:00
{
const size_t new_cap = required + blocksize - required % blocksize;
void** new_arr = (void**)realloc((void*)pool->array, sizeof(void*) * new_cap);
if (!new_arr)
return FALSE;
pool->array = new_arr;
pool->capacity = new_cap;
2013-02-14 20:39:56 -05:00
}
return TRUE;
}
/**
* Returns an object to the pool.
*/
void ObjectPool_Return(wObjectPool* pool, void* obj)
{
ObjectPool_Lock(pool);
if (!ObjectPool_EnsureCapacity(pool, 1))
goto out;
2013-02-14 20:39:56 -05:00
pool->array[(pool->size)++] = obj;
if (pool->object.fnObjectUninit)
pool->object.fnObjectUninit(obj);
out:
ObjectPool_Unlock(pool);
}
wObject* ObjectPool_Object(wObjectPool* pool)
{
2021-06-16 10:40:53 +02:00
WINPR_ASSERT(pool);
return &pool->object;
2013-02-14 20:39:56 -05:00
}
/**
* Releases the buffers currently cached in the pool.
*/
void ObjectPool_Clear(wObjectPool* pool)
{
ObjectPool_Lock(pool);
2013-02-14 20:39:56 -05:00
while (pool->size > 0)
{
(pool->size)--;
if (pool->object.fnObjectFree)
pool->object.fnObjectFree(pool->array[pool->size]);
}
ObjectPool_Unlock(pool);
2013-02-14 20:39:56 -05:00
}
/**
* Construction, Destruction
*/
wObjectPool* ObjectPool_New(BOOL synchronized)
{
wObjectPool* pool = (wObjectPool*)calloc(1, sizeof(wObjectPool));
if (!pool)
goto fail;
2013-02-14 20:39:56 -05:00
pool->synchronized = synchronized;
2013-02-14 20:39:56 -05:00
if (pool->synchronized)
2013-02-14 20:39:56 -05:00
{
if (!InitializeCriticalSectionAndSpinCount(&pool->lock, 4000))
goto fail;
2013-02-14 20:39:56 -05:00
}
if (!ObjectPool_EnsureCapacity(pool, 32))
goto fail;
2013-02-14 20:39:56 -05:00
return pool;
fail:
ObjectPool_Free(pool);
return nullptr;
2013-02-14 20:39:56 -05:00
}
void ObjectPool_Free(wObjectPool* pool)
{
if (!pool)
return;
2013-02-14 20:39:56 -05:00
ObjectPool_Clear(pool);
2013-02-14 20:39:56 -05:00
if (pool->synchronized)
DeleteCriticalSection(&pool->lock);
2013-02-14 20:39:56 -05:00
free((void*)pool->array);
free(pool);
2013-02-14 20:39:56 -05:00
}