2012-12-01 01:13:19 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* System.Collections.Queue
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
|
2012-12-01 02:23:36 +04:00
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
2012-12-01 01:13:19 +04:00
|
|
|
#include <winpr/collections.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* C equivalent of the C# Queue Class:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/system.collections.queue.aspx
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Properties
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the number of elements contained in the Queue.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int Queue_Count(wQueue* queue)
|
|
|
|
{
|
2014-04-18 18:27:51 +04:00
|
|
|
int ret;
|
|
|
|
if (queue->synchronized)
|
|
|
|
EnterCriticalSection(&queue->lock);
|
|
|
|
|
|
|
|
ret = queue->size;
|
|
|
|
|
|
|
|
if (queue->synchronized)
|
|
|
|
LeaveCriticalSection(&queue->lock);
|
|
|
|
return ret;
|
2012-12-01 01:13:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-28 04:06:10 +04:00
|
|
|
* Lock access to the ArrayList
|
2012-12-01 01:13:19 +04:00
|
|
|
*/
|
|
|
|
|
2013-08-02 14:07:05 +04:00
|
|
|
void Queue_Lock(wQueue* queue)
|
2012-12-01 01:13:19 +04:00
|
|
|
{
|
2013-08-02 14:07:05 +04:00
|
|
|
EnterCriticalSection(&queue->lock);
|
2012-12-01 01:13:19 +04:00
|
|
|
}
|
|
|
|
|
2012-12-06 01:04:01 +04:00
|
|
|
/**
|
2013-03-28 04:06:10 +04:00
|
|
|
* Unlock access to the ArrayList
|
2012-12-06 01:04:01 +04:00
|
|
|
*/
|
|
|
|
|
2013-08-02 14:07:05 +04:00
|
|
|
void Queue_Unlock(wQueue* queue)
|
2012-12-06 01:04:01 +04:00
|
|
|
{
|
2013-08-02 14:07:05 +04:00
|
|
|
LeaveCriticalSection(&queue->lock);
|
2012-12-06 01:04:01 +04:00
|
|
|
}
|
|
|
|
|
2012-12-11 03:56:53 +04:00
|
|
|
/**
|
|
|
|
* Gets an event which is set when the queue is non-empty
|
|
|
|
*/
|
|
|
|
|
|
|
|
HANDLE Queue_Event(wQueue* queue)
|
|
|
|
{
|
|
|
|
return queue->event;
|
|
|
|
}
|
|
|
|
|
2012-12-01 01:13:19 +04:00
|
|
|
/**
|
|
|
|
* Methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all objects from the Queue.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void Queue_Clear(wQueue* queue)
|
|
|
|
{
|
2012-12-01 02:23:36 +04:00
|
|
|
int index;
|
2012-12-01 01:13:19 +04:00
|
|
|
|
2012-12-01 02:23:36 +04:00
|
|
|
if (queue->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
EnterCriticalSection(&queue->lock);
|
2012-12-01 02:23:36 +04:00
|
|
|
|
2012-12-24 08:45:55 +04:00
|
|
|
for (index = queue->head; index != queue->tail; index = (index + 1) % queue->capacity)
|
2012-12-01 02:23:36 +04:00
|
|
|
{
|
2012-12-12 08:34:51 +04:00
|
|
|
if (queue->object.fnObjectFree)
|
|
|
|
queue->object.fnObjectFree(queue->array[index]);
|
|
|
|
|
2012-12-01 02:23:36 +04:00
|
|
|
queue->array[index] = NULL;
|
2012-12-01 01:13:19 +04:00
|
|
|
}
|
2012-12-01 02:23:36 +04:00
|
|
|
|
|
|
|
queue->size = 0;
|
|
|
|
queue->head = queue->tail = 0;
|
|
|
|
|
|
|
|
if (queue->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
LeaveCriticalSection(&queue->lock);
|
2012-12-01 01:13:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether an element is in the Queue.
|
|
|
|
*/
|
|
|
|
|
|
|
|
BOOL Queue_Contains(wQueue* queue, void* obj)
|
|
|
|
{
|
2012-12-01 02:23:36 +04:00
|
|
|
int index;
|
|
|
|
BOOL found = FALSE;
|
|
|
|
|
|
|
|
if (queue->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
EnterCriticalSection(&queue->lock);
|
2012-12-01 01:13:19 +04:00
|
|
|
|
2012-12-01 02:23:36 +04:00
|
|
|
for (index = 0; index < queue->tail; index++)
|
|
|
|
{
|
2014-04-18 18:27:51 +04:00
|
|
|
if (queue->object.fnObjectEquals(queue->array[index], obj))
|
2012-12-01 02:23:36 +04:00
|
|
|
{
|
|
|
|
found = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2012-12-01 01:13:19 +04:00
|
|
|
}
|
|
|
|
|
2012-12-01 02:23:36 +04:00
|
|
|
if (queue->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
LeaveCriticalSection(&queue->lock);
|
2012-12-01 02:23:36 +04:00
|
|
|
|
|
|
|
return found;
|
2012-12-01 01:13:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an object to the end of the Queue.
|
|
|
|
*/
|
|
|
|
|
2014-04-18 18:27:51 +04:00
|
|
|
BOOL Queue_Enqueue(wQueue* queue, void* obj)
|
2012-12-01 01:13:19 +04:00
|
|
|
{
|
2014-04-18 18:27:51 +04:00
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
2012-12-01 02:23:36 +04:00
|
|
|
if (queue->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
EnterCriticalSection(&queue->lock);
|
2012-12-01 01:13:19 +04:00
|
|
|
|
2012-12-01 02:23:36 +04:00
|
|
|
if (queue->size == queue->capacity)
|
|
|
|
{
|
2013-01-21 05:44:30 +04:00
|
|
|
int old_capacity;
|
|
|
|
int new_capacity;
|
2014-04-18 18:27:51 +04:00
|
|
|
void **newArray;
|
2013-01-21 05:44:30 +04:00
|
|
|
|
|
|
|
old_capacity = queue->capacity;
|
|
|
|
new_capacity = queue->capacity * queue->growthFactor;
|
|
|
|
|
2014-04-18 18:27:51 +04:00
|
|
|
newArray = (void **)realloc(queue->array, sizeof(void*) * new_capacity);
|
|
|
|
if (!newArray)
|
|
|
|
{
|
|
|
|
ret = FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-01-21 05:44:30 +04:00
|
|
|
queue->capacity = new_capacity;
|
2014-04-18 18:27:51 +04:00
|
|
|
queue->array = newArray;
|
2013-01-21 05:44:30 +04:00
|
|
|
ZeroMemory(&(queue->array[old_capacity]), old_capacity * sizeof(void*));
|
|
|
|
|
2013-02-10 02:13:53 +04:00
|
|
|
if (queue->tail < old_capacity)
|
2013-01-21 05:44:30 +04:00
|
|
|
{
|
|
|
|
CopyMemory(&(queue->array[old_capacity]), queue->array, queue->tail * sizeof(void*));
|
|
|
|
queue->tail += old_capacity;
|
|
|
|
}
|
2012-12-01 01:13:19 +04:00
|
|
|
}
|
2012-12-01 02:23:36 +04:00
|
|
|
|
|
|
|
queue->array[queue->tail] = obj;
|
|
|
|
queue->tail = (queue->tail + 1) % queue->capacity;
|
|
|
|
queue->size++;
|
|
|
|
|
2012-12-11 03:56:53 +04:00
|
|
|
SetEvent(queue->event);
|
|
|
|
|
2014-04-18 18:27:51 +04:00
|
|
|
out:
|
2012-12-01 02:23:36 +04:00
|
|
|
if (queue->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
LeaveCriticalSection(&queue->lock);
|
2014-04-18 18:27:51 +04:00
|
|
|
return ret;
|
2012-12-01 01:13:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes and returns the object at the beginning of the Queue.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void* Queue_Dequeue(wQueue* queue)
|
|
|
|
{
|
2012-12-01 02:23:36 +04:00
|
|
|
void* obj = NULL;
|
|
|
|
|
|
|
|
if (queue->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
EnterCriticalSection(&queue->lock);
|
2012-12-01 01:13:19 +04:00
|
|
|
|
2012-12-01 02:23:36 +04:00
|
|
|
if (queue->size > 0)
|
|
|
|
{
|
|
|
|
obj = queue->array[queue->head];
|
|
|
|
queue->array[queue->head] = NULL;
|
2012-12-06 01:04:01 +04:00
|
|
|
queue->head = (queue->head + 1) % queue->capacity;
|
2012-12-01 02:23:36 +04:00
|
|
|
queue->size--;
|
2012-12-01 01:13:19 +04:00
|
|
|
}
|
|
|
|
|
2012-12-11 03:56:53 +04:00
|
|
|
if (queue->size < 1)
|
|
|
|
ResetEvent(queue->event);
|
|
|
|
|
2012-12-01 02:23:36 +04:00
|
|
|
if (queue->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
LeaveCriticalSection(&queue->lock);
|
2012-12-01 02:23:36 +04:00
|
|
|
|
|
|
|
return obj;
|
2012-12-01 01:13:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the object at the beginning of the Queue without removing it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void* Queue_Peek(wQueue* queue)
|
|
|
|
{
|
2012-12-01 02:23:36 +04:00
|
|
|
void* obj = NULL;
|
2012-12-01 01:13:19 +04:00
|
|
|
|
2012-12-01 02:23:36 +04:00
|
|
|
if (queue->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
EnterCriticalSection(&queue->lock);
|
2012-12-01 01:13:19 +04:00
|
|
|
|
2012-12-01 02:23:36 +04:00
|
|
|
if (queue->size > 0)
|
|
|
|
obj = queue->array[queue->head];
|
|
|
|
|
|
|
|
if (queue->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
LeaveCriticalSection(&queue->lock);
|
2012-12-01 02:23:36 +04:00
|
|
|
|
|
|
|
return obj;
|
2012-12-01 01:13:19 +04:00
|
|
|
}
|
|
|
|
|
2014-04-18 18:27:51 +04:00
|
|
|
static BOOL default_queue_equals(void *obj1, void *obj2)
|
|
|
|
{
|
|
|
|
return (obj1 == obj2);
|
|
|
|
}
|
|
|
|
|
2012-12-01 01:13:19 +04:00
|
|
|
/**
|
|
|
|
* Construction, Destruction
|
|
|
|
*/
|
|
|
|
|
2012-12-06 02:24:01 +04:00
|
|
|
wQueue* Queue_New(BOOL synchronized, int capacity, int growthFactor)
|
2012-12-01 01:13:19 +04:00
|
|
|
{
|
|
|
|
wQueue* queue = NULL;
|
|
|
|
|
2014-04-09 17:26:43 +04:00
|
|
|
queue = (wQueue *)calloc(1, sizeof(wQueue));
|
|
|
|
if (!queue)
|
|
|
|
return NULL;
|
2012-12-01 01:13:19 +04:00
|
|
|
|
2014-04-09 17:26:43 +04:00
|
|
|
queue->capacity = 32;
|
|
|
|
queue->growthFactor = 2;
|
2012-12-01 02:23:36 +04:00
|
|
|
|
2014-04-09 17:26:43 +04:00
|
|
|
queue->synchronized = synchronized;
|
2012-12-01 02:23:36 +04:00
|
|
|
|
2014-04-09 17:26:43 +04:00
|
|
|
if (capacity > 0)
|
|
|
|
queue->capacity = capacity;
|
2012-12-01 02:23:36 +04:00
|
|
|
|
2014-04-09 17:26:43 +04:00
|
|
|
if (growthFactor > 0)
|
|
|
|
queue->growthFactor = growthFactor;
|
2012-12-01 02:23:36 +04:00
|
|
|
|
2014-04-09 17:26:43 +04:00
|
|
|
queue->array = (void **)calloc(queue->capacity, sizeof(void *));
|
|
|
|
if (!queue->array)
|
|
|
|
goto out_free;
|
2012-12-01 02:23:36 +04:00
|
|
|
|
2014-04-09 17:26:43 +04:00
|
|
|
queue->event = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!queue->event)
|
|
|
|
goto out_free_array;
|
2012-12-01 01:13:19 +04:00
|
|
|
|
2014-04-18 18:27:51 +04:00
|
|
|
if (!InitializeCriticalSectionAndSpinCount(&queue->lock, 4000))
|
|
|
|
goto out_free_event;
|
|
|
|
|
|
|
|
queue->object.fnObjectEquals = default_queue_equals;
|
2012-12-01 01:13:19 +04:00
|
|
|
return queue;
|
2014-04-09 17:26:43 +04:00
|
|
|
|
2014-04-18 18:27:51 +04:00
|
|
|
out_free_event:
|
|
|
|
CloseHandle(queue->event);
|
2014-04-09 17:26:43 +04:00
|
|
|
out_free_array:
|
|
|
|
free(queue->array);
|
|
|
|
out_free:
|
|
|
|
free(queue);
|
|
|
|
return NULL;
|
2012-12-01 01:13:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Queue_Free(wQueue* queue)
|
|
|
|
{
|
2014-04-18 18:27:51 +04:00
|
|
|
if (!queue)
|
|
|
|
return;
|
|
|
|
|
2012-12-13 07:03:40 +04:00
|
|
|
Queue_Clear(queue);
|
|
|
|
|
2012-12-11 03:56:53 +04:00
|
|
|
CloseHandle(queue->event);
|
2013-08-02 14:07:05 +04:00
|
|
|
DeleteCriticalSection(&queue->lock);
|
2012-12-01 02:23:36 +04:00
|
|
|
free(queue->array);
|
2012-12-01 01:13:19 +04:00
|
|
|
free(queue);
|
|
|
|
}
|