Logo AND Algorithmique Numérique Distribuée

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