Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'dvfs'
[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 "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_DEBUG("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_DEBUG("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_DEBUG("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 #ifdef HAVE_MERGESORT
642   mergesort(dynar->data, dynar->used, dynar->elmsize, compar_fn);
643 #else
644   qsort(dynar->data, dynar->used, dynar->elmsize, compar_fn);
645 #endif
646 }
647
648 /** @brief Sorts a dynar according to their color assuming elements can have only three colors.
649  * Since there are only three colors, it is linear and much faster than a classical sort.
650  * See for example http://en.wikipedia.org/wiki/Dutch_national_flag_problem
651  *
652  * \param dynar the dynar to sort
653  * \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.
654  *
655  * 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.
656  *
657  * Remark: if the elements stored in the dynar are structures, the color
658  * function has to retrieve the field to sort first.
659  */
660 XBT_PUBLIC(void) xbt_dynar_three_way_partition(xbt_dynar_t const dynar,
661                                                int_f_pvoid_t color)
662 {
663   unsigned long int i;
664   unsigned long int p = -1;
665   unsigned long int q = dynar->used;
666   const unsigned long elmsize = dynar->elmsize;
667   void *tmp = xbt_malloc(elmsize);
668   void *elm;
669
670   for (i = 0; i < q;) {
671     void *elmi = _xbt_dynar_elm(dynar, i);
672     int colori = color(elmi);
673
674     if (colori == 1) {
675       ++i;
676     } else {
677       if (colori == 0) {
678         elm = _xbt_dynar_elm(dynar, ++p);
679         ++i;
680       } else {                  /* colori == 2 */
681         elm = _xbt_dynar_elm(dynar, --q);
682       }
683       if (elm != elmi) {
684         memcpy(tmp,  elm,  elmsize);
685         memcpy(elm,  elmi, elmsize);
686         memcpy(elmi, tmp,  elmsize);
687       }
688     }
689   }
690   xbt_free(tmp);
691 }
692
693 /** @brief Transform a dynar into a NULL terminated array. 
694  *  The dynar won't be usable afterwards.
695  * \param dynar the dynar to transform
696  */
697 XBT_INLINE void * xbt_dynar_to_array (xbt_dynar_t dynar)
698 {
699   void *res;
700   xbt_dynar_shrink(dynar, 1);
701   memset(xbt_dynar_push_ptr(dynar), 0, dynar->elmsize);
702   res = dynar->data;
703   free(dynar);
704   return res;
705 }
706
707 /*
708  * Return 0 if d1 and d2 are equal and 1 if not equal
709  */
710 XBT_INLINE int xbt_dynar_compare(xbt_dynar_t d1, xbt_dynar_t d2,
711           int(*compar)(const void *, const void *))
712 {
713   int i ;
714   int size;
715   if((!d1) && (!d2)) return 0;
716   if((!d1) || (!d2))
717   {
718     XBT_DEBUG("NULL dynar d1=%p d2=%p",d1,d2);
719     xbt_dynar_free(&d2);
720     return 1;
721   }
722   if((d1->elmsize)!=(d2->elmsize))
723   {
724     XBT_DEBUG("Size of elmsize d1=%lu d2=%lu",d1->elmsize,d2->elmsize);
725     xbt_dynar_free(&d2);
726     return 1; // xbt_die
727   }
728   if(xbt_dynar_length(d1) != xbt_dynar_length(d2))
729   {
730     XBT_DEBUG("Size of dynar d1=%lu d2=%lu",xbt_dynar_length(d1),xbt_dynar_length(d2));
731     xbt_dynar_free(&d2);
732     return 1;
733   }
734
735   size = xbt_dynar_length(d1);
736   for(i=0;i<size;i++)
737   {
738     void *data1 = xbt_dynar_get_as(d1, i, void *);
739     void *data2 = xbt_dynar_get_as(d2, i, void *);
740     XBT_DEBUG("link[%d] d1=%p d2=%p",i,data1,data2);
741     if(compar(data1,data2)){
742       xbt_dynar_free(&d2);
743       return 1;
744     }
745   }
746   xbt_dynar_free(&d2);
747   return 0;
748 }
749
750 #ifdef SIMGRID_TEST
751
752 #define NB_ELEM 5000
753
754 XBT_TEST_SUITE("dynar", "Dynar data container");
755 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_dyn);
756
757 XBT_TEST_UNIT("int", test_dynar_int, "Dynars of integers")
758 {
759   /* Vars_decl [doxygen cruft] */
760   xbt_dynar_t d;
761   int i, cpt;
762   unsigned int cursor;
763   int *iptr;
764
765   xbt_test_add("==== Traverse the empty dynar");
766   d = xbt_dynar_new(sizeof(int), NULL);
767   xbt_dynar_foreach(d, cursor, i) {
768     xbt_die( "Damnit, there is something in the empty dynar");
769   }
770   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
771   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
772   /* in your code is naturally the way to go outside a regression test */
773
774   xbt_test_add
775       ("==== Push %d int, set them again 3 times, traverse them, shift them",
776        NB_ELEM);
777   /* Populate_ints [doxygen cruft] */
778   /* 1. Populate the dynar */
779   d = xbt_dynar_new(sizeof(int), NULL);
780   for (cpt = 0; cpt < NB_ELEM; cpt++) {
781     xbt_dynar_push_as(d, int, cpt);     /* This is faster (and possible only with scalars) */
782     /* xbt_dynar_push(d,&cpt);       This would also work */
783     xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
784   }
785
786   /* 2. Traverse manually the dynar */
787   for (cursor = 0; cursor < NB_ELEM; cursor++) {
788     iptr = xbt_dynar_get_ptr(d, cursor);
789     xbt_test_assert(cursor == *iptr,
790                      "The retrieved value is not the same than the injected one (%u!=%d)",
791                      cursor, cpt);
792   }
793
794   /* 3. Traverse the dynar using the neat macro to that extend */
795   xbt_dynar_foreach(d, cursor, cpt) {
796     xbt_test_assert(cursor == cpt,
797                      "The retrieved value is not the same than the injected one (%u!=%d)",
798                      cursor, cpt);
799   }
800   /* end_of_traversal */
801
802   for (cpt = 0; cpt < NB_ELEM; cpt++)
803     *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
804
805   for (cpt = 0; cpt < NB_ELEM; cpt++)
806     *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
807   /*     xbt_dynar_set(d,cpt,&cpt); */
808
809   for (cpt = 0; cpt < NB_ELEM; cpt++)
810     *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
811
812   cpt = 0;
813   xbt_dynar_foreach(d, cursor, i) {
814     xbt_test_assert(i == cpt,
815                      "The retrieved value is not the same than the injected one (%d!=%d)",
816                      i, cpt);
817     cpt++;
818   }
819   xbt_test_assert(cpt == NB_ELEM,
820                    "Cannot retrieve my %d values. Last got one is %d",
821                    NB_ELEM, cpt);
822
823   /* shifting [doxygen cruft] */
824   /* 4. Shift all the values */
825   for (cpt = 0; cpt < NB_ELEM; cpt++) {
826     xbt_dynar_shift(d, &i);
827     xbt_test_assert(i == cpt,
828                      "The retrieved value is not the same than the injected one (%d!=%d)",
829                      i, cpt);
830     xbt_test_log("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
831   }
832
833   /* 5. Free the resources */
834   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
835   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
836   /* in your code is naturally the way to go outside a regression test */
837
838   xbt_test_add("==== Unshift/pop %d int", NB_ELEM);
839   d = xbt_dynar_new(sizeof(int), NULL);
840   for (cpt = 0; cpt < NB_ELEM; cpt++) {
841     xbt_dynar_unshift(d, &cpt);
842     XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
843   }
844   for (cpt = 0; cpt < NB_ELEM; cpt++) {
845     i = xbt_dynar_pop_as(d, int);
846     xbt_test_assert(i == cpt,
847                      "The retrieved value is not the same than the injected one (%d!=%d)",
848                      i, cpt);
849     xbt_test_log("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
850   }
851   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
852   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
853   /* in your code is naturally the way to go outside a regression test */
854
855
856   xbt_test_add
857       ("==== Push %d int, insert 1000 int in the middle, shift everything",
858        NB_ELEM);
859   d = xbt_dynar_new(sizeof(int), NULL);
860   for (cpt = 0; cpt < NB_ELEM; cpt++) {
861     xbt_dynar_push_as(d, int, cpt);
862     XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
863   }
864   for (cpt = 0; cpt < NB_ELEM/5; cpt++) {
865     xbt_dynar_insert_at_as(d, NB_ELEM/2, int, cpt);
866     XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
867   }
868
869   for (cpt = 0; cpt < NB_ELEM/2; cpt++) {
870     xbt_dynar_shift(d, &i);
871     xbt_test_assert(i == cpt,
872                      "The retrieved value is not the same than the injected one at the begining (%d!=%d)",
873                      i, cpt);
874     XBT_DEBUG("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
875   }
876   for (cpt = 999; cpt >= 0; cpt--) {
877     xbt_dynar_shift(d, &i);
878     xbt_test_assert(i == cpt,
879                      "The retrieved value is not the same than the injected one in the middle (%d!=%d)",
880                      i, cpt);
881   }
882   for (cpt = 2500; cpt < NB_ELEM; cpt++) {
883     xbt_dynar_shift(d, &i);
884     xbt_test_assert(i == cpt,
885                      "The retrieved value is not the same than the injected one at the end (%d!=%d)",
886                      i, cpt);
887   }
888   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
889   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
890   /* in your code is naturally the way to go outside a regression test */
891
892   xbt_test_add("==== Push %d int, remove 2000-4000. free the rest",
893                 NB_ELEM);
894   d = xbt_dynar_new(sizeof(int), NULL);
895   for (cpt = 0; cpt < NB_ELEM; cpt++)
896     xbt_dynar_push_as(d, int, cpt);
897
898   for (cpt = 2000; cpt < 4000; cpt++) {
899     xbt_dynar_remove_at(d, 2000, &i);
900     xbt_test_assert(i == cpt,
901                      "Remove a bad value. Got %d, expected %d", i, cpt);
902     XBT_DEBUG("remove %d, length=%lu", cpt, xbt_dynar_length(d));
903   }
904   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
905   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
906   /* in your code is naturally the way to go outside a regression test */
907 }
908
909 /*******************************************************************************/
910 /*******************************************************************************/
911 /*******************************************************************************/
912 XBT_TEST_UNIT("insert",test_dynar_insert,"Using the xbt_dynar_insert and xbt_dynar_remove functions")
913 {
914   xbt_dynar_t d = xbt_dynar_new(sizeof(unsigned int), NULL);
915   unsigned int cursor;
916   int cpt;
917
918   xbt_test_add("==== Insert %d int, traverse them, remove them",NB_ELEM);
919   /* Populate_ints [doxygen cruft] */
920   /* 1. Populate the dynar */
921   for (cpt = 0; cpt < NB_ELEM; cpt++) {
922     xbt_dynar_insert_at(d, cpt, &cpt);
923     xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
924   }
925
926   /* 3. Traverse the dynar */
927   xbt_dynar_foreach(d, cursor, cpt) {
928     xbt_test_assert(cursor == cpt,
929                      "The retrieved value is not the same than the injected one (%u!=%d)",
930                      cursor, cpt);
931   }
932   /* end_of_traversal */
933
934   /* Re-fill with the same values using set_as (and re-verify) */
935   for (cpt = 0; cpt < NB_ELEM; cpt++)
936     xbt_dynar_set_as(d, cpt, int, cpt);
937   xbt_dynar_foreach(d, cursor, cpt)
938     xbt_test_assert(cursor == cpt,
939                      "The retrieved value is not the same than the injected one (%u!=%d)",
940                      cursor, cpt);
941
942   for (cpt = 0; cpt < NB_ELEM; cpt++) {
943     int val;
944     xbt_dynar_remove_at(d,0,&val);
945     xbt_test_assert(cpt == val,
946                      "The retrieved value is not the same than the injected one (%u!=%d)",
947                      cursor, cpt);
948   }
949   xbt_test_assert(xbt_dynar_is_empty(d),
950                    "There is still %lu elements in the dynar after removing everything",
951                    xbt_dynar_length(d));
952   xbt_dynar_free(&d);
953
954   /* ********************* */
955   xbt_test_add("==== Insert %d int in reverse order, traverse them, remove them",NB_ELEM);
956   d = xbt_dynar_new(sizeof(int), NULL);
957   for (cpt = NB_ELEM-1; cpt >=0; cpt--) {
958     xbt_dynar_replace(d, cpt, &cpt);
959     xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
960   }
961
962   /* 3. Traverse the dynar */
963   xbt_dynar_foreach(d, cursor, cpt) {
964     xbt_test_assert(cursor == cpt,
965                      "The retrieved value is not the same than the injected one (%u!=%d)",
966                      cursor, cpt);
967   }
968   /* end_of_traversal */
969
970   for (cpt =NB_ELEM-1; cpt >=0; cpt--) {
971     int val;
972     xbt_dynar_remove_at(d,xbt_dynar_length(d)-1,&val);
973     xbt_test_assert(cpt == val,
974                      "The retrieved value is not the same than the injected one (%u!=%d)",
975                      cursor, cpt);
976   }
977   xbt_test_assert(xbt_dynar_is_empty(d),
978                    "There is still %lu elements in the dynar after removing everything",
979                    xbt_dynar_length(d));
980   xbt_dynar_free(&d);
981 }
982
983 /*******************************************************************************/
984 /*******************************************************************************/
985 /*******************************************************************************/
986 XBT_TEST_UNIT("double", test_dynar_double, "Dynars of doubles")
987 {
988   xbt_dynar_t d;
989   int cpt;
990   unsigned int cursor;
991   double d1, d2;
992
993   xbt_test_add("==== Traverse the empty dynar");
994   d = xbt_dynar_new(sizeof(int), NULL);
995   xbt_dynar_foreach(d, cursor, cpt) {
996     xbt_test_assert(FALSE,
997                      "Damnit, there is something in the empty dynar");
998   }
999   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1000   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1001   /* in your code is naturally the way to go outside a regression test */
1002
1003   xbt_test_add("==== Push/shift 5000 doubles");
1004   d = xbt_dynar_new(sizeof(double), NULL);
1005   for (cpt = 0; cpt < 5000; cpt++) {
1006     d1 = (double) cpt;
1007     xbt_dynar_push(d, &d1);
1008   }
1009   xbt_dynar_foreach(d, cursor, d2) {
1010     d1 = (double) cursor;
1011     xbt_test_assert(d1 == d2,
1012                      "The retrieved value is not the same than the injected one (%f!=%f)",
1013                      d1, d2);
1014   }
1015   for (cpt = 0; cpt < 5000; cpt++) {
1016     d1 = (double) cpt;
1017     xbt_dynar_shift(d, &d2);
1018     xbt_test_assert(d1 == d2,
1019                      "The retrieved value is not the same than the injected one (%f!=%f)",
1020                      d1, d2);
1021   }
1022   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1023   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1024   /* in your code is naturally the way to go outside a regression test */
1025
1026   xbt_test_add("==== Unshift/pop 5000 doubles");
1027   d = xbt_dynar_new(sizeof(double), NULL);
1028   for (cpt = 0; cpt < 5000; cpt++) {
1029     d1 = (double) cpt;
1030     xbt_dynar_unshift(d, &d1);
1031   }
1032   for (cpt = 0; cpt < 5000; cpt++) {
1033     d1 = (double) cpt;
1034     xbt_dynar_pop(d, &d2);
1035     xbt_test_assert(d1 == d2,
1036                      "The retrieved value is not the same than the injected one (%f!=%f)",
1037                      d1, d2);
1038   }
1039   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1040   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1041   /* in your code is naturally the way to go outside a regression test */
1042
1043
1044
1045   xbt_test_add
1046       ("==== Push 5000 doubles, insert 1000 doubles in the middle, shift everything");
1047   d = xbt_dynar_new(sizeof(double), NULL);
1048   for (cpt = 0; cpt < 5000; cpt++) {
1049     d1 = (double) cpt;
1050     xbt_dynar_push(d, &d1);
1051   }
1052   for (cpt = 0; cpt < 1000; cpt++) {
1053     d1 = (double) cpt;
1054     xbt_dynar_insert_at(d, 2500, &d1);
1055   }
1056
1057   for (cpt = 0; cpt < 2500; cpt++) {
1058     d1 = (double) cpt;
1059     xbt_dynar_shift(d, &d2);
1060     xbt_test_assert(d1 == d2,
1061                      "The retrieved value is not the same than the injected one at the begining (%f!=%f)",
1062                      d1, d2);
1063     XBT_DEBUG("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
1064   }
1065   for (cpt = 999; cpt >= 0; 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 in the middle (%f!=%f)",
1070                      d1, d2);
1071   }
1072   for (cpt = 2500; cpt < 5000; cpt++) {
1073     d1 = (double) cpt;
1074     xbt_dynar_shift(d, &d2);
1075     xbt_test_assert(d1 == d2,
1076                      "The retrieved value is not the same than the injected one at the end (%f!=%f)",
1077                      d1, d2);
1078   }
1079   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1080   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1081   /* in your code is naturally the way to go outside a regression test */
1082
1083
1084   xbt_test_add("==== Push 5000 double, remove 2000-4000. free the rest");
1085   d = xbt_dynar_new(sizeof(double), NULL);
1086   for (cpt = 0; cpt < 5000; cpt++) {
1087     d1 = (double) cpt;
1088     xbt_dynar_push(d, &d1);
1089   }
1090   for (cpt = 2000; cpt < 4000; cpt++) {
1091     d1 = (double) cpt;
1092     xbt_dynar_remove_at(d, 2000, &d2);
1093     xbt_test_assert(d1 == d2,
1094                      "Remove a bad value. Got %f, expected %f", d2, d1);
1095   }
1096   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1097   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1098   /* in your code is naturally the way to go outside a regression test */
1099 }
1100
1101
1102 /* doxygen_string_cruft */
1103
1104 /*******************************************************************************/
1105 /*******************************************************************************/
1106 /*******************************************************************************/
1107 XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings")
1108 {
1109   xbt_dynar_t d;
1110   int cpt;
1111   unsigned int iter;
1112   char buf[1024];
1113   char *s1, *s2;
1114
1115   xbt_test_add("==== Traverse the empty dynar");
1116   d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1117   xbt_dynar_foreach(d, iter, s1) {
1118     xbt_test_assert(FALSE,
1119                      "Damnit, there is something in the empty dynar");
1120   }
1121   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1122   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1123   /* in your code is naturally the way to go outside a regression test */
1124
1125   xbt_test_add("==== Push %d strings, set them again 3 times, shift them",
1126                 NB_ELEM);
1127   /* Populate_str [doxygen cruft] */
1128   d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1129   /* 1. Populate the dynar */
1130   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1131     sprintf(buf, "%d", cpt);
1132     s1 = strdup(buf);
1133     xbt_dynar_push(d, &s1);
1134   }
1135   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1136     sprintf(buf, "%d", cpt);
1137     s1 = strdup(buf);
1138     xbt_dynar_replace(d, cpt, &s1);
1139   }
1140   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1141     sprintf(buf, "%d", cpt);
1142     s1 = strdup(buf);
1143     xbt_dynar_replace(d, cpt, &s1);
1144   }
1145   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1146     sprintf(buf, "%d", cpt);
1147     s1 = strdup(buf);
1148     xbt_dynar_replace(d, cpt, &s1);
1149   }
1150   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1151     sprintf(buf, "%d", cpt);
1152     xbt_dynar_shift(d, &s2);
1153     xbt_test_assert(!strcmp(buf, s2),
1154                      "The retrieved value is not the same than the injected one (%s!=%s)",
1155                      buf, s2);
1156     free(s2);
1157   }
1158   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1159   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1160   /* in your code is naturally the way to go outside a regression test */
1161
1162   xbt_test_add("==== Unshift, traverse and pop %d strings", NB_ELEM);
1163   d = xbt_dynar_new(sizeof(char **), &xbt_free_ref);
1164   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1165     sprintf(buf, "%d", cpt);
1166     s1 = strdup(buf);
1167     xbt_dynar_unshift(d, &s1);
1168   }
1169   /* 2. Traverse the dynar with the macro */
1170   xbt_dynar_foreach(d, iter, s1) {
1171     sprintf(buf, "%u", NB_ELEM - iter - 1);
1172     xbt_test_assert(!strcmp(buf, s1),
1173                      "The retrieved value is not the same than the injected one (%s!=%s)",
1174                      buf, s1);
1175   }
1176   /* 3. Traverse the dynar with the macro */
1177   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1178     sprintf(buf, "%d", cpt);
1179     xbt_dynar_pop(d, &s2);
1180     xbt_test_assert(!strcmp(buf, s2),
1181                      "The retrieved value is not the same than the injected one (%s!=%s)",
1182                      buf, s2);
1183     free(s2);
1184   }
1185   /* 4. Free the resources */
1186   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1187   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1188   /* in your code is naturally the way to go outside a regression test */
1189
1190
1191   xbt_test_add
1192       ("==== Push %d strings, insert %d strings in the middle, shift everything",
1193        NB_ELEM, NB_ELEM / 5);
1194   d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1195   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1196     sprintf(buf, "%d", cpt);
1197     s1 = strdup(buf);
1198     xbt_dynar_push(d, &s1);
1199   }
1200   for (cpt = 0; cpt < NB_ELEM / 5; cpt++) {
1201     sprintf(buf, "%d", cpt);
1202     s1 = strdup(buf);
1203     xbt_dynar_insert_at(d, NB_ELEM / 2, &s1);
1204   }
1205
1206   for (cpt = 0; cpt < NB_ELEM / 2; cpt++) {
1207     sprintf(buf, "%d", cpt);
1208     xbt_dynar_shift(d, &s2);
1209     xbt_test_assert(!strcmp(buf, s2),
1210                      "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
1211                      buf, s2);
1212     free(s2);
1213   }
1214   for (cpt = (NB_ELEM / 5) - 1; cpt >= 0; 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 in the middle (%s!=%s)",
1219                      buf, s2);
1220     free(s2);
1221   }
1222   for (cpt = NB_ELEM / 2; cpt < NB_ELEM; 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 at the end (%s!=%s)",
1227                      buf, s2);
1228     free(s2);
1229   }
1230   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1231   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1232   /* in your code is naturally the way to go outside a regression test */
1233
1234
1235   xbt_test_add("==== Push %d strings, remove %d-%d. free the rest",
1236                 NB_ELEM, 2 * (NB_ELEM / 5), 4 * (NB_ELEM / 5));
1237   d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1238   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1239     sprintf(buf, "%d", cpt);
1240     s1 = strdup(buf);
1241     xbt_dynar_push(d, &s1);
1242   }
1243   for (cpt = 2 * (NB_ELEM / 5); cpt < 4 * (NB_ELEM / 5); cpt++) {
1244     sprintf(buf, "%d", cpt);
1245     xbt_dynar_remove_at(d, 2 * (NB_ELEM / 5), &s2);
1246     xbt_test_assert(!strcmp(buf, s2),
1247                      "Remove a bad value. Got %s, expected %s", s2, buf);
1248     free(s2);
1249   }
1250   xbt_dynar_free(&d);           /* end_of_doxygen */
1251 }
1252 #endif                          /* SIMGRID_TEST */