1 /* Copyright (c) 2004-2012. 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 #define fifo_item_mallocator_free_f xbt_free_f
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)
45 * \brief Makes a fifo empty.
48 * None of the objects that was in the fifo is however modified.
50 void xbt_fifo_reset(xbt_fifo_t l)
52 xbt_fifo_item_t b, tmp;
54 for (b = xbt_fifo_get_first_item(l); b;
55 tmp = b, b = b->next, xbt_fifo_free_item(tmp));
56 l->head = l->tail = NULL;
62 * \return the bucket that was just added
64 * Add an object at the tail of the list
66 xbt_fifo_item_t xbt_fifo_push(xbt_fifo_t l, void *t)
70 new = xbt_fifo_new_item();
73 xbt_fifo_push_item(l, new);
79 * \returns the object stored at the tail of the list.
81 * Removes and returns the object stored at the tail of the list.
82 * Returns NULL if the list is empty.
84 void *xbt_fifo_pop(xbt_fifo_t l)
91 if (!(item = xbt_fifo_pop_item(l)))
94 content = item->content;
95 xbt_fifo_free_item(item);
102 * \return the bucket that was just added
104 * Add an object at the head of the list
106 xbt_fifo_item_t xbt_fifo_unshift(xbt_fifo_t l, void *t)
110 new = xbt_fifo_new_item();
112 xbt_fifo_unshift_item(l, new);
118 * \returns the object stored at the head of the list.
120 * Removes and returns the object stored at the head of the list.
121 * Returns NULL if the list is empty.
123 void *xbt_fifo_shift(xbt_fifo_t l)
125 xbt_fifo_item_t item;
130 if (!(item = xbt_fifo_shift_item(l)))
133 content = item->content;
134 xbt_fifo_free_item(item);
142 * Hook up this bucket at the tail of the list
144 void xbt_fifo_push_item(xbt_fifo_t l, xbt_fifo_item_t new)
146 xbt_assert((new->next == NULL) && (new->prev == NULL), "Invalid item!");
148 if (l->head == NULL) {
154 new->prev->next = new;
160 * \returns the bucket that was at the tail of the list.
162 * Returns NULL if the list was empty.
164 xbt_fifo_item_t xbt_fifo_pop_item(xbt_fifo_t l)
166 xbt_fifo_item_t item;
173 l->tail = item->prev;
177 l->tail->next = NULL;
190 * Hook up this bucket at the head of the list
192 void xbt_fifo_unshift_item(xbt_fifo_t l, xbt_fifo_item_t new)
194 xbt_assert((new->next == NULL) && (new->prev == NULL), "Invalid item!");
196 if (l->head == NULL) {
202 new->next->prev = new;
209 * \returns the bucket that was at the head of the list.
211 * Returns NULL if the list was empty.
213 xbt_fifo_item_t xbt_fifo_shift_item(xbt_fifo_t l)
215 xbt_fifo_item_t item;
222 l->head = item->next;
226 l->head->prev = NULL;
239 * removes the first occurence of \a t from \a l.
240 * \warning it will not remove duplicates
241 * \return 1 if an item was removed and 0 otherwise.
243 int xbt_fifo_remove(xbt_fifo_t l, void *t)
245 xbt_fifo_item_t current, current_next;
248 for (current = l->head; current; current = current_next) {
249 current_next = current->next;
250 if (current->content != t)
252 /* remove the item */
253 xbt_fifo_remove_item(l, current);
254 xbt_fifo_free_item(current);
255 /* WILL NOT REMOVE DUPLICATES */
266 * removes all occurences of \a t from \a l.
267 * \return 1 if an item was removed and 0 otherwise.
269 int xbt_fifo_remove_all(xbt_fifo_t l, void *t)
271 xbt_fifo_item_t current, current_next;
274 for (current = l->head; current; current = current_next) {
275 current_next = current->next;
276 if (current->content != t)
278 /* remove the item */
279 xbt_fifo_remove_item(l, current);
280 xbt_fifo_free_item(current);
288 * \param current a bucket
290 * removes a bucket \a current from the list \a l. This function implicitely
291 * assumes (and doesn't check!) that this item belongs to this list...
293 void xbt_fifo_remove_item(xbt_fifo_t l, xbt_fifo_item_t current)
295 if (l->head == l->tail) { /* special case */
296 xbt_assert((current == l->head), "This item is not in the list!");
300 current->prev = current->next = NULL;
304 if (current == l->head) { /* It's the head */
305 l->head = current->next;
306 l->head->prev = NULL;
307 } else if (current == l->tail) { /* It's the tail */
308 l->tail = current->prev;
309 l->tail->next = NULL;
310 } else { /* It's in the middle */
311 current->prev->next = current->next;
312 current->next->prev = current->prev;
315 current->prev = current->next = NULL;
320 * \param content an object
321 * \return 1 if \a content is in \a f.
323 int xbt_fifo_is_in(xbt_fifo_t f, void *content)
325 xbt_fifo_item_t item = xbt_fifo_get_first_item(f);
327 if (item->content == content)
335 * @brief Search the given element in the fifo using a comparison function
337 * This function allows to search an item with a user provided function instead
338 * of the pointer comparison used elsewhere in this module. Assume for example that you have a fifo of
339 * strings. You cannot use xbt_fifo_remove() to remove, say, "TOTO" from it because internally, xbt_fifo_remove()
340 * will do something like "if (item->content == "toto"), then remove it". And the pointer to the item content
341 * and the pointer to "toto" will never match. As a solution, the current function provides a way to search elements
342 * that are semanticaly equivalent instead of only syntaxically. So, removing "Toto" from a fifo can be
346 int my_comparison_function(void *searched, void *seen) {
347 return !strcmp(searched, seen);
350 xbt_fifo_remove_item(fifo,
351 xbt_fifo_search_item(fifo, my_comparison_function, "Toto"));
354 * \param f a fifo list
355 * \param cmp_fun the comparison function. Prototype: void *a,void *b -> int. Semantic: returns true iff a=b
356 * @param closure the element to search. It will be provided as first argument to each call of cmp_fun
357 * \return the first item matching the comparison function, or NULL if no such item exists
359 xbt_fifo_item_t xbt_fifo_search_item(xbt_fifo_t f, int_f_pvoid_pvoid_t cmp_fun, void *closure) {
360 xbt_fifo_item_t item = xbt_fifo_get_first_item(f);
362 if (cmp_fun(closure, item->content))
372 * \return a table with the objects stored in \a f.
374 void **xbt_fifo_to_array(xbt_fifo_t f)
383 array = xbt_new0(void *, f->count);
385 for (i = 0, b = xbt_fifo_get_first_item(f); b; i++, b = b->next) {
386 array[i] = b->content;
393 * \return a copy of \a f.
395 xbt_fifo_t xbt_fifo_copy(xbt_fifo_t f)
397 xbt_fifo_t copy = NULL;
400 copy = xbt_fifo_new();
402 for (b = xbt_fifo_get_first_item(f); b; b = b->next) {
403 xbt_fifo_push(copy, b->content);
408 /* Functions passed to the mallocator constructor */
409 static void *fifo_item_mallocator_new_f(void)
411 return xbt_new(s_xbt_fifo_item_t, 1);
414 static void fifo_item_mallocator_reset_f(void *item)
416 /* memset to zero like calloc */
417 memset(item, 0, sizeof(s_xbt_fifo_item_t));
421 * \return a new bucket
423 XBT_INLINE xbt_fifo_item_t xbt_fifo_new_item(void)
425 return xbt_mallocator_get(item_mallocator);
428 /** \deprecated Use #xbt_fifo_new_item instead.
430 XBT_INLINE xbt_fifo_item_t xbt_fifo_newitem(void)
432 XBT_CWARN(xbt_fifo, "This function is deprecated. Use xbt_fifo_new_item.");
433 return xbt_fifo_new_item();
440 * stores \a v in \a i.
442 XBT_INLINE void xbt_fifo_set_item_content(xbt_fifo_item_t i, void *v)
444 xbt_fifo_setItemcontent(i, v);
449 * \return the object stored \a i.
451 XBT_INLINE void *xbt_fifo_get_item_content(xbt_fifo_item_t i)
453 return xbt_fifo_getItemcontent(i);
457 * \param b poor victim
459 * Free the bucket but does not modifies the object (if any) that was stored in it.
461 XBT_INLINE void xbt_fifo_free_item(xbt_fifo_item_t b)
463 xbt_mallocator_release(item_mallocator, b);
468 * \deprecated Use #xbt_fifo_free_item instead.
470 XBT_INLINE void xbt_fifo_freeitem(xbt_fifo_item_t b)
472 XBT_CWARN(xbt_fifo, "This function is deprecated. Use xbt_fifo_free_item.");
473 xbt_fifo_free_item(b);
479 * \return the number of buckets in \a f.
481 XBT_INLINE int xbt_fifo_size(xbt_fifo_t f)
488 * \return the head of \a l.
490 * Returns NULL if the list is empty.
492 XBT_INLINE xbt_fifo_item_t xbt_fifo_get_first_item(xbt_fifo_t l)
499 * \return the tail of \a l.
501 * Returns NULL if the list is empty.
503 XBT_INLINE xbt_fifo_item_t xbt_fifo_get_last_item(xbt_fifo_t l)
508 /** \deprecated Use #xbt_fifo_get_first_item instead.
510 XBT_INLINE xbt_fifo_item_t xbt_fifo_getFirstItem(xbt_fifo_t l)
512 XBT_CWARN(xbt_fifo, "This function is deprecated. Use xbt_fifo_get_first_item.");
513 return xbt_fifo_get_first_item(l);
518 * \return the bucket that comes next
520 * Returns NULL if \a i is the tail of the list.
522 XBT_INLINE xbt_fifo_item_t xbt_fifo_get_next_item(xbt_fifo_item_t i)
529 /** \deprecated Use #xbt_fifo_get_next_item instead.
531 xbt_fifo_item_t xbt_fifo_getNextItem(xbt_fifo_item_t i)
533 XBT_CWARN(xbt_fifo, "This function is deprecated. Use xbt_fifo_get_next_item.");
534 return xbt_fifo_get_next_item(i);
539 * \return the bucket that is just before \a i.
541 * Returns NULL if \a i is the head of the list.
543 XBT_INLINE xbt_fifo_item_t xbt_fifo_get_prev_item(xbt_fifo_item_t i)
550 /** \deprecated Use #xbt_fifo_get_prev_item instead.
552 xbt_fifo_item_t xbt_fifo_getPrevItem(xbt_fifo_item_t i)
554 XBT_WARN("This function is deprecated. Use xbt_fifo_get_prev_item.");
555 return xbt_fifo_get_prev_item(i);
558 /* Module init/exit handling the fifo item mallocator
559 * These are internal XBT functions called by xbt_preinit/postexit().
560 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
562 void xbt_fifo_preinit(void)
564 item_mallocator = xbt_mallocator_new(65536,
565 fifo_item_mallocator_new_f,
566 fifo_item_mallocator_free_f,
567 fifo_item_mallocator_reset_f);
570 void xbt_fifo_postexit(void)
572 if (item_mallocator != NULL) {
573 xbt_mallocator_free(item_mallocator);
574 item_mallocator = NULL;