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 = xbt_fifo_get_first_item(l);
54 xbt_fifo_item_t tmp = b;
56 xbt_fifo_free_item(tmp);
65 * \return the bucket that was just added
67 * Add an object at the tail of the list
69 xbt_fifo_item_t xbt_fifo_push(xbt_fifo_t l, void *t)
73 new = xbt_fifo_new_item();
76 xbt_fifo_push_item(l, new);
82 * \returns the object stored at the tail of the list.
84 * Removes and returns the object stored at the tail of the list.
85 * Returns NULL if the list is empty.
87 void *xbt_fifo_pop(xbt_fifo_t l)
93 xbt_fifo_item_t item = xbt_fifo_pop_item(l);
97 content = item->content;
98 xbt_fifo_free_item(item);
105 * \return the bucket that was just added
107 * Add an object at the head of the list
109 xbt_fifo_item_t xbt_fifo_unshift(xbt_fifo_t l, void *t)
113 new = xbt_fifo_new_item();
115 xbt_fifo_unshift_item(l, new);
121 * \returns the object stored at the head of the list.
123 * Removes and returns the object stored at the head of the list.
124 * Returns NULL if the list is empty.
126 void *xbt_fifo_shift(xbt_fifo_t l)
132 xbt_fifo_item_t item = xbt_fifo_shift_item(l);
136 content = item->content;
137 xbt_fifo_free_item(item);
145 * Hook up this bucket at the tail of the list
147 void xbt_fifo_push_item(xbt_fifo_t l, xbt_fifo_item_t new)
149 xbt_assert((new->next == NULL) && (new->prev == NULL), "Invalid item!");
151 if (l->head == NULL) {
157 new->prev->next = new;
163 * \returns the bucket that was at the tail of the list.
165 * Returns NULL if the list was empty.
167 xbt_fifo_item_t xbt_fifo_pop_item(xbt_fifo_t l)
169 xbt_fifo_item_t item;
176 l->tail = item->prev;
180 l->tail->next = NULL;
193 * Hook up this bucket at the head of the list
195 void xbt_fifo_unshift_item(xbt_fifo_t l, xbt_fifo_item_t new)
197 xbt_assert((new->next == NULL) && (new->prev == NULL), "Invalid item!");
199 if (l->head == NULL) {
205 new->next->prev = new;
211 * \returns the bucket that was at the head of the list.
213 * Returns NULL if the list was empty.
215 xbt_fifo_item_t xbt_fifo_shift_item(xbt_fifo_t l)
217 xbt_fifo_item_t item;
224 l->head = item->next;
228 l->head->prev = NULL;
241 * removes the first occurrence of \a t from \a l.
242 * \warning it will not remove duplicates
243 * \return 1 if an item was removed and 0 otherwise.
245 int xbt_fifo_remove(xbt_fifo_t l, void *t)
247 xbt_fifo_item_t current, current_next;
249 for (current = l->head; current; current = current_next) {
250 current_next = current->next;
251 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 occurrences 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){
277 /* remove the item */
278 xbt_fifo_remove_item(l, current);
279 xbt_fifo_free_item(current);
288 * \param current a bucket
290 * removes a bucket \a current from the list \a l. This function implicitly
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 = NULL;
301 current->next = NULL;
305 if (current == l->head) { /* It's the head */
306 l->head = current->next;
307 l->head->prev = NULL;
308 } else if (current == l->tail) { /* It's the tail */
309 l->tail = current->prev;
310 l->tail->next = NULL;
311 } else { /* It's in the middle */
312 current->prev->next = current->next;
313 current->next->prev = current->prev;
316 current->prev = NULL;
317 current->next = NULL;
322 * \param content an object
323 * \return 1 if \a content is in \a f.
325 int xbt_fifo_is_in(xbt_fifo_t f, void *content)
327 xbt_fifo_item_t item = xbt_fifo_get_first_item(f);
329 if (item->content == content)
337 * @brief Search the given element in the fifo using a comparison function
339 * This function allows to search an item with a user provided function instead
340 * of the pointer comparison used elsewhere in this module. Assume for example that you have a fifo of
341 * strings. You cannot use xbt_fifo_remove() to remove, say, "TOTO" from it because internally, xbt_fifo_remove()
342 * will do something like "if (item->content == "toto"), then remove it". And the pointer to the item content and the
343 * pointer to "toto" will never match. As a solution, the current function provides a way to search elements that are
344 * semantically equivalent instead of only syntactically. So, removing "Toto" from a fifo can be achieved this way:
347 int my_comparison_function(void *searched, void *seen) {
348 return !strcmp(searched, seen);
351 xbt_fifo_remove_item(fifo, 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))
371 * \return a table with the objects stored in \a f.
373 void **xbt_fifo_to_array(xbt_fifo_t f)
382 array = xbt_new0(void *, f->count);
384 for (i = 0, b = xbt_fifo_get_first_item(f); b; i++, b = b->next) {
385 array[i] = b->content;
392 * \return a copy of \a f.
394 xbt_fifo_t xbt_fifo_copy(xbt_fifo_t f)
396 xbt_fifo_t copy = NULL;
399 copy = xbt_fifo_new();
401 for (b = xbt_fifo_get_first_item(f); b; b = b->next) {
402 xbt_fifo_push(copy, b->content);
407 /* Functions passed to the mallocator constructor */
408 static void *fifo_item_mallocator_new_f(void)
410 return xbt_new(s_xbt_fifo_item_t, 1);
413 static void fifo_item_mallocator_reset_f(void *item)
415 /* memset to zero like calloc */
416 memset(item, 0, sizeof(s_xbt_fifo_item_t));
420 * \return a new bucket
422 inline xbt_fifo_item_t xbt_fifo_new_item(void)
424 return xbt_mallocator_get(item_mallocator);
427 /** \deprecated Use #xbt_fifo_new_item instead.
429 inline xbt_fifo_item_t xbt_fifo_newitem(void)
431 XBT_CWARN(xbt_fifo, "This function is deprecated. Use xbt_fifo_new_item.");
432 return xbt_fifo_new_item();
439 * stores \a v in \a i.
441 inline void xbt_fifo_set_item_content(xbt_fifo_item_t i, void *v)
443 xbt_fifo_setItemcontent(i, v);
448 * \return the object stored \a i.
450 inline void *xbt_fifo_get_item_content(xbt_fifo_item_t i)
452 return xbt_fifo_getItemcontent(i);
456 * \param b poor victim
458 * Free the bucket but does not modifies the object (if any) that was stored in it.
460 inline void xbt_fifo_free_item(xbt_fifo_item_t b)
462 xbt_mallocator_release(item_mallocator, b);
466 * \deprecated Use #xbt_fifo_free_item instead.
468 inline void xbt_fifo_freeitem(xbt_fifo_item_t b)
470 XBT_CWARN(xbt_fifo, "This function is deprecated. Use xbt_fifo_free_item.");
471 xbt_fifo_free_item(b);
476 * \return the number of buckets in \a f.
478 inline int xbt_fifo_size(xbt_fifo_t f)
485 * \return the head of \a l.
487 * Returns NULL if the list is empty.
489 inline xbt_fifo_item_t xbt_fifo_get_first_item(xbt_fifo_t l)
496 * \return the tail of \a l.
498 * Returns NULL if the list is empty.
500 inline xbt_fifo_item_t xbt_fifo_get_last_item(xbt_fifo_t l)
505 /** \deprecated Use #xbt_fifo_get_first_item instead.
507 inline xbt_fifo_item_t xbt_fifo_getFirstItem(xbt_fifo_t l)
509 XBT_CWARN(xbt_fifo, "This function is deprecated. Use xbt_fifo_get_first_item.");
510 return xbt_fifo_get_first_item(l);
515 * \return the bucket that comes next
517 * Returns NULL if \a i is the tail of the list.
519 inline xbt_fifo_item_t xbt_fifo_get_next_item(xbt_fifo_item_t i)
526 /** \deprecated Use #xbt_fifo_get_next_item instead.
528 xbt_fifo_item_t xbt_fifo_getNextItem(xbt_fifo_item_t i)
530 XBT_CWARN(xbt_fifo, "This function is deprecated. Use xbt_fifo_get_next_item.");
531 return xbt_fifo_get_next_item(i);
536 * \return the bucket that is just before \a i.
538 * Returns NULL if \a i is the head of the list.
540 inline xbt_fifo_item_t xbt_fifo_get_prev_item(xbt_fifo_item_t i)
547 /** \deprecated Use #xbt_fifo_get_prev_item instead.
549 xbt_fifo_item_t xbt_fifo_getPrevItem(xbt_fifo_item_t i)
551 XBT_WARN("This function is deprecated. Use xbt_fifo_get_prev_item.");
552 return xbt_fifo_get_prev_item(i);
555 /* Module init/exit handling the fifo item mallocator
556 * These are internal XBT functions called by xbt_preinit/postexit().
557 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
559 void xbt_fifo_preinit()
561 item_mallocator = xbt_mallocator_new(65536, fifo_item_mallocator_new_f,
562 fifo_item_mallocator_free_f, fifo_item_mallocator_reset_f);
565 void xbt_fifo_postexit()
567 if (item_mallocator != NULL) {
568 xbt_mallocator_free(item_mallocator);
569 item_mallocator = NULL;