FreeRDP
collections.h
1 
20 #ifndef WINPR_COLLECTIONS_H
21 #define WINPR_COLLECTIONS_H
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdarg.h>
27 
28 #include <winpr/winpr.h>
29 #include <winpr/wtypes.h>
30 #include <winpr/assert.h>
31 
32 #include <winpr/crt.h>
33 #include <winpr/synch.h>
34 #include <winpr/stream.h>
35 
36 #ifdef __cplusplus
37 extern "C"
38 {
39 #endif
40 
41  typedef void* (*OBJECT_NEW_FN)(const void* val);
42  typedef void (*OBJECT_INIT_FN)(void* obj);
43  typedef void (*OBJECT_UNINIT_FN)(void* obj);
44  typedef void (*OBJECT_FREE_FN)(void* obj);
45  typedef BOOL (*OBJECT_EQUALS_FN)(const void* objA, const void* objB);
46 
56  typedef struct
57  {
58  OBJECT_NEW_FN fnObjectNew;
59  OBJECT_INIT_FN fnObjectInit;
60  OBJECT_UNINIT_FN fnObjectUninit;
61  OBJECT_FREE_FN fnObjectFree;
62  OBJECT_EQUALS_FN fnObjectEquals;
63  } wObject;
64 
65  /* utility function with compatible arguments for string data */
66 
72  WINPR_API void* winpr_ObjectStringClone(const void* pvstr);
73 
79  WINPR_API void* winpr_ObjectWStringClone(const void* pvstr);
80 
85  WINPR_API void winpr_ObjectStringFree(void* pvstr);
86 
87  /* System.Collections.Queue */
88 
89  typedef struct s_wQueue wQueue;
90 
97  WINPR_API size_t Queue_Count(wQueue* queue);
98 
103  WINPR_API void Queue_Lock(wQueue* queue);
104 
109  WINPR_API void Queue_Unlock(wQueue* queue);
110 
116  WINPR_API HANDLE Queue_Event(wQueue* queue);
117 
125  WINPR_API wObject* Queue_Object(wQueue* queue);
126 
131  WINPR_API void Queue_Clear(wQueue* queue);
132 
140  WINPR_API BOOL Queue_Contains(wQueue* queue, const void* obj);
141 
151  WINPR_API BOOL Queue_Enqueue(wQueue* queue, const void* obj);
152 
160  WINPR_API void* Queue_Dequeue(wQueue* queue);
161 
169  WINPR_API void* Queue_Peek(wQueue* queue);
170 
178  WINPR_API void Queue_Discard(wQueue* queue);
179 
184  WINPR_API void Queue_Free(wQueue* queue);
185 
190  WINPR_ATTR_MALLOC(Queue_Free, 1)
191  WINPR_API wQueue* Queue_New(BOOL synchronized, SSIZE_T capacity, SSIZE_T growthFactor);
192 
193  /* System.Collections.Stack */
194 
195  typedef struct s_wStack wStack;
196 
197  WINPR_API size_t Stack_Count(wStack* stack);
198  WINPR_API BOOL Stack_IsSynchronized(wStack* stack);
199 
200  WINPR_API wObject* Stack_Object(wStack* stack);
201 
202  WINPR_API void Stack_Clear(wStack* stack);
203  WINPR_API BOOL Stack_Contains(wStack* stack, const void* obj);
204 
205  WINPR_API void Stack_Push(wStack* stack, void* obj);
206  WINPR_API void* Stack_Pop(wStack* stack);
207 
208  WINPR_API void* Stack_Peek(wStack* stack);
209 
210  WINPR_API void Stack_Free(wStack* stack);
211 
212  WINPR_ATTR_MALLOC(Stack_Free, 1)
213  WINPR_API wStack* Stack_New(BOOL synchronized);
214 
215  /* System.Collections.ArrayList */
216 
217  typedef struct s_wArrayList wArrayList;
218 
219  WINPR_API size_t ArrayList_Capacity(wArrayList* arrayList);
220  WINPR_API size_t ArrayList_Count(wArrayList* arrayList);
221  WINPR_API size_t ArrayList_Items(wArrayList* arrayList, ULONG_PTR** ppItems);
222  WINPR_API BOOL ArrayList_IsFixedSized(wArrayList* arrayList);
223  WINPR_API BOOL ArrayList_IsReadOnly(wArrayList* arrayList);
224  WINPR_API BOOL ArrayList_IsSynchronized(wArrayList* arrayList);
225 
226  WINPR_API void ArrayList_Lock(wArrayList* arrayList);
227  WINPR_API void ArrayList_Unlock(wArrayList* arrayList);
228 
229  WINPR_API void* ArrayList_GetItem(wArrayList* arrayList, size_t index);
230  WINPR_API BOOL ArrayList_SetItem(wArrayList* arrayList, size_t index, const void* obj);
231 
232  WINPR_API wObject* ArrayList_Object(wArrayList* arrayList);
233 
234  typedef BOOL (*ArrayList_ForEachFkt)(void* data, size_t index, va_list ap);
235 
236  WINPR_API BOOL ArrayList_ForEach(wArrayList* arrayList, ArrayList_ForEachFkt fkt, ...);
237  WINPR_API BOOL ArrayList_ForEachAP(wArrayList* arrayList, ArrayList_ForEachFkt fkt, va_list ap);
238 
239  WINPR_API void ArrayList_Clear(wArrayList* arrayList);
240  WINPR_API BOOL ArrayList_Contains(wArrayList* arrayList, const void* obj);
241 
242 #if defined(WITH_WINPR_DEPRECATED)
243  WINPR_API WINPR_DEPRECATED(int ArrayList_Add(wArrayList* arrayList, const void* obj));
244 #endif
245 
246  WINPR_API BOOL ArrayList_Append(wArrayList* arrayList, const void* obj);
247  WINPR_API BOOL ArrayList_Insert(wArrayList* arrayList, size_t index, const void* obj);
248 
249  WINPR_API BOOL ArrayList_Remove(wArrayList* arrayList, const void* obj);
250  WINPR_API BOOL ArrayList_RemoveAt(wArrayList* arrayList, size_t index);
251 
252  WINPR_API SSIZE_T ArrayList_IndexOf(wArrayList* arrayList, const void* obj, SSIZE_T startIndex,
253  SSIZE_T count);
254  WINPR_API SSIZE_T ArrayList_LastIndexOf(wArrayList* arrayList, const void* obj,
255  SSIZE_T startIndex, SSIZE_T count);
256 
257  WINPR_API void ArrayList_Free(wArrayList* arrayList);
258 
259  WINPR_ATTR_MALLOC(ArrayList_Free, 1)
260  WINPR_API wArrayList* ArrayList_New(BOOL synchronized);
261 
262  /* System.Collections.DictionaryBase */
263 
264  /* System.Collections.Specialized.ListDictionary */
265  typedef struct s_wListDictionary wListDictionary;
266 
273  WINPR_API wObject* ListDictionary_KeyObject(wListDictionary* listDictionary);
274 
281  WINPR_API wObject* ListDictionary_ValueObject(wListDictionary* listDictionary);
282 
289  WINPR_API size_t ListDictionary_Count(wListDictionary* listDictionary);
290 
295  WINPR_API void ListDictionary_Lock(wListDictionary* listDictionary);
300  WINPR_API void ListDictionary_Unlock(wListDictionary* listDictionary);
301 
311  WINPR_API BOOL ListDictionary_Add(wListDictionary* listDictionary, const void* key,
312  const void* value);
313 
321  WINPR_API void* ListDictionary_Take(wListDictionary* listDictionary, const void* key);
322 
328  WINPR_API void ListDictionary_Remove(wListDictionary* listDictionary, const void* key);
329 
337  WINPR_API void* ListDictionary_Take_Head(wListDictionary* listDictionary);
338 
343  WINPR_API void ListDictionary_Remove_Head(wListDictionary* listDictionary);
344 
349  WINPR_API void ListDictionary_Clear(wListDictionary* listDictionary);
350 
358  WINPR_API BOOL ListDictionary_Contains(wListDictionary* listDictionary, const void* key);
359 
368  WINPR_API size_t ListDictionary_GetKeys(wListDictionary* listDictionary, ULONG_PTR** ppKeys);
369 
378  WINPR_API void* ListDictionary_GetItemValue(wListDictionary* listDictionary, const void* key);
379 
389  WINPR_API BOOL ListDictionary_SetItemValue(wListDictionary* listDictionary, const void* key,
390  const void* value);
391 
396  WINPR_API void ListDictionary_Free(wListDictionary* listDictionary);
397 
404  WINPR_ATTR_MALLOC(ListDictionary_Free, 1)
405  WINPR_API wListDictionary* ListDictionary_New(BOOL synchronized);
406 
407  /* System.Collections.Generic.LinkedList<T> */
408 
409  typedef struct s_wLinkedList wLinkedList;
410 
417  WINPR_API size_t LinkedList_Count(wLinkedList* list);
418 
425  WINPR_API void* LinkedList_First(wLinkedList* list);
426 
433  WINPR_API void* LinkedList_Last(wLinkedList* list);
434 
442  WINPR_API BOOL LinkedList_Contains(wLinkedList* list, const void* value);
443 
450  WINPR_API void LinkedList_Clear(wLinkedList* list);
451 
460  WINPR_API BOOL LinkedList_AddFirst(wLinkedList* list, const void* value);
461 
470  WINPR_API BOOL LinkedList_AddLast(wLinkedList* list, const void* value);
471 
480  WINPR_API BOOL LinkedList_Remove(wLinkedList* list, const void* value);
481 
488  WINPR_API void LinkedList_RemoveFirst(wLinkedList* list);
489 
496  WINPR_API void LinkedList_RemoveLast(wLinkedList* list);
497 
503  WINPR_API void LinkedList_Enumerator_Reset(wLinkedList* list);
504 
511  WINPR_API void* LinkedList_Enumerator_Current(wLinkedList* list);
512 
519  WINPR_API BOOL LinkedList_Enumerator_MoveNext(wLinkedList* list);
520 
525  WINPR_API void LinkedList_Free(wLinkedList* list);
526 
531  WINPR_ATTR_MALLOC(LinkedList_Free, 1)
532  WINPR_API wLinkedList* LinkedList_New(void);
533 
540  WINPR_API wObject* LinkedList_Object(wLinkedList* list);
541 
542  /* System.Collections.Generic.KeyValuePair<TKey,TValue> */
543 
544  /* Countdown Event */
545 
546  typedef struct CountdownEvent wCountdownEvent;
547 
554  WINPR_API size_t CountdownEvent_CurrentCount(wCountdownEvent* countdown);
555 
562  WINPR_API size_t CountdownEvent_InitialCount(wCountdownEvent* countdown);
563 
570  WINPR_API BOOL CountdownEvent_IsSet(wCountdownEvent* countdown);
571 
579  WINPR_API HANDLE CountdownEvent_WaitHandle(wCountdownEvent* countdown);
580 
587  WINPR_API void CountdownEvent_AddCount(wCountdownEvent* countdown, size_t signalCount);
588 
596  WINPR_API BOOL CountdownEvent_Signal(wCountdownEvent* countdown, size_t signalCount);
597 
603  WINPR_API void CountdownEvent_Reset(wCountdownEvent* countdown, size_t count);
604 
609  WINPR_API void CountdownEvent_Free(wCountdownEvent* countdown);
610 
617  WINPR_ATTR_MALLOC(CountdownEvent_Free, 1)
618  WINPR_API wCountdownEvent* CountdownEvent_New(size_t initialCount);
619 
620  /* Hash Table */
621 
622  typedef UINT32 (*HASH_TABLE_HASH_FN)(const void* key);
623 
624  typedef struct s_wHashTable wHashTable;
625 
626  typedef BOOL (*HASH_TABLE_FOREACH_FN)(const void* key, void* value, void* arg);
627 
628  WINPR_API size_t HashTable_Count(wHashTable* table);
629 
630 #if defined(WITH_WINPR_DEPRECATED)
631  WINPR_API WINPR_DEPRECATED(int HashTable_Add(wHashTable* table, const void* key,
632  const void* value));
633 #endif
634 
635  WINPR_API BOOL HashTable_Insert(wHashTable* table, const void* key, const void* value);
636  WINPR_API BOOL HashTable_Remove(wHashTable* table, const void* key);
637  WINPR_API void HashTable_Clear(wHashTable* table);
638  WINPR_API BOOL HashTable_Contains(wHashTable* table, const void* key);
639  WINPR_API BOOL HashTable_ContainsKey(wHashTable* table, const void* key);
640  WINPR_API BOOL HashTable_ContainsValue(wHashTable* table, const void* value);
641  WINPR_API void* HashTable_GetItemValue(wHashTable* table, const void* key);
642  WINPR_API BOOL HashTable_SetItemValue(wHashTable* table, const void* key, const void* value);
643  WINPR_API size_t HashTable_GetKeys(wHashTable* table, ULONG_PTR** ppKeys);
644  WINPR_API BOOL HashTable_Foreach(wHashTable* table, HASH_TABLE_FOREACH_FN fn, VOID* arg);
645 
646  WINPR_API UINT32 HashTable_PointerHash(const void* pointer);
647  WINPR_API BOOL HashTable_PointerCompare(const void* pointer1, const void* pointer2);
648 
649  WINPR_API UINT32 HashTable_StringHash(const void* key);
650  WINPR_API BOOL HashTable_StringCompare(const void* string1, const void* string2);
651  WINPR_API void* HashTable_StringClone(const void* str);
652  WINPR_API void HashTable_StringFree(void* str);
653 
654  WINPR_API void HashTable_Free(wHashTable* table);
655 
656  WINPR_ATTR_MALLOC(HashTable_Free, 1)
657  WINPR_API wHashTable* HashTable_New(BOOL synchronized);
658 
659  WINPR_API void HashTable_Lock(wHashTable* table);
660  WINPR_API void HashTable_Unlock(wHashTable* table);
661 
662  WINPR_API wObject* HashTable_KeyObject(wHashTable* table);
663  WINPR_API wObject* HashTable_ValueObject(wHashTable* table);
664 
665  WINPR_API BOOL HashTable_SetHashFunction(wHashTable* table, HASH_TABLE_HASH_FN fn);
666 
667  /* Utility function to setup hash table for strings */
668  WINPR_API BOOL HashTable_SetupForStringData(wHashTable* table, BOOL stringValues);
669 
670  /* BufferPool */
671 
672  typedef struct s_wBufferPool wBufferPool;
673 
674  WINPR_API SSIZE_T BufferPool_GetPoolSize(wBufferPool* pool);
675  WINPR_API SSIZE_T BufferPool_GetBufferSize(wBufferPool* pool, const void* buffer);
676 
677  WINPR_API void* BufferPool_Take(wBufferPool* pool, SSIZE_T bufferSize);
678  WINPR_API BOOL BufferPool_Return(wBufferPool* pool, void* buffer);
679  WINPR_API void BufferPool_Clear(wBufferPool* pool);
680 
681  WINPR_API void BufferPool_Free(wBufferPool* pool);
682 
683  WINPR_ATTR_MALLOC(BufferPool_Free, 1)
684  WINPR_API wBufferPool* BufferPool_New(BOOL synchronized, SSIZE_T fixedSize, DWORD alignment);
685 
686  /* ObjectPool */
687 
688  typedef struct s_wObjectPool wObjectPool;
689 
690  WINPR_API void* ObjectPool_Take(wObjectPool* pool);
691  WINPR_API void ObjectPool_Return(wObjectPool* pool, void* obj);
692  WINPR_API void ObjectPool_Clear(wObjectPool* pool);
693 
694  WINPR_API wObject* ObjectPool_Object(wObjectPool* pool);
695 
696  WINPR_API void ObjectPool_Free(wObjectPool* pool);
697 
698  WINPR_ATTR_MALLOC(ObjectPool_Free, 1)
699  WINPR_API wObjectPool* ObjectPool_New(BOOL synchronized);
700 
701  /* Message Queue */
702 
703  typedef struct s_wMessage wMessage;
704 
705  typedef void (*MESSAGE_FREE_FN)(wMessage* message);
706 
707  struct s_wMessage
708  {
709  UINT32 id;
710  void* context;
711  void* wParam;
712  void* lParam;
713  UINT64 time;
714  MESSAGE_FREE_FN Free;
715  };
716 
717  typedef struct s_wMessageQueue wMessageQueue;
718 
719 #define WMQ_QUIT 0xFFFFFFFF
720 
721  WINPR_API wObject* MessageQueue_Object(wMessageQueue* queue);
722  WINPR_API HANDLE MessageQueue_Event(wMessageQueue* queue);
723  WINPR_API BOOL MessageQueue_Wait(wMessageQueue* queue);
724  WINPR_API size_t MessageQueue_Size(wMessageQueue* queue);
725 
726  WINPR_API BOOL MessageQueue_Dispatch(wMessageQueue* queue, const wMessage* message);
727  WINPR_API BOOL MessageQueue_Post(wMessageQueue* queue, void* context, UINT32 type, void* wParam,
728  void* lParam);
729  WINPR_API BOOL MessageQueue_PostQuit(wMessageQueue* queue, int nExitCode);
730 
731  WINPR_API int MessageQueue_Get(wMessageQueue* queue, wMessage* message);
732  WINPR_API int MessageQueue_Peek(wMessageQueue* queue, wMessage* message, BOOL remove);
733 
744  WINPR_API int MessageQueue_Clear(wMessageQueue* queue);
745 
757  WINPR_API void MessageQueue_Free(wMessageQueue* queue);
758 
771  WINPR_ATTR_MALLOC(MessageQueue_Free, 1)
772  WINPR_API wMessageQueue* MessageQueue_New(const wObject* callback);
773 
774  /* Message Pipe */
775 
776  typedef struct
777  {
778  wMessageQueue* In;
779  wMessageQueue* Out;
780  } wMessagePipe;
781 
782  WINPR_API void MessagePipe_PostQuit(wMessagePipe* pipe, int nExitCode);
783 
784  WINPR_API void MessagePipe_Free(wMessagePipe* pipe);
785 
786  WINPR_ATTR_MALLOC(MessagePipe_Free, 1)
787  WINPR_API wMessagePipe* MessagePipe_New(void);
788 
789  /* Publisher/Subscriber Pattern */
790 
791  typedef struct
792  {
793  DWORD Size;
794  const char* Sender;
795  } wEventArgs;
796 
797  typedef void (*pEventHandler)(void* context, const wEventArgs* e);
798 
799 #ifdef __cplusplus
800 #define WINPR_EVENT_CAST(t, val) reinterpret_cast<t>(val)
801 #else
802 #define WINPR_EVENT_CAST(t, val) (t)(val)
803 #endif
804 
805 #define MAX_EVENT_HANDLERS 32
806 
807  typedef struct
808  {
809  const char* EventName;
810  wEventArgs EventArgs;
811  size_t EventHandlerCount;
812  pEventHandler EventHandlers[MAX_EVENT_HANDLERS];
813  } wEventType;
814 
815 #define EventArgsInit(_event_args, _sender) \
816  memset(_event_args, 0, sizeof(*_event_args)); \
817  (_event_args)->e.Size = sizeof(*_event_args); \
818  (_event_args)->e.Sender = _sender
819 
820 #define DEFINE_EVENT_HANDLER(name) \
821  typedef void (*p##name##EventHandler)(void* context, const name##EventArgs* e)
822 
823 #define DEFINE_EVENT_RAISE(name) \
824  static INLINE int PubSub_On##name(wPubSub* pubSub, void* context, const name##EventArgs* e) \
825  { \
826  WINPR_ASSERT(e); \
827  return PubSub_OnEvent(pubSub, #name, context, &e->e); \
828  }
829 
830 #define DEFINE_EVENT_SUBSCRIBE(name) \
831  static INLINE int PubSub_Subscribe##name(wPubSub* pubSub, p##name##EventHandler EventHandler) \
832  { \
833  return PubSub_Subscribe(pubSub, #name, EventHandler); \
834  }
835 
836 #define DEFINE_EVENT_UNSUBSCRIBE(name) \
837  static INLINE int PubSub_Unsubscribe##name(wPubSub* pubSub, \
838  p##name##EventHandler EventHandler) \
839  { \
840  return PubSub_Unsubscribe(pubSub, #name, EventHandler); \
841  }
842 
843 #define DEFINE_EVENT_BEGIN(name) \
844  typedef struct \
845  { \
846  wEventArgs e;
847 
848 #define DEFINE_EVENT_END(name) \
849  } \
850  name##EventArgs; \
851  DEFINE_EVENT_HANDLER(name); \
852  DEFINE_EVENT_RAISE(name) \
853  DEFINE_EVENT_SUBSCRIBE(name) \
854  DEFINE_EVENT_UNSUBSCRIBE(name)
855 
856 #define DEFINE_EVENT_ENTRY(name) \
857  { \
858 #name, { sizeof(name##EventArgs), NULL }, 0, \
859  { \
860  NULL \
861  } \
862  }
863 
864  typedef struct s_wPubSub wPubSub;
865 
866  WINPR_API void PubSub_Lock(wPubSub* pubSub);
867  WINPR_API void PubSub_Unlock(wPubSub* pubSub);
868 
869  WINPR_API wEventType* PubSub_GetEventTypes(wPubSub* pubSub, size_t* count);
870  WINPR_API void PubSub_AddEventTypes(wPubSub* pubSub, wEventType* events, size_t count);
871  WINPR_API wEventType* PubSub_FindEventType(wPubSub* pubSub, const char* EventName);
872 
873  WINPR_API int PubSub_Subscribe(wPubSub* pubSub, const char* EventName, ...);
874  WINPR_API int PubSub_Unsubscribe(wPubSub* pubSub, const char* EventName, ...);
875 
876  WINPR_API int PubSub_OnEvent(wPubSub* pubSub, const char* EventName, void* context,
877  const wEventArgs* e);
878 
879  WINPR_API void PubSub_Free(wPubSub* pubSub);
880 
881  WINPR_ATTR_MALLOC(PubSub_Free, 1)
882  WINPR_API wPubSub* PubSub_New(BOOL synchronized);
883 
884 #ifdef __cplusplus
885 }
886 #endif
887 
888 #endif /* WINPR_COLLECTIONS_H */
This struct contains function pointer to initialize/free objects.
Definition: collections.h:57