FreeRDP
Loading...
Searching...
No Matches
StreamPool.c
1
19
20#include <winpr/config.h>
21#include <winpr/interlocked.h>
22
23#include <winpr/crt.h>
24#include <winpr/wlog.h>
25
26#include <winpr/collections.h>
27
28#include "../stream.h"
29#include "../log.h"
30#define XTAG WINPR_TAG("utils.streampool")
31
32#if !defined(STREAMPOOL_SIZE_LIMIT)
33#error "CMake must define STREAMPOOL_SIZE_LIMIT=<unsigned>"
34#endif
35static const size_t POOL_COMMON_LIMIT = STREAMPOOL_SIZE_LIMIT;
36
37struct s_StreamPoolEntry
38{
39#if defined(WITH_DEBUG_STREAMPOOL)
40 char** msg;
41 size_t lines;
42#endif
43 wStream* s;
44};
45
46struct s_wStreamPool
47{
48 size_t aSize;
49 size_t aCapacity;
50 struct s_StreamPoolEntry* aArray;
51
52 size_t uSize;
53 size_t uCapacity;
54 struct s_StreamPoolEntry* uArray;
55
56 CRITICAL_SECTION lock;
57 BOOL synchronized;
58 size_t defaultSize;
59 wLog* log;
60};
61
62static inline void ispoolvalid(WINPR_ATTR_UNUSED wStreamPool* pool)
63{
64 WINPR_ASSERT(pool);
65 WINPR_ASSERT(pool->aSize <= pool->aCapacity);
66 WINPR_ASSERT(pool->uSize <= pool->uCapacity);
67}
68
69static void discard_entry(struct s_StreamPoolEntry* entry, BOOL discardStream)
70{
71 if (!entry)
72 return;
73
74#if defined(WITH_DEBUG_STREAMPOOL)
75 free((void*)entry->msg);
76#endif
77
78 if (discardStream && entry->s)
79 Stream_Free(entry->s, entry->s->isAllocatedStream);
80
81 const struct s_StreamPoolEntry empty = WINPR_C_ARRAY_INIT;
82 *entry = empty;
83}
84
85static struct s_StreamPoolEntry add_entry(wStream* s)
86{
87 struct s_StreamPoolEntry entry = WINPR_C_ARRAY_INIT;
88
89#if defined(WITH_DEBUG_STREAMPOOL)
90 void* stack = winpr_backtrace(20);
91 if (stack)
92 entry.msg = winpr_backtrace_symbols(stack, &entry.lines);
93 winpr_backtrace_free(stack);
94#endif
95
96 entry.s = s;
97 return entry;
98}
99
103
104static inline void StreamPool_Lock(wStreamPool* pool)
105{
106 ispoolvalid(pool);
107
108 if (pool->synchronized)
109 EnterCriticalSection(&pool->lock);
110}
111
115
116static inline void StreamPool_Unlock(wStreamPool* pool)
117{
118 ispoolvalid(pool);
119
120 if (pool->synchronized)
121 LeaveCriticalSection(&pool->lock);
122}
123
124static BOOL StreamPool_ShrinkToCommonLimit(wStream* s)
125{
126 if (Stream_Capacity(s) <= POOL_COMMON_LIMIT)
127 return TRUE;
128
129 return Stream_ResizeToCapacity(s, POOL_COMMON_LIMIT);
130}
131
132static BOOL StreamPool_EnsureCapacity(wStreamPool* pool, size_t count, BOOL usedOrAvailable)
133{
134 ispoolvalid(pool);
135
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;
139
140 size_t new_cap = 0;
141 if (*cap == 0)
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)
146 new_cap = *cap / 2;
147
148 if (new_cap > 0)
149 {
150 struct s_StreamPoolEntry* new_arr = nullptr;
151
152 if (*cap < *size + count)
153 *cap += count;
154
155 new_arr =
156 (struct s_StreamPoolEntry*)realloc(*array, sizeof(struct s_StreamPoolEntry) * new_cap);
157 if (!new_arr)
158 return FALSE;
159 *cap = new_cap;
160 *array = new_arr;
161 }
162 return TRUE;
163}
164
168
169static void StreamPool_ShiftUsed(wStreamPool* pool, size_t index)
170{
171 ispoolvalid(pool);
172
173 const size_t pcount = 1;
174 const size_t off = index + pcount;
175 if (pool->uSize >= off)
176 {
177 for (size_t x = 0; x < pcount; x++)
178 {
179 struct s_StreamPoolEntry* cur = &pool->uArray[index + x];
180 discard_entry(cur, FALSE);
181 }
182 MoveMemory(&pool->uArray[index], &pool->uArray[index + pcount],
183 (pool->uSize - index - pcount) * sizeof(struct s_StreamPoolEntry));
184 pool->uSize -= pcount;
185 }
186}
187
191
192static void StreamPool_AddUsed(wStreamPool* pool, wStream* s)
193{
194 ispoolvalid(pool);
195
196 StreamPool_EnsureCapacity(pool, 1, TRUE);
197 pool->uArray[pool->uSize] = add_entry(s);
198 pool->uSize++;
199}
200
204
205static void StreamPool_RemoveUsed(wStreamPool* pool, wStream* s)
206{
207 ispoolvalid(pool);
208
209 for (size_t index = 0; index < pool->uSize; index++)
210 {
211 struct s_StreamPoolEntry* cur = &pool->uArray[index];
212 if (cur->s == s)
213 {
214 StreamPool_ShiftUsed(pool, index);
215 break;
216 }
217 }
218
219 ispoolvalid(pool);
220}
221
222static void StreamPool_ShiftAvailable(wStreamPool* pool, size_t index)
223{
224 ispoolvalid(pool);
225
226 const size_t pcount = 1;
227 const size_t off = index + pcount;
228 if (pool->aSize >= off)
229 {
230 for (size_t x = 0; x < pcount; x++)
231 {
232 struct s_StreamPoolEntry* cur = &pool->aArray[index + x];
233 discard_entry(cur, FALSE);
234 }
235
236 MoveMemory(&pool->aArray[index], &pool->aArray[index + pcount],
237 (pool->aSize - index - pcount) * sizeof(struct s_StreamPoolEntry));
238 pool->aSize -= pcount;
239 }
240
241 ispoolvalid(pool);
242}
243
247
248wStream* StreamPool_Take(wStreamPool* pool, size_t size)
249{
250 ispoolvalid(pool);
251
252 BOOL found = FALSE;
253 size_t foundIndex = 0;
254 wStream* s = nullptr;
255
256 StreamPool_Lock(pool);
257
258 if (size == 0)
259 size = pool->defaultSize;
260
261 for (size_t index = 0; index < pool->aSize; index++)
262 {
263 struct s_StreamPoolEntry* cur = &pool->aArray[index];
264 s = cur->s;
265
266 if (Stream_Capacity(s) >= size)
267 {
268 found = TRUE;
269 foundIndex = index;
270 break;
271 }
272 }
273
274 if (!found)
275 {
276 s = Stream_New(nullptr, size);
277 if (!s)
278 goto out_fail;
279 }
280 else if (s)
281 {
282 Stream_ResetPosition(s);
283 if (!Stream_SetLength(s, Stream_Capacity(s)))
284 goto out_fail;
285 StreamPool_ShiftAvailable(pool, foundIndex);
286 }
287
288 if (s)
289 {
290 s->pool = pool;
291 s->count = 1;
292 StreamPool_AddUsed(pool, s);
293 }
294
295out_fail:
296 StreamPool_Unlock(pool);
297
298 return s;
299}
300
304
305static void StreamPool_Remove(wStreamPool* pool, wStream* s)
306{
307 ispoolvalid(pool);
308
309 StreamPool_EnsureCapacity(pool, 1, FALSE);
310 Stream_EnsureValidity(s);
311 StreamPool_ShrinkToCommonLimit(s);
312 for (size_t x = 0; x < pool->aSize; x++)
313 {
314 wStream* cs = pool->aArray[x].s;
315 if (cs == s)
316 return;
317 }
318 pool->aArray[(pool->aSize)++] = add_entry(s);
319 ispoolvalid(pool);
320
321 StreamPool_RemoveUsed(pool, s);
322}
323
324static void StreamPool_ReleaseOrReturn(wStreamPool* pool, wStream* s)
325{
326 ispoolvalid(pool);
327
328 StreamPool_Lock(pool);
329 StreamPool_Remove(pool, s);
330 StreamPool_Unlock(pool);
331}
332
333void StreamPool_Return(wStreamPool* pool, wStream* s)
334{
335 ispoolvalid(pool);
336 if (!s)
337 return;
338
339 StreamPool_Lock(pool);
340 StreamPool_Remove(pool, s);
341 StreamPool_Unlock(pool);
342}
343
347
348void Stream_AddRef(wStream* s)
349{
350 WINPR_ASSERT(s);
351 InterlockedIncrement(&s->count);
352}
353
357
358void Stream_Release(wStream* s)
359{
360 WINPR_ASSERT(s);
361
362 const LONG val = InterlockedDecrement(&s->count);
363 if (val == 0)
364 {
365 if (s->pool)
366 StreamPool_ReleaseOrReturn(s->pool, s);
367 else
368 Stream_Free(s, TRUE);
369 }
370 WINPR_ASSERT(val >= 0);
371}
372
376
377wStream* StreamPool_Find(wStreamPool* pool, const BYTE* ptr)
378{
379 ispoolvalid(pool);
380 wStream* s = nullptr;
381
382 StreamPool_Lock(pool);
383
384 for (size_t index = 0; index < pool->uSize; index++)
385 {
386 struct s_StreamPoolEntry* cur = &pool->uArray[index];
387
388 if ((ptr >= Stream_Buffer(cur->s)) &&
389 (ptr < (Stream_Buffer(cur->s) + Stream_Capacity(cur->s))))
390 {
391 s = cur->s;
392 break;
393 }
394 }
395
396 StreamPool_Unlock(pool);
397
398 return s;
399}
400
404
405void StreamPool_Clear(wStreamPool* pool)
406{
407 ispoolvalid(pool);
408
409 StreamPool_Lock(pool);
410
411 for (size_t x = 0; x < pool->aSize; x++)
412 {
413 struct s_StreamPoolEntry* cur = &pool->aArray[x];
414 discard_entry(cur, TRUE);
415 }
416 pool->aSize = 0;
417
418 if (pool->uSize > 0)
419 {
420 WLog_Print(pool->log, WLOG_WARN,
421 "Clearing StreamPool, but there are %" PRIuz " streams currently in use",
422 pool->uSize);
423 for (size_t x = 0; x < pool->uSize; x++)
424 {
425 struct s_StreamPoolEntry* cur = &pool->uArray[x];
426 discard_entry(cur, TRUE);
427 }
428 pool->uSize = 0;
429 }
430
431 StreamPool_Unlock(pool);
432}
433
434size_t StreamPool_UsedCount(wStreamPool* pool)
435{
436 ispoolvalid(pool);
437
438 StreamPool_Lock(pool);
439 size_t usize = pool->uSize;
440 StreamPool_Unlock(pool);
441 return usize;
442}
443
447
448wStreamPool* StreamPool_New(BOOL synchronized, size_t defaultSize)
449{
450 wStreamPool* pool = calloc(1, sizeof(wStreamPool));
451
452 if (!pool)
453 return nullptr;
454
455 pool->log = WLog_Create(XTAG, WLog_GetRoot());
456 if (!pool->log)
457 goto fail;
458
459 pool->synchronized = synchronized;
460 pool->defaultSize = defaultSize;
461
462 if (!StreamPool_EnsureCapacity(pool, 32, FALSE))
463 goto fail;
464 if (!StreamPool_EnsureCapacity(pool, 32, TRUE))
465 goto fail;
466
467 if (!InitializeCriticalSectionAndSpinCount(&pool->lock, 4000))
468 goto fail;
469
470 return pool;
471fail:
472 WINPR_PRAGMA_DIAG_PUSH
473 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
474 StreamPool_Free(pool);
475 WINPR_PRAGMA_DIAG_POP
476 return nullptr;
477}
478
479void StreamPool_Free(wStreamPool* pool)
480{
481 if (!pool)
482 return;
483
484 ispoolvalid(pool);
485
486 StreamPool_Clear(pool);
487
488 DeleteCriticalSection(&pool->lock);
489
490 free(pool->aArray);
491 free(pool->uArray);
492
493 WLog_Discard(pool->log);
494 free(pool);
495}
496
497char* StreamPool_GetStatistics(wStreamPool* pool, char* buffer, size_t size)
498{
499 ispoolvalid(pool);
500
501 if (!buffer || (size < 1))
502 return nullptr;
503
504 size_t used = 0;
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;
511
512#if defined(WITH_DEBUG_STREAMPOOL)
513 StreamPool_Lock(pool);
514
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++)
519 {
520 const struct s_StreamPoolEntry* cur = &pool->uArray[x];
521 WINPR_ASSERT(cur->msg || (cur->lines == 0));
522
523 for (size_t y = 0; y < cur->lines; y++)
524 {
525 offset = _snprintf(&buffer[used], size - 1 - used, "[%" PRIuz " | %" PRIuz "]: %s\n", x,
526 y, cur->msg[y]);
527 if ((offset > 0) && ((size_t)offset < size - used))
528 used += (size_t)offset;
529 }
530 }
531
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;
535
536 struct s_StreamPoolEntry entry = WINPR_C_ARRAY_INIT;
537 void* stack = winpr_backtrace(20);
538 if (stack)
539 entry.msg = winpr_backtrace_symbols(stack, &entry.lines);
540 winpr_backtrace_free(stack);
541
542 for (size_t x = 0; x < entry.lines; x++)
543 {
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;
548 }
549 free((void*)entry.msg);
550 StreamPool_Unlock(pool);
551#endif
552 buffer[used] = '\0';
553 return buffer;
554}
555
556BOOL StreamPool_WaitForReturn(wStreamPool* pool, UINT32 timeoutMS)
557{
558 ispoolvalid(pool);
559
560 /* HACK: We disconnected the transport above, now wait without a read or write lock until all
561 * streams in use have been returned to the pool. */
562 while (timeoutMS > 0)
563 {
564 const size_t used = StreamPool_UsedCount(pool);
565 if (used == 0)
566 return TRUE;
567 WLog_Print(pool->log, WLOG_DEBUG, "%" PRIuz " streams still in use, sleeping...", used);
568
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);
572
573 UINT32 diff = 10;
574 if (timeoutMS != INFINITE)
575 {
576 diff = timeoutMS > 10 ? 10 : timeoutMS;
577 timeoutMS -= diff;
578 }
579 Sleep(diff);
580 }
581
582 return FALSE;
583}