2012-12-01 01:13:19 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* System.Collections.Stack
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <winpr/collections.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* C equivalent of the C# Stack Class:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/system.collections.stack.aspx
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Properties
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the number of elements contained in the Stack.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int Stack_Count(wStack* stack)
|
|
|
|
{
|
2014-04-18 18:27:51 +04:00
|
|
|
int ret;
|
|
|
|
|
2012-12-06 02:24:01 +04:00
|
|
|
if (stack->synchronized)
|
2014-04-18 18:27:51 +04:00
|
|
|
EnterCriticalSection(&stack->lock);
|
2012-12-01 01:13:19 +04:00
|
|
|
|
2014-04-18 18:27:51 +04:00
|
|
|
ret = stack->size;
|
2012-12-01 01:13:19 +04:00
|
|
|
|
2014-04-18 18:27:51 +04:00
|
|
|
if (stack->synchronized)
|
|
|
|
LeaveCriticalSection(&stack->lock);
|
|
|
|
|
|
|
|
return ret;
|
2012-12-01 01:13:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a value indicating whether access to the Stack is synchronized (thread safe).
|
|
|
|
*/
|
|
|
|
|
|
|
|
BOOL Stack_IsSynchronized(wStack* stack)
|
|
|
|
{
|
2012-12-06 02:24:01 +04:00
|
|
|
return stack->synchronized;
|
2012-12-01 01:13:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all objects from the Stack.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void Stack_Clear(wStack* stack)
|
|
|
|
{
|
2014-04-18 18:27:51 +04:00
|
|
|
int index;
|
|
|
|
|
|
|
|
if (stack->synchronized)
|
|
|
|
EnterCriticalSection(&stack->lock);
|
|
|
|
|
|
|
|
for (index = 0; index < stack->size; index++)
|
|
|
|
{
|
|
|
|
if (stack->object.fnObjectFree)
|
|
|
|
stack->object.fnObjectFree(stack->array[index]);
|
|
|
|
|
|
|
|
stack->array[index] = NULL;
|
|
|
|
}
|
2012-12-01 01:13:19 +04:00
|
|
|
|
2014-04-18 18:27:51 +04:00
|
|
|
stack->size = 0;
|
|
|
|
|
|
|
|
if (stack->synchronized)
|
|
|
|
LeaveCriticalSection(&stack->lock);
|
2012-12-01 01:13:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether an element is in the Stack.
|
|
|
|
*/
|
|
|
|
|
|
|
|
BOOL Stack_Contains(wStack* stack, void* obj)
|
|
|
|
{
|
2014-04-18 18:27:51 +04:00
|
|
|
int i;
|
|
|
|
BOOL found = FALSE;
|
|
|
|
|
|
|
|
if (stack->synchronized)
|
|
|
|
EnterCriticalSection(&stack->lock);
|
|
|
|
|
|
|
|
for (i = 0; i < stack->size; i++)
|
|
|
|
{
|
|
|
|
if (stack->object.fnObjectEquals(stack->array[i], obj))
|
|
|
|
{
|
|
|
|
found = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stack->synchronized)
|
|
|
|
LeaveCriticalSection(&stack->lock);
|
|
|
|
|
|
|
|
return found;
|
2012-12-01 01:13:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts an object at the top of the Stack.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void Stack_Push(wStack* stack, void* obj)
|
|
|
|
{
|
2012-12-06 02:24:01 +04:00
|
|
|
if (stack->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
EnterCriticalSection(&stack->lock);
|
2012-12-01 01:13:19 +04:00
|
|
|
|
2013-02-04 00:16:37 +04:00
|
|
|
if ((stack->size + 1) >= stack->capacity)
|
|
|
|
{
|
|
|
|
stack->capacity *= 2;
|
|
|
|
stack->array = (void**) realloc(stack->array, sizeof(void*) * stack->capacity);
|
2012-12-01 01:13:19 +04:00
|
|
|
}
|
2013-02-04 00:16:37 +04:00
|
|
|
|
|
|
|
stack->array[(stack->size)++] = obj;
|
|
|
|
|
|
|
|
if (stack->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
LeaveCriticalSection(&stack->lock);
|
2012-12-01 01:13:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes and returns the object at the top of the Stack.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void* Stack_Pop(wStack* stack)
|
|
|
|
{
|
2013-02-04 00:16:37 +04:00
|
|
|
void* obj = NULL;
|
|
|
|
|
2012-12-06 02:24:01 +04:00
|
|
|
if (stack->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
EnterCriticalSection(&stack->lock);
|
2012-12-01 01:13:19 +04:00
|
|
|
|
2013-02-04 00:16:37 +04:00
|
|
|
if (stack->size > 0)
|
|
|
|
obj = stack->array[--(stack->size)];
|
|
|
|
|
|
|
|
if (stack->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
LeaveCriticalSection(&stack->lock);
|
2012-12-01 01:13:19 +04:00
|
|
|
|
2013-02-04 00:16:37 +04:00
|
|
|
return obj;
|
2012-12-01 01:13:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the object at the top of the Stack without removing it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void* Stack_Peek(wStack* stack)
|
|
|
|
{
|
2013-02-04 00:16:37 +04:00
|
|
|
void* obj = NULL;
|
|
|
|
|
2012-12-06 02:24:01 +04:00
|
|
|
if (stack->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
EnterCriticalSection(&stack->lock);
|
2012-12-01 01:13:19 +04:00
|
|
|
|
2013-02-04 00:16:37 +04:00
|
|
|
if (stack->size > 0)
|
|
|
|
obj = stack->array[stack->size];
|
|
|
|
|
|
|
|
if (stack->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
LeaveCriticalSection(&stack->lock);
|
2012-12-01 01:13:19 +04:00
|
|
|
|
2013-02-04 00:16:37 +04:00
|
|
|
return obj;
|
2012-12-01 01:13:19 +04:00
|
|
|
}
|
|
|
|
|
2014-04-18 18:27:51 +04:00
|
|
|
|
|
|
|
static BOOL default_stack_equals(void *obj1, void *obj2)
|
|
|
|
{
|
|
|
|
return (obj1 == obj2);
|
|
|
|
}
|
|
|
|
|
2012-12-01 01:13:19 +04:00
|
|
|
/**
|
|
|
|
* Construction, Destruction
|
|
|
|
*/
|
|
|
|
|
2013-02-04 00:16:37 +04:00
|
|
|
wStack* Stack_New(BOOL synchronized)
|
2012-12-01 01:13:19 +04:00
|
|
|
{
|
|
|
|
wStack* stack = NULL;
|
|
|
|
|
2014-04-18 18:27:51 +04:00
|
|
|
stack = (wStack *)calloc(1, sizeof(wStack));
|
|
|
|
if (!stack)
|
|
|
|
return NULL;
|
2012-12-01 01:13:19 +04:00
|
|
|
|
2014-04-18 18:27:51 +04:00
|
|
|
stack->object.fnObjectEquals = default_stack_equals;
|
|
|
|
stack->synchronized = synchronized;
|
2013-02-04 00:16:37 +04:00
|
|
|
|
2014-04-18 18:27:51 +04:00
|
|
|
stack->capacity = 32;
|
|
|
|
stack->array = (void**) malloc(sizeof(void*) * stack->capacity);
|
|
|
|
if (!stack->array)
|
|
|
|
goto out_free;
|
2013-02-04 00:16:37 +04:00
|
|
|
|
2014-04-18 18:27:51 +04:00
|
|
|
if (stack->synchronized && !InitializeCriticalSectionAndSpinCount(&stack->lock, 4000))
|
|
|
|
goto out_free_array;
|
2012-12-01 01:13:19 +04:00
|
|
|
|
|
|
|
return stack;
|
2014-04-18 18:27:51 +04:00
|
|
|
|
|
|
|
out_free_array:
|
|
|
|
free(stack->array);
|
|
|
|
out_free:
|
|
|
|
free(stack);
|
|
|
|
return NULL;
|
2012-12-01 01:13:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Stack_Free(wStack* stack)
|
|
|
|
{
|
2014-11-16 18:48:50 +03:00
|
|
|
if (stack)
|
2013-02-04 00:16:37 +04:00
|
|
|
{
|
|
|
|
if (stack->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
DeleteCriticalSection(&stack->lock);
|
2013-02-04 00:16:37 +04:00
|
|
|
|
|
|
|
free(stack->array);
|
|
|
|
|
|
|
|
free(stack);
|
|
|
|
}
|
2012-12-01 01:13:19 +04:00
|
|
|
}
|