20 #include <winpr/config.h>
22 #include <winpr/crt.h>
24 #include <winpr/collections.h>
26 #define MAX(a, b) ((a) > (b)) ? (a) : (b)
47 wBufferPoolItem* aArray;
51 wBufferPoolItem* uArray;
54 static BOOL BufferPool_Lock(wBufferPool* pool)
59 if (pool->synchronized)
60 EnterCriticalSection(&pool->lock);
64 static BOOL BufferPool_Unlock(wBufferPool* pool)
69 if (pool->synchronized)
70 LeaveCriticalSection(&pool->lock);
83 static BOOL BufferPool_ShiftAvailable(wBufferPool* pool,
size_t index,
int count)
87 if (pool->aSize + count > pool->aCapacity)
89 wBufferPoolItem* newArray = NULL;
90 SSIZE_T newCapacity = pool->aSize + count;
91 newCapacity += (newCapacity + 2) / 2;
93 WINPR_ASSERT(newCapacity > 0);
94 if (pool->alignment > 0)
95 newArray = (wBufferPoolItem*)winpr_aligned_realloc(
97 sizeof(wBufferPoolItem) * WINPR_ASSERTING_INT_CAST(
size_t, newCapacity),
100 newArray = (wBufferPoolItem*)realloc(
102 sizeof(wBufferPoolItem) * WINPR_ASSERTING_INT_CAST(
size_t, newCapacity));
105 pool->aArray = newArray;
106 pool->aCapacity = newCapacity;
110 &pool->aArray[index + WINPR_ASSERTING_INT_CAST(
size_t, count)], &pool->aArray[index],
111 (WINPR_ASSERTING_INT_CAST(
size_t, pool->aSize) - index) *
sizeof(wBufferPoolItem));
112 pool->aSize += count;
117 &pool->aArray[index], &pool->aArray[index + WINPR_ASSERTING_INT_CAST(
size_t, -count)],
118 (WINPR_ASSERTING_INT_CAST(
size_t, pool->aSize) - index) *
sizeof(wBufferPoolItem));
119 pool->aSize += count;
124 static BOOL BufferPool_ShiftUsed(wBufferPool* pool, SSIZE_T index, SSIZE_T count)
128 if (pool->uSize + count > pool->uCapacity)
130 SSIZE_T newUCapacity = pool->uCapacity * 2;
131 wBufferPoolItem* newUArray = NULL;
132 if (pool->alignment > 0)
133 newUArray = (wBufferPoolItem*)winpr_aligned_realloc(
135 sizeof(wBufferPoolItem) * WINPR_ASSERTING_INT_CAST(
size_t, newUCapacity),
138 newUArray = (wBufferPoolItem*)realloc(
140 sizeof(wBufferPoolItem) * WINPR_ASSERTING_INT_CAST(
size_t, newUCapacity));
143 pool->uCapacity = newUCapacity;
144 pool->uArray = newUArray;
147 MoveMemory(&pool->uArray[index + count], &pool->uArray[index],
148 WINPR_ASSERTING_INT_CAST(
size_t, pool->uSize - index) *
sizeof(wBufferPoolItem));
149 pool->uSize += count;
153 MoveMemory(&pool->uArray[index], &pool->uArray[index - count],
154 WINPR_ASSERTING_INT_CAST(
size_t, pool->uSize - index) *
sizeof(wBufferPoolItem));
155 pool->uSize += count;
164 SSIZE_T BufferPool_GetPoolSize(wBufferPool* pool)
168 BufferPool_Lock(pool);
181 BufferPool_Unlock(pool);
190 SSIZE_T BufferPool_GetBufferSize(wBufferPool* pool,
const void* buffer)
195 BufferPool_Lock(pool);
200 size = pool->fixedSize;
207 for (SSIZE_T index = 0; index < pool->uSize; index++)
209 if (pool->uArray[index].buffer == buffer)
211 size = pool->uArray[index].size;
218 BufferPool_Unlock(pool);
220 return (found) ? size : -1;
227 void* BufferPool_Take(wBufferPool* pool, SSIZE_T size)
230 SSIZE_T maxIndex = 0;
231 SSIZE_T foundIndex = -1;
235 BufferPool_Lock(pool);
242 buffer = pool->array[--(pool->size)];
247 buffer = winpr_aligned_malloc(WINPR_ASSERTING_INT_CAST(
size_t, pool->fixedSize),
250 buffer = malloc(WINPR_ASSERTING_INT_CAST(
size_t, pool->fixedSize));
264 size = pool->fixedSize;
266 for (SSIZE_T index = 0; index < pool->aSize; index++)
268 if (pool->aArray[index].size > maxSize)
271 maxSize = pool->aArray[index].size;
274 if (pool->aArray[index].size >= size)
282 if (!found && maxSize)
284 foundIndex = maxIndex;
295 buffer = winpr_aligned_malloc(WINPR_ASSERTING_INT_CAST(
size_t, size),
298 buffer = malloc(WINPR_ASSERTING_INT_CAST(
size_t, size));
306 buffer = pool->aArray[foundIndex].buffer;
310 void* newBuffer = NULL;
312 newBuffer = winpr_aligned_realloc(
313 buffer, WINPR_ASSERTING_INT_CAST(
size_t, size), pool->alignment);
315 newBuffer = realloc(buffer, WINPR_ASSERTING_INT_CAST(
size_t, size));
318 goto out_error_no_free;
323 if (!BufferPool_ShiftAvailable(pool, WINPR_ASSERTING_INT_CAST(
size_t, foundIndex), -1))
330 if (pool->uSize + 1 > pool->uCapacity)
332 size_t newUCapacity = WINPR_ASSERTING_INT_CAST(
size_t, pool->uCapacity);
333 newUCapacity += (newUCapacity + 2) / 2;
334 if (newUCapacity > SSIZE_MAX)
336 wBufferPoolItem* newUArray =
337 (wBufferPoolItem*)realloc(pool->uArray,
sizeof(wBufferPoolItem) * newUCapacity);
341 pool->uCapacity = (SSIZE_T)newUCapacity;
342 pool->uArray = newUArray;
345 pool->uArray[pool->uSize].buffer = buffer;
346 pool->uArray[pool->uSize].size = size;
350 BufferPool_Unlock(pool);
356 winpr_aligned_free(buffer);
360 BufferPool_Unlock(pool);
368 BOOL BufferPool_Return(wBufferPool* pool,
void* buffer)
374 BufferPool_Lock(pool);
380 if ((pool->size + 1) >= pool->capacity)
382 SSIZE_T newCapacity = MAX(1, pool->size + (pool->size + 2) / 2 + 1);
383 void** newArray = (
void**)realloc(
384 (
void*)pool->array,
sizeof(
void*) * WINPR_ASSERTING_INT_CAST(
size_t, newCapacity));
388 pool->capacity = newCapacity;
389 pool->array = newArray;
392 pool->array[(pool->size)++] = buffer;
399 for (; index < pool->uSize; index++)
401 if (pool->uArray[index].buffer == buffer)
410 size = pool->uArray[index].size;
411 if (!BufferPool_ShiftUsed(pool, index, -1))
417 if ((pool->aSize + 1) >= pool->aCapacity)
419 SSIZE_T newCapacity = MAX(1, pool->aSize + (pool->aSize + 2) / 2 + 1);
420 wBufferPoolItem* newArray = (wBufferPoolItem*)realloc(
422 sizeof(wBufferPoolItem) * WINPR_ASSERTING_INT_CAST(
size_t, newCapacity));
426 pool->aCapacity = newCapacity;
427 pool->aArray = newArray;
430 pool->aArray[pool->aSize].buffer = buffer;
431 pool->aArray[pool->aSize].size = size;
438 BufferPool_Unlock(pool);
446 void BufferPool_Clear(wBufferPool* pool)
448 BufferPool_Lock(pool);
454 while (pool->size > 0)
459 winpr_aligned_free(pool->array[pool->size]);
461 free(pool->array[pool->size]);
468 while (pool->aSize > 0)
473 winpr_aligned_free(pool->aArray[pool->aSize].buffer);
475 free(pool->aArray[pool->aSize].buffer);
478 while (pool->uSize > 0)
483 winpr_aligned_free(pool->uArray[pool->uSize].buffer);
485 free(pool->uArray[pool->uSize].buffer);
489 BufferPool_Unlock(pool);
496 wBufferPool* BufferPool_New(BOOL
synchronized, SSIZE_T fixedSize, DWORD alignment)
498 wBufferPool* pool = NULL;
500 pool = (wBufferPool*)calloc(1,
sizeof(wBufferPool));
504 pool->fixedSize = fixedSize;
506 if (pool->fixedSize < 0)
509 pool->alignment = alignment;
510 pool->synchronized =
synchronized;
512 if (pool->synchronized)
513 InitializeCriticalSectionAndSpinCount(&pool->lock, 4000);
522 (
void**)calloc(WINPR_ASSERTING_INT_CAST(
size_t, pool->capacity),
sizeof(
void*));
531 pool->aCapacity = 32;
532 pool->aArray = (wBufferPoolItem*)calloc(
533 WINPR_ASSERTING_INT_CAST(
size_t, pool->aCapacity),
sizeof(wBufferPoolItem));
538 pool->uCapacity = 32;
539 pool->uArray = (wBufferPoolItem*)calloc(
540 WINPR_ASSERTING_INT_CAST(
size_t, pool->uCapacity),
sizeof(wBufferPoolItem));
549 WINPR_PRAGMA_DIAG_PUSH
550 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
551 BufferPool_Free(pool);
552 WINPR_PRAGMA_DIAG_POP
556 void BufferPool_Free(wBufferPool* pool)
560 BufferPool_Clear(pool);
562 if (pool->synchronized)
563 DeleteCriticalSection(&pool->lock);
569 free((
void*)pool->array);