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)
26 xbt_fifo_t fifo = xbt_new0(struct xbt_fifo, 1);
32 * \param l poor victim
34 * Free the fifo structure. None of the objects that was in the fifo is however modified.
36 void xbt_fifo_free(xbt_fifo_t l)
43 * \brief Makes a fifo empty.
46 * None of the objects that was in the fifo is however modified.
48 void xbt_fifo_reset(xbt_fifo_t l)
50 xbt_fifo_item_t b = xbt_fifo_get_first_item(l);
53 xbt_fifo_item_t tmp = b;
55 xbt_fifo_free_item(tmp);
64 * \return the bucket that was just added
66 * Add an object at the tail of the list
68 xbt_fifo_item_t xbt_fifo_push(xbt_fifo_t l, void *t)
70 xbt_fifo_item_t 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)
88 xbt_fifo_item_t item = xbt_fifo_pop_item(l);
92 void *content = item->content;
93 xbt_fifo_free_item(item);
100 * \return the bucket that was just added
102 * Add an object at the head of the list
104 xbt_fifo_item_t xbt_fifo_unshift(xbt_fifo_t l, void *t)
106 xbt_fifo_item_t new = xbt_fifo_new_item();
108 xbt_fifo_unshift_item(l, new);
114 * \returns the object stored at the head of the list.
116 * Removes and returns the object stored at the head of the list.
117 * Returns NULL if the list is empty.
119 void *xbt_fifo_shift(xbt_fifo_t l)
123 xbt_fifo_item_t item = xbt_fifo_shift_item(l);
127 void *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_assert((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)
163 xbt_fifo_item_t item = l->tail;
165 l->tail = item->prev;
169 l->tail->next = NULL;
182 * Hook up this bucket at the head of the list
184 void xbt_fifo_unshift_item(xbt_fifo_t l, xbt_fifo_item_t new)
186 xbt_assert((new->next == NULL) && (new->prev == NULL), "Invalid item!");
188 if (l->head == NULL) {
194 new->next->prev = new;
200 * \returns the bucket that was at the head of the list.
202 * Returns NULL if the list was empty.
204 xbt_fifo_item_t xbt_fifo_shift_item(xbt_fifo_t l)
209 xbt_fifo_item_t item = l->head;
211 l->head = item->next;
215 l->head->prev = NULL;
228 * removes the first occurrence 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;
235 xbt_fifo_item_t current_next;
237 for (current = l->head; current; current = current_next) {
238 current_next = current->next;
239 if (current->content == t) {
240 /* remove the item */
241 xbt_fifo_remove_item(l, current);
242 xbt_fifo_free_item(current);
243 /* WILL NOT REMOVE DUPLICATES */
254 * removes all occurrences of \a t from \a l.
255 * \return 1 if an item was removed and 0 otherwise.
257 int xbt_fifo_remove_all(xbt_fifo_t l, void *t)
259 xbt_fifo_item_t current;
260 xbt_fifo_item_t current_next;
263 for (current = l->head; current; current = current_next) {
264 current_next = current->next;
265 if (current->content == t){
266 /* remove the item */
267 xbt_fifo_remove_item(l, current);
268 xbt_fifo_free_item(current);
277 * \param current a bucket
279 * removes a bucket \a current from the list \a l. This function implicitly
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_assert((current == l->head), "This item is not in the list!");
289 current->prev = NULL;
290 current->next = NULL;
294 if (current == l->head) { /* It's the head */
295 l->head = current->next;
296 l->head->prev = NULL;
297 } else if (current == l->tail) { /* It's the tail */
298 l->tail = current->prev;
299 l->tail->next = NULL;
300 } else { /* It's in the middle */
301 current->prev->next = current->next;
302 current->next->prev = current->prev;
305 current->prev = NULL;
306 current->next = NULL;
311 * \param content an object
312 * \return 1 if \a content is in \a f.
314 int xbt_fifo_is_in(xbt_fifo_t f, void *content)
316 xbt_fifo_item_t item = xbt_fifo_get_first_item(f);
318 if (item->content == content)
326 * @brief Search the given element in the fifo using a comparison function
328 * This function allows to search an item with a user provided function instead
329 * of the pointer comparison used elsewhere in this module. Assume for example that you have a fifo of
330 * strings. You cannot use xbt_fifo_remove() to remove, say, "TOTO" from it because internally, xbt_fifo_remove()
331 * will do something like "if (item->content == "toto"), then remove it". And the pointer to the item content and the
332 * pointer to "toto" will never match. As a solution, the current function provides a way to search elements that are
333 * semantically equivalent instead of only syntactically. So, removing "Toto" from a fifo can be achieved this way:
336 int my_comparison_function(void *searched, void *seen) {
337 return !strcmp(searched, seen);
340 xbt_fifo_remove_item(fifo, xbt_fifo_search_item(fifo, my_comparison_function, "Toto"));
343 * \param f a fifo list
344 * \param cmp_fun the comparison function. Prototype: void *a,void *b -> int. Semantic: returns true iff a=b
345 * @param closure the element to search. It will be provided as first argument to each call of cmp_fun
346 * \return the first item matching the comparison function, or NULL if no such item exists
348 xbt_fifo_item_t xbt_fifo_search_item(xbt_fifo_t f, int_f_pvoid_pvoid_t cmp_fun, void *closure) {
349 xbt_fifo_item_t item = xbt_fifo_get_first_item(f);
351 if (cmp_fun(closure, item->content))
360 * \return a table with the objects stored in \a f.
362 void **xbt_fifo_to_array(xbt_fifo_t f)
371 array = xbt_new0(void *, f->count);
373 for (i = 0, b = xbt_fifo_get_first_item(f); b; i++, b = b->next) {
374 array[i] = b->content;
381 * \return a copy of \a f.
383 xbt_fifo_t xbt_fifo_copy(xbt_fifo_t f)
385 xbt_fifo_t copy = xbt_fifo_new();
388 for (b = xbt_fifo_get_first_item(f); b; b = b->next) {
389 xbt_fifo_push(copy, b->content);
394 /* Functions passed to the mallocator constructor */
395 static void *fifo_item_mallocator_new_f(void)
397 return xbt_new(s_xbt_fifo_item_t, 1);
400 static void fifo_item_mallocator_reset_f(void *item)
402 /* memset to zero like calloc */
403 memset(item, 0, sizeof(s_xbt_fifo_item_t));
407 * \return a new bucket
409 inline xbt_fifo_item_t xbt_fifo_new_item(void)
411 return xbt_mallocator_get(item_mallocator);
414 /** \deprecated Use #xbt_fifo_new_item instead.
416 inline xbt_fifo_item_t xbt_fifo_newitem(void)
418 XBT_CWARN(xbt_fifo, "This function is deprecated. Use xbt_fifo_new_item.");
419 return xbt_fifo_new_item();
426 * stores \a v in \a i.
428 inline void xbt_fifo_set_item_content(xbt_fifo_item_t i, void *v)
430 xbt_fifo_setItemcontent(i, v);
435 * \return the object stored \a i.
437 inline void *xbt_fifo_get_item_content(xbt_fifo_item_t i)
439 return xbt_fifo_getItemcontent(i);
443 * \param b poor victim
445 * Free the bucket but does not modifies the object (if any) that was stored in it.
447 inline void xbt_fifo_free_item(xbt_fifo_item_t b)
449 xbt_mallocator_release(item_mallocator, b);
453 * \deprecated Use #xbt_fifo_free_item instead.
455 inline void xbt_fifo_freeitem(xbt_fifo_item_t b)
457 XBT_CWARN(xbt_fifo, "This function is deprecated. Use xbt_fifo_free_item.");
458 xbt_fifo_free_item(b);
463 * \return the number of buckets in \a f.
465 inline int xbt_fifo_size(xbt_fifo_t f)
472 * \return the head of \a l.
474 * Returns NULL if the list is empty.
476 inline xbt_fifo_item_t xbt_fifo_get_first_item(xbt_fifo_t l)
483 * \return the tail of \a l.
485 * Returns NULL if the list is empty.
487 inline xbt_fifo_item_t xbt_fifo_get_last_item(xbt_fifo_t l)
492 /** \deprecated Use #xbt_fifo_get_first_item instead.
494 inline xbt_fifo_item_t xbt_fifo_getFirstItem(xbt_fifo_t l)
496 XBT_CWARN(xbt_fifo, "This function is deprecated. Use xbt_fifo_get_first_item.");
497 return xbt_fifo_get_first_item(l);
502 * \return the bucket that comes next
504 * Returns NULL if \a i is the tail of the list.
506 inline xbt_fifo_item_t xbt_fifo_get_next_item(xbt_fifo_item_t i)
513 /** \deprecated Use #xbt_fifo_get_next_item instead.
515 xbt_fifo_item_t xbt_fifo_getNextItem(xbt_fifo_item_t i)
517 XBT_CWARN(xbt_fifo, "This function is deprecated. Use xbt_fifo_get_next_item.");
518 return xbt_fifo_get_next_item(i);
523 * \return the bucket that is just before \a i.
525 * Returns NULL if \a i is the head of the list.
527 inline xbt_fifo_item_t xbt_fifo_get_prev_item(xbt_fifo_item_t i)
534 /** \deprecated Use #xbt_fifo_get_prev_item instead.
536 xbt_fifo_item_t xbt_fifo_getPrevItem(xbt_fifo_item_t i)
538 XBT_WARN("This function is deprecated. Use xbt_fifo_get_prev_item.");
539 return xbt_fifo_get_prev_item(i);
542 /* Module init/exit handling the fifo item mallocator
543 * These are internal XBT functions called by xbt_preinit/postexit().
544 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
546 void xbt_fifo_preinit()
548 item_mallocator = xbt_mallocator_new(65536, fifo_item_mallocator_new_f,
549 fifo_item_mallocator_free_f, fifo_item_mallocator_reset_f);
552 void xbt_fifo_postexit()
554 if (item_mallocator != NULL) {
555 xbt_mallocator_free(item_mallocator);
556 item_mallocator = NULL;