Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
unique_ptr for dynar, automaton, swag, etc.
authorGabriel Corona <gabriel.corona@loria.fr>
Tue, 8 Mar 2016 11:27:05 +0000 (12:27 +0100)
committerGabriel Corona <gabriel.corona@loria.fr>
Tue, 8 Mar 2016 12:32:15 +0000 (13:32 +0100)
13 files changed:
include/xbt/automaton.h
include/xbt/dict.h
include/xbt/dynar.h
include/xbt/fifo.h
include/xbt/heap.h
include/xbt/lib.h
include/xbt/memory.hpp [new file with mode: 0644]
include/xbt/parmap.h
include/xbt/swag.h
src/mc/mc_liveness.cpp
src/mc/mc_liveness.h
src/mc/mc_visited.cpp
tools/cmake/DefinePackages.cmake

index cbf5f32..90af851 100644 (file)
@@ -114,4 +114,16 @@ XBT_PUBLIC(void) xbt_automaton_propositional_symbol_free_voidp(void *ps);
 XBT_PUBLIC(void) xbt_automaton_free(xbt_automaton_t a);
 
 SG_END_DECL()
+
+#ifdef __cplusplus
+namespace simgrid {
+namespace xbt {
+  inline void destroy(xbt_automaton_t a)
+  {
+    xbt_automaton_free(a);
+  }
+}
+}
+#endif
+
 #endif
index 4a03727..6a5e912 100644 (file)
@@ -173,4 +173,16 @@ xbt_dict_foreach(head, cursor, key, data) {
 /** @} */
 
 SG_END_DECL()
+
+#ifdef __cplusplus
+namespace simgrid {
+namespace xbt {
+  inline void destroy(xbt_dict_t d)
+  {
+    xbt_dict_free(&d);
+  }
+}
+}
+#endif
+
 #endif                          /* _XBT_DICT_H */
index 6dcebc9..8aa9d0d 100644 (file)
@@ -256,6 +256,17 @@ xbt_dynar_foreach (dyn,cpt,str) {
             (_cursor)++         )
 #endif
 /** @} */
-
 SG_END_DECL()
+
+#ifdef __cplusplus
+namespace simgrid {
+namespace xbt {
+  inline void destroy(xbt_dynar_t s)
+  {
+    xbt_dynar_free(&s);
+  }
+}
+}
+#endif
+
 #endif                          /* _XBT_DYNAR_H */
index 9dfd700..ec66ad1 100644 (file)
@@ -111,4 +111,16 @@ XBT_PUBLIC(xbt_fifo_item_t) xbt_fifo_getPrevItem(xbt_fifo_item_t i);
 
 
 SG_END_DECL()
+
+#ifdef __cplusplus
+namespace simgrid {
+namespace xbt {
+  inline void destroy(xbt_fifo_t f)
+  {
+    xbt_fifo_free(f);
+  }
+}
+}
+#endif
+
 #endif                          /* _XBT_FIFO_H */
index 4d9ef1d..8ab5753 100644 (file)
@@ -36,4 +36,16 @@ XBT_PUBLIC(void ) xbt_heap_update(xbt_heap_t H, int i, double key);
 
 /* @} */
 SG_END_DECL()
+
+#ifdef __cplusplus
+namespace simgrid {
+namespace xbt {
+  inline void destroy(xbt_heap_t h)
+  {
+    xbt_heap_free(h);
+  }
+}
+}
+#endif
+
 #endif                          /* _XBT_HEAP_H */
index 2c971ab..404ed25 100644 (file)
@@ -74,4 +74,16 @@ XBT_PUBLIC(void) xbt_lib_remove(xbt_lib_t lib, const char *key);
   xbt_dict_foreach((lib)->dict, cursor, key, data)
 
 SG_END_DECL()
+
+#ifdef __cplusplus
+namespace simgrid {
+namespace xbt {
+  inline void destroy(xbt_lib_t l)
+  {
+    xbt_lib_free(&l);
+  }
+}
+}
+#endif
+
 #endif                          /* _XBT_LIB_H */
diff --git a/include/xbt/memory.hpp b/include/xbt/memory.hpp
new file mode 100644 (file)
index 0000000..ac71407
--- /dev/null
@@ -0,0 +1,34 @@
+/* Copyright (c) 2016. The SimGrid Team.
+ * All rights reserved.                                                     */
+
+/* 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. */
+
+#ifndef SIMGRIX_XBT_MEMORY_HPP
+#define SIMGRIX_XBT_MEMORY_HPP
+
+#include <memory>
+
+namespace simgrid {
+namespace xbt {
+
+/** Delete operator which call a `destroy()` free function */
+template<class T>
+struct destroy_delete {
+  void operator()(T* t) const
+  {
+    destroy(t);
+  }
+};
+
+/** A `unique_ptr` which works for SimGrid C types (dynar, swag, automaton, etc.)
+ *
+ *  It uses an overloaded `destroy()` function to delete the object.
+ */
+template<class T>
+using unique_ptr = std::unique_ptr<T, destroy_delete<T> >;
+
+}
+}
+
+#endif
index e3fa67a..8f80930 100644 (file)
@@ -53,4 +53,16 @@ XBT_PUBLIC(int) xbt_parmap_mc_apply(xbt_parmap_t parmap, int_f_pvoid_pvoid_t fun
 /** \} */
 
 SG_END_DECL()
+
+#ifdef __cplusplus
+namespace simgrid {
+namespace xbt {
+  inline void destroy(xbt_parmap_t p)
+  {
+    xbt_parmap_destroy(p);
+  }
+}
+}
+#endif
+
 #endif
index fa96ed4..e406066 100644 (file)
@@ -189,4 +189,16 @@ static inline void *xbt_swag_getFirst(xbt_swag_t swag)
 /* @} */
 
 SG_END_DECL()
+
+#ifdef __cplusplus
+namespace simgrid {
+namespace xbt {
+  inline void destroy(xbt_swag_t s)
+  {
+    xbt_swag_free(s);
+  }
+}
+}
+#endif
+
 #endif                          /* _XBT_SWAG_H */
index 670f11c..1cb632c 100644 (file)
@@ -48,29 +48,26 @@ Pair::Pair() : num(++mc_stats->expanded_pairs),
 {}
 
 Pair::~Pair() {
-  this->automaton_state = nullptr;
   if (this->visited_pair_removed)
     MC_state_delete(this->graph_state, 1);
-  xbt_dynar_free(&(this->atomic_propositions));
 }
 
-static xbt_dynar_t get_atomic_propositions_values()
+static simgrid::xbt::unique_ptr<s_xbt_dynar_t> get_atomic_propositions_values()
 {
   unsigned int cursor = 0;
   xbt_automaton_propositional_symbol_t ps = nullptr;
-  xbt_dynar_t values = xbt_dynar_new(sizeof(int), nullptr);
+  simgrid::xbt::unique_ptr<s_xbt_dynar_t> values = simgrid::xbt::unique_ptr<s_xbt_dynar_t>(xbt_dynar_new(sizeof(int), nullptr));
   xbt_dynar_foreach(simgrid::mc::property_automaton->propositional_symbols, cursor, ps) {
     int res = xbt_automaton_propositional_symbol_evaluate(ps);
-    xbt_dynar_push_as(values, int, res);
+    xbt_dynar_push_as(values.get(), int, res);
   }
-
-  return values;
+  return std::move(values);
 }
 
 static simgrid::mc::VisitedPair* is_reached_acceptance_pair(simgrid::mc::Pair* pair)
 {
   simgrid::mc::VisitedPair* new_pair = nullptr;
-  new_pair = simgrid::mc::visited_pair_new(pair->num, pair->automaton_state, pair->atomic_propositions, pair->graph_state);
+  new_pair = simgrid::mc::visited_pair_new(pair->num, pair->automaton_state, pair->atomic_propositions.get(), pair->graph_state);
   new_pair->acceptance_pair = 1;
 
   if (xbt_dynar_is_empty(acceptance_pairs))
@@ -221,7 +218,7 @@ static int MC_modelcheck_liveness_main(void)
   xbt_automaton_transition_t transition_succ = nullptr;
   int cursor = 0;
   simgrid::mc::Pair* next_pair = nullptr;
-  xbt_dynar_t prop_values = nullptr;
+  simgrid::xbt::unique_ptr<s_xbt_dynar_t> prop_values;
   simgrid::mc::VisitedPair* reached_pair = nullptr;
   
   while(xbt_fifo_size(mc_stack) > 0){
@@ -314,7 +311,7 @@ static int MC_modelcheck_liveness_main(void)
          cursor = xbt_dynar_length(current_pair->automaton_state->out) - 1;
          while (cursor >= 0) {
            transition_succ = (xbt_automaton_transition_t)xbt_dynar_get_as(current_pair->automaton_state->out, cursor, xbt_automaton_transition_t);
-           res = MC_automaton_evaluate_label(transition_succ->label, prop_values);
+           res = MC_automaton_evaluate_label(transition_succ->label, prop_values.get());
            if (res == 1 || res == 2) { /* 1 = True transition (always enabled), 2 = enabled transition according to atomic prop values */
               next_pair = new Pair();
               next_pair->graph_state = MC_state_new();
@@ -347,7 +344,7 @@ static int MC_modelcheck_liveness_main(void)
       if(visited_num == -1)
         XBT_DEBUG("No more request to execute. Looking for backtracking point.");
     
-      xbt_dynar_free(&prop_values);
+      prop_values.reset();
     
       /* Traverse the stack backwards until a pair with a non empty interleave
          set is found, deleting all the pairs that have it empty in the way. */
index b75d935..ececec4 100644 (file)
@@ -14,6 +14,7 @@
 #include <xbt/fifo.h>
 #include <xbt/dynar.h>
 #include <xbt/automaton.h>
+#include <xbt/memory.hpp>
 #include "src/mc/mc_state.h"
 
 SG_BEGIN_DECL()
@@ -30,7 +31,7 @@ struct XBT_PRIVATE Pair {
   int search_cycle = 0;
   mc_state_t graph_state = nullptr; /* System state included */
   xbt_automaton_state_t automaton_state = nullptr;
-  xbt_dynar_t atomic_propositions = nullptr;
+  simgrid::xbt::unique_ptr<s_xbt_dynar_t> atomic_propositions;
   int requests = 0;
   int depth = 0;
   int exploration_started = 0;
index d9f93a2..95bb48c 100644 (file)
@@ -381,7 +381,7 @@ int is_visited_pair(simgrid::mc::VisitedPair* visited_pair, simgrid::mc::Pair* p
   simgrid::mc::VisitedPair* new_visited_pair = nullptr;
   if (visited_pair == nullptr)
     new_visited_pair = simgrid::mc::visited_pair_new(
-      pair->num, pair->automaton_state, pair->atomic_propositions,
+      pair->num, pair->automaton_state, pair->atomic_propositions.get(),
       pair->graph_state);
   else
     new_visited_pair = visited_pair;
index f7a60cf..e50a798 100644 (file)
@@ -679,6 +679,7 @@ set(headers_to_install
   include/xbt/log.h
   include/xbt/mallocator.h
   include/xbt/matrix.h
+  include/xbt/memory.hpp
   include/xbt/misc.h
   include/xbt/mmalloc.h
   include/xbt/module.h