1 /* Authors: Arnaud Legrand */
3 /* This program is free software; you can redistribute it and/or modify it
4 under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "xbt/sysdep.h"
8 #include "fifo_private.h"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(fifo,xbt,"FIFO");
15 xbt_fifo_t xbt_fifo_new(void)
18 fifo = xbt_new0(struct xbt_fifo,1);
25 void xbt_fifo_free(xbt_fifo_t l)
27 xbt_fifo_item_t b, tmp;
29 for (b = xbt_fifo_getFirstitem(l); b;
30 tmp = b, b = b->next, xbt_fifo_freeitem(tmp));
39 xbt_fifo_item_t xbt_fifo_push(xbt_fifo_t l, void *t)
43 new = xbt_fifo_newitem();
46 xbt_fifo_push_item(l,new);
54 void *xbt_fifo_pop(xbt_fifo_t l)
59 item = xbt_fifo_pop_item(l);
60 if(item==NULL) return NULL;
62 content = item->content;
63 xbt_fifo_freeitem(item);
71 xbt_fifo_item_t xbt_fifo_unshift(xbt_fifo_t l, void *t)
75 new = xbt_fifo_newitem();
77 xbt_fifo_unshift_item(l,new);
85 void *xbt_fifo_shift(xbt_fifo_t l)
90 item = xbt_fifo_shift_item(l);
91 if(l==NULL) return NULL;
93 content = item->content;
94 xbt_fifo_freeitem(item);
99 * xbt_fifo_push_item()
102 void xbt_fifo_push_item(xbt_fifo_t l, xbt_fifo_item_t new)
105 if (l->head == NULL) {
111 new->prev->next = new;
116 * xbt_fifo_pop_item()
119 xbt_fifo_item_t xbt_fifo_pop_item(xbt_fifo_t l)
121 xbt_fifo_item_t item;
128 l->tail = item->prev;
132 l->tail->next = NULL;
139 * xbt_fifo_unshift_item()
142 void xbt_fifo_unshift_item(xbt_fifo_t l, xbt_fifo_item_t new)
145 if (l->head == NULL) {
151 new->next->prev = new;
157 * xbt_fifo_shift_item()
160 xbt_fifo_item_t xbt_fifo_shift_item(xbt_fifo_t l)
162 xbt_fifo_item_t item;
169 l->head = item->next;
173 l->head->prev = NULL;
181 * removes an xbt_fifo_item_t using its content from the xbt_fifo
183 void xbt_fifo_remove(xbt_fifo_t l, void *t)
185 xbt_fifo_item_t current, current_next;
188 for (current = l->head; current; current = current_next) {
189 current_next = current->next;
190 if (current->content != t)
192 /* remove the item */
193 xbt_fifo_remove_item(l, current);
194 xbt_fifo_freeitem(current);
195 /* WILL NOT REMOVE DUPLICATES */
202 * xbt_fifo_remove_item()
203 * removes a given xbt_fifo_item_t from the xbt_fifo
205 void xbt_fifo_remove_item(xbt_fifo_t l, xbt_fifo_item_t current)
207 if (l->head == l->tail) { /* special case */
214 if (current == l->head) { /* It's the head */
215 l->head = current->next;
216 l->head->prev = NULL;
217 } else if (current == l->tail) { /* It's the tail */
218 l->tail = current->prev;
219 l->tail->next = NULL;
220 } else { /* It's in the middle */
221 current->prev->next = current->next;
222 current->next->prev = current->prev;
230 int xbt_fifo_is_in(xbt_fifo_t f, void *content)
232 xbt_fifo_item_t item = xbt_fifo_getFirstitem(f);
234 if (item->content == content)
242 * xbt_fifo_to_array()
244 void **xbt_fifo_to_array(xbt_fifo_t f)
253 array = xbt_new0(void *, f->count);
255 for (i = 0, b = xbt_fifo_getFirstitem(f); b; i++, b = b->next) {
256 array[i] = b->content;
264 xbt_fifo_t xbt_fifo_copy(xbt_fifo_t f)
266 xbt_fifo_t copy = NULL;
269 copy = xbt_fifo_new();
271 for (b = xbt_fifo_getFirstitem(f); b; b = b->next) {
272 xbt_fifo_push(copy, b->content);
280 xbt_fifo_item_t xbt_fifo_newitem(void)
282 return xbt_new0(struct xbt_fifo_item,1);
285 void xbt_fifo_set_item_content(xbt_fifo_item_t i , void *v)
287 xbt_fifo_setItemcontent(i,v);
290 void *xbt_fifo_get_item_content(xbt_fifo_item_t i)
292 return xbt_fifo_getItemcontent(i);
296 * xbt_fifo_freeitem()
298 void xbt_fifo_freeitem(xbt_fifo_item_t b)
304 int xbt_fifo_size(xbt_fifo_t f)