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);
34 * \param l poor victim
36 * Free the fifo structure. None of the objects that was in the fifo is however modified.
38 void xbt_fifo_free(xbt_fifo_t l)
40 xbt_fifo_item_t b, tmp;
42 for (b = xbt_fifo_get_first_item(l); b;
43 tmp = b, b = b->next, xbt_fifo_free_item(tmp));
51 * \return the bucket that was just added
53 * Add an object at the tail of the list
55 xbt_fifo_item_t xbt_fifo_push(xbt_fifo_t l, void *t)
59 new = xbt_fifo_new_item();
62 xbt_fifo_push_item(l, new);
68 * \returns the object stored at the tail of the list.
70 * Removes and returns the object stored at the tail of the list.
71 * Returns NULL if the list is empty.
73 void *xbt_fifo_pop(xbt_fifo_t l)
80 if (!(item = xbt_fifo_pop_item(l)))
83 content = item->content;
84 xbt_fifo_free_item(item);
91 * \return the bucket that was just added
93 * Add an object at the head of the list
95 xbt_fifo_item_t xbt_fifo_unshift(xbt_fifo_t l, void *t)
99 new = xbt_fifo_new_item();
101 xbt_fifo_unshift_item(l, new);
107 * \returns the object stored at the head of the list.
109 * Removes and returns the object stored at the head of the list.
110 * Returns NULL if the list is empty.
112 void *xbt_fifo_shift(xbt_fifo_t l)
114 xbt_fifo_item_t item;
119 if (!(item = xbt_fifo_shift_item(l)))
122 content = item->content;
123 xbt_fifo_free_item(item);
131 * Hook up this bucket at the tail of the list
133 void xbt_fifo_push_item(xbt_fifo_t l, xbt_fifo_item_t new)
135 xbt_assert0((new->next == NULL) && (new->prev == NULL), "Invalid item!");
137 if (l->head == NULL) {
143 new->prev->next = new;
149 * \returns the bucket that was at the tail of the list.
151 * Returns NULL if the list was empty.
153 xbt_fifo_item_t xbt_fifo_pop_item(xbt_fifo_t l)
155 xbt_fifo_item_t item;
162 l->tail = item->prev;
166 l->tail->next = NULL;
179 * Hook up this bucket at the head of the list
181 void xbt_fifo_unshift_item(xbt_fifo_t l, xbt_fifo_item_t new)
183 xbt_assert0((new->next == NULL) && (new->prev == NULL), "Invalid item!");
185 if (l->head == NULL) {
191 new->next->prev = new;
198 * \returns the bucket that was at the head of the list.
200 * Returns NULL if the list was empty.
202 xbt_fifo_item_t xbt_fifo_shift_item(xbt_fifo_t l)
204 xbt_fifo_item_t item;
211 l->head = item->next;
215 l->head->prev = NULL;
228 * removes the first occurence of \a t from \a l.
229 * \warning it will not remove duplicates
230 * \return 1 if an item was removed and 0 otherwise.
232 int xbt_fifo_remove(xbt_fifo_t l, void *t)
234 xbt_fifo_item_t current, current_next;
237 for (current = l->head; current; current = current_next) {
238 current_next = current->next;
239 if (current->content != t)
241 /* remove the item */
242 xbt_fifo_remove_item(l, current);
243 xbt_fifo_free_item(current);
244 /* WILL NOT REMOVE DUPLICATES */
255 * removes all occurences of \a t from \a l.
256 * \return 1 if an item was removed and 0 otherwise.
258 int xbt_fifo_remove_all(xbt_fifo_t l, void *t)
260 xbt_fifo_item_t current, current_next;
263 for (current = l->head; current; current = current_next) {
264 current_next = current->next;
265 if (current->content != t)
267 /* remove the item */
268 xbt_fifo_remove_item(l, current);
269 xbt_fifo_free_item(current);
277 * \param current a bucket
279 * removes a bucket \a current from the list \a l. This function implicitely
280 * assumes (and doesn't check!) that this item belongs to this list...
282 void xbt_fifo_remove_item(xbt_fifo_t l, xbt_fifo_item_t current)
284 if (l->head == l->tail) { /* special case */
285 xbt_assert0((current == l->head), "This item is not in the list!");
289 current->prev = current->next = NULL;
293 if (current == l->head) { /* It's the head */
294 l->head = current->next;
295 l->head->prev = NULL;
296 } else if (current == l->tail) { /* It's the tail */
297 l->tail = current->prev;
298 l->tail->next = NULL;
299 } else { /* It's in the middle */
300 current->prev->next = current->next;
301 current->next->prev = current->prev;
304 current->prev = current->next = NULL;
309 * \param content an object
310 * \return 1 if \a content is in \a f.
312 int xbt_fifo_is_in(xbt_fifo_t f, void *content)
314 xbt_fifo_item_t item = xbt_fifo_get_first_item(f);
316 if (item->content == content)
325 * \return a table with the objects stored in \a f.
327 void **xbt_fifo_to_array(xbt_fifo_t f)
336 array = xbt_new0(void *, f->count);
338 for (i = 0, b = xbt_fifo_get_first_item(f); b; i++, b = b->next) {
339 array[i] = b->content;
346 * \return a copy of \a f.
348 xbt_fifo_t xbt_fifo_copy(xbt_fifo_t f)
350 xbt_fifo_t copy = NULL;
353 copy = xbt_fifo_new();
355 for (b = xbt_fifo_get_first_item(f); b; b = b->next) {
356 xbt_fifo_push(copy, b->content);
361 /* Functions passed to the mallocator constructor */
362 static void *fifo_item_mallocator_new_f(void)
364 return xbt_new(s_xbt_fifo_item_t, 1);
367 static void fifo_item_mallocator_free_f(void *item)
372 static void fifo_item_mallocator_reset_f(void *item)
374 /* memset to zero like calloc */
375 memset(item, 0, sizeof(s_xbt_fifo_item_t));
379 * \return a new bucket
381 XBT_INLINE xbt_fifo_item_t xbt_fifo_new_item(void)
383 return xbt_mallocator_get(item_mallocator);
386 /** \deprecated Use #xbt_fifo_new_item instead.
388 XBT_INLINE xbt_fifo_item_t xbt_fifo_newitem(void)
390 WARN0("This function is deprecated. Use xbt_fifo_new_item.");
391 return xbt_fifo_new_item();
398 * stores \a v in \a i.
400 XBT_INLINE void xbt_fifo_set_item_content(xbt_fifo_item_t i, void *v)
402 xbt_fifo_setItemcontent(i, v);
407 * \return the object stored \a i.
409 XBT_INLINE void *xbt_fifo_get_item_content(xbt_fifo_item_t i)
411 return xbt_fifo_getItemcontent(i);
415 * \param b poor victim
417 * Free the bucket but does not modifies the object (if any) that was stored in it.
419 XBT_INLINE void xbt_fifo_free_item(xbt_fifo_item_t b)
421 xbt_mallocator_release(item_mallocator, b);
426 * \deprecated Use #xbt_fifo_free_item instead.
428 XBT_INLINE void xbt_fifo_freeitem(xbt_fifo_item_t b)
430 WARN0("This function is deprecated. Use xbt_fifo_free_item.");
431 xbt_fifo_free_item(b);
437 * \return the number of buckets in \a f.
439 XBT_INLINE int xbt_fifo_size(xbt_fifo_t f)
446 * \return the head of \a l.
448 XBT_INLINE xbt_fifo_item_t xbt_fifo_get_first_item(xbt_fifo_t l)
455 * \return the tail of \a l.
457 XBT_INLINE xbt_fifo_item_t xbt_fifo_get_last_item(xbt_fifo_t l)
462 /** \deprecated Use #xbt_fifo_get_first_item instead.
464 XBT_INLINE xbt_fifo_item_t xbt_fifo_getFirstItem(xbt_fifo_t l)
466 WARN0("This function is deprecated. Use xbt_fifo_get_first_item.");
467 return xbt_fifo_get_first_item(l);
472 * \return the bucket that comes next
474 XBT_INLINE xbt_fifo_item_t xbt_fifo_get_next_item(xbt_fifo_item_t i)
481 /** \deprecated Use #xbt_fifo_get_next_item instead.
483 xbt_fifo_item_t xbt_fifo_getNextItem(xbt_fifo_item_t i)
485 WARN0("This function is deprecated. Use xbt_fifo_get_next_item.");
486 return xbt_fifo_get_next_item(i);
491 * \return the bucket that is just before \a i.
493 XBT_INLINE xbt_fifo_item_t xbt_fifo_get_prev_item(xbt_fifo_item_t i)
500 /** \deprecated Use #xbt_fifo_get_prev_item instead.
502 xbt_fifo_item_t xbt_fifo_getPrevItem(xbt_fifo_item_t i)
504 WARN0("This function is deprecated. Use xbt_fifo_get_prev_item.");
505 return xbt_fifo_get_prev_item(i);
508 /* Module init/exit handling the fifo item mallocator
509 * These are internal XBT functions called by xbt_preinit/postexit().
510 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
512 void xbt_fifo_preinit(void) {
513 if (item_mallocator != NULL) {
514 /* Already created. I guess we want to switch to MC mode, so kill the previously created mallocator */
515 xbt_mallocator_free(item_mallocator);
518 item_mallocator = xbt_mallocator_new(256,
519 fifo_item_mallocator_new_f,
520 fifo_item_mallocator_free_f,
521 fifo_item_mallocator_reset_f);
524 void xbt_fifo_postexit(void) {
525 if (item_mallocator != NULL) {
526 xbt_mallocator_free(item_mallocator);
527 item_mallocator = NULL;