Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix commit f48cc395ebecc84d865ab551a672d2a2358624e5
[simgrid.git] / include / xbt / dynar.h
index 172ff10..34ca2ba 100644 (file)
@@ -27,7 +27,7 @@ SG_BEGIN_DECL()
   * structure creation (of type void_f_ppvoid_t or void_f_pvoid_t).
   *
   * \section XBT_dynar_exscal Example with scalar
-  * \don'tinclude dynar.c
+  * \dontinclude dynar.c
   *
   * \skip Vars_decl
   * \skip dyn
@@ -231,8 +231,8 @@ static XBT_INLINE void
 _xbt_dynar_cursor_first(const xbt_dynar_t dynar,
                         unsigned int *const cursor)
 {
-  /* don't test for dynar!=NULL. The segfault would tell us */
-  if (dynar->mutex)             /* ie _dynar_lock(dynar) but not public */
+  /* iterating over a NULL dynar is a no-op (but we don't want to have uninitialized counters) */
+  if (dynar && dynar->mutex)             /* ie _dynar_lock(dynar) but not public */
     xbt_mutex_acquire(dynar->mutex);
 
   //XBT_DEBUG("Set cursor on %p to the first position", (void *) dynar);
@@ -243,6 +243,8 @@ static XBT_INLINE int
 _xbt_dynar_cursor_get(const xbt_dynar_t dynar,
                       unsigned int idx, void *const dst)
 {
+  if (!dynar) /* iterating over a NULL dynar is a no-op */
+    return FALSE;
 
   if (idx >= dynar->used) {
     //XBT_DEBUG("Cursor on %p already on last elem", (void *) dynar);
@@ -279,7 +281,7 @@ xbt_dynar_foreach (dyn,cpt,str) {
  */
 #define xbt_dynar_foreach(_dynar,_cursor,_data) \
        for (_xbt_dynar_cursor_first(_dynar,&(_cursor))      ; \
-           _xbt_dynar_cursor_get(_dynar,_cursor,&_data) ; \
+      _xbt_dynar_cursor_get(_dynar,_cursor,&_data) ; \
             (_cursor)++         )
 
 /** @} */