Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Ups, you may need this
[simgrid.git] / src / xbt / dynar.c
index 7090ea8..41602f9 100644 (file)
@@ -7,24 +7,18 @@
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
+#include "portable.h" /* SIZEOF_MAX */
 #include "xbt/misc.h"
 #include "xbt/sysdep.h"
 #include "xbt/log.h"
-#include "xbt/error.h"
 #include "xbt/dynar.h"
 #include <sys/types.h>
 
-
+#include "xbt/dynar_private.h" /* type definition, which we share with the 
+                                 code in charge of sending this across the net */
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dynar,xbt,"Dynamic arrays");
 
-typedef struct xbt_dynar_s {
-  unsigned long          size;
-  unsigned long          used;
-  unsigned long          elmsize;
-  void           *data;
-  void_f_pvoid_t *free_f;
-} s_xbt_dynar_t;
 
 #define __sanity_check_dynar(dynar)       \
            xbt_assert0(dynar,           \
@@ -52,10 +46,9 @@ void _xbt_clear_mem(void * const ptr,
 }
 
 static _XBT_INLINE
-xbt_error_t
+void
 _xbt_dynar_expand(xbt_dynar_t const dynar,
                    const int          nb) {
-  xbt_error_t errcode     = no_error;
   const unsigned long old_size    = dynar->size;
 
   if (nb > old_size) {
@@ -84,8 +77,6 @@ _xbt_dynar_expand(xbt_dynar_t const dynar,
     dynar->size = new_size;
     dynar->data = new_data;
   }
-
-  return errcode;
 }
 
 static _XBT_INLINE
@@ -120,6 +111,12 @@ _xbt_dynar_put_elm(const xbt_dynar_t  dynar,
   memcpy(elm, src, elmsize);
 }
 
+void
+xbt_dynar_dump(xbt_dynar_t dynar) {
+  INFO5("Dynar dump: size=%lu; used=%lu; elmsize=%lu; data=%p; free_f=%p",
+       dynar->size, dynar->used, dynar->elmsize, dynar->data, dynar->free_f);
+}      
+
 /** @brief Constructor
  * 
  * \param elmsize size of each element in the dynar
@@ -242,7 +239,7 @@ xbt_dynar_get_cpy(const xbt_dynar_t dynar,
  */
 void*
 xbt_dynar_get_ptr(const xbt_dynar_t dynar,
-                  const int          idx) {
+          const int          idx) {
 
   __sanity_check_dynar(dynar);
   __sanity_check_idx(idx);
@@ -363,29 +360,29 @@ xbt_dynar_remove_at(xbt_dynar_t  const dynar,
                      const int            idx,
                      void         * const object) {
 
+  unsigned long nb_shift;
+  unsigned long offset;
+
   __sanity_check_dynar(dynar);
   __sanity_check_idx(idx);
   __check_inbound_idx(dynar, idx);
 
-  if (object)
+  if (object) {
     _xbt_dynar_get_elm(object, dynar, idx);
+  } else if (dynar->free_f) {
+    char elm[SIZEOF_MAX];
+    _xbt_dynar_get_elm(elm, dynar, idx);
+    (*dynar->free_f)(elm);
+  }
 
-  {
-    const unsigned long old_used = dynar->used;
-    const unsigned long new_used = old_used - 1;
-
-    const unsigned long nb_shift =  old_used-1 - idx;
-    const unsigned long elmsize  =  dynar->elmsize;
-
-    const unsigned long offset   =  nb_shift*elmsize;
-
-    void * const elm_src  = _xbt_dynar_elm(dynar, idx+1);
-    void * const elm_dst  = _xbt_dynar_elm(dynar, idx);
+  nb_shift =  dynar->used-1 - idx;
+  offset   =  nb_shift * dynar->elmsize;
 
-    memmove(elm_dst, elm_src, offset);
+  memmove(_xbt_dynar_elm(dynar, idx),
+          _xbt_dynar_elm(dynar, idx+1), 
+          offset);
 
-    dynar->used = new_used;
-  }
+  dynar->used--;
 }
 
 /** @brief Make room at the end of the dynar for a new element, and return a pointer to it.
@@ -423,7 +420,7 @@ xbt_dynar_pop_ptr(xbt_dynar_t  const dynar) {
 /** @brief Get and remove the last element of the dynar */
 void
 xbt_dynar_pop(xbt_dynar_t  const dynar,
-               void         * const dst) {
+              void         * const dst) {
 
   /* sanity checks done by remove_at */
   DEBUG1("Pop %p",(void*)dynar);
@@ -466,7 +463,7 @@ xbt_dynar_map(const xbt_dynar_t  dynar,
   __sanity_check_dynar(dynar);
 
   {
-    char         elm[64];
+    char         elm[SIZEOF_MAX];
     const unsigned long used = dynar->used;
     unsigned long       i    = 0;