Logo AND Algorithmique Numérique Distribuée

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