Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add xbt_fifo_search(), to search an item with a user-provided comparison function
[simgrid.git] / src / xbt / fifo.c
1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "xbt/sysdep.h"
8 #include "xbt/log.h"
9 #include "xbt/mallocator.h"
10 #include "fifo_private.h"
11 #include "xbt_modinter.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_fifo, xbt, "FIFO");
14
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);
18
19 static xbt_mallocator_t item_mallocator = NULL;
20
21 /** Constructor
22  * \return a new fifo
23  */
24 xbt_fifo_t xbt_fifo_new(void)
25 {
26   xbt_fifo_t fifo;
27   fifo = xbt_new0(struct xbt_fifo, 1);
28
29   return fifo;
30 }
31
32
33 /** Destructor
34  * \param l poor victim
35  *
36  * Free the fifo structure. None of the objects that was in the fifo is however modified.
37  */
38 void xbt_fifo_free(xbt_fifo_t l)
39 {
40   xbt_fifo_reset(l);
41   xbt_free(l);
42 }
43
44 /**
45  * \brief Makes a fifo empty.
46  * \param l a fifo
47  *
48  * None of the objects that was in the fifo is however modified.
49  */
50 void xbt_fifo_reset(xbt_fifo_t l)
51 {
52   xbt_fifo_item_t b, tmp;
53
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;
57 }
58
59 /** Push
60  * \param l list
61  * \param t element
62  * \return the bucket that was just added
63  *
64  * Add an object at the tail of the list
65  */
66 xbt_fifo_item_t xbt_fifo_push(xbt_fifo_t l, void *t)
67 {
68   xbt_fifo_item_t new;
69
70   new = xbt_fifo_new_item();
71   new->content = t;
72
73   xbt_fifo_push_item(l, new);
74   return new;
75 }
76
77 /** Pop
78  * \param l list
79  * \returns the object stored at the tail of the list.
80  *
81  * Removes and returns the object stored at the tail of the list.
82  * Returns NULL if the list is empty.
83  */
84 void *xbt_fifo_pop(xbt_fifo_t l)
85 {
86   xbt_fifo_item_t item;
87   void *content;
88
89   if (l == NULL)
90     return NULL;
91   if (!(item = xbt_fifo_pop_item(l)))
92     return NULL;
93
94   content = item->content;
95   xbt_fifo_free_item(item);
96   return content;
97 }
98
99 /**
100  * \param l list
101  * \param t element
102  * \return the bucket that was just added
103  *
104  * Add an object at the head of the list
105  */
106 xbt_fifo_item_t xbt_fifo_unshift(xbt_fifo_t l, void *t)
107 {
108   xbt_fifo_item_t new;
109
110   new = xbt_fifo_new_item();
111   new->content = t;
112   xbt_fifo_unshift_item(l, new);
113   return new;
114 }
115
116 /** Shift
117  * \param l list
118  * \returns the object stored at the head of the list.
119  *
120  * Removes and returns the object stored at the head of the list.
121  * Returns NULL if the list is empty.
122  */
123 void *xbt_fifo_shift(xbt_fifo_t l)
124 {
125   xbt_fifo_item_t item;
126   void *content;
127
128   if (l == NULL)
129     return NULL;
130   if (!(item = xbt_fifo_shift_item(l)))
131     return NULL;
132
133   content = item->content;
134   xbt_fifo_free_item(item);
135   return content;
136 }
137
138 /** Push a bucket
139  * \param l list
140  * \param new bucket
141  *
142  * Hook up this bucket at the tail of the list
143  */
144 void xbt_fifo_push_item(xbt_fifo_t l, xbt_fifo_item_t new)
145 {
146   xbt_assert((new->next == NULL) && (new->prev == NULL), "Invalid item!");
147   (l->count)++;
148   if (l->head == NULL) {
149     l->head = new;
150     l->tail = new;
151     return;
152   }
153   new->prev = l->tail;
154   new->prev->next = new;
155   l->tail = new;
156 }
157
158 /** Pop bucket
159  * \param l
160  * \returns the bucket that was at the tail of the list.
161  *
162  * Returns NULL if the list was empty.
163  */
164 xbt_fifo_item_t xbt_fifo_pop_item(xbt_fifo_t l)
165 {
166   xbt_fifo_item_t item;
167
168   if (l->tail == NULL)
169     return NULL;
170
171   item = l->tail;
172
173   l->tail = item->prev;
174   if (l->tail == NULL)
175     l->head = NULL;
176   else
177     l->tail->next = NULL;
178
179   (l->count)--;
180
181   item->prev = NULL;
182
183   return item;
184 }
185
186 /** Push a bucket
187  * \param l list
188  * \param new bucket
189  *
190  * Hook up this bucket at the head of the list
191  */
192 void xbt_fifo_unshift_item(xbt_fifo_t l, xbt_fifo_item_t new)
193 {
194   xbt_assert((new->next == NULL) && (new->prev == NULL), "Invalid item!");
195   (l->count)++;
196   if (l->head == NULL) {
197     l->head = new;
198     l->tail = new;
199     return;
200   }
201   new->next = l->head;
202   new->next->prev = new;
203   l->head = new;
204   return;
205 }
206
207 /** Shift bucket
208  * \param l
209  * \returns the bucket that was at the head of the list.
210  *
211  * Returns NULL if the list was empty.
212  */
213 xbt_fifo_item_t xbt_fifo_shift_item(xbt_fifo_t l)
214 {
215   xbt_fifo_item_t item;
216
217   if (l->head == NULL)
218     return NULL;
219
220   item = l->head;
221
222   l->head = item->next;
223   if (l->head == NULL)
224     l->tail = NULL;
225   else
226     l->head->prev = NULL;
227
228   (l->count)--;
229
230   item->next = NULL;
231
232   return item;
233 }
234
235 /**
236  * \param l
237  * \param t an objet
238  *
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.
242  */
243 int xbt_fifo_remove(xbt_fifo_t l, void *t)
244 {
245   xbt_fifo_item_t current, current_next;
246
247
248   for (current = l->head; current; current = current_next) {
249     current_next = current->next;
250     if (current->content != t)
251       continue;
252     /* remove the item */
253     xbt_fifo_remove_item(l, current);
254     xbt_fifo_free_item(current);
255     /* WILL NOT REMOVE DUPLICATES */
256     return 1;
257   }
258   return 0;
259 }
260
261
262 /**
263  * \param l
264  * \param t an objet
265  *
266  * removes all occurences of \a t from \a l.
267  * \return 1 if an item was removed and 0 otherwise.
268  */
269 int xbt_fifo_remove_all(xbt_fifo_t l, void *t)
270 {
271   xbt_fifo_item_t current, current_next;
272   int res = 0;
273
274   for (current = l->head; current; current = current_next) {
275     current_next = current->next;
276     if (current->content != t)
277       continue;
278     /* remove the item */
279     xbt_fifo_remove_item(l, current);
280     xbt_fifo_free_item(current);
281     res = 1;
282   }
283   return res;
284 }
285
286 /**
287  * \param l a list
288  * \param current a bucket
289  *
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...
292  */
293 void xbt_fifo_remove_item(xbt_fifo_t l, xbt_fifo_item_t current)
294 {
295   if (l->head == l->tail) {     /* special case */
296     xbt_assert((current == l->head), "This item is not in the list!");
297     l->head = NULL;
298     l->tail = NULL;
299     (l->count)--;
300     current->prev = current->next = NULL;
301     return;
302   }
303
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;
313   }
314   (l->count)--;
315   current->prev = current->next = NULL;
316 }
317
318 /**
319  * \param f a list
320  * \param content an object
321  * \return 1 if \a content is in \a f.
322  */
323 int xbt_fifo_is_in(xbt_fifo_t f, void *content)
324 {
325   xbt_fifo_item_t item = xbt_fifo_get_first_item(f);
326   while (item) {
327     if (item->content == content)
328       return 1;
329     item = item->next;
330   }
331   return 0;
332 }
333
334 /**
335  * @brief Search the given element in the fifo using a comparison function
336  *
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, this function provides a way to search elements
342  * that are semanticaly equivalent instead of only syntaxically. So, removing "Toto" from a fifo can be
343  * achieved this way:
344  *
345  *  int my_comparison_function(void *searched, void *seen) {
346  *    return !strcmp(searched, seen);
347  *  }
348  *
349  *  xbt_fifo_remove_item(fifo,
350  *                       xbt_fifo_search_item(fifo, my_comparison_function, "Toto"));
351  *
352  * \param f a fifo list
353  * \param cmp_fun the comparison function. Prototype: void *a,void *b -> int. Semantic: returns true iff a=b
354  * @param closure the element to search. It will be provided as first argument to each call of cmp_fun
355  * \return the first item matching the comparison function, or NULL if no such item exists
356  */
357 xbt_fifo_item_t xbt_fifo_search_item(xbt_fifo_t f, int_f_pvoid_pvoid_t cmp_fun, void *closure) {
358   xbt_fifo_item_t item = xbt_fifo_get_first_item(f);
359   while (item) {
360     if (cmp_fun(closure, item->content))
361       return item;
362     item = item->next;
363   }
364   return NULL;
365
366 }
367
368 /**
369  * \param f a list
370  * \return a table with the objects stored in \a f.
371  */
372 void **xbt_fifo_to_array(xbt_fifo_t f)
373 {
374   void **array;
375   xbt_fifo_item_t b;
376   int i;
377
378   if (f->count == 0)
379     return NULL;
380   else
381     array = xbt_new0(void *, f->count);
382
383   for (i = 0, b = xbt_fifo_get_first_item(f); b; i++, b = b->next) {
384     array[i] = b->content;
385   }
386   return array;
387 }
388
389 /**
390  * \param f a list
391  * \return a copy of \a f.
392  */
393 xbt_fifo_t xbt_fifo_copy(xbt_fifo_t f)
394 {
395   xbt_fifo_t copy = NULL;
396   xbt_fifo_item_t b;
397
398   copy = xbt_fifo_new();
399
400   for (b = xbt_fifo_get_first_item(f); b; b = b->next) {
401     xbt_fifo_push(copy, b->content);
402   }
403   return copy;
404 }
405
406 /* Functions passed to the mallocator constructor */
407 static void *fifo_item_mallocator_new_f(void)
408 {
409   return xbt_new(s_xbt_fifo_item_t, 1);
410 }
411
412 static void fifo_item_mallocator_reset_f(void *item)
413 {
414   /* memset to zero like calloc */
415   memset(item, 0, sizeof(s_xbt_fifo_item_t));
416 }
417
418 /** Constructor
419  * \return a new bucket
420  */
421 XBT_INLINE xbt_fifo_item_t xbt_fifo_new_item(void)
422 {
423   return xbt_mallocator_get(item_mallocator);
424 }
425
426 /** \deprecated Use #xbt_fifo_new_item instead.
427  */
428 XBT_INLINE xbt_fifo_item_t xbt_fifo_newitem(void)
429 {
430   XBT_WARN("This function is deprecated. Use xbt_fifo_new_item.");
431   return xbt_fifo_new_item();
432 }
433
434 /**
435  * \param i a bucket
436  * \param v an object
437  *
438  * stores \a v in \a i.
439  */
440 XBT_INLINE void xbt_fifo_set_item_content(xbt_fifo_item_t i, void *v)
441 {
442   xbt_fifo_setItemcontent(i, v);
443 }
444
445 /**
446  * \param i a bucket
447  * \return the object stored \a i.
448  */
449 XBT_INLINE void *xbt_fifo_get_item_content(xbt_fifo_item_t i)
450 {
451   return xbt_fifo_getItemcontent(i);
452 }
453
454 /** Destructor
455  * \param b poor victim
456  *
457  * Free the bucket but does not modifies the object (if any) that was stored in it.
458  */
459 XBT_INLINE void xbt_fifo_free_item(xbt_fifo_item_t b)
460 {
461   xbt_mallocator_release(item_mallocator, b);
462   return;
463 }
464
465 /** Destructor
466  * \deprecated Use #xbt_fifo_free_item instead.
467  */
468 XBT_INLINE void xbt_fifo_freeitem(xbt_fifo_item_t b)
469 {
470   XBT_WARN("This function is deprecated. Use xbt_fifo_free_item.");
471   xbt_fifo_free_item(b);
472   return;
473 }
474
475 /**
476  * \param f a list
477  * \return the number of buckets in \a f.
478  */
479 XBT_INLINE int xbt_fifo_size(xbt_fifo_t f)
480 {
481   return f->count;
482 }
483
484 /**
485  * \param l a list
486  * \return the head of \a l.
487  *
488  * Returns NULL if the list is empty.
489  */
490 XBT_INLINE xbt_fifo_item_t xbt_fifo_get_first_item(xbt_fifo_t l)
491 {
492   return l->head;
493 }
494
495 /**
496  * \param l a list
497  * \return the tail of \a l.
498  *
499  * Returns NULL if the list is empty.
500  */
501 XBT_INLINE xbt_fifo_item_t xbt_fifo_get_last_item(xbt_fifo_t l)
502 {
503   return l->tail;
504 }
505
506 /** \deprecated Use #xbt_fifo_get_first_item instead.
507  */
508 XBT_INLINE xbt_fifo_item_t xbt_fifo_getFirstItem(xbt_fifo_t l)
509 {
510   XBT_WARN("This function is deprecated. Use xbt_fifo_get_first_item.");
511   return xbt_fifo_get_first_item(l);
512 }
513
514 /**
515  * \param i a bucket
516  * \return the bucket that comes next
517  *
518  * Returns NULL if \a i is the tail of the list.
519  */
520 XBT_INLINE xbt_fifo_item_t xbt_fifo_get_next_item(xbt_fifo_item_t i)
521 {
522   if (i)
523     return i->next;
524   return NULL;
525 }
526
527 /** \deprecated Use #xbt_fifo_get_next_item instead.
528  */
529 xbt_fifo_item_t xbt_fifo_getNextItem(xbt_fifo_item_t i)
530 {
531   XBT_WARN("This function is deprecated. Use xbt_fifo_get_next_item.");
532   return xbt_fifo_get_next_item(i);
533 }
534
535 /**
536  * \param i a bucket
537  * \return the bucket that is just before \a i.
538  *
539  * Returns NULL if \a i is the head of the list.
540  */
541 XBT_INLINE xbt_fifo_item_t xbt_fifo_get_prev_item(xbt_fifo_item_t i)
542 {
543   if (i)
544     return i->prev;
545   return NULL;
546 }
547
548 /** \deprecated Use #xbt_fifo_get_prev_item instead.
549  */
550 xbt_fifo_item_t xbt_fifo_getPrevItem(xbt_fifo_item_t i)
551 {
552   XBT_WARN("This function is deprecated. Use xbt_fifo_get_prev_item.");
553   return xbt_fifo_get_prev_item(i);
554 }
555
556 /* Module init/exit handling the fifo item mallocator
557  * These are internal XBT functions called by xbt_preinit/postexit().
558  * It can be used several times to recreate the mallocator, for example when you switch to MC mode
559  */
560 void xbt_fifo_preinit(void)
561 {
562   item_mallocator = xbt_mallocator_new(65536,
563                                        fifo_item_mallocator_new_f,
564                                        fifo_item_mallocator_free_f,
565                                        fifo_item_mallocator_reset_f);
566 }
567
568 void xbt_fifo_postexit(void)
569 {
570   if (item_mallocator != NULL) {
571     xbt_mallocator_free(item_mallocator);
572     item_mallocator = NULL;
573   }
574 }
575
576 /* @} */