FreeRDP
Queue.c
1 
20 #include <winpr/config.h>
21 
22 #include <winpr/crt.h>
23 #include <winpr/assert.h>
24 
25 #include <winpr/collections.h>
26 
27 struct s_wQueue
28 {
29  size_t capacity;
30  size_t growthFactor;
31  BOOL synchronized;
32 
33  BYTE padding[4];
34 
35  size_t head;
36  size_t tail;
37  size_t size;
38  void** array;
39  CRITICAL_SECTION lock;
40  HANDLE event;
41 
42  wObject object;
43  BOOL haveLock;
44 
45  BYTE padding2[4];
46 };
47 
61 size_t Queue_Count(wQueue* queue)
62 {
63  size_t ret = 0;
64 
65  Queue_Lock(queue);
66 
67  ret = queue->size;
68 
69  Queue_Unlock(queue);
70 
71  return ret;
72 }
73 
78 void Queue_Lock(wQueue* queue)
79 {
80  WINPR_ASSERT(queue);
81  if (queue->synchronized)
82  EnterCriticalSection(&queue->lock);
83 }
84 
89 void Queue_Unlock(wQueue* queue)
90 {
91  WINPR_ASSERT(queue);
92  if (queue->synchronized)
93  LeaveCriticalSection(&queue->lock);
94 }
95 
100 HANDLE Queue_Event(wQueue* queue)
101 {
102  WINPR_ASSERT(queue);
103  return queue->event;
104 }
105 
106 wObject* Queue_Object(wQueue* queue)
107 {
108  WINPR_ASSERT(queue);
109  return &queue->object;
110 }
111 
120 void Queue_Clear(wQueue* queue)
121 {
122  Queue_Lock(queue);
123 
124  for (size_t index = queue->head; index != queue->tail; index = (index + 1) % queue->capacity)
125  {
126  if (queue->object.fnObjectFree)
127  queue->object.fnObjectFree(queue->array[index]);
128 
129  queue->array[index] = NULL;
130  }
131 
132  queue->size = 0;
133  queue->head = queue->tail = 0;
134  (void)ResetEvent(queue->event);
135  Queue_Unlock(queue);
136 }
137 
142 BOOL Queue_Contains(wQueue* queue, const void* obj)
143 {
144  BOOL found = FALSE;
145 
146  Queue_Lock(queue);
147 
148  for (size_t index = 0; index < queue->tail; index++)
149  {
150  if (queue->object.fnObjectEquals(queue->array[index], obj))
151  {
152  found = TRUE;
153  break;
154  }
155  }
156 
157  Queue_Unlock(queue);
158 
159  return found;
160 }
161 
162 static BOOL Queue_EnsureCapacity(wQueue* queue, size_t count)
163 {
164  WINPR_ASSERT(queue);
165 
166  if (queue->size + count >= queue->capacity)
167  {
168  const size_t old_capacity = queue->capacity;
169  size_t new_capacity = queue->capacity * queue->growthFactor;
170  void** newArray = NULL;
171  if (new_capacity < queue->size + count)
172  new_capacity = queue->size + count;
173  newArray = (void**)realloc(queue->array, sizeof(void*) * new_capacity);
174 
175  if (!newArray)
176  return FALSE;
177 
178  queue->capacity = new_capacity;
179  queue->array = newArray;
180  ZeroMemory(&(queue->array[old_capacity]), (new_capacity - old_capacity) * sizeof(void*));
181 
182  /* rearrange wrapped entries */
183  if (queue->tail <= queue->head)
184  {
185  CopyMemory(&(queue->array[old_capacity]), queue->array, queue->tail * sizeof(void*));
186  queue->tail += old_capacity;
187  }
188  }
189  return TRUE;
190 }
191 
196 BOOL Queue_Enqueue(wQueue* queue, const void* obj)
197 {
198  BOOL ret = TRUE;
199 
200  Queue_Lock(queue);
201 
202  if (!Queue_EnsureCapacity(queue, 1))
203  goto out;
204 
205  if (queue->object.fnObjectNew)
206  queue->array[queue->tail] = queue->object.fnObjectNew(obj);
207  else
208  {
209  union
210  {
211  const void* cv;
212  void* v;
213  } cnv;
214  cnv.cv = obj;
215  queue->array[queue->tail] = cnv.v;
216  }
217  queue->tail = (queue->tail + 1) % queue->capacity;
218 
219  const BOOL signalSet = queue->size == 0;
220  queue->size++;
221 
222  if (signalSet)
223  (void)SetEvent(queue->event);
224 out:
225 
226  Queue_Unlock(queue);
227 
228  return ret;
229 }
230 
235 void* Queue_Dequeue(wQueue* queue)
236 {
237  void* obj = NULL;
238 
239  Queue_Lock(queue);
240 
241  if (queue->size > 0)
242  {
243  obj = queue->array[queue->head];
244  queue->array[queue->head] = NULL;
245  queue->head = (queue->head + 1) % queue->capacity;
246  queue->size--;
247  }
248 
249  if (queue->size < 1)
250  (void)ResetEvent(queue->event);
251 
252  Queue_Unlock(queue);
253 
254  return obj;
255 }
256 
261 void* Queue_Peek(wQueue* queue)
262 {
263  void* obj = NULL;
264 
265  Queue_Lock(queue);
266 
267  if (queue->size > 0)
268  obj = queue->array[queue->head];
269 
270  Queue_Unlock(queue);
271 
272  return obj;
273 }
274 
275 void Queue_Discard(wQueue* queue)
276 {
277  void* obj = NULL;
278 
279  Queue_Lock(queue);
280  obj = Queue_Dequeue(queue);
281 
282  if (queue->object.fnObjectFree)
283  queue->object.fnObjectFree(obj);
284  Queue_Unlock(queue);
285 }
286 
287 static BOOL default_queue_equals(const void* obj1, const void* obj2)
288 {
289  return (obj1 == obj2);
290 }
291 
296 wQueue* Queue_New(BOOL synchronized, SSIZE_T capacity, SSIZE_T growthFactor)
297 {
298  wObject* obj = NULL;
299  wQueue* queue = NULL;
300  queue = (wQueue*)calloc(1, sizeof(wQueue));
301 
302  if (!queue)
303  return NULL;
304 
305  queue->synchronized = synchronized;
306 
307  queue->growthFactor = 2;
308  if (growthFactor > 0)
309  queue->growthFactor = (size_t)growthFactor;
310 
311  if (capacity <= 0)
312  capacity = 32;
313  if (!InitializeCriticalSectionAndSpinCount(&queue->lock, 4000))
314  goto fail;
315  queue->haveLock = TRUE;
316  if (!Queue_EnsureCapacity(queue, (size_t)capacity))
317  goto fail;
318 
319  queue->event = CreateEvent(NULL, TRUE, FALSE, NULL);
320 
321  if (!queue->event)
322  goto fail;
323 
324  obj = Queue_Object(queue);
325  obj->fnObjectEquals = default_queue_equals;
326 
327  return queue;
328 fail:
329  WINPR_PRAGMA_DIAG_PUSH
330  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
331  Queue_Free(queue);
332  WINPR_PRAGMA_DIAG_POP
333  return NULL;
334 }
335 
336 void Queue_Free(wQueue* queue)
337 {
338  if (!queue)
339  return;
340 
341  if (queue->haveLock)
342  {
343  Queue_Clear(queue);
344  DeleteCriticalSection(&queue->lock);
345  }
346  (void)CloseHandle(queue->event);
347  free(queue->array);
348  free(queue);
349 }
This struct contains function pointer to initialize/free objects.
Definition: collections.h:57