20 #include <winpr/config.h>
22 #include <winpr/collections.h>
23 #include <winpr/assert.h>
48 size_t Stack_Count(wStack* stack)
52 if (stack->synchronized)
53 EnterCriticalSection(&stack->lock);
57 if (stack->synchronized)
58 LeaveCriticalSection(&stack->lock);
67 BOOL Stack_IsSynchronized(wStack* stack)
70 return stack->synchronized;
73 wObject* Stack_Object(wStack* stack)
76 return &stack->object;
87 void Stack_Clear(wStack* stack)
90 if (stack->synchronized)
91 EnterCriticalSection(&stack->lock);
93 for (
size_t index = 0; index < stack->size; index++)
95 if (stack->object.fnObjectFree)
96 stack->object.fnObjectFree(stack->array[index]);
98 stack->array[index] = NULL;
103 if (stack->synchronized)
104 LeaveCriticalSection(&stack->lock);
111 BOOL Stack_Contains(wStack* stack,
const void* obj)
116 if (stack->synchronized)
117 EnterCriticalSection(&stack->lock);
119 for (
size_t i = 0; i < stack->size; i++)
121 if (stack->object.fnObjectEquals(stack->array[i], obj))
128 if (stack->synchronized)
129 LeaveCriticalSection(&stack->lock);
138 void Stack_Push(wStack* stack,
void* obj)
141 if (stack->synchronized)
142 EnterCriticalSection(&stack->lock);
144 if ((stack->size + 1) >= stack->capacity)
146 const size_t new_cap = stack->capacity * 2;
147 void** new_arr = (
void**)realloc(stack->array,
sizeof(
void*) * new_cap);
152 stack->array = new_arr;
153 stack->capacity = new_cap;
156 stack->array[(stack->size)++] = obj;
159 if (stack->synchronized)
160 LeaveCriticalSection(&stack->lock);
167 void* Stack_Pop(wStack* stack)
172 if (stack->synchronized)
173 EnterCriticalSection(&stack->lock);
176 obj = stack->array[--(stack->size)];
178 if (stack->synchronized)
179 LeaveCriticalSection(&stack->lock);
188 void* Stack_Peek(wStack* stack)
193 if (stack->synchronized)
194 EnterCriticalSection(&stack->lock);
197 obj = stack->array[stack->size - 1];
199 if (stack->synchronized)
200 LeaveCriticalSection(&stack->lock);
205 static BOOL default_stack_equals(
const void* obj1,
const void* obj2)
207 return (obj1 == obj2);
214 wStack* Stack_New(BOOL
synchronized)
216 wStack* stack = NULL;
217 stack = (wStack*)calloc(1,
sizeof(wStack));
222 stack->object.fnObjectEquals = default_stack_equals;
223 stack->synchronized =
synchronized;
224 stack->capacity = 32;
225 stack->array = (
void**)calloc(stack->capacity,
sizeof(
void*));
230 if (stack->synchronized && !InitializeCriticalSectionAndSpinCount(&stack->lock, 4000))
235 WINPR_PRAGMA_DIAG_PUSH
236 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
238 WINPR_PRAGMA_DIAG_POP
242 void Stack_Free(wStack* stack)
246 if (stack->synchronized)
247 DeleteCriticalSection(&stack->lock);
This struct contains function pointer to initialize/free objects.