Logo AND Algorithmique Numérique Distribuée

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