aboutsummaryrefslogtreecommitdiff
path: root/utils/src/queue.c
diff options
context:
space:
mode:
authorHo-Eun Ryu <ho-eun.ryu@windriver.com>2009-09-25 10:19:52 +0900
committerPatrick Tjin <pattjin@google.com>2014-07-21 22:03:21 -0700
commit3597788ce7c666b2e86df3932968f0745f4b7bd1 (patch)
treecca9197add4cf8b72dd3cdf33fbaa64ba6144498 /utils/src/queue.c
parent08373062d895bb9673e7391551d4756cd68e37fa (diff)
downloadwrs_omxil_core-3597788ce7c666b2e86df3932968f0745f4b7bd1.tar.gz
utils:list,queue,module: add utility routines
it has list, queue, module and etc
Diffstat (limited to 'utils/src/queue.c')
-rw-r--r--utils/src/queue.c189
1 files changed, 189 insertions, 0 deletions
diff --git a/utils/src/queue.c b/utils/src/queue.c
new file mode 100644
index 0000000..2717928
--- /dev/null
+++ b/utils/src/queue.c
@@ -0,0 +1,189 @@
+/*
+ * Copyright (C) 2009 Wind River Systems.
+ */
+
+#include <stdlib.h>
+
+#include <queue.h>
+
+inline void __queue_init(struct queue *queue)
+{
+ queue->head = NULL;
+ queue->tail = NULL;
+ queue->length = 0;
+}
+
+struct queue *queue_alloc(void)
+{
+ struct queue *queue;
+
+ queue = malloc(sizeof(struct queue));
+ if (queue)
+ __queue_init(queue);
+
+ return queue;
+}
+
+inline void __queue_free(struct queue *queue)
+{
+ free(queue);
+}
+
+void queue_free_all(struct queue *queue)
+{
+ struct list *list = queue->head;
+
+ list_free_all(list);
+ __queue_init(queue);
+}
+
+void __queue_push_head(struct queue *queue, struct list *entry)
+{
+ queue->head = __list_add_head(queue->head, entry);
+ if (!queue->tail)
+ queue->tail = queue->head;
+
+ queue->length++;
+}
+
+int queue_push_head(struct queue *queue, void *data)
+{
+ struct list *entry = list_alloc(data);
+
+ if (!entry)
+ return -1;
+ else
+ queue->head = __list_add_head(queue->head, entry);
+
+ if (!queue->tail)
+ queue->tail = queue->head;
+
+ queue->length++;
+ return 0;
+}
+
+void __queue_push_tail(struct queue *queue, struct list *entry)
+{
+ queue->tail = list_add_tail(queue->tail, entry);
+ if (queue->tail->next)
+ queue->tail = queue->tail->next;
+ else
+ queue->head = queue->tail;
+
+ queue->length++;
+}
+
+int queue_push_tail(struct queue *queue, void *data)
+{
+ struct list *entry = list_alloc(data);
+
+ if (!entry)
+ return -1;
+ else
+ queue->tail = __list_add_tail(queue->tail, entry);
+
+ if (queue->tail->next)
+ queue->tail = queue->tail->next;
+ else
+ queue->head = queue->tail;
+
+ queue->length++;
+ return 0;
+}
+
+struct list *__queue_pop_head(struct queue *queue)
+{
+ struct list *entry = queue->head;
+
+ if (entry) {
+ queue->head = __list_remove(queue->head, entry);
+ if (!queue->head)
+ queue->tail = NULL;
+
+ queue->length--;
+ }
+
+ return entry;
+}
+
+void *queue_pop_head(struct queue *queue)
+{
+ struct list *entry;
+ void *data = NULL;
+
+ entry = __queue_pop_head(queue);
+ if (entry) {
+ data = entry->data;
+ __list_free(entry);
+ }
+
+ return data;
+}
+
+struct list *__queue_pop_tail(struct queue *queue)
+{
+ struct list *entry = queue->tail;
+ struct list *prev;
+
+ if (entry) {
+ prev = entry->prev;
+ queue->head = __list_remove(queue->head, entry);
+ queue->tail = prev;
+ queue->length--;
+ }
+
+ return entry;
+}
+
+void *queue_pop_tail(struct queue *queue)
+{
+ struct list *entry;
+ void *data = NULL;
+
+ entry = __queue_pop_tail(queue);
+ if (entry) {
+ data = entry->data;
+ __list_free(entry);
+ }
+
+ return data;
+}
+
+inline struct list *__queue_peek_head(struct queue *queue)
+{
+ return queue->head;
+}
+
+inline struct list *__queue_peek_tail(struct queue *queue)
+{
+ return queue->tail;
+}
+
+inline void *queue_peek_head(struct queue *queue)
+{
+ struct list *entry;
+ void *data = NULL;
+
+ entry = __queue_peek_head(queue);
+ if (entry)
+ data = entry->data;
+
+ return data;
+}
+
+inline void *queue_peek_tail(struct queue *queue)
+{
+ struct list *entry;
+ void *data = NULL;
+
+ entry = __queue_peek_tail(queue);
+ if (entry)
+ data = entry->data;
+
+ return data;
+}
+
+int queue_length(struct queue *queue)
+{
+ return queue->length;
+}