Fixed #6136: Cleaned up Stack API
This commit is contained in:
parent
c2f4b3b975
commit
844ec8f74c
@ -94,24 +94,15 @@ extern "C"
|
|||||||
|
|
||||||
/* System.Collections.Stack */
|
/* System.Collections.Stack */
|
||||||
|
|
||||||
struct _wStack
|
|
||||||
{
|
|
||||||
int size;
|
|
||||||
int capacity;
|
|
||||||
void** array;
|
|
||||||
CRITICAL_SECTION lock;
|
|
||||||
BOOL synchronized;
|
|
||||||
wObject object;
|
|
||||||
};
|
|
||||||
typedef struct _wStack wStack;
|
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);
|
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 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_Push(wStack* stack, void* obj);
|
||||||
WINPR_API void* Stack_Pop(wStack* stack);
|
WINPR_API void* Stack_Pop(wStack* stack);
|
||||||
|
@ -23,6 +23,16 @@
|
|||||||
|
|
||||||
#include <winpr/collections.h>
|
#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:
|
* C equivalent of the C# Stack Class:
|
||||||
* http://msdn.microsoft.com/en-us/library/system.collections.stack.aspx
|
* http://msdn.microsoft.com/en-us/library/system.collections.stack.aspx
|
||||||
@ -36,9 +46,9 @@
|
|||||||
* Gets the number of elements contained in the Stack.
|
* 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)
|
if (stack->synchronized)
|
||||||
EnterCriticalSection(&stack->lock);
|
EnterCriticalSection(&stack->lock);
|
||||||
@ -60,6 +70,13 @@ BOOL Stack_IsSynchronized(wStack* stack)
|
|||||||
return stack->synchronized;
|
return stack->synchronized;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wObject* Stack_Object(wStack* stack)
|
||||||
|
{
|
||||||
|
if (!stack)
|
||||||
|
return NULL;
|
||||||
|
return &stack->object;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Methods
|
* Methods
|
||||||
*/
|
*/
|
||||||
@ -70,7 +87,7 @@ BOOL Stack_IsSynchronized(wStack* stack)
|
|||||||
|
|
||||||
void Stack_Clear(wStack* stack)
|
void Stack_Clear(wStack* stack)
|
||||||
{
|
{
|
||||||
int index;
|
size_t index;
|
||||||
|
|
||||||
if (stack->synchronized)
|
if (stack->synchronized)
|
||||||
EnterCriticalSection(&stack->lock);
|
EnterCriticalSection(&stack->lock);
|
||||||
@ -93,9 +110,9 @@ void Stack_Clear(wStack* stack)
|
|||||||
* Determines whether an element is in the 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;
|
BOOL found = FALSE;
|
||||||
|
|
||||||
if (stack->synchronized)
|
if (stack->synchronized)
|
||||||
@ -127,13 +144,11 @@ void Stack_Push(wStack* stack, void* obj)
|
|||||||
|
|
||||||
if ((stack->size + 1) >= stack->capacity)
|
if ((stack->size + 1) >= stack->capacity)
|
||||||
{
|
{
|
||||||
int new_cap;
|
const size_t new_cap = stack->capacity * 2;
|
||||||
void** new_arr;
|
void** new_arr = (void**)realloc(stack->array, sizeof(void*) * new_cap);
|
||||||
new_cap = stack->capacity * 2;
|
|
||||||
new_arr = (void**)realloc(stack->array, sizeof(void*) * new_cap);
|
|
||||||
|
|
||||||
if (!new_arr)
|
if (new_arr)
|
||||||
return;
|
goto end;
|
||||||
|
|
||||||
stack->array = new_arr;
|
stack->array = new_arr;
|
||||||
stack->capacity = new_cap;
|
stack->capacity = new_cap;
|
||||||
@ -141,6 +156,7 @@ void Stack_Push(wStack* stack, void* obj)
|
|||||||
|
|
||||||
stack->array[(stack->size)++] = obj;
|
stack->array[(stack->size)++] = obj;
|
||||||
|
|
||||||
|
end:
|
||||||
if (stack->synchronized)
|
if (stack->synchronized)
|
||||||
LeaveCriticalSection(&stack->lock);
|
LeaveCriticalSection(&stack->lock);
|
||||||
}
|
}
|
||||||
@ -211,13 +227,11 @@ wStack* Stack_New(BOOL synchronized)
|
|||||||
goto out_free;
|
goto out_free;
|
||||||
|
|
||||||
if (stack->synchronized && !InitializeCriticalSectionAndSpinCount(&stack->lock, 4000))
|
if (stack->synchronized && !InitializeCriticalSectionAndSpinCount(&stack->lock, 4000))
|
||||||
goto out_free_array;
|
goto out_free;
|
||||||
|
|
||||||
return stack;
|
return stack;
|
||||||
out_free_array:
|
|
||||||
free(stack->array);
|
|
||||||
out_free:
|
out_free:
|
||||||
free(stack);
|
Stack_Free(stack);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user