Fixed #6136: Cleaned up Stack API

This commit is contained in:
akallabeth 2020-05-05 10:02:21 +02:00 committed by akallabeth
parent c2f4b3b975
commit 844ec8f74c
2 changed files with 32 additions and 27 deletions

View File

@ -94,24 +94,15 @@ extern "C"
/* System.Collections.Stack */
struct _wStack
{
int size;
int capacity;
void** array;
CRITICAL_SECTION lock;
BOOL synchronized;
wObject object;
};
typedef struct _wStack wStack;
WINPR_API int Stack_Count(wStack* stack);
WINPR_API size_t Stack_Count(wStack* stack);
WINPR_API BOOL Stack_IsSynchronized(wStack* stack);
#define Stack_Object(_stack) (&_stack->object)
WINPR_API wObject* Stack_Object(wStack* stack);
WINPR_API void Stack_Clear(wStack* stack);
WINPR_API BOOL Stack_Contains(wStack* stack, void* obj);
WINPR_API BOOL Stack_Contains(wStack* stack, const void* obj);
WINPR_API void Stack_Push(wStack* stack, void* obj);
WINPR_API void* Stack_Pop(wStack* stack);

View File

@ -23,6 +23,16 @@
#include <winpr/collections.h>
struct _wStack
{
size_t size;
size_t capacity;
void** array;
CRITICAL_SECTION lock;
BOOL synchronized;
wObject object;
};
/**
* C equivalent of the C# Stack Class:
* http://msdn.microsoft.com/en-us/library/system.collections.stack.aspx
@ -36,9 +46,9 @@
* Gets the number of elements contained in the Stack.
*/
int Stack_Count(wStack* stack)
size_t Stack_Count(wStack* stack)
{
int ret;
size_t ret;
if (stack->synchronized)
EnterCriticalSection(&stack->lock);
@ -60,6 +70,13 @@ BOOL Stack_IsSynchronized(wStack* stack)
return stack->synchronized;
}
wObject* Stack_Object(wStack* stack)
{
if (!stack)
return NULL;
return &stack->object;
}
/**
* Methods
*/
@ -70,7 +87,7 @@ BOOL Stack_IsSynchronized(wStack* stack)
void Stack_Clear(wStack* stack)
{
int index;
size_t index;
if (stack->synchronized)
EnterCriticalSection(&stack->lock);
@ -93,9 +110,9 @@ void Stack_Clear(wStack* stack)
* Determines whether an element is in the Stack.
*/
BOOL Stack_Contains(wStack* stack, void* obj)
BOOL Stack_Contains(wStack* stack, const void* obj)
{
int i;
size_t i;
BOOL found = FALSE;
if (stack->synchronized)
@ -127,13 +144,11 @@ void Stack_Push(wStack* stack, void* obj)
if ((stack->size + 1) >= stack->capacity)
{
int new_cap;
void** new_arr;
new_cap = stack->capacity * 2;
new_arr = (void**)realloc(stack->array, sizeof(void*) * new_cap);
const size_t new_cap = stack->capacity * 2;
void** new_arr = (void**)realloc(stack->array, sizeof(void*) * new_cap);
if (!new_arr)
return;
if (new_arr)
goto end;
stack->array = new_arr;
stack->capacity = new_cap;
@ -141,6 +156,7 @@ void Stack_Push(wStack* stack, void* obj)
stack->array[(stack->size)++] = obj;
end:
if (stack->synchronized)
LeaveCriticalSection(&stack->lock);
}
@ -211,13 +227,11 @@ wStack* Stack_New(BOOL synchronized)
goto out_free;
if (stack->synchronized && !InitializeCriticalSectionAndSpinCount(&stack->lock, 4000))
goto out_free_array;
goto out_free;
return stack;
out_free_array:
free(stack->array);
out_free:
free(stack);
Stack_Free(stack);
return NULL;
}