2013-04-12 05:59:02 +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>
|
2020-05-12 15:57:39 +03:00
|
|
|
#include <winpr/wlog.h>
|
2013-04-12 05:59:02 +04:00
|
|
|
|
|
|
|
#include <winpr/collections.h>
|
|
|
|
|
2020-05-12 15:57:39 +03:00
|
|
|
struct _wStreamPool
|
|
|
|
{
|
|
|
|
size_t aSize;
|
|
|
|
size_t aCapacity;
|
|
|
|
wStream** aArray;
|
|
|
|
|
|
|
|
size_t uSize;
|
|
|
|
size_t uCapacity;
|
|
|
|
wStream** uArray;
|
|
|
|
|
|
|
|
CRITICAL_SECTION lock;
|
|
|
|
BOOL synchronized;
|
|
|
|
size_t defaultSize;
|
|
|
|
};
|
|
|
|
|
|
|
|
static BOOL StreamPool_EnsureCapacity(wStreamPool* pool, size_t count, BOOL usedOrAvailable)
|
|
|
|
{
|
|
|
|
size_t new_cap = 0;
|
2020-05-19 20:27:58 +03:00
|
|
|
size_t* cap;
|
|
|
|
size_t* size;
|
|
|
|
wStream*** array;
|
2020-05-12 15:57:39 +03:00
|
|
|
|
|
|
|
if (!pool)
|
|
|
|
return FALSE;
|
2020-05-19 20:27:58 +03:00
|
|
|
|
|
|
|
cap = (usedOrAvailable) ? &pool->uCapacity : &pool->aCapacity;
|
|
|
|
size = (usedOrAvailable) ? &pool->uSize : &pool->aSize;
|
|
|
|
array = (usedOrAvailable) ? &pool->uArray : &pool->aArray;
|
2020-05-12 15:57:39 +03:00
|
|
|
if (*cap == 0)
|
|
|
|
new_cap = *size + count;
|
|
|
|
else if (*size + count > *cap)
|
|
|
|
new_cap = *cap * 2;
|
|
|
|
else if ((*size + count) < *cap / 3)
|
|
|
|
new_cap = *cap / 2;
|
|
|
|
|
|
|
|
if (new_cap > 0)
|
|
|
|
{
|
|
|
|
wStream** new_arr;
|
|
|
|
|
|
|
|
if (*cap < *size + count)
|
|
|
|
*cap += count;
|
|
|
|
|
|
|
|
new_arr = (wStream**)realloc(*array, sizeof(wStream*) * new_cap);
|
|
|
|
if (!new_arr)
|
|
|
|
return FALSE;
|
|
|
|
*cap = new_cap;
|
|
|
|
*array = new_arr;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-04-12 05:59:02 +04:00
|
|
|
/**
|
|
|
|
* Methods
|
|
|
|
*/
|
|
|
|
|
2020-05-12 15:57:39 +03:00
|
|
|
static void StreamPool_ShiftUsed(wStreamPool* pool, size_t index, INT64 count)
|
2013-04-12 20:20:20 +04:00
|
|
|
{
|
|
|
|
if (count > 0)
|
|
|
|
{
|
2020-05-12 15:57:39 +03:00
|
|
|
StreamPool_EnsureCapacity(pool, (size_t)count, TRUE);
|
2013-04-12 20:20:20 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
MoveMemory(&pool->uArray[index + count], &pool->uArray[index],
|
|
|
|
(pool->uSize - index) * sizeof(wStream*));
|
2013-04-12 20:20:20 +04:00
|
|
|
pool->uSize += count;
|
|
|
|
}
|
|
|
|
else if (count < 0)
|
|
|
|
{
|
2013-08-07 10:01:33 +04:00
|
|
|
if (pool->uSize - index + count > 0)
|
2013-11-08 02:37:58 +04:00
|
|
|
{
|
|
|
|
MoveMemory(&pool->uArray[index], &pool->uArray[index - count],
|
2019-11-06 17:24:51 +03:00
|
|
|
(pool->uSize - index + count) * sizeof(wStream*));
|
2013-11-08 02:37:58 +04:00
|
|
|
}
|
|
|
|
|
2013-04-12 20:20:20 +04:00
|
|
|
pool->uSize += count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a used stream to the pool.
|
|
|
|
*/
|
|
|
|
|
2019-11-20 13:30:14 +03:00
|
|
|
static void StreamPool_AddUsed(wStreamPool* pool, wStream* s)
|
2013-04-12 20:20:20 +04:00
|
|
|
{
|
2020-05-12 15:57:39 +03:00
|
|
|
StreamPool_EnsureCapacity(pool, 1, TRUE);
|
2013-04-12 20:20:20 +04:00
|
|
|
pool->uArray[(pool->uSize)++] = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a used stream from the pool.
|
|
|
|
*/
|
|
|
|
|
2019-11-20 13:30:14 +03:00
|
|
|
static void StreamPool_RemoveUsed(wStreamPool* pool, wStream* s)
|
2013-04-12 20:20:20 +04:00
|
|
|
{
|
2020-05-12 15:57:39 +03:00
|
|
|
size_t index;
|
2013-04-12 20:20:20 +04:00
|
|
|
BOOL found = FALSE;
|
|
|
|
|
|
|
|
for (index = 0; index < pool->uSize; index++)
|
|
|
|
{
|
|
|
|
if (pool->uArray[index] == s)
|
|
|
|
{
|
|
|
|
found = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
StreamPool_ShiftUsed(pool, index, -1);
|
|
|
|
}
|
|
|
|
|
2020-05-12 15:57:39 +03:00
|
|
|
static void StreamPool_ShiftAvailable(wStreamPool* pool, size_t index, INT64 count)
|
2013-04-30 06:35:15 +04:00
|
|
|
{
|
|
|
|
if (count > 0)
|
|
|
|
{
|
2020-05-12 15:57:39 +03:00
|
|
|
StreamPool_EnsureCapacity(pool, (size_t)count, FALSE);
|
2019-11-06 17:24:51 +03:00
|
|
|
MoveMemory(&pool->aArray[index + count], &pool->aArray[index],
|
|
|
|
(pool->aSize - index) * sizeof(wStream*));
|
2013-04-30 06:35:15 +04:00
|
|
|
pool->aSize += count;
|
|
|
|
}
|
|
|
|
else if (count < 0)
|
|
|
|
{
|
2013-08-07 10:01:33 +04:00
|
|
|
if (pool->aSize - index + count > 0)
|
2013-11-08 02:37:58 +04:00
|
|
|
{
|
|
|
|
MoveMemory(&pool->aArray[index], &pool->aArray[index - count],
|
2019-11-06 17:24:51 +03:00
|
|
|
(pool->aSize - index + count) * sizeof(wStream*));
|
2013-11-08 02:37:58 +04:00
|
|
|
}
|
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
pool->aSize += count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-12 05:59:02 +04:00
|
|
|
/**
|
|
|
|
* Gets a stream from the pool.
|
|
|
|
*/
|
|
|
|
|
|
|
|
wStream* StreamPool_Take(wStreamPool* pool, size_t size)
|
|
|
|
{
|
2020-05-12 15:57:39 +03:00
|
|
|
size_t index;
|
|
|
|
SSIZE_T foundIndex;
|
2013-04-12 05:59:02 +04:00
|
|
|
wStream* s = NULL;
|
|
|
|
|
|
|
|
if (pool->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
EnterCriticalSection(&pool->lock);
|
2013-04-12 05:59:02 +04:00
|
|
|
|
2013-04-12 21:44:23 +04:00
|
|
|
if (size == 0)
|
2013-04-12 20:20:20 +04:00
|
|
|
size = pool->defaultSize;
|
2013-04-12 05:59:02 +04:00
|
|
|
|
2013-11-08 02:37:58 +04:00
|
|
|
foundIndex = -1;
|
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
for (index = 0; index < pool->aSize; index++)
|
2013-04-12 05:59:02 +04:00
|
|
|
{
|
2013-04-30 06:35:15 +04:00
|
|
|
s = pool->aArray[index];
|
|
|
|
|
2013-05-15 20:14:26 +04:00
|
|
|
if (Stream_Capacity(s) >= size)
|
2013-04-30 06:35:15 +04:00
|
|
|
{
|
2013-04-30 08:36:16 +04:00
|
|
|
foundIndex = index;
|
2013-04-30 06:35:15 +04:00
|
|
|
break;
|
|
|
|
}
|
2013-04-12 05:59:02 +04:00
|
|
|
}
|
2013-04-30 06:35:15 +04:00
|
|
|
|
2013-11-08 02:37:58 +04:00
|
|
|
if (foundIndex < 0)
|
2013-04-30 07:55:44 +04:00
|
|
|
{
|
2013-04-30 06:35:15 +04:00
|
|
|
s = Stream_New(NULL, size);
|
2015-03-26 19:09:47 +03:00
|
|
|
if (!s)
|
|
|
|
goto out_fail;
|
2013-04-30 07:55:44 +04:00
|
|
|
}
|
2013-04-12 05:59:02 +04:00
|
|
|
else
|
2013-04-30 07:55:44 +04:00
|
|
|
{
|
2013-11-08 00:14:59 +04:00
|
|
|
Stream_SetPosition(s, 0);
|
2018-10-02 15:22:27 +03:00
|
|
|
Stream_SetLength(s, Stream_Capacity(s));
|
2015-03-26 19:09:47 +03:00
|
|
|
StreamPool_ShiftAvailable(pool, foundIndex, -1);
|
2013-04-30 07:55:44 +04:00
|
|
|
}
|
2013-04-12 05:59:02 +04:00
|
|
|
|
2015-03-23 19:25:23 +03:00
|
|
|
if (s)
|
|
|
|
{
|
|
|
|
s->pool = pool;
|
|
|
|
s->count = 1;
|
|
|
|
StreamPool_AddUsed(pool, s);
|
|
|
|
}
|
2013-04-12 20:20:20 +04:00
|
|
|
|
2015-03-26 19:09:47 +03:00
|
|
|
out_fail:
|
2013-04-12 05:59:02 +04:00
|
|
|
if (pool->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
LeaveCriticalSection(&pool->lock);
|
2013-04-12 05:59:02 +04:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an object to the pool.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void StreamPool_Return(wStreamPool* pool, wStream* s)
|
|
|
|
{
|
|
|
|
if (pool->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
EnterCriticalSection(&pool->lock);
|
2013-04-12 05:59:02 +04:00
|
|
|
|
2020-05-12 15:57:39 +03:00
|
|
|
StreamPool_EnsureCapacity(pool, 1, FALSE);
|
2013-04-12 20:20:20 +04:00
|
|
|
pool->aArray[(pool->aSize)++] = s;
|
|
|
|
StreamPool_RemoveUsed(pool, s);
|
2013-04-12 05:59:02 +04:00
|
|
|
|
|
|
|
if (pool->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
LeaveCriticalSection(&pool->lock);
|
2013-04-12 05:59:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lock the stream pool
|
|
|
|
*/
|
|
|
|
|
2019-11-20 13:30:14 +03:00
|
|
|
static void StreamPool_Lock(wStreamPool* pool)
|
2013-04-12 05:59:02 +04:00
|
|
|
{
|
2013-08-02 14:07:05 +04:00
|
|
|
EnterCriticalSection(&pool->lock);
|
2013-04-12 05:59:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unlock the stream pool
|
|
|
|
*/
|
|
|
|
|
2019-11-20 13:30:14 +03:00
|
|
|
static void StreamPool_Unlock(wStreamPool* pool)
|
2013-04-12 05:59:02 +04:00
|
|
|
{
|
2013-08-02 14:07:05 +04:00
|
|
|
LeaveCriticalSection(&pool->lock);
|
2013-04-12 05:59:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Increment stream reference count
|
|
|
|
*/
|
|
|
|
|
|
|
|
void Stream_AddRef(wStream* s)
|
|
|
|
{
|
|
|
|
if (s->pool)
|
|
|
|
{
|
|
|
|
StreamPool_Lock(s->pool);
|
|
|
|
s->count++;
|
|
|
|
StreamPool_Unlock(s->pool);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decrement stream reference count
|
|
|
|
*/
|
|
|
|
|
|
|
|
void Stream_Release(wStream* s)
|
|
|
|
{
|
|
|
|
DWORD count;
|
|
|
|
|
|
|
|
if (s->pool)
|
|
|
|
{
|
|
|
|
StreamPool_Lock(s->pool);
|
|
|
|
count = --(s->count);
|
|
|
|
StreamPool_Unlock(s->pool);
|
|
|
|
|
|
|
|
if (count == 0)
|
|
|
|
StreamPool_Return(s->pool, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-12 20:20:20 +04:00
|
|
|
/**
|
|
|
|
* Find stream in pool using pointer inside buffer
|
|
|
|
*/
|
|
|
|
|
|
|
|
wStream* StreamPool_Find(wStreamPool* pool, BYTE* ptr)
|
|
|
|
{
|
2020-05-12 15:57:39 +03:00
|
|
|
size_t index;
|
2013-04-12 20:20:20 +04:00
|
|
|
wStream* s = NULL;
|
|
|
|
BOOL found = FALSE;
|
|
|
|
|
2013-08-02 14:07:05 +04:00
|
|
|
EnterCriticalSection(&pool->lock);
|
2013-04-12 20:20:20 +04:00
|
|
|
|
|
|
|
for (index = 0; index < pool->uSize; index++)
|
|
|
|
{
|
|
|
|
s = pool->uArray[index];
|
|
|
|
|
2013-05-15 20:14:26 +04:00
|
|
|
if ((ptr >= Stream_Buffer(s)) && (ptr < (Stream_Buffer(s) + Stream_Capacity(s))))
|
2013-04-12 20:20:20 +04:00
|
|
|
{
|
|
|
|
found = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-02 14:07:05 +04:00
|
|
|
LeaveCriticalSection(&pool->lock);
|
2013-04-12 20:20:20 +04:00
|
|
|
|
|
|
|
return (found) ? s : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find stream in pool and increment reference count
|
|
|
|
*/
|
|
|
|
|
|
|
|
void StreamPool_AddRef(wStreamPool* pool, BYTE* ptr)
|
|
|
|
{
|
|
|
|
wStream* s;
|
|
|
|
|
|
|
|
s = StreamPool_Find(pool, ptr);
|
|
|
|
|
|
|
|
if (s)
|
|
|
|
Stream_AddRef(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find stream in pool and decrement reference count
|
|
|
|
*/
|
|
|
|
|
|
|
|
void StreamPool_Release(wStreamPool* pool, BYTE* ptr)
|
|
|
|
{
|
|
|
|
wStream* s;
|
|
|
|
|
|
|
|
s = StreamPool_Find(pool, ptr);
|
|
|
|
|
|
|
|
if (s)
|
|
|
|
Stream_Release(s);
|
|
|
|
}
|
|
|
|
|
2013-04-12 05:59:02 +04:00
|
|
|
/**
|
|
|
|
* Releases the streams currently cached in the pool.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void StreamPool_Clear(wStreamPool* pool)
|
|
|
|
{
|
|
|
|
if (pool->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
EnterCriticalSection(&pool->lock);
|
2013-04-12 05:59:02 +04:00
|
|
|
|
2013-04-12 20:20:20 +04:00
|
|
|
while (pool->aSize > 0)
|
2013-04-12 05:59:02 +04:00
|
|
|
{
|
2013-04-12 20:20:20 +04:00
|
|
|
(pool->aSize)--;
|
|
|
|
Stream_Free(pool->aArray[pool->aSize], TRUE);
|
2013-04-12 05:59:02 +04:00
|
|
|
}
|
|
|
|
|
2020-05-12 15:57:39 +03:00
|
|
|
while (pool->uSize > 0)
|
|
|
|
{
|
|
|
|
(pool->uSize)--;
|
|
|
|
Stream_Free(pool->uArray[pool->uSize], TRUE);
|
|
|
|
}
|
|
|
|
|
2013-04-12 05:59:02 +04:00
|
|
|
if (pool->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
LeaveCriticalSection(&pool->lock);
|
2013-04-12 05:59:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construction, Destruction
|
|
|
|
*/
|
|
|
|
|
2013-04-12 20:20:20 +04:00
|
|
|
wStreamPool* StreamPool_New(BOOL synchronized, size_t defaultSize)
|
2013-04-12 05:59:02 +04:00
|
|
|
{
|
|
|
|
wStreamPool* pool = NULL;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
pool = (wStreamPool*)calloc(1, sizeof(wStreamPool));
|
2013-04-12 05:59:02 +04:00
|
|
|
|
|
|
|
if (pool)
|
|
|
|
{
|
|
|
|
pool->synchronized = synchronized;
|
2013-04-12 20:20:20 +04:00
|
|
|
pool->defaultSize = defaultSize;
|
2013-04-12 05:59:02 +04:00
|
|
|
|
2020-05-12 15:57:39 +03:00
|
|
|
if (!StreamPool_EnsureCapacity(pool, 32, FALSE))
|
|
|
|
goto fail;
|
|
|
|
if (!StreamPool_EnsureCapacity(pool, 32, TRUE))
|
|
|
|
goto fail;
|
2014-04-29 00:44:52 +04:00
|
|
|
|
|
|
|
InitializeCriticalSectionAndSpinCount(&pool->lock, 4000);
|
2013-04-12 05:59:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return pool;
|
2020-05-12 15:57:39 +03:00
|
|
|
fail:
|
|
|
|
StreamPool_Free(pool);
|
|
|
|
return NULL;
|
2013-04-12 05:59:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void StreamPool_Free(wStreamPool* pool)
|
|
|
|
{
|
|
|
|
if (pool)
|
|
|
|
{
|
|
|
|
StreamPool_Clear(pool);
|
|
|
|
|
2013-08-02 14:07:05 +04:00
|
|
|
DeleteCriticalSection(&pool->lock);
|
2013-04-12 05:59:02 +04:00
|
|
|
|
2013-04-12 20:20:20 +04:00
|
|
|
free(pool->aArray);
|
|
|
|
free(pool->uArray);
|
2013-04-12 05:59:02 +04:00
|
|
|
|
|
|
|
free(pool);
|
|
|
|
}
|
|
|
|
}
|
2020-05-12 15:57:39 +03:00
|
|
|
|
|
|
|
char* StreamPool_GetStatistics(wStreamPool* pool, char* buffer, size_t size)
|
|
|
|
{
|
|
|
|
if (!pool || !buffer || (size < 1))
|
|
|
|
return NULL;
|
|
|
|
_snprintf(buffer, size - 1,
|
|
|
|
"aSize =%" PRIuz ", uSize =%" PRIuz "aCapacity=%" PRIuz ", uCapacity=%" PRIuz,
|
|
|
|
pool->aSize, pool->uSize, pool->aCapacity, pool->uCapacity);
|
|
|
|
buffer[size - 1] = '\0';
|
|
|
|
return buffer;
|
|
|
|
}
|