Logo AND Algorithmique Numérique Distribuée

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