FreeRDP
Loading...
Searching...
No Matches
BufferPool.c
1
19
20#include <winpr/config.h>
21
22#include <winpr/crt.h>
23
24#include <winpr/collections.h>
25
26#ifndef MAX
27#define MAX(a, b) ((a) > (b)) ? (a) : (b)
28#endif
29
30typedef struct
31{
32 SSIZE_T size;
33 void* buffer;
34} wBufferPoolItem;
35
36struct s_wBufferPool
37{
38 SSIZE_T fixedSize;
39 DWORD alignment;
40 BOOL synchronized;
41 CRITICAL_SECTION lock;
42
43 SSIZE_T size;
44 SSIZE_T capacity;
45 void** array;
46
47 SSIZE_T aSize;
48 SSIZE_T aCapacity;
49 wBufferPoolItem* aArray;
50
51 SSIZE_T uSize;
52 SSIZE_T uCapacity;
53 wBufferPoolItem* uArray;
54};
55
56static BOOL BufferPool_Lock(wBufferPool* pool)
57{
58 if (!pool)
59 return FALSE;
60
61 if (pool->synchronized)
62 EnterCriticalSection(&pool->lock);
63 return TRUE;
64}
65
66static BOOL BufferPool_Unlock(wBufferPool* pool)
67{
68 if (!pool)
69 return FALSE;
70
71 if (pool->synchronized)
72 LeaveCriticalSection(&pool->lock);
73 return TRUE;
74}
75
80
84
85static BOOL BufferPool_ShiftAvailable(wBufferPool* pool, size_t index, int count)
86{
87 if (count > 0)
88 {
89 if (pool->aSize + count > pool->aCapacity)
90 {
91 wBufferPoolItem* newArray = nullptr;
92 SSIZE_T newCapacity = pool->aSize + count;
93 newCapacity += (newCapacity + 2) / 2;
94
95 newArray = (wBufferPoolItem*)winpr_aligned_recalloc(
96 pool->aArray, sizeof(wBufferPoolItem),
97 WINPR_ASSERTING_INT_CAST(size_t, newCapacity), pool->alignment);
98 if (!newArray)
99 return FALSE;
100 pool->aArray = newArray;
101 pool->aCapacity = newCapacity;
102 }
103
104 MoveMemory(
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;
108 }
109 else if (count < 0)
110 {
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);
113 // the removed entries must lie within the array
114 if (offset > size)
115 return FALSE;
116
117 MoveMemory(&pool->aArray[index], &pool->aArray[offset],
118 (size - offset) * sizeof(wBufferPoolItem));
119 pool->aSize += count;
120 }
121 return TRUE;
122}
123
124static BOOL BufferPool_ShiftUsed(wBufferPool* pool, SSIZE_T index, SSIZE_T count)
125{
126 if (count > 0)
127 {
128 const SSIZE_T required = pool->uSize + count;
129 // check for overflow
130 if ((required < count) || (required < pool->uSize))
131 return FALSE;
132
133 if (required > pool->uCapacity)
134 {
135 SSIZE_T newUCapacity = pool->uCapacity;
136 do
137 {
138 if (newUCapacity > SSIZE_MAX - 128ll)
139 return FALSE;
140 newUCapacity += 128ll;
141 } while (newUCapacity <= required);
142 wBufferPoolItem* newUArray = nullptr;
143 newUArray = (wBufferPoolItem*)winpr_aligned_realloc(
144 pool->uArray,
145 sizeof(wBufferPoolItem) * WINPR_ASSERTING_INT_CAST(size_t, newUCapacity),
146 pool->alignment);
147
148 if (!newUArray)
149 return FALSE;
150 pool->uCapacity = newUCapacity;
151 pool->uArray = newUArray;
152 }
153
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;
157 }
158 else if (count < 0)
159 {
160 const SSIZE_T offset = index - count;
161 // the removed entries must lie within the array
162 if (offset > pool->uSize)
163 return FALSE;
164
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;
169 }
170 return TRUE;
171}
172
176
177SSIZE_T BufferPool_GetPoolSize(wBufferPool* pool)
178{
179 SSIZE_T size = 0;
180
181 BufferPool_Lock(pool);
182
183 if (pool->fixedSize)
184 {
185 /* fixed size buffers */
186 size = pool->size;
187 }
188 else
189 {
190 /* variable size buffers */
191 size = pool->uSize;
192 }
193
194 BufferPool_Unlock(pool);
195
196 return size;
197}
198
202
203SSIZE_T BufferPool_GetBufferSize(wBufferPool* pool, const void* buffer)
204{
205 SSIZE_T size = 0;
206 BOOL found = FALSE;
207
208 BufferPool_Lock(pool);
209
210 if (pool->fixedSize)
211 {
212 /* fixed size buffers */
213 size = pool->fixedSize;
214 found = TRUE;
215 }
216 else
217 {
218 /* variable size buffers */
219
220 for (SSIZE_T index = 0; index < pool->uSize; index++)
221 {
222 if (pool->uArray[index].buffer == buffer)
223 {
224 size = pool->uArray[index].size;
225 found = TRUE;
226 break;
227 }
228 }
229 }
230
231 BufferPool_Unlock(pool);
232
233 return (found) ? size : -1;
234}
235
239
240void* BufferPool_Take(wBufferPool* pool, SSIZE_T size)
241{
242 SSIZE_T maxSize = 0;
243 SSIZE_T maxIndex = 0;
244 SSIZE_T foundIndex = -1;
245 BOOL found = FALSE;
246 void* buffer = nullptr;
247
248 BufferPool_Lock(pool);
249
250 if (pool->fixedSize)
251 {
252 /* fixed size buffers */
253
254 if (pool->size > 0)
255 buffer = pool->array[--(pool->size)];
256
257 if (!buffer)
258 {
259 buffer = winpr_aligned_calloc(WINPR_ASSERTING_INT_CAST(size_t, pool->fixedSize), 1,
260 pool->alignment);
261 }
262
263 if (!buffer)
264 goto out_error;
265 }
266 else
267 {
268 /* variable size buffers */
269 SSIZE_T bufferSize = 0;
270
271 maxSize = 0;
272 maxIndex = 0;
273
274 if (size < 1)
275 size = pool->fixedSize;
276
277 for (SSIZE_T index = 0; index < pool->aSize; index++)
278 {
279 if (pool->aArray[index].size > maxSize)
280 {
281 maxIndex = index;
282 maxSize = pool->aArray[index].size;
283 }
284
285 if (pool->aArray[index].size >= size)
286 {
287 foundIndex = index;
288 found = TRUE;
289 break;
290 }
291 }
292
293 if (!found && maxSize)
294 {
295 foundIndex = maxIndex;
296 found = TRUE;
297 }
298
299 if (!found)
300 {
301 bufferSize = size;
302 if (!size)
303 buffer = nullptr;
304 else
305 {
306 buffer = winpr_aligned_calloc(WINPR_ASSERTING_INT_CAST(size_t, size), 1,
307 pool->alignment);
308
309 if (!buffer)
310 goto out_error;
311 }
312 }
313 else
314 {
315 buffer = pool->aArray[foundIndex].buffer;
316 bufferSize = pool->aArray[foundIndex].size;
317
318 if (maxSize < size)
319 {
320 void* newBuffer = nullptr;
321 newBuffer = winpr_aligned_realloc(buffer, WINPR_ASSERTING_INT_CAST(size_t, size),
322 pool->alignment);
323
324 if (!newBuffer)
325 goto out_error_no_free;
326
327 buffer = newBuffer;
328 bufferSize = size;
329 }
330
331 if (!BufferPool_ShiftAvailable(pool, WINPR_ASSERTING_INT_CAST(size_t, foundIndex), -1))
332 goto out_error;
333 }
334
335 if (!buffer)
336 goto out_error;
337
338 if (pool->uSize + 1 > pool->uCapacity)
339 {
340 size_t newUCapacity = WINPR_ASSERTING_INT_CAST(size_t, pool->uCapacity);
341 newUCapacity += (newUCapacity + 2) / 2;
342 if (newUCapacity > SSIZE_MAX)
343 goto out_error;
344 wBufferPoolItem* newUArray =
345 (wBufferPoolItem*)realloc(pool->uArray, sizeof(wBufferPoolItem) * newUCapacity);
346 if (!newUArray)
347 goto out_error;
348
349 pool->uCapacity = (SSIZE_T)newUCapacity;
350 pool->uArray = newUArray;
351 }
352
353 pool->uArray[pool->uSize].buffer = buffer;
354 pool->uArray[pool->uSize].size = bufferSize;
355 (pool->uSize)++;
356 }
357
358 BufferPool_Unlock(pool);
359
360 return buffer;
361
362out_error:
363 winpr_aligned_free(buffer);
364out_error_no_free:
365 BufferPool_Unlock(pool);
366 return nullptr;
367}
368
372
373BOOL BufferPool_Return(wBufferPool* pool, void* buffer)
374{
375 BOOL rc = FALSE;
376 SSIZE_T size = 0;
377 BOOL found = FALSE;
378
379 BufferPool_Lock(pool);
380
381 if (pool->fixedSize)
382 {
383 /* fixed size buffers */
384
385 if ((pool->size + 1) >= pool->capacity)
386 {
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));
390 if (!newArray)
391 goto out_error;
392
393 pool->capacity = newCapacity;
394 pool->array = newArray;
395 }
396
397 pool->array[(pool->size)++] = buffer;
398 }
399 else
400 {
401 /* variable size buffers */
402
403 SSIZE_T index = 0;
404 for (; index < pool->uSize; index++)
405 {
406 if (pool->uArray[index].buffer == buffer)
407 {
408 found = TRUE;
409 break;
410 }
411 }
412
413 if (found)
414 {
415 size = pool->uArray[index].size;
416 if (!BufferPool_ShiftUsed(pool, index, -1))
417 goto out_error;
418 }
419
420 if (size)
421 {
422 if ((pool->aSize + 1) >= pool->aCapacity)
423 {
424 SSIZE_T newCapacity = MAX(2, pool->aSize + (pool->aSize + 2) / 2 + 1);
425 wBufferPoolItem* newArray = (wBufferPoolItem*)realloc(
426 pool->aArray,
427 sizeof(wBufferPoolItem) * WINPR_ASSERTING_INT_CAST(size_t, newCapacity));
428 if (!newArray)
429 goto out_error;
430
431 pool->aCapacity = newCapacity;
432 pool->aArray = newArray;
433 }
434
435 pool->aArray[pool->aSize].buffer = buffer;
436 pool->aArray[pool->aSize].size = size;
437 (pool->aSize)++;
438 }
439 }
440
441 rc = TRUE;
442out_error:
443 BufferPool_Unlock(pool);
444 return rc;
445}
446
450
451void BufferPool_Clear(wBufferPool* pool)
452{
453 BufferPool_Lock(pool);
454
455 if (pool->fixedSize)
456 {
457 /* fixed size buffers */
458
459 while (pool->size > 0)
460 {
461 (pool->size)--;
462
463 winpr_aligned_free(pool->array[pool->size]);
464 }
465 }
466 else
467 {
468 /* variable size buffers */
469
470 while (pool->aSize > 0)
471 {
472 (pool->aSize)--;
473
474 winpr_aligned_free(pool->aArray[pool->aSize].buffer);
475 }
476
477 while (pool->uSize > 0)
478 {
479 (pool->uSize)--;
480
481 winpr_aligned_free(pool->uArray[pool->uSize].buffer);
482 }
483 }
484
485 BufferPool_Unlock(pool);
486}
487
491
492wBufferPool* BufferPool_New(BOOL synchronized, SSIZE_T fixedSize, DWORD alignment)
493{
494 wBufferPool* pool = nullptr;
495
496 pool = (wBufferPool*)calloc(1, sizeof(wBufferPool));
497
498 if (pool)
499 {
500 pool->fixedSize = fixedSize;
501
502 if (pool->fixedSize < 0)
503 pool->fixedSize = 0;
504
505 pool->alignment = alignment;
506 pool->synchronized = synchronized;
507
508 if (pool->synchronized)
509 {
510 if (!InitializeCriticalSectionAndSpinCount(&pool->lock, 4000))
511 goto out_error;
512 }
513
514 if (pool->fixedSize)
515 {
516 /* fixed size buffers */
517
518 pool->size = 0;
519 pool->capacity = 32;
520 pool->array =
521 (void**)calloc(WINPR_ASSERTING_INT_CAST(size_t, pool->capacity), sizeof(void*));
522 if (!pool->array)
523 goto out_error;
524 }
525 else
526 {
527 /* variable size buffers */
528
529 pool->aSize = 0;
530 pool->aCapacity = 32;
531 pool->aArray = (wBufferPoolItem*)calloc(
532 WINPR_ASSERTING_INT_CAST(size_t, pool->aCapacity), sizeof(wBufferPoolItem));
533 if (!pool->aArray)
534 goto out_error;
535
536 pool->uSize = 0;
537 pool->uCapacity = 32;
538 pool->uArray = (wBufferPoolItem*)calloc(
539 WINPR_ASSERTING_INT_CAST(size_t, pool->uCapacity), sizeof(wBufferPoolItem));
540 if (!pool->uArray)
541 goto out_error;
542 }
543 }
544
545 return pool;
546
547out_error:
548 WINPR_PRAGMA_DIAG_PUSH
549 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
550 BufferPool_Free(pool);
551 WINPR_PRAGMA_DIAG_POP
552 return nullptr;
553}
554
555void BufferPool_Free(wBufferPool* pool)
556{
557 if (pool)
558 {
559 BufferPool_Clear(pool);
560
561 if (pool->synchronized)
562 DeleteCriticalSection(&pool->lock);
563
564 if (pool->fixedSize)
565 {
566 /* fixed size buffers */
567
568 free((void*)pool->array);
569 }
570 else
571 {
572 /* variable size buffers */
573
574 free(pool->aArray);
575 free(pool->uArray);
576 }
577
578 free(pool);
579 }
580}