]> git.saurik.com Git - apple/security.git/blob - SecurityTests/regressions/test/testeventqueue.c
Security-57031.1.35.tar.gz
[apple/security.git] / SecurityTests / regressions / test / testeventqueue.c
1 #include <stdlib.h>
2 #include <string.h>
3
4 #include "testeventqueue.h"
5
6
7
8 struct CallbackDataQueueElement;
9 typedef struct CallbackDataQueueElement CallbackDataQueueElement;
10
11 struct CallbackDataQueueElement
12 {
13 CallbackData callbackData;
14 CallbackDataQueueElement *forward;
15 CallbackDataQueueElement *back;
16 };
17
18 // allocate static storage for the queue header, which is a circularly linked list
19 static CallbackDataQueueElement gCallbackQueue = {{0, 0, NULL, NULL, 0}, &gCallbackQueue, &gCallbackQueue};
20 static int gNumItemsInQueue = 0;
21
22
23 void TEQ_Enqueue (CallbackData *cd)
24 {
25 // allocate storage for the queue element and copy it.
26 CallbackDataQueueElement* element = (CallbackDataQueueElement*) malloc (sizeof (CallbackDataQueueElement));
27 memcpy (&element->callbackData, cd, sizeof (CallbackData));
28
29 // enqueue the new element -- always at the end
30 CallbackDataQueueElement* tail = gCallbackQueue.back;
31 element->forward = tail->forward;
32 element->forward->back = element;
33 element->back = tail;
34 tail->forward = element;
35
36 gNumItemsInQueue += 1;
37 }
38
39
40
41 bool TEQ_Dequeue (CallbackData *cd)
42 {
43 if (TEQ_ItemsInQueue () == 0)
44 {
45 return false;
46 }
47
48 // pull the element out of the queue and copy the data
49 CallbackDataQueueElement* element = gCallbackQueue.forward;
50 element->forward->back = element->back;
51 element->back->forward = element->forward;
52 memcpy (cd, &element->callbackData, sizeof (CallbackData));
53
54 free (element);
55
56 gNumItemsInQueue -= 1;
57 return true;
58 }
59
60
61
62 int TEQ_ItemsInQueue ()
63 {
64 return gNumItemsInQueue;
65 }
66
67
68
69 void TEQ_FlushQueue ()
70 {
71 CallbackDataQueueElement* element = gCallbackQueue.forward;
72 while (element != &gCallbackQueue)
73 {
74 CallbackDataQueueElement* forward = element->forward;
75 free (element);
76 element = forward;
77 }
78
79 gNumItemsInQueue = 0;
80 }
81
82
83
84 void TEQ_Release (CallbackData *cd)
85 {
86 if (cd->itemRef != NULL)
87 {
88 CFRelease (cd->itemRef);
89 }
90
91 if (cd->keychain != NULL)
92 {
93 CFRelease (cd->keychain);
94 }
95 }