20#include <winpr/config.h>
24#include <winpr/collections.h>
27#define MAX(a, b) ((a) > (b)) ? (a) : (b)
41 CRITICAL_SECTION lock;
49 wBufferPoolItem* aArray;
53 wBufferPoolItem* uArray;
56static BOOL BufferPool_Lock(wBufferPool* pool)
61 if (pool->synchronized)
62 EnterCriticalSection(&pool->lock);
66static BOOL BufferPool_Unlock(wBufferPool* pool)
71 if (pool->synchronized)
72 LeaveCriticalSection(&pool->lock);
85static BOOL BufferPool_ShiftAvailable(wBufferPool* pool,
size_t index,
int count)
89 if (pool->aSize + count > pool->aCapacity)
91 wBufferPoolItem* newArray =
nullptr;
92 SSIZE_T newCapacity = pool->aSize + count;
93 newCapacity += (newCapacity + 2) / 2;
95 newArray = (wBufferPoolItem*)winpr_aligned_recalloc(
96 pool->aArray,
sizeof(wBufferPoolItem),
97 WINPR_ASSERTING_INT_CAST(
size_t, newCapacity), pool->alignment);
100 pool->aArray = newArray;
101 pool->aCapacity = newCapacity;
105 &pool->aArray[index + WINPR_ASSERTING_INT_CAST(
size_t, count)], &pool->aArray[index],
106 (WINPR_ASSERTING_INT_CAST(
size_t, pool->aSize) - index) *
sizeof(wBufferPoolItem));
107 pool->aSize += count;
111 const size_t size = WINPR_ASSERTING_INT_CAST(
size_t, pool->aSize);
112 const size_t offset = index + WINPR_ASSERTING_INT_CAST(
size_t, -count);
117 MoveMemory(&pool->aArray[index], &pool->aArray[offset],
118 (size - offset) *
sizeof(wBufferPoolItem));
119 pool->aSize += count;
124static BOOL BufferPool_ShiftUsed(wBufferPool* pool, SSIZE_T index, SSIZE_T count)
128 const SSIZE_T required = pool->uSize + count;
130 if ((required < count) || (required < pool->uSize))
133 if (required > pool->uCapacity)
135 SSIZE_T newUCapacity = pool->uCapacity;
138 if (newUCapacity > SSIZE_MAX - 128ll)
140 newUCapacity += 128ll;
141 }
while (newUCapacity <= required);
142 wBufferPoolItem* newUArray =
nullptr;
143 newUArray = (wBufferPoolItem*)winpr_aligned_realloc(
145 sizeof(wBufferPoolItem) * WINPR_ASSERTING_INT_CAST(
size_t, newUCapacity),
150 pool->uCapacity = newUCapacity;
151 pool->uArray = newUArray;
154 MoveMemory(&pool->uArray[index + count], &pool->uArray[index],
155 WINPR_ASSERTING_INT_CAST(
size_t, pool->uSize - index) *
sizeof(wBufferPoolItem));
156 pool->uSize += count;
160 const SSIZE_T offset = index - count;
162 if (offset > pool->uSize)
165 MoveMemory(&pool->uArray[index], &pool->uArray[offset],
166 WINPR_ASSERTING_INT_CAST(
size_t, pool->uSize - offset) *
167 sizeof(wBufferPoolItem));
168 pool->uSize += count;
177SSIZE_T BufferPool_GetPoolSize(wBufferPool* pool)
181 BufferPool_Lock(pool);
194 BufferPool_Unlock(pool);
203SSIZE_T BufferPool_GetBufferSize(wBufferPool* pool,
const void* buffer)
208 BufferPool_Lock(pool);
213 size = pool->fixedSize;
220 for (SSIZE_T index = 0; index < pool->uSize; index++)
222 if (pool->uArray[index].buffer == buffer)
224 size = pool->uArray[index].size;
231 BufferPool_Unlock(pool);
233 return (found) ? size : -1;
240void* BufferPool_Take(wBufferPool* pool, SSIZE_T size)
243 SSIZE_T maxIndex = 0;
244 SSIZE_T foundIndex = -1;
246 void* buffer =
nullptr;
248 BufferPool_Lock(pool);
255 buffer = pool->array[--(pool->size)];
259 buffer = winpr_aligned_calloc(WINPR_ASSERTING_INT_CAST(
size_t, pool->fixedSize), 1,
269 SSIZE_T bufferSize = 0;
275 size = pool->fixedSize;
277 for (SSIZE_T index = 0; index < pool->aSize; index++)
279 if (pool->aArray[index].size > maxSize)
282 maxSize = pool->aArray[index].size;
285 if (pool->aArray[index].size >= size)
293 if (!found && maxSize)
295 foundIndex = maxIndex;
306 buffer = winpr_aligned_calloc(WINPR_ASSERTING_INT_CAST(
size_t, size), 1,
315 buffer = pool->aArray[foundIndex].buffer;
316 bufferSize = pool->aArray[foundIndex].size;
320 void* newBuffer =
nullptr;
321 newBuffer = winpr_aligned_realloc(buffer, WINPR_ASSERTING_INT_CAST(
size_t, size),
325 goto out_error_no_free;
331 if (!BufferPool_ShiftAvailable(pool, WINPR_ASSERTING_INT_CAST(
size_t, foundIndex), -1))
338 if (pool->uSize + 1 > pool->uCapacity)
340 size_t newUCapacity = WINPR_ASSERTING_INT_CAST(
size_t, pool->uCapacity);
341 newUCapacity += (newUCapacity + 2) / 2;
342 if (newUCapacity > SSIZE_MAX)
344 wBufferPoolItem* newUArray =
345 (wBufferPoolItem*)realloc(pool->uArray,
sizeof(wBufferPoolItem) * newUCapacity);
349 pool->uCapacity = (SSIZE_T)newUCapacity;
350 pool->uArray = newUArray;
353 pool->uArray[pool->uSize].buffer = buffer;
354 pool->uArray[pool->uSize].size = bufferSize;
358 BufferPool_Unlock(pool);
363 winpr_aligned_free(buffer);
365 BufferPool_Unlock(pool);
373BOOL BufferPool_Return(wBufferPool* pool,
void* buffer)
379 BufferPool_Lock(pool);
385 if ((pool->size + 1) >= pool->capacity)
387 SSIZE_T newCapacity = MAX(2, pool->size + (pool->size + 2) / 2 + 1);
388 void** newArray = (
void**)realloc(
389 (
void*)pool->array,
sizeof(
void*) * WINPR_ASSERTING_INT_CAST(
size_t, newCapacity));
393 pool->capacity = newCapacity;
394 pool->array = newArray;
397 pool->array[(pool->size)++] = buffer;
404 for (; index < pool->uSize; index++)
406 if (pool->uArray[index].buffer == buffer)
415 size = pool->uArray[index].size;
416 if (!BufferPool_ShiftUsed(pool, index, -1))
422 if ((pool->aSize + 1) >= pool->aCapacity)
424 SSIZE_T newCapacity = MAX(2, pool->aSize + (pool->aSize + 2) / 2 + 1);
425 wBufferPoolItem* newArray = (wBufferPoolItem*)realloc(
427 sizeof(wBufferPoolItem) * WINPR_ASSERTING_INT_CAST(
size_t, newCapacity));
431 pool->aCapacity = newCapacity;
432 pool->aArray = newArray;
435 pool->aArray[pool->aSize].buffer = buffer;
436 pool->aArray[pool->aSize].size = size;
443 BufferPool_Unlock(pool);
451void BufferPool_Clear(wBufferPool* pool)
453 BufferPool_Lock(pool);
459 while (pool->size > 0)
463 winpr_aligned_free(pool->array[pool->size]);
470 while (pool->aSize > 0)
474 winpr_aligned_free(pool->aArray[pool->aSize].buffer);
477 while (pool->uSize > 0)
481 winpr_aligned_free(pool->uArray[pool->uSize].buffer);
485 BufferPool_Unlock(pool);
492wBufferPool* BufferPool_New(BOOL
synchronized, SSIZE_T fixedSize, DWORD alignment)
494 wBufferPool* pool =
nullptr;
496 pool = (wBufferPool*)calloc(1,
sizeof(wBufferPool));
500 pool->fixedSize = fixedSize;
502 if (pool->fixedSize < 0)
505 pool->alignment = alignment;
506 pool->synchronized =
synchronized;
508 if (pool->synchronized)
510 if (!InitializeCriticalSectionAndSpinCount(&pool->lock, 4000))
521 (
void**)calloc(WINPR_ASSERTING_INT_CAST(
size_t, pool->capacity),
sizeof(
void*));
530 pool->aCapacity = 32;
531 pool->aArray = (wBufferPoolItem*)calloc(
532 WINPR_ASSERTING_INT_CAST(
size_t, pool->aCapacity),
sizeof(wBufferPoolItem));
537 pool->uCapacity = 32;
538 pool->uArray = (wBufferPoolItem*)calloc(
539 WINPR_ASSERTING_INT_CAST(
size_t, pool->uCapacity),
sizeof(wBufferPoolItem));
548 WINPR_PRAGMA_DIAG_PUSH
549 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
550 BufferPool_Free(pool);
551 WINPR_PRAGMA_DIAG_POP
555void BufferPool_Free(wBufferPool* pool)
559 BufferPool_Clear(pool);
561 if (pool->synchronized)
562 DeleteCriticalSection(&pool->lock);
568 free((
void*)pool->array);