ObjectPool.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include <winpr/crt.h>
  23. #include <winpr/collections.h>
  24. /**
  25. * C Object Pool similar to C# BufferManager Class:
  26. * http://msdn.microsoft.com/en-us/library/ms405814.aspx
  27. */
  28. /**
  29. * Methods
  30. */
  31. /**
  32. * Gets an object from the pool.
  33. */
  34. void* ObjectPool_Take(wObjectPool* pool)
  35. {
  36. void* obj = NULL;
  37. if (pool->synchronized)
  38. EnterCriticalSection(&pool->lock);
  39. if (pool->size > 0)
  40. obj = pool->array[--(pool->size)];
  41. if (!obj)
  42. {
  43. if (pool->object.fnObjectNew)
  44. obj = pool->object.fnObjectNew(NULL);
  45. }
  46. if (pool->object.fnObjectInit)
  47. pool->object.fnObjectInit(obj);
  48. if (pool->synchronized)
  49. LeaveCriticalSection(&pool->lock);
  50. return obj;
  51. }
  52. /**
  53. * Returns an object to the pool.
  54. */
  55. void ObjectPool_Return(wObjectPool* pool, void* obj)
  56. {
  57. if (pool->synchronized)
  58. EnterCriticalSection(&pool->lock);
  59. if ((pool->size + 1) >= pool->capacity)
  60. {
  61. size_t new_cap;
  62. void** new_arr;
  63. new_cap = pool->capacity * 2;
  64. new_arr = (void**)realloc(pool->array, sizeof(void*) * new_cap);
  65. if (!new_arr)
  66. goto out;
  67. pool->array = new_arr;
  68. pool->capacity = new_cap;
  69. }
  70. pool->array[(pool->size)++] = obj;
  71. if (pool->object.fnObjectUninit)
  72. pool->object.fnObjectUninit(obj);
  73. out:
  74. if (pool->synchronized)
  75. LeaveCriticalSection(&pool->lock);
  76. }
  77. /**
  78. * Releases the buffers currently cached in the pool.
  79. */
  80. void ObjectPool_Clear(wObjectPool* pool)
  81. {
  82. if (pool->synchronized)
  83. EnterCriticalSection(&pool->lock);
  84. while (pool->size > 0)
  85. {
  86. (pool->size)--;
  87. if (pool->object.fnObjectFree)
  88. pool->object.fnObjectFree(pool->array[pool->size]);
  89. }
  90. if (pool->synchronized)
  91. LeaveCriticalSection(&pool->lock);
  92. }
  93. /**
  94. * Construction, Destruction
  95. */
  96. wObjectPool* ObjectPool_New(BOOL synchronized)
  97. {
  98. wObjectPool* pool = NULL;
  99. pool = (wObjectPool*)calloc(1, sizeof(wObjectPool));
  100. if (pool)
  101. {
  102. pool->capacity = 32;
  103. pool->size = 0;
  104. pool->array = (void**)calloc(pool->capacity, sizeof(void*));
  105. if (!pool->array)
  106. {
  107. free(pool);
  108. return NULL;
  109. }
  110. pool->synchronized = synchronized;
  111. if (pool->synchronized)
  112. InitializeCriticalSectionAndSpinCount(&pool->lock, 4000);
  113. }
  114. return pool;
  115. }
  116. void ObjectPool_Free(wObjectPool* pool)
  117. {
  118. if (pool)
  119. {
  120. ObjectPool_Clear(pool);
  121. if (pool->synchronized)
  122. DeleteCriticalSection(&pool->lock);
  123. free(pool->array);
  124. free(pool);
  125. }
  126. }