4 #include "testeventqueue.h"
8 struct CallbackDataQueueElement
;
9 typedef struct CallbackDataQueueElement CallbackDataQueueElement
;
11 struct CallbackDataQueueElement
13 CallbackData callbackData
;
14 CallbackDataQueueElement
*forward
;
15 CallbackDataQueueElement
*back
;
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;
23 void TEQ_Enqueue (CallbackData
*cd
)
25 // allocate storage for the queue element and copy it.
26 CallbackDataQueueElement
* element
= (CallbackDataQueueElement
*) malloc (sizeof (CallbackDataQueueElement
));
27 memcpy (&element
->callbackData
, cd
, sizeof (CallbackData
));
29 // enqueue the new element -- always at the end
30 CallbackDataQueueElement
* tail
= gCallbackQueue
.back
;
31 element
->forward
= tail
->forward
;
32 element
->forward
->back
= element
;
34 tail
->forward
= element
;
36 gNumItemsInQueue
+= 1;
41 bool TEQ_Dequeue (CallbackData
*cd
)
43 if (TEQ_ItemsInQueue () == 0)
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
));
56 gNumItemsInQueue
-= 1;
62 int TEQ_ItemsInQueue ()
64 return gNumItemsInQueue
;
69 void TEQ_FlushQueue ()
71 CallbackDataQueueElement
* element
= gCallbackQueue
.forward
;
72 while (element
!= &gCallbackQueue
)
74 CallbackDataQueueElement
* forward
= element
->forward
;
84 void TEQ_Release (CallbackData
*cd
)
86 if (cd
->itemRef
!= NULL
)
88 CFRelease (cd
->itemRef
);
91 if (cd
->keychain
!= NULL
)
93 CFRelease (cd
->keychain
);