Logo AND Algorithmique Numérique Distribuée

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