1 | /*
|
---|
2 | * Copyright (c) 2009 Lukas Mejdrech
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /** @file
|
---|
34 | * Dynamic first in first out positive integer queue implementation.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <errno.h>
|
---|
38 | #include <malloc.h>
|
---|
39 | #include <mem.h>
|
---|
40 |
|
---|
41 | #include <adt/dynamic_fifo.h>
|
---|
42 |
|
---|
43 | /** Internal magic value for a consistency check.
|
---|
44 | */
|
---|
45 | #define DYN_FIFO_MAGIC_VALUE 0x58627659
|
---|
46 |
|
---|
47 | /** Returns the next queue index.
|
---|
48 | * The queue field is circular.
|
---|
49 | * @param[in] fifo The dynamic queue.
|
---|
50 | * @param[in] index The actual index to be shifted.
|
---|
51 | */
|
---|
52 | #define NEXT_INDEX(fifo, index) (((index) + 1) % ((fifo)->size + 1))
|
---|
53 |
|
---|
54 | /** Checks if the queue is valid.
|
---|
55 | * @param[in] fifo The dynamic queue.
|
---|
56 | * @returns TRUE if the queue is valid.
|
---|
57 | * @returns FALSE otherwise.
|
---|
58 | */
|
---|
59 | static int dyn_fifo_is_valid(dyn_fifo_ref fifo){
|
---|
60 | return fifo && (fifo->magic_value == DYN_FIFO_MAGIC_VALUE);
|
---|
61 | }
|
---|
62 |
|
---|
63 | int dyn_fifo_initialize(dyn_fifo_ref fifo, int size){
|
---|
64 | if(! fifo){
|
---|
65 | return EBADMEM;
|
---|
66 | }
|
---|
67 | if(size <= 0){
|
---|
68 | return EINVAL;
|
---|
69 | }
|
---|
70 | fifo->items = (int *) malloc(sizeof(int) * size + 1);
|
---|
71 | if(! fifo->items){
|
---|
72 | return ENOMEM;
|
---|
73 | }
|
---|
74 | fifo->size = size;
|
---|
75 | fifo->head = 0;
|
---|
76 | fifo->tail = 0;
|
---|
77 | fifo->magic_value = DYN_FIFO_MAGIC_VALUE;
|
---|
78 | return EOK;
|
---|
79 | }
|
---|
80 |
|
---|
81 | int dyn_fifo_push(dyn_fifo_ref fifo, int value, int max_size){
|
---|
82 | int * new_items;
|
---|
83 |
|
---|
84 | if(! dyn_fifo_is_valid(fifo)){
|
---|
85 | return EINVAL;
|
---|
86 | }
|
---|
87 | if(NEXT_INDEX(fifo, fifo->tail) == fifo->head){
|
---|
88 | if((max_size > 0) && ((fifo->size * 2) > max_size)){
|
---|
89 | if(fifo->size >= max_size){
|
---|
90 | return ENOMEM;
|
---|
91 | }
|
---|
92 | }else{
|
---|
93 | max_size = fifo->size * 2;
|
---|
94 | }
|
---|
95 | new_items = realloc(fifo->items, sizeof(int) * max_size + 1);
|
---|
96 | if(! new_items){
|
---|
97 | return ENOMEM;
|
---|
98 | }
|
---|
99 | fifo->items = new_items;
|
---|
100 | if(fifo->tail < fifo->head){
|
---|
101 | if(fifo->tail < max_size - fifo->size){
|
---|
102 | memcpy(fifo->items + fifo->size + 1, fifo->items, fifo->tail * sizeof(int));
|
---|
103 | fifo->tail += fifo->size + 1;
|
---|
104 | }else{
|
---|
105 | memcpy(fifo->items + fifo->size + 1, fifo->items, (max_size - fifo->size) * sizeof(int));
|
---|
106 | memcpy(fifo->items, fifo->items + max_size - fifo->size, fifo->tail - max_size + fifo->size);
|
---|
107 | fifo->tail -= max_size - fifo->size;
|
---|
108 | }
|
---|
109 | }
|
---|
110 | fifo->size = max_size;
|
---|
111 | }
|
---|
112 | fifo->items[fifo->tail] = value;
|
---|
113 | fifo->tail = NEXT_INDEX(fifo, fifo->tail);
|
---|
114 | return EOK;
|
---|
115 | }
|
---|
116 |
|
---|
117 | int dyn_fifo_pop(dyn_fifo_ref fifo){
|
---|
118 | int value;
|
---|
119 |
|
---|
120 | if(! dyn_fifo_is_valid(fifo)){
|
---|
121 | return EINVAL;
|
---|
122 | }
|
---|
123 | if(fifo->head == fifo->tail){
|
---|
124 | return ENOENT;
|
---|
125 | }
|
---|
126 | value = fifo->items[fifo->head];
|
---|
127 | fifo->head = NEXT_INDEX(fifo, fifo->head);
|
---|
128 | return value;
|
---|
129 | }
|
---|
130 |
|
---|
131 | int dyn_fifo_value(dyn_fifo_ref fifo){
|
---|
132 | if(! dyn_fifo_is_valid(fifo)){
|
---|
133 | return EINVAL;
|
---|
134 | }
|
---|
135 | if(fifo->head == fifo->tail){
|
---|
136 | return ENOENT;
|
---|
137 | }
|
---|
138 | return fifo->items[fifo->head];
|
---|
139 | }
|
---|
140 |
|
---|
141 | int dyn_fifo_destroy(dyn_fifo_ref fifo){
|
---|
142 | if(! dyn_fifo_is_valid(fifo)){
|
---|
143 | return EINVAL;
|
---|
144 | }
|
---|
145 | free(fifo->items);
|
---|
146 | fifo->magic_value = 0;
|
---|
147 | return EOK;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /** @}
|
---|
151 | */
|
---|