From: Gabriel Corona Date: Tue, 8 Mar 2016 11:27:05 +0000 (+0100) Subject: unique_ptr for dynar, automaton, swag, etc. X-Git-Tag: v3_13~480 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/7b6d9a992f6c0c59eff98ba72868b5b9b93afa38?hp=124b3dee9ed0b4342bc495061c2a5167a9fc6318 unique_ptr for dynar, automaton, swag, etc. --- diff --git a/include/xbt/automaton.h b/include/xbt/automaton.h index cbf5f32d43..90af851f0b 100644 --- a/include/xbt/automaton.h +++ b/include/xbt/automaton.h @@ -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 diff --git a/include/xbt/dict.h b/include/xbt/dict.h index 4a03727ab0..6a5e9126c5 100644 --- a/include/xbt/dict.h +++ b/include/xbt/dict.h @@ -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 */ diff --git a/include/xbt/dynar.h b/include/xbt/dynar.h index 6dcebc9fc0..8aa9d0db01 100644 --- a/include/xbt/dynar.h +++ b/include/xbt/dynar.h @@ -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 */ diff --git a/include/xbt/fifo.h b/include/xbt/fifo.h index 9dfd70007e..ec66ad1d5d 100644 --- a/include/xbt/fifo.h +++ b/include/xbt/fifo.h @@ -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 */ diff --git a/include/xbt/heap.h b/include/xbt/heap.h index 4d9ef1d889..8ab575331d 100644 --- a/include/xbt/heap.h +++ b/include/xbt/heap.h @@ -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 */ diff --git a/include/xbt/lib.h b/include/xbt/lib.h index 2c971abaca..404ed253c0 100644 --- a/include/xbt/lib.h +++ b/include/xbt/lib.h @@ -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 index 0000000000..ac71407975 --- /dev/null +++ b/include/xbt/memory.hpp @@ -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 + +namespace simgrid { +namespace xbt { + +/** Delete operator which call a `destroy()` free function */ +template +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 +using unique_ptr = std::unique_ptr >; + +} +} + +#endif diff --git a/include/xbt/parmap.h b/include/xbt/parmap.h index e3fa67a591..8f80930622 100644 --- a/include/xbt/parmap.h +++ b/include/xbt/parmap.h @@ -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 diff --git a/include/xbt/swag.h b/include/xbt/swag.h index fa96ed4d7d..e406066160 100644 --- a/include/xbt/swag.h +++ b/include/xbt/swag.h @@ -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 */ diff --git a/src/mc/mc_liveness.cpp b/src/mc/mc_liveness.cpp index 670f11c8f9..1cb632c6e1 100644 --- a/src/mc/mc_liveness.cpp +++ b/src/mc/mc_liveness.cpp @@ -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 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 values = simgrid::xbt::unique_ptr(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 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. */ diff --git a/src/mc/mc_liveness.h b/src/mc/mc_liveness.h index b75d93552b..ececec48af 100644 --- a/src/mc/mc_liveness.h +++ b/src/mc/mc_liveness.h @@ -14,6 +14,7 @@ #include #include #include +#include #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 atomic_propositions; int requests = 0; int depth = 0; int exploration_started = 0; diff --git a/src/mc/mc_visited.cpp b/src/mc/mc_visited.cpp index d9f93a2808..95bb48c382 100644 --- a/src/mc/mc_visited.cpp +++ b/src/mc/mc_visited.cpp @@ -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; diff --git a/tools/cmake/DefinePackages.cmake b/tools/cmake/DefinePackages.cmake index f7a60cf060..e50a7989c6 100644 --- a/tools/cmake/DefinePackages.cmake +++ b/tools/cmake/DefinePackages.cmake @@ -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