/src/FreeRDP/winpr/libwinpr/utils/collections/StreamPool.c
Line | Count | Source |
1 | | /** |
2 | | * WinPR: Windows Portable Runtime |
3 | | * Object Pool |
4 | | * |
5 | | * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <winpr/config.h> |
21 | | |
22 | | #include <winpr/crt.h> |
23 | | #include <winpr/wlog.h> |
24 | | |
25 | | #include <winpr/collections.h> |
26 | | |
27 | | #include "../stream.h" |
28 | | #include "../log.h" |
29 | 20 | #define XTAG WINPR_TAG("utils.streampool") |
30 | | |
31 | | #if !defined(STREAMPOOL_SIZE_LIMIT) |
32 | | #error "CMake must define STREAMPOOL_SIZE_LIMIT=<unsigned>" |
33 | | #endif |
34 | | static const size_t POOL_COMMON_LIMIT = STREAMPOOL_SIZE_LIMIT; |
35 | | |
36 | | struct s_StreamPoolEntry |
37 | | { |
38 | | #if defined(WITH_DEBUG_STREAMPOOL) |
39 | | char** msg; |
40 | | size_t lines; |
41 | | #endif |
42 | | wStream* s; |
43 | | }; |
44 | | |
45 | | struct s_wStreamPool |
46 | | { |
47 | | size_t aSize; |
48 | | size_t aCapacity; |
49 | | struct s_StreamPoolEntry* aArray; |
50 | | |
51 | | size_t uSize; |
52 | | size_t uCapacity; |
53 | | struct s_StreamPoolEntry* uArray; |
54 | | |
55 | | CRITICAL_SECTION lock; |
56 | | BOOL synchronized; |
57 | | size_t defaultSize; |
58 | | wLog* log; |
59 | | }; |
60 | | |
61 | | static void discard_entry(struct s_StreamPoolEntry* entry, BOOL discardStream) |
62 | 40 | { |
63 | 40 | if (!entry) |
64 | 0 | return; |
65 | | |
66 | 40 | #if defined(WITH_DEBUG_STREAMPOOL) |
67 | 40 | free((void*)entry->msg); |
68 | 40 | #endif |
69 | | |
70 | 40 | if (discardStream && entry->s) |
71 | 20 | Stream_Free(entry->s, entry->s->isAllocatedStream); |
72 | | |
73 | 40 | const struct s_StreamPoolEntry empty = WINPR_C_ARRAY_INIT; |
74 | 40 | *entry = empty; |
75 | 40 | } |
76 | | |
77 | | static struct s_StreamPoolEntry add_entry(wStream* s) |
78 | 40 | { |
79 | 40 | struct s_StreamPoolEntry entry = WINPR_C_ARRAY_INIT; |
80 | | |
81 | 40 | #if defined(WITH_DEBUG_STREAMPOOL) |
82 | 40 | void* stack = winpr_backtrace(20); |
83 | 40 | if (stack) |
84 | 40 | entry.msg = winpr_backtrace_symbols(stack, &entry.lines); |
85 | 40 | winpr_backtrace_free(stack); |
86 | 40 | #endif |
87 | | |
88 | 40 | entry.s = s; |
89 | 40 | return entry; |
90 | 40 | } |
91 | | |
92 | | /** |
93 | | * Lock the stream pool |
94 | | */ |
95 | | |
96 | | static inline void StreamPool_Lock(wStreamPool* pool) |
97 | 80 | { |
98 | 80 | WINPR_ASSERT(pool); |
99 | 80 | if (pool->synchronized) |
100 | 80 | EnterCriticalSection(&pool->lock); |
101 | 80 | } |
102 | | |
103 | | /** |
104 | | * Unlock the stream pool |
105 | | */ |
106 | | |
107 | | static inline void StreamPool_Unlock(wStreamPool* pool) |
108 | 80 | { |
109 | 80 | WINPR_ASSERT(pool); |
110 | 80 | if (pool->synchronized) |
111 | 80 | LeaveCriticalSection(&pool->lock); |
112 | 80 | } |
113 | | |
114 | | static BOOL StreamPool_ShrinkToCommonLimit(wStream* s) |
115 | 20 | { |
116 | 20 | if (Stream_Capacity(s) <= POOL_COMMON_LIMIT) |
117 | 20 | return TRUE; |
118 | | |
119 | 0 | return Stream_ResizeToCapacity(s, POOL_COMMON_LIMIT); |
120 | 20 | } |
121 | | |
122 | | static BOOL StreamPool_EnsureCapacity(wStreamPool* pool, size_t count, BOOL usedOrAvailable) |
123 | 80 | { |
124 | 80 | WINPR_ASSERT(pool); |
125 | | |
126 | 80 | size_t* cap = (usedOrAvailable) ? &pool->uCapacity : &pool->aCapacity; |
127 | 80 | size_t* size = (usedOrAvailable) ? &pool->uSize : &pool->aSize; |
128 | 80 | struct s_StreamPoolEntry** array = (usedOrAvailable) ? &pool->uArray : &pool->aArray; |
129 | | |
130 | 80 | size_t new_cap = 0; |
131 | 80 | if (*cap == 0) |
132 | 40 | new_cap = *size + count; |
133 | 40 | else if (*size + count > *cap) |
134 | 0 | new_cap = (*size + count + 2) / 2 * 3; |
135 | 40 | else if ((*size + count) < *cap / 3) |
136 | 40 | new_cap = *cap / 2; |
137 | | |
138 | 80 | if (new_cap > 0) |
139 | 80 | { |
140 | 80 | struct s_StreamPoolEntry* new_arr = nullptr; |
141 | | |
142 | 80 | if (*cap < *size + count) |
143 | 40 | *cap += count; |
144 | | |
145 | 80 | new_arr = |
146 | 80 | (struct s_StreamPoolEntry*)realloc(*array, sizeof(struct s_StreamPoolEntry) * new_cap); |
147 | 80 | if (!new_arr) |
148 | 0 | return FALSE; |
149 | 80 | *cap = new_cap; |
150 | 80 | *array = new_arr; |
151 | 80 | } |
152 | 80 | return TRUE; |
153 | 80 | } |
154 | | |
155 | | /** |
156 | | * Methods |
157 | | */ |
158 | | |
159 | | static void StreamPool_ShiftUsed(wStreamPool* pool, size_t index) |
160 | 20 | { |
161 | 20 | WINPR_ASSERT(pool); |
162 | | |
163 | 20 | const size_t pcount = 1; |
164 | 20 | const size_t off = index + pcount; |
165 | 20 | if (pool->uSize >= off) |
166 | 20 | { |
167 | 40 | for (size_t x = 0; x < pcount; x++) |
168 | 20 | { |
169 | 20 | struct s_StreamPoolEntry* cur = &pool->uArray[index + x]; |
170 | 20 | discard_entry(cur, FALSE); |
171 | 20 | } |
172 | 20 | MoveMemory(&pool->uArray[index], &pool->uArray[index + pcount], |
173 | 20 | (pool->uSize - index - pcount) * sizeof(struct s_StreamPoolEntry)); |
174 | 20 | pool->uSize -= pcount; |
175 | 20 | } |
176 | 20 | } |
177 | | |
178 | | /** |
179 | | * Adds a used stream to the pool. |
180 | | */ |
181 | | |
182 | | static void StreamPool_AddUsed(wStreamPool* pool, wStream* s) |
183 | 20 | { |
184 | 20 | StreamPool_EnsureCapacity(pool, 1, TRUE); |
185 | 20 | pool->uArray[pool->uSize] = add_entry(s); |
186 | 20 | pool->uSize++; |
187 | 20 | } |
188 | | |
189 | | /** |
190 | | * Removes a used stream from the pool. |
191 | | */ |
192 | | |
193 | | static void StreamPool_RemoveUsed(wStreamPool* pool, wStream* s) |
194 | 20 | { |
195 | 20 | WINPR_ASSERT(pool); |
196 | 20 | for (size_t index = 0; index < pool->uSize; index++) |
197 | 20 | { |
198 | 20 | struct s_StreamPoolEntry* cur = &pool->uArray[index]; |
199 | 20 | if (cur->s == s) |
200 | 20 | { |
201 | 20 | StreamPool_ShiftUsed(pool, index); |
202 | 20 | break; |
203 | 20 | } |
204 | 20 | } |
205 | 20 | } |
206 | | |
207 | | static void StreamPool_ShiftAvailable(wStreamPool* pool, size_t index) |
208 | 0 | { |
209 | 0 | WINPR_ASSERT(pool); |
210 | |
|
211 | 0 | const size_t pcount = 1; |
212 | 0 | const size_t off = index + pcount; |
213 | 0 | if (pool->aSize >= off) |
214 | 0 | { |
215 | 0 | for (size_t x = 0; x < pcount; x++) |
216 | 0 | { |
217 | 0 | struct s_StreamPoolEntry* cur = &pool->aArray[index + x]; |
218 | 0 | discard_entry(cur, FALSE); |
219 | 0 | } |
220 | |
|
221 | 0 | MoveMemory(&pool->aArray[index], &pool->aArray[index + pcount], |
222 | 0 | (pool->aSize - index - pcount) * sizeof(struct s_StreamPoolEntry)); |
223 | 0 | pool->aSize -= pcount; |
224 | 0 | } |
225 | 0 | } |
226 | | |
227 | | /** |
228 | | * Gets a stream from the pool. |
229 | | */ |
230 | | |
231 | | wStream* StreamPool_Take(wStreamPool* pool, size_t size) |
232 | 20 | { |
233 | 20 | BOOL found = FALSE; |
234 | 20 | size_t foundIndex = 0; |
235 | 20 | wStream* s = nullptr; |
236 | | |
237 | 20 | StreamPool_Lock(pool); |
238 | | |
239 | 20 | if (size == 0) |
240 | 20 | size = pool->defaultSize; |
241 | | |
242 | 20 | for (size_t index = 0; index < pool->aSize; index++) |
243 | 0 | { |
244 | 0 | struct s_StreamPoolEntry* cur = &pool->aArray[index]; |
245 | 0 | s = cur->s; |
246 | |
|
247 | 0 | if (Stream_Capacity(s) >= size) |
248 | 0 | { |
249 | 0 | found = TRUE; |
250 | 0 | foundIndex = index; |
251 | 0 | break; |
252 | 0 | } |
253 | 0 | } |
254 | | |
255 | 20 | if (!found) |
256 | 20 | { |
257 | 20 | s = Stream_New(nullptr, size); |
258 | 20 | if (!s) |
259 | 0 | goto out_fail; |
260 | 20 | } |
261 | 0 | else if (s) |
262 | 0 | { |
263 | 0 | Stream_ResetPosition(s); |
264 | 0 | if (!Stream_SetLength(s, Stream_Capacity(s))) |
265 | 0 | goto out_fail; |
266 | 0 | StreamPool_ShiftAvailable(pool, foundIndex); |
267 | 0 | } |
268 | | |
269 | 20 | if (s) |
270 | 20 | { |
271 | 20 | s->pool = pool; |
272 | 20 | s->count = 1; |
273 | 20 | StreamPool_AddUsed(pool, s); |
274 | 20 | } |
275 | | |
276 | 20 | out_fail: |
277 | 20 | StreamPool_Unlock(pool); |
278 | | |
279 | 20 | return s; |
280 | 20 | } |
281 | | |
282 | | /** |
283 | | * Returns an object to the pool. |
284 | | */ |
285 | | |
286 | | static void StreamPool_Remove(wStreamPool* pool, wStream* s) |
287 | 20 | { |
288 | 20 | StreamPool_EnsureCapacity(pool, 1, FALSE); |
289 | 20 | Stream_EnsureValidity(s); |
290 | 20 | StreamPool_ShrinkToCommonLimit(s); |
291 | 20 | for (size_t x = 0; x < pool->aSize; x++) |
292 | 0 | { |
293 | 0 | wStream* cs = pool->aArray[x].s; |
294 | 0 | if (cs == s) |
295 | 0 | return; |
296 | 0 | } |
297 | 20 | pool->aArray[(pool->aSize)++] = add_entry(s); |
298 | 20 | StreamPool_RemoveUsed(pool, s); |
299 | 20 | } |
300 | | |
301 | | static void StreamPool_ReleaseOrReturn(wStreamPool* pool, wStream* s) |
302 | 20 | { |
303 | 20 | StreamPool_Lock(pool); |
304 | 20 | StreamPool_Remove(pool, s); |
305 | 20 | StreamPool_Unlock(pool); |
306 | 20 | } |
307 | | |
308 | | void StreamPool_Return(wStreamPool* pool, wStream* s) |
309 | 0 | { |
310 | 0 | WINPR_ASSERT(pool); |
311 | 0 | if (!s) |
312 | 0 | return; |
313 | | |
314 | 0 | StreamPool_Lock(pool); |
315 | 0 | StreamPool_Remove(pool, s); |
316 | 0 | StreamPool_Unlock(pool); |
317 | 0 | } |
318 | | |
319 | | /** |
320 | | * Increment stream reference count |
321 | | */ |
322 | | |
323 | | void Stream_AddRef(wStream* s) |
324 | 0 | { |
325 | 0 | WINPR_ASSERT(s); |
326 | 0 | s->count++; |
327 | 0 | } |
328 | | |
329 | | /** |
330 | | * Decrement stream reference count |
331 | | */ |
332 | | |
333 | | void Stream_Release(wStream* s) |
334 | 20 | { |
335 | 20 | WINPR_ASSERT(s); |
336 | | |
337 | 20 | if (s->count > 0) |
338 | 20 | s->count--; |
339 | 20 | if (s->count == 0) |
340 | 20 | { |
341 | 20 | if (s->pool) |
342 | 20 | StreamPool_ReleaseOrReturn(s->pool, s); |
343 | 0 | else |
344 | 0 | Stream_Free(s, TRUE); |
345 | 20 | } |
346 | 20 | } |
347 | | |
348 | | /** |
349 | | * Find stream in pool using pointer inside buffer |
350 | | */ |
351 | | |
352 | | wStream* StreamPool_Find(wStreamPool* pool, const BYTE* ptr) |
353 | 0 | { |
354 | 0 | wStream* s = nullptr; |
355 | |
|
356 | 0 | StreamPool_Lock(pool); |
357 | |
|
358 | 0 | for (size_t index = 0; index < pool->uSize; index++) |
359 | 0 | { |
360 | 0 | struct s_StreamPoolEntry* cur = &pool->uArray[index]; |
361 | |
|
362 | 0 | if ((ptr >= Stream_Buffer(cur->s)) && |
363 | 0 | (ptr < (Stream_Buffer(cur->s) + Stream_Capacity(cur->s)))) |
364 | 0 | { |
365 | 0 | s = cur->s; |
366 | 0 | break; |
367 | 0 | } |
368 | 0 | } |
369 | |
|
370 | 0 | StreamPool_Unlock(pool); |
371 | |
|
372 | 0 | return s; |
373 | 0 | } |
374 | | |
375 | | /** |
376 | | * Releases the streams currently cached in the pool. |
377 | | */ |
378 | | |
379 | | void StreamPool_Clear(wStreamPool* pool) |
380 | 20 | { |
381 | 20 | StreamPool_Lock(pool); |
382 | | |
383 | 40 | for (size_t x = 0; x < pool->aSize; x++) |
384 | 20 | { |
385 | 20 | struct s_StreamPoolEntry* cur = &pool->aArray[x]; |
386 | 20 | discard_entry(cur, TRUE); |
387 | 20 | } |
388 | 20 | pool->aSize = 0; |
389 | | |
390 | 20 | if (pool->uSize > 0) |
391 | 0 | { |
392 | 0 | WLog_Print(pool->log, WLOG_WARN, |
393 | 0 | "Clearing StreamPool, but there are %" PRIuz " streams currently in use", |
394 | 0 | pool->uSize); |
395 | 0 | for (size_t x = 0; x < pool->uSize; x++) |
396 | 0 | { |
397 | 0 | struct s_StreamPoolEntry* cur = &pool->uArray[x]; |
398 | 0 | discard_entry(cur, TRUE); |
399 | 0 | } |
400 | 0 | pool->uSize = 0; |
401 | 0 | } |
402 | | |
403 | 20 | StreamPool_Unlock(pool); |
404 | 20 | } |
405 | | |
406 | | size_t StreamPool_UsedCount(wStreamPool* pool) |
407 | 20 | { |
408 | 20 | StreamPool_Lock(pool); |
409 | 20 | size_t usize = pool->uSize; |
410 | 20 | StreamPool_Unlock(pool); |
411 | 20 | return usize; |
412 | 20 | } |
413 | | |
414 | | /** |
415 | | * Construction, Destruction |
416 | | */ |
417 | | |
418 | | wStreamPool* StreamPool_New(BOOL synchronized, size_t defaultSize) |
419 | 20 | { |
420 | 20 | wStreamPool* pool = calloc(1, sizeof(wStreamPool)); |
421 | | |
422 | 20 | if (!pool) |
423 | 0 | return nullptr; |
424 | | |
425 | 20 | pool->log = WLog_Create(XTAG, WLog_GetRoot()); |
426 | 20 | if (!pool->log) |
427 | 0 | goto fail; |
428 | | |
429 | 20 | pool->synchronized = synchronized; |
430 | 20 | pool->defaultSize = defaultSize; |
431 | | |
432 | 20 | if (!StreamPool_EnsureCapacity(pool, 32, FALSE)) |
433 | 0 | goto fail; |
434 | 20 | if (!StreamPool_EnsureCapacity(pool, 32, TRUE)) |
435 | 0 | goto fail; |
436 | | |
437 | 20 | if (!InitializeCriticalSectionAndSpinCount(&pool->lock, 4000)) |
438 | 0 | goto fail; |
439 | | |
440 | 20 | return pool; |
441 | 0 | fail: |
442 | 0 | WINPR_PRAGMA_DIAG_PUSH |
443 | 0 | WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC |
444 | 0 | StreamPool_Free(pool); |
445 | 0 | WINPR_PRAGMA_DIAG_POP |
446 | 0 | return nullptr; |
447 | 20 | } |
448 | | |
449 | | void StreamPool_Free(wStreamPool* pool) |
450 | 20 | { |
451 | 20 | if (!pool) |
452 | 0 | return; |
453 | | |
454 | 20 | StreamPool_Clear(pool); |
455 | | |
456 | 20 | DeleteCriticalSection(&pool->lock); |
457 | | |
458 | 20 | free(pool->aArray); |
459 | 20 | free(pool->uArray); |
460 | | |
461 | 20 | WLog_Discard(pool->log); |
462 | 20 | free(pool); |
463 | 20 | } |
464 | | |
465 | | char* StreamPool_GetStatistics(wStreamPool* pool, char* buffer, size_t size) |
466 | 0 | { |
467 | 0 | WINPR_ASSERT(pool); |
468 | |
|
469 | 0 | if (!buffer || (size < 1)) |
470 | 0 | return nullptr; |
471 | | |
472 | 0 | size_t used = 0; |
473 | 0 | int offset = _snprintf(buffer, size - 1, |
474 | 0 | "aSize =%" PRIuz ", uSize =%" PRIuz ", aCapacity=%" PRIuz |
475 | 0 | ", uCapacity=%" PRIuz, |
476 | 0 | pool->aSize, pool->uSize, pool->aCapacity, pool->uCapacity); |
477 | 0 | if ((offset > 0) && ((size_t)offset < size)) |
478 | 0 | used += (size_t)offset; |
479 | |
|
480 | 0 | #if defined(WITH_DEBUG_STREAMPOOL) |
481 | 0 | StreamPool_Lock(pool); |
482 | |
|
483 | 0 | offset = _snprintf(&buffer[used], size - 1 - used, "\n-- dump used array take locations --\n"); |
484 | 0 | if ((offset > 0) && ((size_t)offset < size - used)) |
485 | 0 | used += (size_t)offset; |
486 | 0 | for (size_t x = 0; x < pool->uSize; x++) |
487 | 0 | { |
488 | 0 | const struct s_StreamPoolEntry* cur = &pool->uArray[x]; |
489 | 0 | WINPR_ASSERT(cur->msg || (cur->lines == 0)); |
490 | |
|
491 | 0 | for (size_t y = 0; y < cur->lines; y++) |
492 | 0 | { |
493 | 0 | offset = _snprintf(&buffer[used], size - 1 - used, "[%" PRIuz " | %" PRIuz "]: %s\n", x, |
494 | 0 | y, cur->msg[y]); |
495 | 0 | if ((offset > 0) && ((size_t)offset < size - used)) |
496 | 0 | used += (size_t)offset; |
497 | 0 | } |
498 | 0 | } |
499 | |
|
500 | 0 | offset = _snprintf(&buffer[used], size - 1 - used, "\n-- statistics called from --\n"); |
501 | 0 | if ((offset > 0) && ((size_t)offset < size - used)) |
502 | 0 | used += (size_t)offset; |
503 | |
|
504 | 0 | struct s_StreamPoolEntry entry = WINPR_C_ARRAY_INIT; |
505 | 0 | void* stack = winpr_backtrace(20); |
506 | 0 | if (stack) |
507 | 0 | entry.msg = winpr_backtrace_symbols(stack, &entry.lines); |
508 | 0 | winpr_backtrace_free(stack); |
509 | |
|
510 | 0 | for (size_t x = 0; x < entry.lines; x++) |
511 | 0 | { |
512 | 0 | const char* msg = entry.msg[x]; |
513 | 0 | offset = _snprintf(&buffer[used], size - 1 - used, "[%" PRIuz "]: %s\n", x, msg); |
514 | 0 | if ((offset > 0) && ((size_t)offset < size - used)) |
515 | 0 | used += (size_t)offset; |
516 | 0 | } |
517 | 0 | free((void*)entry.msg); |
518 | 0 | StreamPool_Unlock(pool); |
519 | 0 | #endif |
520 | 0 | buffer[used] = '\0'; |
521 | 0 | return buffer; |
522 | 0 | } |
523 | | |
524 | | BOOL StreamPool_WaitForReturn(wStreamPool* pool, UINT32 timeoutMS) |
525 | 20 | { |
526 | | /* HACK: We disconnected the transport above, now wait without a read or write lock until all |
527 | | * streams in use have been returned to the pool. */ |
528 | 20 | while (timeoutMS > 0) |
529 | 20 | { |
530 | 20 | const size_t used = StreamPool_UsedCount(pool); |
531 | 20 | if (used == 0) |
532 | 20 | return TRUE; |
533 | 0 | WLog_Print(pool->log, WLOG_DEBUG, "%" PRIuz " streams still in use, sleeping...", used); |
534 | |
|
535 | 0 | char buffer[4096] = WINPR_C_ARRAY_INIT; |
536 | 0 | StreamPool_GetStatistics(pool, buffer, sizeof(buffer)); |
537 | 0 | WLog_Print(pool->log, WLOG_TRACE, "Pool statistics: %s", buffer); |
538 | |
|
539 | 0 | UINT32 diff = 10; |
540 | 0 | if (timeoutMS != INFINITE) |
541 | 0 | { |
542 | 0 | diff = timeoutMS > 10 ? 10 : timeoutMS; |
543 | 0 | timeoutMS -= diff; |
544 | 0 | } |
545 | 0 | Sleep(diff); |
546 | 0 | } |
547 | | |
548 | 0 | return FALSE; |
549 | 20 | } |