Logo AND Algorithmique Numérique Distribuée

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