Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add a test trying to access the properties of a remote host -- it works
[simgrid.git] / src / xbt / dynar.c
1 /* a generic DYNamic ARray implementation.                                  */
2
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "portable.h"           /* SIZEOF_MAX */
10 #include "xbt/misc.h"
11 #include "xbt/sysdep.h"
12 #include "xbt/log.h"
13 #include "xbt/ex.h"
14 #include "xbt/dynar.h"
15 #include <sys/types.h>
16
17 /* IMPLEMENTATION NOTE ON SYNCHRONIZATION: every functions which name is prefixed by _
18  * assumes that the dynar is already locked if we have to.
19  * Other functions (public ones) check for this.
20  */
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dyn, xbt, "Dynamic arrays");
23
24 static XBT_INLINE void _dynar_lock(xbt_dynar_t dynar)
25 {
26   if (dynar->mutex)
27     xbt_mutex_acquire(dynar->mutex);
28 }
29
30 static XBT_INLINE void _dynar_unlock(xbt_dynar_t dynar)
31 {
32   if (dynar->mutex)
33     xbt_mutex_release(dynar->mutex);
34 }
35
36 static XBT_INLINE void _sanity_check_dynar(xbt_dynar_t dynar)
37 {
38   xbt_assert(dynar, "dynar is NULL");
39 }
40
41 static XBT_INLINE void _sanity_check_idx(int idx)
42 {
43   xbt_assert(idx >= 0, "dynar idx(=%d) < 0", (int) (idx));
44 }
45
46 static XBT_INLINE void _check_inbound_idx(xbt_dynar_t dynar, int idx)
47 {
48   if (idx < 0 || idx >= dynar->used) {
49     _dynar_unlock(dynar);
50     THROWF(bound_error, idx,
51            "dynar is not that long. You asked %d, but it's only %lu long",
52            (int) (idx), (unsigned long) dynar->used);
53   }
54 }
55
56 static XBT_INLINE void _check_sloppy_inbound_idx(xbt_dynar_t dynar,
57                                                  int idx)
58 {
59   if (idx > dynar->used) {
60     _dynar_unlock(dynar);
61     THROWF(bound_error, idx,
62            "dynar is not that long. You asked %d, but it's only %lu long (could have been equal to it)",
63            (int) (idx), (unsigned long) dynar->used);
64   }
65 }
66
67 static XBT_INLINE void _check_populated_dynar(xbt_dynar_t dynar)
68 {
69   if (dynar->used == 0) {
70     _dynar_unlock(dynar);
71     THROWF(bound_error, 0, "dynar %p is empty", dynar);
72   }
73 }
74
75 static void _dynar_map(const xbt_dynar_t dynar, void_f_pvoid_t const op);
76
77 static XBT_INLINE
78     void _xbt_clear_mem(void *const ptr, const unsigned long length)
79 {
80   memset(ptr, 0, length);
81 }
82
83 static XBT_INLINE
84     void _xbt_dynar_expand(xbt_dynar_t const dynar, const unsigned long nb)
85 {
86   const unsigned long old_size = dynar->size;
87
88   if (nb > old_size) {
89     void *const old_data = dynar->data;
90     const unsigned long elmsize = dynar->elmsize;
91     const unsigned long old_length = old_size * elmsize;
92
93     const unsigned long expand = 2 * (old_size + 1);
94     const unsigned long new_size = (nb > expand ? nb : expand);
95     const unsigned long new_length = new_size * elmsize;
96     void *const new_data = xbt_realloc(old_data, new_length);
97
98     XBT_DEBUG("expand %p from %lu to %lu elements", dynar, old_size, new_size);
99
100     _xbt_clear_mem((char *)new_data + old_length, new_length - old_length);
101
102     dynar->size = new_size;
103     dynar->data = new_data;
104   }
105 }
106
107 static XBT_INLINE
108     void *_xbt_dynar_elm(const xbt_dynar_t dynar, const unsigned long idx)
109 {
110   char *const data = (char *) dynar->data;
111   const unsigned long elmsize = dynar->elmsize;
112
113   return data + idx * elmsize;
114 }
115
116 static XBT_INLINE
117     void
118 _xbt_dynar_get_elm(void *const dst,
119                    const xbt_dynar_t dynar, const unsigned long idx)
120 {
121   void *const elm = _xbt_dynar_elm(dynar, idx);
122
123   memcpy(dst, elm, dynar->elmsize);
124 }
125
126 static XBT_INLINE
127     void
128 _xbt_dynar_put_elm(const xbt_dynar_t dynar,
129                    const unsigned long idx, const void *const src)
130 {
131   void *const elm = _xbt_dynar_elm(dynar, idx);
132   const unsigned long elmsize = dynar->elmsize;
133
134   memcpy(elm, src, elmsize);
135 }
136
137 static XBT_INLINE
138     void
139 _xbt_dynar_remove_at(xbt_dynar_t const dynar,
140                      const unsigned long idx, void *const object)
141 {
142
143   unsigned long nb_shift;
144   unsigned long offset;
145
146   _sanity_check_dynar(dynar);
147   _check_inbound_idx(dynar, idx);
148
149   if (object) {
150     _xbt_dynar_get_elm(object, dynar, idx);
151   } else if (dynar->free_f) {
152     if (dynar->elmsize <= SIZEOF_MAX) {
153       char elm[SIZEOF_MAX];
154       _xbt_dynar_get_elm(elm, dynar, idx);
155       dynar->free_f(elm);
156     } else {
157       char *elm = malloc(dynar->elmsize);
158       _xbt_dynar_get_elm(elm, dynar, idx);
159       dynar->free_f(elm);
160       free(elm);
161     }
162   }
163
164   nb_shift = dynar->used - 1 - idx;
165
166   if (nb_shift) {
167     offset = nb_shift * dynar->elmsize;
168     memmove(_xbt_dynar_elm(dynar, idx), _xbt_dynar_elm(dynar, idx + 1),
169             offset);
170   }
171
172   dynar->used--;
173 }
174
175 void xbt_dynar_dump(xbt_dynar_t dynar)
176 {
177   XBT_INFO("Dynar dump: size=%lu; used=%lu; elmsize=%lu; data=%p; free_f=%p",
178         dynar->size, dynar->used, dynar->elmsize, dynar->data,
179         dynar->free_f);
180 }
181
182 /** @brief Constructor
183  *
184  * \param elmsize size of each element in the dynar
185  * \param free_f function to call each time we want to get rid of an element (or NULL if nothing to do).
186  *
187  * Creates a new dynar. If a free_func is provided, the elements have to be
188  * pointer of pointer. That is to say that dynars can contain either base
189  * types (int, char, double, etc) or pointer of pointers (struct **).
190  */
191 xbt_dynar_t
192 xbt_dynar_new(const unsigned long elmsize, void_f_pvoid_t const free_f)
193 {
194
195   xbt_dynar_t dynar = xbt_new0(s_xbt_dynar_t, 1);
196
197   dynar->size = 0;
198   dynar->used = 0;
199   dynar->elmsize = elmsize;
200   dynar->data = NULL;
201   dynar->free_f = free_f;
202   dynar->mutex = NULL;
203
204   return dynar;
205 }
206
207 /** @brief Creates a synchronized dynar.
208  *
209  * Just like #xbt_dynar_new, but each access to the structure will be protected by a mutex
210  *
211  */
212 xbt_dynar_t
213 xbt_dynar_new_sync(const unsigned long elmsize,
214                    void_f_pvoid_t const free_f)
215 {
216   xbt_dynar_t res = xbt_dynar_new(elmsize, free_f);
217   res->mutex = xbt_mutex_init();
218   return res;
219 }
220
221 /** @brief Destructor of the structure not touching to the content
222  *
223  * \param dynar poor victim
224  *
225  * kilkil a dynar BUT NOT its content. Ie, the array is freed, but the content
226  * is not touched (the \a free_f function is not used)
227  */
228 void xbt_dynar_free_container(xbt_dynar_t * dynar)
229 {
230   if (dynar && *dynar) {
231
232     if ((*dynar)->data) {
233       _xbt_clear_mem((*dynar)->data, (*dynar)->size);
234       free((*dynar)->data);
235     }
236
237     if ((*dynar)->mutex)
238       xbt_mutex_destroy((*dynar)->mutex);
239
240     _xbt_clear_mem(*dynar, sizeof(s_xbt_dynar_t));
241
242     free(*dynar);
243     *dynar = NULL;
244   }
245 }
246
247 /** @brief Frees the content and set the size to 0
248  *
249  * \param dynar who to squeeze
250  */
251 XBT_INLINE void xbt_dynar_reset(xbt_dynar_t const dynar)
252 {
253   _dynar_lock(dynar);
254
255   _sanity_check_dynar(dynar);
256
257   XBT_DEBUG("Reset the dynar %p", (void *) dynar);
258   if (dynar->free_f) {
259     _dynar_map(dynar, dynar->free_f);
260   }
261   /*
262      free(dynar->data);
263
264      dynar->size = 0;
265    */
266   dynar->used = 0;
267
268   _dynar_unlock(dynar);
269
270   /*  dynar->data = NULL; */
271 }
272
273 /**
274  * \brief Shrink the dynar by removing empty slots at the end of the internal array
275  * \param dynar a dynar
276  * \param empty_slots_wanted number of empty slots you want to keep at the end of the
277  * internal array for further insertions
278  *
279  * Reduces the internal array size of the dynar to the number of elements plus
280  * \a empty_slots_wanted.
281  * After removing elements from the dynar, you can call this function to make
282  * the dynar use less memory.
283  * Set \a empty_slots_wanted to zero to reduce the dynar internal array as much
284  * as possible.
285  * Note that if \a empty_slots_wanted is greater than the array size, the internal
286  * array is expanded instead of shriked.
287  */
288 void xbt_dynar_shrink(xbt_dynar_t dynar, int empty_slots_wanted)
289 {
290   unsigned long size_wanted;
291
292   _dynar_lock(dynar);
293
294   size_wanted = dynar->used + empty_slots_wanted;
295   if (size_wanted != dynar->size) {
296     dynar->size = size_wanted;
297     dynar->data = xbt_realloc(dynar->data, dynar->elmsize * dynar->size);
298   }
299   _dynar_unlock(dynar);
300 }
301
302 /** @brief Destructor
303  *
304  * \param dynar poor victim
305  *
306  * kilkil a dynar and its content
307  */
308
309 XBT_INLINE void xbt_dynar_free(xbt_dynar_t * dynar)
310 {
311   if (dynar && *dynar) {
312     xbt_dynar_reset(*dynar);
313     xbt_dynar_free_container(dynar);
314   }
315 }
316
317 /** \brief free a dynar passed as void* (handy to store dynar in dynars or dict) */
318 void xbt_dynar_free_voidp(void *d)
319 {
320   xbt_dynar_t dynar = (xbt_dynar_t)d;
321   xbt_dynar_free(&dynar);
322 }
323
324 /** @brief Count of dynar's elements
325  *
326  * \param dynar the dynar we want to mesure
327  */
328 XBT_INLINE unsigned long xbt_dynar_length(const xbt_dynar_t dynar)
329 {
330   return (dynar ? (unsigned long) dynar->used : (unsigned long) 0);
331 }
332
333 /**@brief check if a dynar is empty
334  *
335  *\param dynar the dynat we want to check
336  */
337
338 XBT_INLINE int xbt_dynar_is_empty(const xbt_dynar_t dynar)
339 {
340   return (xbt_dynar_length(dynar) == 0);
341 }
342
343 /** @brief Retrieve a copy of the Nth element of a dynar.
344  *
345  * \param dynar information dealer
346  * \param idx index of the slot we want to retrieve
347  * \param[out] dst where to put the result to.
348  */
349 XBT_INLINE void
350 xbt_dynar_get_cpy(const xbt_dynar_t dynar,
351                   const unsigned long idx, void *const dst)
352 {
353   _dynar_lock(dynar);
354   _sanity_check_dynar(dynar);
355   _check_inbound_idx(dynar, idx);
356
357   _xbt_dynar_get_elm(dst, dynar, idx);
358   _dynar_unlock(dynar);
359 }
360
361 /** @brief Retrieve a pointer to the Nth element of a dynar.
362  *
363  * \param dynar information dealer
364  * \param idx index of the slot we want to retrieve
365  * \return the \a idx-th element of \a dynar.
366  *
367  * \warning The returned value is the actual content of the dynar.
368  * Make a copy before fooling with it.
369  */
370 XBT_INLINE void *xbt_dynar_get_ptr(const xbt_dynar_t dynar,
371                                    const unsigned long idx)
372 {
373
374   void *res;
375   _dynar_lock(dynar);
376   _sanity_check_dynar(dynar);
377   _check_inbound_idx(dynar, idx);
378
379   res = _xbt_dynar_elm(dynar, idx);
380   _dynar_unlock(dynar);
381   return res;
382 }
383
384 XBT_INLINE void *xbt_dynar_set_at_ptr(const xbt_dynar_t dynar,
385                                       const unsigned long idx)
386 {
387
388   void *res;
389   _dynar_lock(dynar);
390   _sanity_check_dynar(dynar);
391
392   _xbt_dynar_expand(dynar, idx + 1);
393
394   if (idx >= dynar->used) {
395     _xbt_clear_mem(((char * const)dynar->data) + dynar->used * dynar->elmsize,
396                    (idx + 1 - dynar->used)*dynar->elmsize);
397     dynar->used = idx + 1;
398   }
399   
400   _dynar_unlock(dynar);
401
402   res = _xbt_dynar_elm(dynar, idx);
403
404   return res;
405 }
406
407 static void XBT_INLINE          /* not synchronized */
408 _xbt_dynar_set(xbt_dynar_t dynar,
409                const unsigned long idx, const void *const src)
410 {
411
412   _sanity_check_dynar(dynar);
413
414   _xbt_dynar_expand(dynar, idx + 1);
415
416   if (idx >= dynar->used) {
417     _xbt_clear_mem(((char * const)dynar->data) + dynar->used * dynar->elmsize,
418                    (idx + 1 - dynar->used)*dynar->elmsize);
419     dynar->used = idx + 1;
420   }
421
422   _xbt_dynar_put_elm(dynar, idx, src);
423 }
424
425 /** @brief Set the Nth element of a dynar (expanded if needed). Previous value at this position is NOT freed
426  *
427  * \param dynar information dealer
428  * \param idx index of the slot we want to modify
429  * \param src What will be feeded to the dynar
430  *
431  * If you want to free the previous content, use xbt_dynar_replace().
432  */
433 XBT_INLINE void xbt_dynar_set(xbt_dynar_t dynar, const int idx,
434                               const void *const src)
435 {
436
437   _dynar_lock(dynar);
438   _xbt_dynar_set(dynar, idx, src);
439   _dynar_unlock(dynar);
440 }
441
442 /** @brief Set the Nth element of a dynar (expanded if needed). Previous value is freed
443  *
444  * \param dynar
445  * \param idx
446  * \param object
447  *
448  * Set the Nth element of a dynar, expanding the dynar if needed, AND DO
449  * free the previous value at this position. If you don't want to free the
450  * previous content, use xbt_dynar_set().
451  */
452 void
453 xbt_dynar_replace(xbt_dynar_t dynar,
454                   const unsigned long idx, const void *const object)
455 {
456   _dynar_lock(dynar);
457   _sanity_check_dynar(dynar);
458
459   if (idx < dynar->used && dynar->free_f) {
460     void *const old_object = _xbt_dynar_elm(dynar, idx);
461
462     dynar->free_f(old_object);
463   }
464
465   _xbt_dynar_set(dynar, idx, object);
466   _dynar_unlock(dynar);
467 }
468
469 static XBT_INLINE void *_xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar,
470                                                  const unsigned long idx)
471 {
472   void *res;
473   unsigned long old_used;
474   unsigned long new_used;
475   long nb_shift;
476
477   _sanity_check_dynar(dynar);
478   _sanity_check_idx(idx);
479
480   old_used = dynar->used;
481   new_used = old_used + 1;
482
483   _xbt_dynar_expand(dynar, new_used);
484
485   nb_shift = old_used - idx;
486
487   if (nb_shift>0) {
488     memmove(_xbt_dynar_elm(dynar, idx + 1),
489             _xbt_dynar_elm(dynar, idx), nb_shift * dynar->elmsize);
490   }
491
492   dynar->used = new_used;
493   res = _xbt_dynar_elm(dynar, idx);
494   return res;
495 }
496
497 /** @brief Make room for a new element, and return a pointer to it
498  *
499  * You can then use regular affectation to set its value instead of relying
500  * on the slow memcpy. This is what xbt_dynar_insert_at_as() does.
501  */
502 void *xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar, const int idx)
503 {
504   void *res;
505
506   _dynar_lock(dynar);
507   res = _xbt_dynar_insert_at_ptr(dynar, idx);
508   _dynar_unlock(dynar);
509   return res;
510 }
511
512 /** @brief Set the Nth dynar's element, expanding the dynar and sliding the previous values to the right
513  *
514  * Set the Nth element of a dynar, expanding the dynar if needed, and
515  * moving the previously existing value and all subsequent ones to one
516  * position right in the dynar.
517  */
518 XBT_INLINE void
519 xbt_dynar_insert_at(xbt_dynar_t const dynar,
520                     const int idx, const void *const src)
521 {
522
523   _dynar_lock(dynar);
524   /* checks done in xbt_dynar_insert_at_ptr */
525   memcpy(_xbt_dynar_insert_at_ptr(dynar, idx), src, dynar->elmsize);
526   _dynar_unlock(dynar);
527 }
528
529 /** @brief Remove the Nth dynar's element, sliding the previous values to the left
530  *
531  * Get the Nth element of a dynar, removing it from the dynar and moving
532  * all subsequent values to one position left in the dynar.
533  *
534  * If the object argument of this function is a non-null pointer, the removed
535  * element is copied to this address. If not, the element is freed using the
536  * free_f function passed at dynar creation.
537  */
538 void
539 xbt_dynar_remove_at(xbt_dynar_t const dynar,
540                     const int idx, void *const object)
541 {
542
543   _dynar_lock(dynar);
544   _xbt_dynar_remove_at(dynar, idx, object);
545   _dynar_unlock(dynar);
546 }
547
548 /** @brief Returns the position of the element in the dynar
549  *
550  * Raises not_found_error if not found.
551  */
552 unsigned int xbt_dynar_search(xbt_dynar_t const dynar, void *const elem)
553 {
554   unsigned long it;
555
556   _dynar_lock(dynar);
557   for (it = 0; it < dynar->used; it++)
558     if (!memcmp(_xbt_dynar_elm(dynar, it), elem, dynar->elmsize)) {
559       _dynar_unlock(dynar);
560       return it;
561     }
562
563   _dynar_unlock(dynar);
564   THROWF(not_found_error, 0, "Element %p not part of dynar %p", elem,
565          dynar);
566 }
567
568 /** @brief Returns a boolean indicating whether the element is part of the dynar */
569 int xbt_dynar_member(xbt_dynar_t const dynar, void *const elem)
570 {
571
572   xbt_ex_t e;
573
574   TRY {
575     xbt_dynar_search(dynar, elem);
576   }
577   CATCH(e) {
578     if (e.category == not_found_error) {
579       xbt_ex_free(e);
580       return 0;
581     }
582     RETHROW;
583   }
584   return 1;
585 }
586
587 /** @brief Make room at the end of the dynar for a new element, and return a pointer to it.
588  *
589  * You can then use regular affectation to set its value instead of relying
590  * on the slow memcpy. This is what xbt_dynar_push_as() does.
591  */
592 XBT_INLINE void *xbt_dynar_push_ptr(xbt_dynar_t const dynar)
593 {
594   void *res;
595
596   /* we have to inline xbt_dynar_insert_at_ptr here to make sure that
597      dynar->used don't change between reading it and getting the lock
598      within xbt_dynar_insert_at_ptr */
599   _dynar_lock(dynar);
600   res = _xbt_dynar_insert_at_ptr(dynar, dynar->used);
601   _dynar_unlock(dynar);
602   return res;
603 }
604
605 /** @brief Add an element at the end of the dynar */
606 XBT_INLINE void xbt_dynar_push(xbt_dynar_t const dynar,
607                                const void *const src)
608 {
609   _dynar_lock(dynar);
610   /* checks done in xbt_dynar_insert_at_ptr */
611   memcpy(_xbt_dynar_insert_at_ptr(dynar, dynar->used), src,
612          dynar->elmsize);
613   _dynar_unlock(dynar);
614 }
615
616 /** @brief Mark the last dynar's element as unused and return a pointer to it.
617  *
618  * You can then use regular affectation to set its value instead of relying
619  * on the slow memcpy. This is what xbt_dynar_pop_as() does.
620  */
621 XBT_INLINE void *xbt_dynar_pop_ptr(xbt_dynar_t const dynar)
622 {
623   void *res;
624
625   _dynar_lock(dynar);
626   _check_populated_dynar(dynar);
627   XBT_DEBUG("Pop %p", (void *) dynar);
628   dynar->used--;
629   res = _xbt_dynar_elm(dynar, dynar->used);
630   _dynar_unlock(dynar);
631   return res;
632 }
633
634 /** @brief Get and remove the last element of the dynar */
635 XBT_INLINE void xbt_dynar_pop(xbt_dynar_t const dynar, void *const dst)
636 {
637
638   /* sanity checks done by remove_at */
639   XBT_DEBUG("Pop %p", (void *) dynar);
640   _dynar_lock(dynar);
641   _xbt_dynar_remove_at(dynar, dynar->used - 1, dst);
642   _dynar_unlock(dynar);
643 }
644
645 /** @brief Add an element at the begining of the dynar.
646  *
647  * This is less efficient than xbt_dynar_push()
648  */
649 XBT_INLINE void xbt_dynar_unshift(xbt_dynar_t const dynar,
650                                   const void *const src)
651 {
652
653   /* sanity checks done by insert_at */
654   xbt_dynar_insert_at(dynar, 0, src);
655 }
656
657 /** @brief Get and remove the first element of the dynar.
658  *
659  * This is less efficient than xbt_dynar_pop()
660  */
661 XBT_INLINE void xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst)
662 {
663
664   /* sanity checks done by remove_at */
665   xbt_dynar_remove_at(dynar, 0, dst);
666 }
667
668 static void _dynar_map(const xbt_dynar_t dynar, void_f_pvoid_t const op)
669 {
670   char *const data = (char *) dynar->data;
671   const unsigned long elmsize = dynar->elmsize;
672   const unsigned long used = dynar->used;
673   unsigned long i;
674
675   for (i = 0; i < used; i++) {
676     char* elm = (char*) data + i * elmsize;
677     op(elm);
678   }
679 }
680
681 /** @brief Apply a function to each member of a dynar
682  *
683  * The mapped function may change the value of the element itself,
684  * but should not mess with the structure of the dynar.
685  *
686  * If the dynar is synchronized, it is locked during the whole map
687  * operation, so make sure your function don't call any function
688  * from xbt_dynar_* on it, or you'll get a deadlock.
689  */
690 XBT_INLINE void xbt_dynar_map(const xbt_dynar_t dynar,
691                               void_f_pvoid_t const op)
692 {
693
694   _sanity_check_dynar(dynar);
695   _dynar_lock(dynar);
696
697   _dynar_map(dynar, op);
698
699   _dynar_unlock(dynar);
700 }
701
702
703 /** @brief Removes and free the entry pointed by the cursor
704  *
705  * This function can be used while traversing without problem.
706  */
707 XBT_INLINE void xbt_dynar_cursor_rm(xbt_dynar_t dynar,
708                                     unsigned int *const cursor)
709 {
710
711   _xbt_dynar_remove_at(dynar, (*cursor)--, NULL);
712 }
713
714 /** @brief Unlocks a synchronized dynar when you want to break the traversal
715  *
716  * This function must be used if you <tt>break</tt> the
717  * xbt_dynar_foreach loop, but shouldn't be called at the end of a
718  * regular traversal reaching the end of the elements
719  */
720 XBT_INLINE void xbt_dynar_cursor_unlock(xbt_dynar_t dynar)
721 {
722   _dynar_unlock(dynar);
723 }
724
725 /** @brief Sorts a dynar according to the function <tt>compar_fn</tt>
726  *
727  * \param dynar the dynar to sort
728  * \param compar_fn comparison function of type (int (compar_fn*) (void*) (void*)).
729  *
730  * Remark: if the elements stored in the dynar are structures, the compar_fn
731  * function has to retrieve the field to sort first.
732  */
733 XBT_INLINE void xbt_dynar_sort(xbt_dynar_t dynar,
734                                int_f_cpvoid_cpvoid_t compar_fn)
735 {
736
737   _dynar_lock(dynar);
738
739 #ifdef HAVE_MERGESORT
740   mergesort(dynar->data, dynar->used, dynar->elmsize, compar_fn);
741 #else
742   qsort(dynar->data, dynar->used, dynar->elmsize, compar_fn);
743 #endif
744   _dynar_unlock(dynar);
745 }
746
747 /** @brief Transform a dynar into a NULL terminated array
748  *
749  * \param dynar the dynar to transform
750  */
751 XBT_INLINE void * xbt_dynar_to_array (xbt_dynar_t dynar)
752 {
753   void * res;
754         void * last = xbt_new0(char,dynar->elmsize);
755         xbt_dynar_push(dynar, last);
756         free(last);
757         res = dynar->data;
758         free(dynar);
759         return res;
760 }
761
762 /*
763  * Return 0 if d1 and d2 are equal and 1 if not equal
764  */
765 XBT_INLINE int xbt_dynar_compare(xbt_dynar_t d1, xbt_dynar_t d2,
766                                         int(*compar)(const void *, const void *))
767 {
768         int i ;
769         int size;
770         if((!d1) && (!d2)) return 0;
771         if((!d1) || (!d2))
772         {
773                 XBT_DEBUG("NULL dynar d1=%p d2=%p",d1,d2);
774                 xbt_dynar_free(&d2);
775                 return 1;
776         }
777         if((d1->elmsize)!=(d2->elmsize))
778         {
779                 XBT_DEBUG("Size of elmsize d1=%ld d2=%ld",d1->elmsize,d2->elmsize);
780                 xbt_dynar_free(&d2);
781                 return 1; // xbt_die
782         }
783         if(xbt_dynar_length(d1) != xbt_dynar_length(d2))
784         {
785                 XBT_DEBUG("Size of dynar d1=%ld d2=%ld",xbt_dynar_length(d1),xbt_dynar_length(d2));
786                 xbt_dynar_free(&d2);
787                 return 1;
788         }
789
790         size = xbt_dynar_length(d1);
791         for(i=0;i<size;i++)
792         {
793                 void *data1 = xbt_dynar_get_as(d1, i, void *);
794                 void *data2 = xbt_dynar_get_as(d2, i, void *);
795                 XBT_DEBUG("link[%d] d1=%p d2=%p",i,data1,data2);
796                 if(compar(data1,data2)){
797                         xbt_dynar_free(&d2);
798                         return 1;
799                 }
800         }
801         xbt_dynar_free(&d2);
802         return 0;
803 }
804
805 #ifdef SIMGRID_TEST
806
807 #define NB_ELEM 5000
808
809 XBT_TEST_SUITE("dynar", "Dynar data container");
810 XBT_LOG_EXTERNAL_CATEGORY(xbt_dyn);
811 XBT_LOG_DEFAULT_CATEGORY(xbt_dyn);
812
813 XBT_TEST_UNIT("int", test_dynar_int, "Dynars of integers")
814 {
815   /* Vars_decl [doxygen cruft] */
816   xbt_dynar_t d;
817   int i, cpt;
818   unsigned int cursor;
819   int *iptr;
820
821   xbt_test_add("==== Traverse the empty dynar");
822   d = xbt_dynar_new(sizeof(int), NULL);
823   xbt_dynar_foreach(d, cursor, i) {
824     xbt_die( "Damnit, there is something in the empty dynar");
825   }
826   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
827   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
828   /* in your code is naturally the way to go outside a regression test */
829
830   xbt_test_add
831       ("==== Push %d int, set them again 3 times, traverse them, shift them",
832        NB_ELEM);
833   /* Populate_ints [doxygen cruft] */
834   /* 1. Populate the dynar */
835   d = xbt_dynar_new(sizeof(int), NULL);
836   for (cpt = 0; cpt < NB_ELEM; cpt++) {
837     xbt_dynar_push_as(d, int, cpt);     /* This is faster (and possible only with scalars) */
838     /* xbt_dynar_push(d,&cpt);       This would also work */
839     xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
840   }
841
842   /* 2. Traverse manually the dynar */
843   for (cursor = 0; cursor < NB_ELEM; cursor++) {
844     iptr = xbt_dynar_get_ptr(d, cursor);
845     xbt_test_assert(cursor == *iptr,
846                      "The retrieved value is not the same than the injected one (%d!=%d)",
847                      cursor, cpt);
848   }
849
850   /* 3. Traverse the dynar using the neat macro to that extend */
851   xbt_dynar_foreach(d, cursor, cpt) {
852     xbt_test_assert(cursor == cpt,
853                      "The retrieved value is not the same than the injected one (%d!=%d)",
854                      cursor, cpt);
855   }
856   /* end_of_traversal */
857
858   for (cpt = 0; cpt < NB_ELEM; cpt++)
859     *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
860
861   for (cpt = 0; cpt < NB_ELEM; cpt++)
862     *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
863   /*     xbt_dynar_set(d,cpt,&cpt); */
864
865   for (cpt = 0; cpt < NB_ELEM; cpt++)
866     *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
867
868   cpt = 0;
869   xbt_dynar_foreach(d, cursor, i) {
870     xbt_test_assert(i == cpt,
871                      "The retrieved value is not the same than the injected one (%d!=%d)",
872                      i, cpt);
873     cpt++;
874   }
875   xbt_test_assert(cpt == NB_ELEM,
876                    "Cannot retrieve my %d values. Last got one is %d",
877                    NB_ELEM, cpt);
878
879   /* shifting [doxygen cruft] */
880   /* 4. Shift all the values */
881   for (cpt = 0; cpt < NB_ELEM; cpt++) {
882     xbt_dynar_shift(d, &i);
883     xbt_test_assert(i == cpt,
884                      "The retrieved value is not the same than the injected one (%d!=%d)",
885                      i, cpt);
886     xbt_test_log("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
887   }
888
889   /* 5. Free the resources */
890   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
891   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
892   /* in your code is naturally the way to go outside a regression test */
893
894   xbt_test_add("==== Unshift/pop %d int", NB_ELEM);
895   d = xbt_dynar_new(sizeof(int), NULL);
896   for (cpt = 0; cpt < NB_ELEM; cpt++) {
897     xbt_dynar_unshift(d, &cpt);
898     XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
899   }
900   for (cpt = 0; cpt < NB_ELEM; cpt++) {
901     i = xbt_dynar_pop_as(d, int);
902     xbt_test_assert(i == cpt,
903                      "The retrieved value is not the same than the injected one (%d!=%d)",
904                      i, cpt);
905     xbt_test_log("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
906   }
907   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
908   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
909   /* in your code is naturally the way to go outside a regression test */
910
911
912   xbt_test_add
913       ("==== Push %d int, insert 1000 int in the middle, shift everything",
914        NB_ELEM);
915   d = xbt_dynar_new(sizeof(int), NULL);
916   for (cpt = 0; cpt < NB_ELEM; cpt++) {
917     xbt_dynar_push_as(d, int, cpt);
918     XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
919   }
920   for (cpt = 0; cpt < NB_ELEM/5; cpt++) {
921     xbt_dynar_insert_at_as(d, NB_ELEM/2, int, cpt);
922     XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
923   }
924
925   for (cpt = 0; cpt < NB_ELEM/2; cpt++) {
926     xbt_dynar_shift(d, &i);
927     xbt_test_assert(i == cpt,
928                      "The retrieved value is not the same than the injected one at the begining (%d!=%d)",
929                      i, cpt);
930     XBT_DEBUG("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
931   }
932   for (cpt = 999; cpt >= 0; cpt--) {
933     xbt_dynar_shift(d, &i);
934     xbt_test_assert(i == cpt,
935                      "The retrieved value is not the same than the injected one in the middle (%d!=%d)",
936                      i, cpt);
937   }
938   for (cpt = 2500; cpt < NB_ELEM; cpt++) {
939     xbt_dynar_shift(d, &i);
940     xbt_test_assert(i == cpt,
941                      "The retrieved value is not the same than the injected one at the end (%d!=%d)",
942                      i, cpt);
943   }
944   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
945   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
946   /* in your code is naturally the way to go outside a regression test */
947
948   xbt_test_add("==== Push %d int, remove 2000-4000. free the rest",
949                 NB_ELEM);
950   d = xbt_dynar_new(sizeof(int), NULL);
951   for (cpt = 0; cpt < NB_ELEM; cpt++)
952     xbt_dynar_push_as(d, int, cpt);
953
954   for (cpt = 2000; cpt < 4000; cpt++) {
955     xbt_dynar_remove_at(d, 2000, &i);
956     xbt_test_assert(i == cpt,
957                      "Remove a bad value. Got %d, expected %d", i, cpt);
958     XBT_DEBUG("remove %d, length=%lu", cpt, xbt_dynar_length(d));
959   }
960   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
961   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
962   /* in your code is naturally the way to go outside a regression test */
963 }
964
965 /*******************************************************************************/
966 /*******************************************************************************/
967 /*******************************************************************************/
968 XBT_TEST_UNIT("insert",test_dynar_insert,"Using the xbt_dynar_insert and xbt_dynar_remove functions")
969 {
970   xbt_dynar_t d = xbt_dynar_new(sizeof(unsigned int), NULL);
971   unsigned int cursor;
972   int cpt;
973
974   xbt_test_add("==== Insert %d int, traverse them, remove them",NB_ELEM);
975   /* Populate_ints [doxygen cruft] */
976   /* 1. Populate the dynar */
977   for (cpt = 0; cpt < NB_ELEM; cpt++) {
978     xbt_dynar_insert_at(d, cpt, &cpt);
979     xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
980   }
981
982   /* 3. Traverse the dynar */
983   xbt_dynar_foreach(d, cursor, cpt) {
984     xbt_test_assert(cursor == cpt,
985                      "The retrieved value is not the same than the injected one (%d!=%d)",
986                      cursor, cpt);
987   }
988   /* end_of_traversal */
989
990   /* Re-fill with the same values using set_as (and re-verify) */
991   for (cpt = 0; cpt < NB_ELEM; cpt++)
992     xbt_dynar_set_as(d, cpt, int, cpt);
993   xbt_dynar_foreach(d, cursor, cpt)
994     xbt_test_assert(cursor == cpt,
995                      "The retrieved value is not the same than the injected one (%d!=%d)",
996                      cursor, cpt);
997
998   for (cpt = 0; cpt < NB_ELEM; cpt++) {
999     int val;
1000     xbt_dynar_remove_at(d,0,&val);
1001     xbt_test_assert(cpt == val,
1002                      "The retrieved value is not the same than the injected one (%d!=%d)",
1003                      cursor, cpt);
1004   }
1005   xbt_test_assert(xbt_dynar_is_empty(d),
1006                    "There is still %lu elements in the dynar after removing everything",
1007                    xbt_dynar_length(d));
1008   xbt_dynar_free(&d);
1009
1010   /* ********************* */
1011   xbt_test_add("==== Insert %d int in reverse order, traverse them, remove them",NB_ELEM);
1012   d = xbt_dynar_new(sizeof(int), NULL);
1013   for (cpt = NB_ELEM-1; cpt >=0; cpt--) {
1014     xbt_dynar_replace(d, cpt, &cpt);
1015     xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
1016   }
1017
1018   /* 3. Traverse the dynar */
1019   xbt_dynar_foreach(d, cursor, cpt) {
1020     xbt_test_assert(cursor == cpt,
1021                      "The retrieved value is not the same than the injected one (%d!=%d)",
1022                      cursor, cpt);
1023   }
1024   /* end_of_traversal */
1025
1026   for (cpt =NB_ELEM-1; cpt >=0; cpt--) {
1027     int val;
1028     xbt_dynar_remove_at(d,xbt_dynar_length(d)-1,&val);
1029     xbt_test_assert(cpt == val,
1030                      "The retrieved value is not the same than the injected one (%d!=%d)",
1031                      cursor, cpt);
1032   }
1033   xbt_test_assert(xbt_dynar_is_empty(d),
1034                    "There is still %lu elements in the dynar after removing everything",
1035                    xbt_dynar_length(d));
1036   xbt_dynar_free(&d);
1037 }
1038
1039 /*******************************************************************************/
1040 /*******************************************************************************/
1041 /*******************************************************************************/
1042 XBT_TEST_UNIT("double", test_dynar_double, "Dynars of doubles")
1043 {
1044   xbt_dynar_t d;
1045   int cpt;
1046   unsigned int cursor;
1047   double d1, d2;
1048
1049   xbt_test_add("==== Traverse the empty dynar");
1050   d = xbt_dynar_new(sizeof(int), NULL);
1051   xbt_dynar_foreach(d, cursor, cpt) {
1052     xbt_test_assert(FALSE,
1053                      "Damnit, there is something in the empty dynar");
1054   }
1055   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1056   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1057   /* in your code is naturally the way to go outside a regression test */
1058
1059   xbt_test_add("==== Push/shift 5000 doubles");
1060   d = xbt_dynar_new(sizeof(double), NULL);
1061   for (cpt = 0; cpt < 5000; cpt++) {
1062     d1 = (double) cpt;
1063     xbt_dynar_push(d, &d1);
1064   }
1065   xbt_dynar_foreach(d, cursor, d2) {
1066     d1 = (double) cursor;
1067     xbt_test_assert(d1 == d2,
1068                      "The retrieved value is not the same than the injected one (%f!=%f)",
1069                      d1, d2);
1070   }
1071   for (cpt = 0; cpt < 5000; cpt++) {
1072     d1 = (double) cpt;
1073     xbt_dynar_shift(d, &d2);
1074     xbt_test_assert(d1 == d2,
1075                      "The retrieved value is not the same than the injected one (%f!=%f)",
1076                      d1, d2);
1077   }
1078   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1079   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1080   /* in your code is naturally the way to go outside a regression test */
1081
1082   xbt_test_add("==== Unshift/pop 5000 doubles");
1083   d = xbt_dynar_new(sizeof(double), NULL);
1084   for (cpt = 0; cpt < 5000; cpt++) {
1085     d1 = (double) cpt;
1086     xbt_dynar_unshift(d, &d1);
1087   }
1088   for (cpt = 0; cpt < 5000; cpt++) {
1089     d1 = (double) cpt;
1090     xbt_dynar_pop(d, &d2);
1091     xbt_test_assert(d1 == d2,
1092                      "The retrieved value is not the same than the injected one (%f!=%f)",
1093                      d1, d2);
1094   }
1095   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1096   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1097   /* in your code is naturally the way to go outside a regression test */
1098
1099
1100
1101   xbt_test_add
1102       ("==== Push 5000 doubles, insert 1000 doubles in the middle, shift everything");
1103   d = xbt_dynar_new(sizeof(double), NULL);
1104   for (cpt = 0; cpt < 5000; cpt++) {
1105     d1 = (double) cpt;
1106     xbt_dynar_push(d, &d1);
1107   }
1108   for (cpt = 0; cpt < 1000; cpt++) {
1109     d1 = (double) cpt;
1110     xbt_dynar_insert_at(d, 2500, &d1);
1111   }
1112
1113   for (cpt = 0; cpt < 2500; cpt++) {
1114     d1 = (double) cpt;
1115     xbt_dynar_shift(d, &d2);
1116     xbt_test_assert(d1 == d2,
1117                      "The retrieved value is not the same than the injected one at the begining (%f!=%f)",
1118                      d1, d2);
1119     XBT_DEBUG("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
1120   }
1121   for (cpt = 999; cpt >= 0; cpt--) {
1122     d1 = (double) cpt;
1123     xbt_dynar_shift(d, &d2);
1124     xbt_test_assert(d1 == d2,
1125                      "The retrieved value is not the same than the injected one in the middle (%f!=%f)",
1126                      d1, d2);
1127   }
1128   for (cpt = 2500; cpt < 5000; cpt++) {
1129     d1 = (double) cpt;
1130     xbt_dynar_shift(d, &d2);
1131     xbt_test_assert(d1 == d2,
1132                      "The retrieved value is not the same than the injected one at the end (%f!=%f)",
1133                      d1, d2);
1134   }
1135   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1136   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1137   /* in your code is naturally the way to go outside a regression test */
1138
1139
1140   xbt_test_add("==== Push 5000 double, remove 2000-4000. free the rest");
1141   d = xbt_dynar_new(sizeof(double), NULL);
1142   for (cpt = 0; cpt < 5000; cpt++) {
1143     d1 = (double) cpt;
1144     xbt_dynar_push(d, &d1);
1145   }
1146   for (cpt = 2000; cpt < 4000; cpt++) {
1147     d1 = (double) cpt;
1148     xbt_dynar_remove_at(d, 2000, &d2);
1149     xbt_test_assert(d1 == d2,
1150                      "Remove a bad value. Got %f, expected %f", d2, d1);
1151   }
1152   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1153   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1154   /* in your code is naturally the way to go outside a regression test */
1155 }
1156
1157
1158 /* doxygen_string_cruft */
1159
1160 /*******************************************************************************/
1161 /*******************************************************************************/
1162 /*******************************************************************************/
1163 XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings")
1164 {
1165   xbt_dynar_t d;
1166   int cpt;
1167   unsigned int iter;
1168   char buf[1024];
1169   char *s1, *s2;
1170
1171   xbt_test_add("==== Traverse the empty dynar");
1172   d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1173   xbt_dynar_foreach(d, iter, s1) {
1174     xbt_test_assert(FALSE,
1175                      "Damnit, there is something in the empty dynar");
1176   }
1177   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1178   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1179   /* in your code is naturally the way to go outside a regression test */
1180
1181   xbt_test_add("==== Push %d strings, set them again 3 times, shift them",
1182                 NB_ELEM);
1183   /* Populate_str [doxygen cruft] */
1184   d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1185   /* 1. Populate the dynar */
1186   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1187     sprintf(buf, "%d", cpt);
1188     s1 = strdup(buf);
1189     xbt_dynar_push(d, &s1);
1190   }
1191   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1192     sprintf(buf, "%d", cpt);
1193     s1 = strdup(buf);
1194     xbt_dynar_replace(d, cpt, &s1);
1195   }
1196   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1197     sprintf(buf, "%d", cpt);
1198     s1 = strdup(buf);
1199     xbt_dynar_replace(d, cpt, &s1);
1200   }
1201   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1202     sprintf(buf, "%d", cpt);
1203     s1 = strdup(buf);
1204     xbt_dynar_replace(d, cpt, &s1);
1205   }
1206   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1207     sprintf(buf, "%d", cpt);
1208     xbt_dynar_shift(d, &s2);
1209     xbt_test_assert(!strcmp(buf, s2),
1210                      "The retrieved value is not the same than the injected one (%s!=%s)",
1211                      buf, s2);
1212     free(s2);
1213   }
1214   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1215   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1216   /* in your code is naturally the way to go outside a regression test */
1217
1218   xbt_test_add("==== Unshift, traverse and pop %d strings", NB_ELEM);
1219   d = xbt_dynar_new(sizeof(char **), &xbt_free_ref);
1220   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1221     sprintf(buf, "%d", cpt);
1222     s1 = strdup(buf);
1223     xbt_dynar_unshift(d, &s1);
1224   }
1225   /* 2. Traverse the dynar with the macro */
1226   xbt_dynar_foreach(d, iter, s1) {
1227     sprintf(buf, "%d", NB_ELEM - iter - 1);
1228     xbt_test_assert(!strcmp(buf, s1),
1229                      "The retrieved value is not the same than the injected one (%s!=%s)",
1230                      buf, s1);
1231   }
1232   /* 3. Traverse the dynar with the macro */
1233   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1234     sprintf(buf, "%d", cpt);
1235     xbt_dynar_pop(d, &s2);
1236     xbt_test_assert(!strcmp(buf, s2),
1237                      "The retrieved value is not the same than the injected one (%s!=%s)",
1238                      buf, s2);
1239     free(s2);
1240   }
1241   /* 4. Free the resources */
1242   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1243   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1244   /* in your code is naturally the way to go outside a regression test */
1245
1246
1247   xbt_test_add
1248       ("==== Push %d strings, insert %d strings in the middle, shift everything",
1249        NB_ELEM, NB_ELEM / 5);
1250   d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1251   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1252     sprintf(buf, "%d", cpt);
1253     s1 = strdup(buf);
1254     xbt_dynar_push(d, &s1);
1255   }
1256   for (cpt = 0; cpt < NB_ELEM / 5; cpt++) {
1257     sprintf(buf, "%d", cpt);
1258     s1 = strdup(buf);
1259     xbt_dynar_insert_at(d, NB_ELEM / 2, &s1);
1260   }
1261
1262   for (cpt = 0; cpt < NB_ELEM / 2; cpt++) {
1263     sprintf(buf, "%d", cpt);
1264     xbt_dynar_shift(d, &s2);
1265     xbt_test_assert(!strcmp(buf, s2),
1266                      "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
1267                      buf, s2);
1268     free(s2);
1269   }
1270   for (cpt = (NB_ELEM / 5) - 1; cpt >= 0; cpt--) {
1271     sprintf(buf, "%d", cpt);
1272     xbt_dynar_shift(d, &s2);
1273     xbt_test_assert(!strcmp(buf, s2),
1274                      "The retrieved value is not the same than the injected one in the middle (%s!=%s)",
1275                      buf, s2);
1276     free(s2);
1277   }
1278   for (cpt = NB_ELEM / 2; cpt < NB_ELEM; cpt++) {
1279     sprintf(buf, "%d", cpt);
1280     xbt_dynar_shift(d, &s2);
1281     xbt_test_assert(!strcmp(buf, s2),
1282                      "The retrieved value is not the same than the injected one at the end (%s!=%s)",
1283                      buf, s2);
1284     free(s2);
1285   }
1286   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1287   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1288   /* in your code is naturally the way to go outside a regression test */
1289
1290
1291   xbt_test_add("==== Push %d strings, remove %d-%d. free the rest",
1292                 NB_ELEM, 2 * (NB_ELEM / 5), 4 * (NB_ELEM / 5));
1293   d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1294   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1295     sprintf(buf, "%d", cpt);
1296     s1 = strdup(buf);
1297     xbt_dynar_push(d, &s1);
1298   }
1299   for (cpt = 2 * (NB_ELEM / 5); cpt < 4 * (NB_ELEM / 5); cpt++) {
1300     sprintf(buf, "%d", cpt);
1301     xbt_dynar_remove_at(d, 2 * (NB_ELEM / 5), &s2);
1302     xbt_test_assert(!strcmp(buf, s2),
1303                      "Remove a bad value. Got %s, expected %s", s2, buf);
1304     free(s2);
1305   }
1306   xbt_dynar_free(&d);           /* end_of_doxygen */
1307 }
1308
1309
1310 /*******************************************************************************/
1311 /*******************************************************************************/
1312 /*******************************************************************************/
1313 #include "xbt/synchro.h"
1314 static void pusher_f(void *a)
1315 {
1316   xbt_dynar_t d = (xbt_dynar_t) a;
1317   int i;
1318   for (i = 0; i < 500; i++) {
1319     xbt_dynar_push(d, &i);
1320   }
1321 }
1322
1323 static void poper_f(void *a)
1324 {
1325   xbt_dynar_t d = (xbt_dynar_t) a;
1326   volatile int i;
1327   int data;
1328   xbt_ex_t e;
1329
1330   for (i = 0; i < 500; i++) {
1331     TRY {
1332       xbt_dynar_pop(d, &data);
1333     }
1334     CATCH(e) {
1335       if (e.category == bound_error) {
1336         xbt_ex_free(e);
1337         i--;
1338       } else {
1339         RETHROW;
1340       }
1341     }
1342   }
1343 }
1344
1345
1346 XBT_TEST_UNIT("synchronized int", test_dynar_sync_int, "Synchronized dynars of integers")
1347 {
1348   /* Vars_decl [doxygen cruft] */
1349   xbt_dynar_t d;
1350   xbt_thread_t pusher, poper;
1351
1352   xbt_test_add("==== Have a pusher and a popper on the dynar");
1353   d = xbt_dynar_new_sync(sizeof(int), NULL);
1354   pusher = xbt_thread_create("pusher", pusher_f, d, 0 /*not joinable */ );
1355   poper = xbt_thread_create("poper", poper_f, d, 0 /*not joinable */ );
1356   xbt_thread_join(pusher);
1357   xbt_thread_join(poper);
1358   xbt_dynar_free(&d);
1359 }
1360
1361 #endif                          /* SIMGRID_TEST */