Logo AND Algorithmique Numérique Distribuée

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