Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
don't malloc tons of dynars in mpi_waitany
[simgrid.git] / src / xbt / dynar.cpp
index 62a607e..375b1c5 100644 (file)
@@ -102,6 +102,18 @@ xbt_dynar_t xbt_dynar_new(const unsigned long elmsize, void_f_pvoid_t const free
   return dynar;
 }
 
+/** @brief Initialize a dynar structure that was not malloc'ed
+ * This can be useful to keep temporary dynars on the stack
+ */
+void xbt_dynar_init(xbt_dynar_t dynar, const unsigned long elmsize, void_f_pvoid_t const free_f)
+{
+  dynar->size    = 0;
+  dynar->used    = 0;
+  dynar->elmsize = elmsize;
+  dynar->data    = nullptr;
+  dynar->free_f  = free_f;
+}
+
 /** @brief Destructor of the structure not touching to the content
  *
  * \param dynar poor victim
@@ -620,7 +632,7 @@ XBT_PUBLIC(void) xbt_dynar_three_way_partition(xbt_dynar_t const dynar, int_f_pv
   unsigned long int p = -1;
   unsigned long int q = dynar->used;
   const unsigned long elmsize = dynar->elmsize;
-  void *tmp = xbt_malloc(elmsize);
+  char* tmp[elmsize];
   void *elm;
 
   for (i = 0; i < q;) {
@@ -643,7 +655,6 @@ XBT_PUBLIC(void) xbt_dynar_three_way_partition(xbt_dynar_t const dynar, int_f_pv
       }
     }
   }
-  xbt_free(tmp);
 }
 
 /** @brief Transform a dynar into a nullptr terminated array.