Logo AND Algorithmique Numérique Distribuée

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