1 /* Copyright (c) 2004-2014. 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 "src/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);
33 * \param l poor victim
35 * Free the fifo structure. None of the objects that was in the fifo is however modified.
37 void xbt_fifo_free(xbt_fifo_t l)
44 * \brief Makes a fifo empty.
47 * None of the objects that was in the fifo is however modified.
49 void xbt_fifo_reset(xbt_fifo_t l)
51 xbt_fifo_item_t b, tmp;
53 for (b = xbt_fifo_get_first_item(l); b;
54 tmp = b, b = b->next, xbt_fifo_free_item(tmp));
55 l->head = l->tail = NULL;
61 * \return the bucket that was just added
63 * Add an object at the tail of the list
65 xbt_fifo_item_t xbt_fifo_push(xbt_fifo_t l, void *t)
69 new = xbt_fifo_new_item();
72 xbt_fifo_push_item(l, new);
78 * \returns the object stored at the tail of the list.
80 * Removes and returns the object stored at the tail of the list.
81 * Returns NULL if the list is empty.
83 void *xbt_fifo_pop(xbt_fifo_t l)
90 if (!(item = xbt_fifo_pop_item(l)))
93 content = item->content;
94 xbt_fifo_free_item(item);
101 * \return the bucket that was just added
103 * Add an object at the head of the list
105 xbt_fifo_item_t xbt_fifo_unshift(xbt_fifo_t l, void *t)
109 new = xbt_fifo_new_item();
111 xbt_fifo_unshift_item(l, new);
117 * \returns the object stored at the head of the list.
119 * Removes and returns the object stored at the head of the list.
120 * Returns NULL if the list is empty.
122 void *xbt_fifo_shift(xbt_fifo_t l)
124 xbt_fifo_item_t item;
129 if (!(item = xbt_fifo_shift_item(l)))
132 content = item->content;
133 xbt_fifo_free_item(item);
141 * Hook up this bucket at the tail of the list
143 void xbt_fifo_push_item(xbt_fifo_t l, xbt_fifo_item_t new)
145 xbt_assert((new->next == NULL) && (new->prev == NULL), "Invalid item!");
147 if (l->head == NULL) {
153 new->prev->next = new;
159 * \returns the bucket that was at the tail of the list.
161 * Returns NULL if the list was empty.
163 xbt_fifo_item_t xbt_fifo_pop_item(xbt_fifo_t l)
165 xbt_fifo_item_t item;
172 l->tail = item->prev;
176 l->tail->next = NULL;
189 * Hook up this bucket at the head of the list
191 void xbt_fifo_unshift_item(xbt_fifo_t l, xbt_fifo_item_t new)
193 xbt_assert((new->next == NULL) && (new->prev == NULL), "Invalid item!");
195 if (l->head == NULL) {
201 new->next->prev = new;
208 * \returns the bucket that was at the head of the list.
210 * Returns NULL if the list was empty.
212 xbt_fifo_item_t xbt_fifo_shift_item(xbt_fifo_t l)
214 xbt_fifo_item_t item;
221 l->head = item->next;
225 l->head->prev = NULL;
238 * removes the first occurence of \a t from \a l.
239 * \warning it will not remove duplicates
240 * \return 1 if an item was removed and 0 otherwise.
242 int xbt_fifo_remove(xbt_fifo_t l, void *t)
244 xbt_fifo_item_t current, current_next;
246 for (current = l->head; current; current = current_next) {
247 current_next = current->next;
248 if (current->content != t)
250 /* remove the item */
251 xbt_fifo_remove_item(l, current);
252 xbt_fifo_free_item(current);
253 /* WILL NOT REMOVE DUPLICATES */
263 * removes all occurences of \a t from \a l.
264 * \return 1 if an item was removed and 0 otherwise.
266 int xbt_fifo_remove_all(xbt_fifo_t l, void *t)
268 xbt_fifo_item_t current, current_next;
271 for (current = l->head; current; current = current_next) {
272 current_next = current->next;
273 if (current->content != t)
275 /* remove the item */
276 xbt_fifo_remove_item(l, current);
277 xbt_fifo_free_item(current);
285 * \param current a bucket
287 * removes a bucket \a current from the list \a l. This function implicitely
288 * assumes (and doesn't check!) that this item belongs to this list...
290 void xbt_fifo_remove_item(xbt_fifo_t l, xbt_fifo_item_t current)
292 if (l->head == l->tail) { /* special case */
293 xbt_assert((current == l->head), "This item is not in the list!");
297 current->prev = current->next = NULL;
301 if (current == l->head) { /* It's the head */
302 l->head = current->next;
303 l->head->prev = NULL;
304 } else if (current == l->tail) { /* It's the tail */
305 l->tail = current->prev;
306 l->tail->next = NULL;
307 } else { /* It's in the middle */
308 current->prev->next = current->next;
309 current->next->prev = current->prev;
312 current->prev = current->next = NULL;
317 * \param content an object
318 * \return 1 if \a content is in \a f.
320 int xbt_fifo_is_in(xbt_fifo_t f, void *content)
322 xbt_fifo_item_t item = xbt_fifo_get_first_item(f);
324 if (item->content == content)
332 * @brief Search the given element in the fifo using a comparison function
334 * This function allows to search an item with a user provided function instead
335 * of the pointer comparison used elsewhere in this module. Assume for example that you have a fifo of
336 * strings. You cannot use xbt_fifo_remove() to remove, say, "TOTO" from it because internally, xbt_fifo_remove()
337 * will do something like "if (item->content == "toto"), then remove it". And the pointer to the item content and the
338 * pointer to "toto" will never match. As a solution, the current function provides a way to search elements that are
339 * semantically equivalent instead of only syntactically. So, removing "Toto" from a fifo can be achieved this way:
342 int my_comparison_function(void *searched, void *seen) {
343 return !strcmp(searched, seen);
346 xbt_fifo_remove_item(fifo, xbt_fifo_search_item(fifo, my_comparison_function, "Toto"));
349 * \param f a fifo list
350 * \param cmp_fun the comparison function. Prototype: void *a,void *b -> int. Semantic: returns true iff a=b
351 * @param closure the element to search. It will be provided as first argument to each call of cmp_fun
352 * \return the first item matching the comparison function, or NULL if no such item exists
354 xbt_fifo_item_t xbt_fifo_search_item(xbt_fifo_t f, int_f_pvoid_pvoid_t cmp_fun, void *closure) {
355 xbt_fifo_item_t item = xbt_fifo_get_first_item(f);
357 if (cmp_fun(closure, item->content))
366 * \return a table with the objects stored in \a f.
368 void **xbt_fifo_to_array(xbt_fifo_t f)
377 array = xbt_new0(void *, f->count);
379 for (i = 0, b = xbt_fifo_get_first_item(f); b; i++, b = b->next) {
380 array[i] = b->content;
387 * \return a copy of \a f.
389 xbt_fifo_t xbt_fifo_copy(xbt_fifo_t f)
391 xbt_fifo_t copy = NULL;
394 copy = xbt_fifo_new();
396 for (b = xbt_fifo_get_first_item(f); b; b = b->next) {
397 xbt_fifo_push(copy, b->content);
402 /* Functions passed to the mallocator constructor */
403 static void *fifo_item_mallocator_new_f(void)
405 return xbt_new(s_xbt_fifo_item_t, 1);
408 static void fifo_item_mallocator_reset_f(void *item)
410 /* memset to zero like calloc */
411 memset(item, 0, sizeof(s_xbt_fifo_item_t));
415 * \return a new bucket
417 inline xbt_fifo_item_t xbt_fifo_new_item(void)
419 return xbt_mallocator_get(item_mallocator);
422 /** \deprecated Use #xbt_fifo_new_item instead.
424 inline xbt_fifo_item_t xbt_fifo_newitem(void)
426 XBT_CWARN(xbt_fifo, "This function is deprecated. Use xbt_fifo_new_item.");
427 return xbt_fifo_new_item();
434 * stores \a v in \a i.
436 inline void xbt_fifo_set_item_content(xbt_fifo_item_t i, void *v)
438 xbt_fifo_setItemcontent(i, v);
443 * \return the object stored \a i.
445 inline void *xbt_fifo_get_item_content(xbt_fifo_item_t i)
447 return xbt_fifo_getItemcontent(i);
451 * \param b poor victim
453 * Free the bucket but does not modifies the object (if any) that was stored in it.
455 inline void xbt_fifo_free_item(xbt_fifo_item_t b)
457 xbt_mallocator_release(item_mallocator, b);
462 * \deprecated Use #xbt_fifo_free_item instead.
464 inline void xbt_fifo_freeitem(xbt_fifo_item_t b)
466 XBT_CWARN(xbt_fifo, "This function is deprecated. Use xbt_fifo_free_item.");
467 xbt_fifo_free_item(b);
473 * \return the number of buckets in \a f.
475 inline int xbt_fifo_size(xbt_fifo_t f)
482 * \return the head of \a l.
484 * Returns NULL if the list is empty.
486 inline xbt_fifo_item_t xbt_fifo_get_first_item(xbt_fifo_t l)
493 * \return the tail of \a l.
495 * Returns NULL if the list is empty.
497 inline xbt_fifo_item_t xbt_fifo_get_last_item(xbt_fifo_t l)
502 /** \deprecated Use #xbt_fifo_get_first_item instead.
504 inline xbt_fifo_item_t xbt_fifo_getFirstItem(xbt_fifo_t l)
506 XBT_CWARN(xbt_fifo, "This function is deprecated. Use xbt_fifo_get_first_item.");
507 return xbt_fifo_get_first_item(l);
512 * \return the bucket that comes next
514 * Returns NULL if \a i is the tail of the list.
516 inline xbt_fifo_item_t xbt_fifo_get_next_item(xbt_fifo_item_t i)
523 /** \deprecated Use #xbt_fifo_get_next_item instead.
525 xbt_fifo_item_t xbt_fifo_getNextItem(xbt_fifo_item_t i)
527 XBT_CWARN(xbt_fifo, "This function is deprecated. Use xbt_fifo_get_next_item.");
528 return xbt_fifo_get_next_item(i);
533 * \return the bucket that is just before \a i.
535 * Returns NULL if \a i is the head of the list.
537 inline xbt_fifo_item_t xbt_fifo_get_prev_item(xbt_fifo_item_t i)
544 /** \deprecated Use #xbt_fifo_get_prev_item instead.
546 xbt_fifo_item_t xbt_fifo_getPrevItem(xbt_fifo_item_t i)
548 XBT_WARN("This function is deprecated. Use xbt_fifo_get_prev_item.");
549 return xbt_fifo_get_prev_item(i);
552 /* Module init/exit handling the fifo item mallocator
553 * These are internal XBT functions called by xbt_preinit/postexit().
554 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
556 void xbt_fifo_preinit(void)
558 item_mallocator = xbt_mallocator_new(65536, fifo_item_mallocator_new_f,
559 fifo_item_mallocator_free_f, fifo_item_mallocator_reset_f);
562 void xbt_fifo_postexit(void)
564 if (item_mallocator != NULL) {
565 xbt_mallocator_free(item_mallocator);
566 item_mallocator = NULL;