20#include <winpr/config.h>
21#include <winpr/interlocked.h>
24#include <winpr/wlog.h>
26#include <winpr/collections.h>
30#define XTAG WINPR_TAG("utils.streampool")
32#if !defined(STREAMPOOL_SIZE_LIMIT)
33#error "CMake must define STREAMPOOL_SIZE_LIMIT=<unsigned>"
35static const size_t POOL_COMMON_LIMIT = STREAMPOOL_SIZE_LIMIT;
37struct s_StreamPoolEntry
39#if defined(WITH_DEBUG_STREAMPOOL)
50 struct s_StreamPoolEntry* aArray;
54 struct s_StreamPoolEntry* uArray;
56 CRITICAL_SECTION lock;
62static inline void ispoolvalid(WINPR_ATTR_UNUSED wStreamPool* pool)
65 WINPR_ASSERT(pool->aSize <= pool->aCapacity);
66 WINPR_ASSERT(pool->uSize <= pool->uCapacity);
69static void discard_entry(
struct s_StreamPoolEntry* entry, BOOL discardStream)
74#if defined(WITH_DEBUG_STREAMPOOL)
75 free((
void*)entry->msg);
78 if (discardStream && entry->s)
79 Stream_Free(entry->s, entry->s->isAllocatedStream);
81 const struct s_StreamPoolEntry empty = WINPR_C_ARRAY_INIT;
85static struct s_StreamPoolEntry add_entry(
wStream* s)
87 struct s_StreamPoolEntry entry = WINPR_C_ARRAY_INIT;
89#if defined(WITH_DEBUG_STREAMPOOL)
90 void* stack = winpr_backtrace(20);
92 entry.msg = winpr_backtrace_symbols(stack, &entry.lines);
93 winpr_backtrace_free(stack);
104static inline void StreamPool_Lock(wStreamPool* pool)
108 if (pool->synchronized)
109 EnterCriticalSection(&pool->lock);
116static inline void StreamPool_Unlock(wStreamPool* pool)
120 if (pool->synchronized)
121 LeaveCriticalSection(&pool->lock);
124static BOOL StreamPool_ShrinkToCommonLimit(
wStream* s)
126 if (Stream_Capacity(s) <= POOL_COMMON_LIMIT)
129 return Stream_ResizeToCapacity(s, POOL_COMMON_LIMIT);
132static BOOL StreamPool_EnsureCapacity(wStreamPool* pool,
size_t count, BOOL usedOrAvailable)
136 size_t* cap = (usedOrAvailable) ? &pool->uCapacity : &pool->aCapacity;
137 size_t* size = (usedOrAvailable) ? &pool->uSize : &pool->aSize;
138 struct s_StreamPoolEntry** array = (usedOrAvailable) ? &pool->uArray : &pool->aArray;
142 new_cap = *size + count;
143 else if (*size + count > *cap)
144 new_cap = (*size + count + 2) / 2 * 3;
145 else if ((*size + count) < *cap / 3)
150 struct s_StreamPoolEntry* new_arr =
nullptr;
152 if (*cap < *size + count)
156 (
struct s_StreamPoolEntry*)realloc(*array,
sizeof(
struct s_StreamPoolEntry) * new_cap);
169static void StreamPool_ShiftUsed(wStreamPool* pool,
size_t index)
173 const size_t pcount = 1;
174 const size_t off = index + pcount;
175 if (pool->uSize >= off)
177 for (
size_t x = 0; x < pcount; x++)
179 struct s_StreamPoolEntry* cur = &pool->uArray[index + x];
180 discard_entry(cur, FALSE);
182 MoveMemory(&pool->uArray[index], &pool->uArray[index + pcount],
183 (pool->uSize - index - pcount) *
sizeof(
struct s_StreamPoolEntry));
184 pool->uSize -= pcount;
192static void StreamPool_AddUsed(wStreamPool* pool,
wStream* s)
196 StreamPool_EnsureCapacity(pool, 1, TRUE);
197 pool->uArray[pool->uSize] = add_entry(s);
205static void StreamPool_RemoveUsed(wStreamPool* pool,
wStream* s)
209 for (
size_t index = 0; index < pool->uSize; index++)
211 struct s_StreamPoolEntry* cur = &pool->uArray[index];
214 StreamPool_ShiftUsed(pool, index);
222static void StreamPool_ShiftAvailable(wStreamPool* pool,
size_t index)
226 const size_t pcount = 1;
227 const size_t off = index + pcount;
228 if (pool->aSize >= off)
230 for (
size_t x = 0; x < pcount; x++)
232 struct s_StreamPoolEntry* cur = &pool->aArray[index + x];
233 discard_entry(cur, FALSE);
236 MoveMemory(&pool->aArray[index], &pool->aArray[index + pcount],
237 (pool->aSize - index - pcount) *
sizeof(
struct s_StreamPoolEntry));
238 pool->aSize -= pcount;
248wStream* StreamPool_Take(wStreamPool* pool,
size_t size)
253 size_t foundIndex = 0;
256 StreamPool_Lock(pool);
259 size = pool->defaultSize;
261 for (
size_t index = 0; index < pool->aSize; index++)
263 struct s_StreamPoolEntry* cur = &pool->aArray[index];
266 if (Stream_Capacity(s) >= size)
276 s = Stream_New(
nullptr, size);
282 Stream_ResetPosition(s);
283 if (!Stream_SetLength(s, Stream_Capacity(s)))
285 StreamPool_ShiftAvailable(pool, foundIndex);
292 StreamPool_AddUsed(pool, s);
296 StreamPool_Unlock(pool);
305static void StreamPool_Remove(wStreamPool* pool,
wStream* s)
309 StreamPool_EnsureCapacity(pool, 1, FALSE);
310 Stream_EnsureValidity(s);
311 StreamPool_ShrinkToCommonLimit(s);
312 for (
size_t x = 0; x < pool->aSize; x++)
314 wStream* cs = pool->aArray[x].s;
318 pool->aArray[(pool->aSize)++] = add_entry(s);
321 StreamPool_RemoveUsed(pool, s);
324static void StreamPool_ReleaseOrReturn(wStreamPool* pool,
wStream* s)
328 StreamPool_Lock(pool);
329 StreamPool_Remove(pool, s);
330 StreamPool_Unlock(pool);
333void StreamPool_Return(wStreamPool* pool,
wStream* s)
339 StreamPool_Lock(pool);
340 StreamPool_Remove(pool, s);
341 StreamPool_Unlock(pool);
351 InterlockedIncrement(&s->count);
362 const LONG val = InterlockedDecrement(&s->count);
366 StreamPool_ReleaseOrReturn(s->pool, s);
368 Stream_Free(s, TRUE);
370 WINPR_ASSERT(val >= 0);
377wStream* StreamPool_Find(wStreamPool* pool,
const BYTE* ptr)
382 StreamPool_Lock(pool);
384 for (
size_t index = 0; index < pool->uSize; index++)
386 struct s_StreamPoolEntry* cur = &pool->uArray[index];
388 if ((ptr >= Stream_Buffer(cur->s)) &&
389 (ptr < (Stream_Buffer(cur->s) + Stream_Capacity(cur->s))))
396 StreamPool_Unlock(pool);
405void StreamPool_Clear(wStreamPool* pool)
409 StreamPool_Lock(pool);
411 for (
size_t x = 0; x < pool->aSize; x++)
413 struct s_StreamPoolEntry* cur = &pool->aArray[x];
414 discard_entry(cur, TRUE);
420 WLog_Print(pool->log, WLOG_WARN,
421 "Clearing StreamPool, but there are %" PRIuz
" streams currently in use",
423 for (
size_t x = 0; x < pool->uSize; x++)
425 struct s_StreamPoolEntry* cur = &pool->uArray[x];
426 discard_entry(cur, TRUE);
431 StreamPool_Unlock(pool);
434size_t StreamPool_UsedCount(wStreamPool* pool)
438 StreamPool_Lock(pool);
439 size_t usize = pool->uSize;
440 StreamPool_Unlock(pool);
448wStreamPool* StreamPool_New(BOOL
synchronized,
size_t defaultSize)
450 wStreamPool* pool = calloc(1,
sizeof(wStreamPool));
455 pool->log = WLog_Create(XTAG, WLog_GetRoot());
459 pool->synchronized =
synchronized;
460 pool->defaultSize = defaultSize;
462 if (!StreamPool_EnsureCapacity(pool, 32, FALSE))
464 if (!StreamPool_EnsureCapacity(pool, 32, TRUE))
467 if (!InitializeCriticalSectionAndSpinCount(&pool->lock, 4000))
472 WINPR_PRAGMA_DIAG_PUSH
473 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
474 StreamPool_Free(pool);
475 WINPR_PRAGMA_DIAG_POP
479void StreamPool_Free(wStreamPool* pool)
486 StreamPool_Clear(pool);
488 DeleteCriticalSection(&pool->lock);
493 WLog_Discard(pool->log);
497char* StreamPool_GetStatistics(wStreamPool* pool,
char* buffer,
size_t size)
501 if (!buffer || (size < 1))
505 int offset = _snprintf(buffer, size - 1,
506 "aSize =%" PRIuz
", uSize =%" PRIuz
", aCapacity=%" PRIuz
507 ", uCapacity=%" PRIuz,
508 pool->aSize, pool->uSize, pool->aCapacity, pool->uCapacity);
509 if ((offset > 0) && ((
size_t)offset < size))
510 used += (size_t)offset;
512#if defined(WITH_DEBUG_STREAMPOOL)
513 StreamPool_Lock(pool);
515 offset = _snprintf(&buffer[used], size - 1 - used,
"\n-- dump used array take locations --\n");
516 if ((offset > 0) && ((
size_t)offset < size - used))
517 used += (size_t)offset;
518 for (
size_t x = 0; x < pool->uSize; x++)
520 const struct s_StreamPoolEntry* cur = &pool->uArray[x];
521 WINPR_ASSERT(cur->msg || (cur->lines == 0));
523 for (
size_t y = 0; y < cur->lines; y++)
525 offset = _snprintf(&buffer[used], size - 1 - used,
"[%" PRIuz
" | %" PRIuz
"]: %s\n", x,
527 if ((offset > 0) && ((
size_t)offset < size - used))
528 used += (size_t)offset;
532 offset = _snprintf(&buffer[used], size - 1 - used,
"\n-- statistics called from --\n");
533 if ((offset > 0) && ((
size_t)offset < size - used))
534 used += (size_t)offset;
536 struct s_StreamPoolEntry entry = WINPR_C_ARRAY_INIT;
537 void* stack = winpr_backtrace(20);
539 entry.msg = winpr_backtrace_symbols(stack, &entry.lines);
540 winpr_backtrace_free(stack);
542 for (
size_t x = 0; x < entry.lines; x++)
544 const char* msg = entry.msg[x];
545 offset = _snprintf(&buffer[used], size - 1 - used,
"[%" PRIuz
"]: %s\n", x, msg);
546 if ((offset > 0) && ((
size_t)offset < size - used))
547 used += (size_t)offset;
549 free((
void*)entry.msg);
550 StreamPool_Unlock(pool);
556BOOL StreamPool_WaitForReturn(wStreamPool* pool, UINT32 timeoutMS)
562 while (timeoutMS > 0)
564 const size_t used = StreamPool_UsedCount(pool);
567 WLog_Print(pool->log, WLOG_DEBUG,
"%" PRIuz
" streams still in use, sleeping...", used);
569 char buffer[4096] = WINPR_C_ARRAY_INIT;
570 StreamPool_GetStatistics(pool, buffer,
sizeof(buffer));
571 WLog_Print(pool->log, WLOG_TRACE,
"Pool statistics: %s", buffer);
574 if (timeoutMS != INFINITE)
576 diff = timeoutMS > 10 ? 10 : timeoutMS;