Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[xbt] Documentation and cleanup
[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; tmp = b, b = b->next, xbt_fifo_free_item(tmp));
54   l->head = NULL;
55   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   void *content;
86
87   if (l == NULL)
88     return NULL;
89   xbt_fifo_item_t item = xbt_fifo_pop_item(l);
90   if (!item)
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   void *content;
125
126   if (l == NULL)
127     return NULL;
128   xbt_fifo_item_t item = xbt_fifo_shift_item(l);
129   if (!item)
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 occurrence 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       /* remove the item */
250       xbt_fifo_remove_item(l, current);
251       xbt_fifo_free_item(current);
252       /* WILL NOT REMOVE DUPLICATES */
253       return 1;
254     }
255   }
256   return 0;
257 }
258
259 /**
260  * \param l
261  * \param t an objet
262  *
263  * removes all occurrences 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       /* remove the item */
275       xbt_fifo_remove_item(l, current);
276       xbt_fifo_free_item(current);
277       res = 1;
278     }
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 implicitly
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 = NULL;
298     current->next = NULL;
299     return;
300   }
301
302   if (current == l->head) {     /* It's the head */
303     l->head = current->next;
304     l->head->prev = NULL;
305   } else if (current == l->tail) {      /* It's the tail */
306     l->tail = current->prev;
307     l->tail->next = NULL;
308   } else {                      /* It's in the middle */
309     current->prev->next = current->next;
310     current->next->prev = current->prev;
311   }
312   (l->count)--;
313   current->prev = NULL;
314   current->next = NULL;
315 }
316
317 /**
318  * \param f a list
319  * \param content an object
320  * \return 1 if \a content is in \a f.
321  */
322 int xbt_fifo_is_in(xbt_fifo_t f, void *content)
323 {
324   xbt_fifo_item_t item = xbt_fifo_get_first_item(f);
325   while (item) {
326     if (item->content == content)
327       return 1;
328     item = item->next;
329   }
330   return 0;
331 }
332
333 /**
334  * @brief Search the given element in the fifo using a comparison function
335  *
336  * This function allows to search an item with a user provided function instead
337  * of the pointer comparison used elsewhere in this module. Assume for example that you have a fifo of
338  * strings. You cannot use xbt_fifo_remove() to remove, say, "TOTO" from it because internally, xbt_fifo_remove()
339  * will do something like "if (item->content == "toto"), then remove it". And the pointer to the item content and the
340  * pointer to "toto" will never match. As a solution, the current function provides a way to search elements that are
341  * semantically equivalent instead of only syntactically. So, removing "Toto" from a fifo can be achieved this way:
342  *
343  *  @verbatim
344 int my_comparison_function(void *searched, void *seen) {
345   return !strcmp(searched, seen);
346 }
347
348   xbt_fifo_remove_item(fifo, xbt_fifo_search_item(fifo, my_comparison_function, "Toto"));
349 @endverbatim
350  *
351  * \param f a fifo list
352  * \param cmp_fun the comparison function. Prototype: void *a,void *b -> int. Semantic: returns true iff a=b
353  * @param closure the element to search. It will be provided as first argument to each call of cmp_fun
354  * \return the first item matching the comparison function, or NULL if no such item exists
355  */
356 xbt_fifo_item_t xbt_fifo_search_item(xbt_fifo_t f, int_f_pvoid_pvoid_t cmp_fun, void *closure) {
357   xbt_fifo_item_t item = xbt_fifo_get_first_item(f);
358   while (item) {
359     if (cmp_fun(closure, item->content))
360       return item;
361     item = item->next;
362   }
363   return NULL;
364 }
365
366 /**
367  * \param f a list
368  * \return a table with the objects stored in \a f.
369  */
370 void **xbt_fifo_to_array(xbt_fifo_t f)
371 {
372   void **array;
373   xbt_fifo_item_t b;
374   int i;
375
376   if (f->count == 0)
377     return NULL;
378   else
379     array = xbt_new0(void *, f->count);
380
381   for (i = 0, b = xbt_fifo_get_first_item(f); b; i++, b = b->next) {
382     array[i] = b->content;
383   }
384   return array;
385 }
386
387 /**
388  * \param f a list
389  * \return a copy of \a f.
390  */
391 xbt_fifo_t xbt_fifo_copy(xbt_fifo_t f)
392 {
393   xbt_fifo_t copy = NULL;
394   xbt_fifo_item_t b;
395
396   copy = xbt_fifo_new();
397
398   for (b = xbt_fifo_get_first_item(f); b; b = b->next) {
399     xbt_fifo_push(copy, b->content);
400   }
401   return copy;
402 }
403
404 /* Functions passed to the mallocator constructor */
405 static void *fifo_item_mallocator_new_f(void)
406 {
407   return xbt_new(s_xbt_fifo_item_t, 1);
408 }
409
410 static void fifo_item_mallocator_reset_f(void *item)
411 {
412   /* memset to zero like calloc */
413   memset(item, 0, sizeof(s_xbt_fifo_item_t));
414 }
415
416 /** Constructor
417  * \return a new bucket
418  */
419 inline xbt_fifo_item_t xbt_fifo_new_item(void)
420 {
421   return xbt_mallocator_get(item_mallocator);
422 }
423
424 /** \deprecated Use #xbt_fifo_new_item instead.
425  */
426 inline xbt_fifo_item_t xbt_fifo_newitem(void)
427 {
428   XBT_CWARN(xbt_fifo, "This function is deprecated. Use xbt_fifo_new_item.");
429   return xbt_fifo_new_item();
430 }
431
432 /**
433  * \param i a bucket
434  * \param v an object
435  *
436  * stores \a v in \a i.
437  */
438 inline void xbt_fifo_set_item_content(xbt_fifo_item_t i, void *v)
439 {
440   xbt_fifo_setItemcontent(i, v);
441 }
442
443 /**
444  * \param i a bucket
445  * \return the object stored \a i.
446  */
447 inline void *xbt_fifo_get_item_content(xbt_fifo_item_t i)
448 {
449   return xbt_fifo_getItemcontent(i);
450 }
451
452 /** Destructor
453  * \param b poor victim
454  *
455  * Free the bucket but does not modifies the object (if any) that was stored in it.
456  */
457 inline void xbt_fifo_free_item(xbt_fifo_item_t b)
458 {
459   xbt_mallocator_release(item_mallocator, b);
460   return;
461 }
462
463 /** Destructor
464  * \deprecated Use #xbt_fifo_free_item instead.
465  */
466 inline void xbt_fifo_freeitem(xbt_fifo_item_t b)
467 {
468   XBT_CWARN(xbt_fifo, "This function is deprecated. Use xbt_fifo_free_item.");
469   xbt_fifo_free_item(b);
470   return;
471 }
472
473 /**
474  * \param f a list
475  * \return the number of buckets in \a f.
476  */
477 inline int xbt_fifo_size(xbt_fifo_t f)
478 {
479   return f->count;
480 }
481
482 /**
483  * \param l a list
484  * \return the head of \a l.
485  *
486  * Returns NULL if the list is empty.
487  */
488 inline xbt_fifo_item_t xbt_fifo_get_first_item(xbt_fifo_t l)
489 {
490   return l->head;
491 }
492
493 /**
494  * \param l a list
495  * \return the tail of \a l.
496  *
497  * Returns NULL if the list is empty.
498  */
499 inline xbt_fifo_item_t xbt_fifo_get_last_item(xbt_fifo_t l)
500 {
501   return l->tail;
502 }
503
504 /** \deprecated Use #xbt_fifo_get_first_item instead.
505  */
506 inline xbt_fifo_item_t xbt_fifo_getFirstItem(xbt_fifo_t l)
507 {
508   XBT_CWARN(xbt_fifo, "This function is deprecated. Use xbt_fifo_get_first_item.");
509   return xbt_fifo_get_first_item(l);
510 }
511
512 /**
513  * \param i a bucket
514  * \return the bucket that comes next
515  *
516  * Returns NULL if \a i is the tail of the list.
517  */
518 inline xbt_fifo_item_t xbt_fifo_get_next_item(xbt_fifo_item_t i)
519 {
520   if (i)
521     return i->next;
522   return NULL;
523 }
524
525 /** \deprecated Use #xbt_fifo_get_next_item instead.
526  */
527 xbt_fifo_item_t xbt_fifo_getNextItem(xbt_fifo_item_t i)
528 {
529   XBT_CWARN(xbt_fifo, "This function is deprecated. Use xbt_fifo_get_next_item.");
530   return xbt_fifo_get_next_item(i);
531 }
532
533 /**
534  * \param i a bucket
535  * \return the bucket that is just before \a i.
536  *
537  * Returns NULL if \a i is the head of the list.
538  */
539 inline xbt_fifo_item_t xbt_fifo_get_prev_item(xbt_fifo_item_t i)
540 {
541   if (i)
542     return i->prev;
543   return NULL;
544 }
545
546 /** \deprecated Use #xbt_fifo_get_prev_item instead.
547  */
548 xbt_fifo_item_t xbt_fifo_getPrevItem(xbt_fifo_item_t i)
549 {
550   XBT_WARN("This function is deprecated. Use xbt_fifo_get_prev_item.");
551   return xbt_fifo_get_prev_item(i);
552 }
553
554 /* Module init/exit handling the fifo item mallocator
555  * These are internal XBT functions called by xbt_preinit/postexit().
556  * It can be used several times to recreate the mallocator, for example when you switch to MC mode
557  */
558 void xbt_fifo_preinit(void)
559 {
560   item_mallocator = xbt_mallocator_new(65536, fifo_item_mallocator_new_f,
561                                        fifo_item_mallocator_free_f, fifo_item_mallocator_reset_f);
562 }
563
564 void xbt_fifo_postexit(void)
565 {
566   if (item_mallocator != NULL) {
567     xbt_mallocator_free(item_mallocator);
568     item_mallocator = NULL;
569   }
570 }
571 /* @} */