1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "xbt/sysdep.h"
9 #include "xbt/mallocator.h"
10 #include "fifo_private.h"
11 #include "xbt_modinter.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_fifo, xbt, "FIFO");
15 static void *fifo_item_mallocator_new_f(void);
16 static void fifo_item_mallocator_free_f(void *item);
17 static void fifo_item_mallocator_reset_f(void *item);
19 static xbt_mallocator_t item_mallocator = NULL;
24 xbt_fifo_t xbt_fifo_new(void)
27 fifo = xbt_new0(struct xbt_fifo, 1);
29 if (item_mallocator == NULL) {
30 item_mallocator = xbt_mallocator_new(256,
31 fifo_item_mallocator_new_f,
32 fifo_item_mallocator_free_f,
33 fifo_item_mallocator_reset_f);
39 * \param l poor victim
41 * Free the fifo structure. None of the objects that was in the fifo is however modified.
43 void xbt_fifo_free(xbt_fifo_t l)
45 xbt_fifo_item_t b, tmp;
47 for (b = xbt_fifo_get_first_item(l); b;
48 tmp = b, b = b->next, xbt_fifo_free_item(tmp));
56 * \return the bucket that was just added
58 * Add an object at the tail of the list
60 xbt_fifo_item_t xbt_fifo_push(xbt_fifo_t l, void *t)
64 new = xbt_fifo_new_item();
67 xbt_fifo_push_item(l, new);
73 * \returns the object stored at the tail of the list.
75 * Removes and returns the object stored at the tail of the list.
76 * Returns NULL if the list is empty.
78 void *xbt_fifo_pop(xbt_fifo_t l)
85 if (!(item = xbt_fifo_pop_item(l)))
88 content = item->content;
89 xbt_fifo_free_item(item);
96 * \return the bucket that was just added
98 * Add an object at the head of the list
100 xbt_fifo_item_t xbt_fifo_unshift(xbt_fifo_t l, void *t)
104 new = xbt_fifo_new_item();
106 xbt_fifo_unshift_item(l, new);
112 * \returns the object stored at the head of the list.
114 * Removes and returns the object stored at the head of the list.
115 * Returns NULL if the list is empty.
117 void *xbt_fifo_shift(xbt_fifo_t l)
119 xbt_fifo_item_t item;
124 if (!(item = xbt_fifo_shift_item(l)))
127 content = item->content;
128 xbt_fifo_free_item(item);
136 * Hook up this bucket at the tail of the list
138 void xbt_fifo_push_item(xbt_fifo_t l, xbt_fifo_item_t new)
140 xbt_assert0((new->next == NULL) && (new->prev == NULL), "Invalid item!");
142 if (l->head == NULL) {
148 new->prev->next = new;
154 * \returns the bucket that was at the tail of the list.
156 * Returns NULL if the list was empty.
158 xbt_fifo_item_t xbt_fifo_pop_item(xbt_fifo_t l)
160 xbt_fifo_item_t item;
167 l->tail = item->prev;
171 l->tail->next = NULL;
184 * Hook up this bucket at the head of the list
186 void xbt_fifo_unshift_item(xbt_fifo_t l, xbt_fifo_item_t new)
188 xbt_assert0((new->next == NULL) && (new->prev == NULL), "Invalid item!");
190 if (l->head == NULL) {
196 new->next->prev = new;
203 * \returns the bucket that was at the head of the list.
205 * Returns NULL if the list was empty.
207 xbt_fifo_item_t xbt_fifo_shift_item(xbt_fifo_t l)
209 xbt_fifo_item_t item;
216 l->head = item->next;
220 l->head->prev = NULL;
233 * removes the first occurence of \a t from \a l.
234 * \warning it will not remove duplicates
235 * \return 1 if an item was removed and 0 otherwise.
237 int xbt_fifo_remove(xbt_fifo_t l, void *t)
239 xbt_fifo_item_t current, current_next;
242 for (current = l->head; current; current = current_next) {
243 current_next = current->next;
244 if (current->content != t)
246 /* remove the item */
247 xbt_fifo_remove_item(l, current);
248 xbt_fifo_free_item(current);
249 /* WILL NOT REMOVE DUPLICATES */
260 * removes all occurences of \a t from \a l.
261 * \return 1 if an item was removed and 0 otherwise.
263 int xbt_fifo_remove_all(xbt_fifo_t l, void *t)
265 xbt_fifo_item_t current, current_next;
268 for (current = l->head; current; current = current_next) {
269 current_next = current->next;
270 if (current->content != t)
272 /* remove the item */
273 xbt_fifo_remove_item(l, current);
274 xbt_fifo_free_item(current);
282 * \param current a bucket
284 * removes a bucket \a current from the list \a l. This function implicitely
285 * assumes (and doesn't check!) that this item belongs to this list...
287 void xbt_fifo_remove_item(xbt_fifo_t l, xbt_fifo_item_t current)
289 if (l->head == l->tail) { /* special case */
290 xbt_assert0((current == l->head), "This item is not in the list!");
294 current->prev = current->next = NULL;
298 if (current == l->head) { /* It's the head */
299 l->head = current->next;
300 l->head->prev = NULL;
301 } else if (current == l->tail) { /* It's the tail */
302 l->tail = current->prev;
303 l->tail->next = NULL;
304 } else { /* It's in the middle */
305 current->prev->next = current->next;
306 current->next->prev = current->prev;
309 current->prev = current->next = NULL;
314 * \param content an object
315 * \return 1 if \a content is in \a f.
317 int xbt_fifo_is_in(xbt_fifo_t f, void *content)
319 xbt_fifo_item_t item = xbt_fifo_get_first_item(f);
321 if (item->content == content)
330 * \return a table with the objects stored in \a f.
332 void **xbt_fifo_to_array(xbt_fifo_t f)
341 array = xbt_new0(void *, f->count);
343 for (i = 0, b = xbt_fifo_get_first_item(f); b; i++, b = b->next) {
344 array[i] = b->content;
351 * \return a copy of \a f.
353 xbt_fifo_t xbt_fifo_copy(xbt_fifo_t f)
355 xbt_fifo_t copy = NULL;
358 copy = xbt_fifo_new();
360 for (b = xbt_fifo_get_first_item(f); b; b = b->next) {
361 xbt_fifo_push(copy, b->content);
366 /* Functions passed to the mallocator constructor */
367 static void *fifo_item_mallocator_new_f(void)
369 return xbt_new(s_xbt_fifo_item_t, 1);
372 static void fifo_item_mallocator_free_f(void *item)
377 static void fifo_item_mallocator_reset_f(void *item)
379 /* memset to zero like calloc */
380 memset(item, 0, sizeof(s_xbt_fifo_item_t));
384 * \return a new bucket
386 XBT_INLINE xbt_fifo_item_t xbt_fifo_new_item(void)
388 return xbt_mallocator_get(item_mallocator);
391 /** \deprecated Use #xbt_fifo_new_item instead.
393 XBT_INLINE xbt_fifo_item_t xbt_fifo_newitem(void)
395 WARN0("This function is deprecated. Use xbt_fifo_new_item.");
396 return xbt_fifo_new_item();
403 * stores \a v in \a i.
405 XBT_INLINE void xbt_fifo_set_item_content(xbt_fifo_item_t i, void *v)
407 xbt_fifo_setItemcontent(i, v);
412 * \return the object stored \a i.
414 XBT_INLINE void *xbt_fifo_get_item_content(xbt_fifo_item_t i)
416 return xbt_fifo_getItemcontent(i);
420 * \param b poor victim
422 * Free the bucket but does not modifies the object (if any) that was stored in it.
424 XBT_INLINE void xbt_fifo_free_item(xbt_fifo_item_t b)
426 xbt_mallocator_release(item_mallocator, b);
431 * \deprecated Use #xbt_fifo_free_item instead.
433 XBT_INLINE void xbt_fifo_freeitem(xbt_fifo_item_t b)
435 WARN0("This function is deprecated. Use xbt_fifo_free_item.");
436 xbt_fifo_free_item(b);
442 * \return the number of buckets in \a f.
444 XBT_INLINE int xbt_fifo_size(xbt_fifo_t f)
451 * \return the head of \a l.
453 XBT_INLINE xbt_fifo_item_t xbt_fifo_get_first_item(xbt_fifo_t l)
460 * \return the tail of \a l.
462 XBT_INLINE xbt_fifo_item_t xbt_fifo_get_last_item(xbt_fifo_t l)
467 /** \deprecated Use #xbt_fifo_get_first_item instead.
469 XBT_INLINE xbt_fifo_item_t xbt_fifo_getFirstItem(xbt_fifo_t l)
471 WARN0("This function is deprecated. Use xbt_fifo_get_first_item.");
472 return xbt_fifo_get_first_item(l);
477 * \return the bucket that comes next
479 XBT_INLINE xbt_fifo_item_t xbt_fifo_get_next_item(xbt_fifo_item_t i)
486 /** \deprecated Use #xbt_fifo_get_next_item instead.
488 xbt_fifo_item_t xbt_fifo_getNextItem(xbt_fifo_item_t i)
490 WARN0("This function is deprecated. Use xbt_fifo_get_next_item.");
491 return xbt_fifo_get_next_item(i);
496 * \return the bucket that is just before \a i.
498 XBT_INLINE xbt_fifo_item_t xbt_fifo_get_prev_item(xbt_fifo_item_t i)
505 /** \deprecated Use #xbt_fifo_get_prev_item instead.
507 xbt_fifo_item_t xbt_fifo_getPrevItem(xbt_fifo_item_t i)
509 WARN0("This function is deprecated. Use xbt_fifo_get_prev_item.");
510 return xbt_fifo_get_prev_item(i);
514 * Destroy the fifo item mallocator.
515 * This is an internal XBT function called by xbt_exit().
517 void xbt_fifo_exit(void)
519 if (item_mallocator != NULL) {
520 xbt_mallocator_free(item_mallocator);
521 item_mallocator = NULL;