From: Gabriel Corona Date: Mon, 6 Jun 2016 14:22:59 +0000 (+0200) Subject: Getting rid of C exceptions X-Git-Tag: v3_14~1009^2 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/138e6f86566611611b1115c957cae0a1c6a85a4b Getting rid of C exceptions The goal if to used native C++ exception instead our own SLJL-based C exceptions: * this removes the problem of exception conversion between C and C++ context and the lack of interoperability of SimGrid THROW/TRY/CATCH with its C++ counterpart; * we can still throw exceptions from C; * but we can't catch exception from C anymore, you should use C++ instead if order to do that; Things to do afterwards: * remove exceptions from the C public APIs; * restore the multiple usage debugging; * fix the backtrace code and separate from the exception code in order to be able to add backtraces to any exception and get backtraces without generating exceptions; * add support for attaching backtraces to any exception; * attach context (baktraces and cause) everywhere; --- diff --git a/examples/msg/app-bittorrent/peer.c b/examples/msg/app-bittorrent/peer.c index d1eafc689f..10153cd178 100644 --- a/examples/msg/app-bittorrent/peer.c +++ b/examples/msg/app-bittorrent/peer.c @@ -276,15 +276,8 @@ void update_active_peers_set(peer_t peer, connection_t remote_peer) if ((remote_peer->interested != 0) && (remote_peer->choked_upload == 0)) { //add in the active peers set xbt_dict_set_ext(peer->active_peers, (char *) &remote_peer->id, sizeof(int), remote_peer, NULL); - } else { - //remove - xbt_ex_t e; - TRY { - xbt_dict_remove_ext(peer->active_peers, (char *) &remote_peer->id, sizeof(int)); - } - CATCH(e) { - xbt_ex_free(e); - } + } else if (xbt_dict_get_or_null_ext(peer->active_peers, (char *) &remote_peer->id, sizeof(int))) { + xbt_dict_remove_ext(peer->active_peers, (char *) &remote_peer->id, sizeof(int)); } } diff --git a/examples/simdag/CMakeLists.txt b/examples/simdag/CMakeLists.txt index 11ae13588c..86fae0072c 100644 --- a/examples/simdag/CMakeLists.txt +++ b/examples/simdag/CMakeLists.txt @@ -1,4 +1,4 @@ -foreach(x availability daxload fail typed_tasks io properties throttling scheduling test) +foreach(x availability daxload fail typed_tasks io properties throttling scheduling) add_executable (sd_${x} ${x}/sd_${x}.c) target_link_libraries(sd_${x} simgrid) set_target_properties(sd_${x} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${x}) @@ -6,6 +6,14 @@ foreach(x availability daxload fail typed_tasks io properties throttling schedul set(tesh_files ${tesh_files} ${CMAKE_CURRENT_SOURCE_DIR}/${x}/sd_${x}.tesh) endforeach() +foreach(x test) + add_executable (sd_${x} ${x}/sd_${x}.cpp) + target_link_libraries(sd_${x} simgrid) + set_target_properties(sd_${x} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${x}) + set(examples_src ${examples_src} ${CMAKE_CURRENT_SOURCE_DIR}/${x}/sd_${x}.cpp) + set(tesh_files ${tesh_files} ${CMAKE_CURRENT_SOURCE_DIR}/${x}/sd_${x}.tesh) +endforeach() + foreach(x dag-dotload ptg-dotload schedule-dotload) if(HAVE_GRAPHVIZ) add_executable (sd_${x} ${x}/sd_${x}.c) @@ -39,4 +47,4 @@ if(HAVE_GRAPHVIZ) foreach(x dag-dotload ptg-dotload schedule-dotload) ADD_TESH(simdag-${x} --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/simdag --cd ${CMAKE_BINARY_DIR}/examples/simdag ${CMAKE_HOME_DIRECTORY}/examples/simdag/${x}/sd_${x}.tesh) endforeach() -endif() \ No newline at end of file +endif() diff --git a/examples/simdag/scheduling/sd_scheduling.c b/examples/simdag/scheduling/sd_scheduling.c index f116b3d3d7..7269bc6ab2 100644 --- a/examples/simdag/scheduling/sd_scheduling.c +++ b/examples/simdag/scheduling/sd_scheduling.c @@ -77,7 +77,7 @@ static xbt_dynar_t get_ready_tasks(xbt_dynar_t dax) static double finish_on_at(SD_task_t task, sg_host_t host) { - volatile double result; + double result; unsigned int i; double data_available = 0.; double redist_time = 0; diff --git a/examples/simdag/test/sd_test.c b/examples/simdag/test/sd_test.cpp similarity index 95% rename from examples/simdag/test/sd_test.c rename to examples/simdag/test/sd_test.cpp index 02bbd3870b..0815389294 100644 --- a/examples/simdag/test/sd_test.c +++ b/examples/simdag/test/sd_test.cpp @@ -15,7 +15,6 @@ int main(int argc, char **argv) unsigned int ctr; SD_task_t checkB, checkD; xbt_dynar_t changed_tasks; - xbt_ex_t ex; const int host_count = 2; sg_host_t host_list[2]; double computation_amount[2]; @@ -71,44 +70,40 @@ int main(int argc, char **argv) SD_task_dependency_add(NULL, NULL, taskD, taskC); SD_task_dependency_add(NULL, NULL, taskB, taskC); - TRY { + try { SD_task_dependency_add(NULL, NULL, taskA, taskA); /* shouldn't work and must raise an exception */ xbt_die("Hey, I can add a dependency between Task A and Task A!"); } - CATCH(ex) { + catch (xbt_ex& ex) { if (ex.category != arg_error) - RETHROW; /* this is a serious error */ - xbt_ex_free(ex); + throw; /* this is a serious error */ } - TRY { + try { SD_task_dependency_add(NULL, NULL, taskB, taskA); /* shouldn't work and must raise an exception */ xbt_die("Oh oh, I can add an already existing dependency!"); } - CATCH(ex) { + catch (xbt_ex& ex) { if (ex.category != arg_error) - RETHROW; - xbt_ex_free(ex); + throw; } - TRY { + try { SD_task_dependency_remove(taskA, taskC); /* shouldn't work and must raise an exception */ xbt_die("Dude, I can remove an unknown dependency!"); } - CATCH(ex) { + catch (xbt_ex& ex) { if (ex.category != arg_error) - RETHROW; - xbt_ex_free(ex); + throw; } - TRY { + try { SD_task_dependency_remove(taskC, taskC); /* shouldn't work and must raise an exception */ xbt_die("Wow, I can remove a dependency between Task C and itself!"); } - CATCH(ex) { + catch (xbt_ex& ex) { if (ex.category != arg_error) - RETHROW; - xbt_ex_free(ex); + throw; } /* if everything is ok, no exception is forwarded or rethrown by main() */ diff --git a/include/xbt/dynar.h b/include/xbt/dynar.h index 8aa9d0db01..83621209fa 100644 --- a/include/xbt/dynar.h +++ b/include/xbt/dynar.h @@ -26,7 +26,7 @@ SG_BEGIN_DECL() * structure creation (of type void_f_ppvoid_t or void_f_pvoid_t). * * \section XBT_dynar_exscal Example with scalar - * \dontinclude dynar.c + * \dontinclude dynar.cpp * * \skip Vars_decl * \skip dyn diff --git a/include/xbt/ex.h b/include/xbt/ex.h index 2113c3ee14..a7ec2f895d 100644 --- a/include/xbt/ex.h +++ b/include/xbt/ex.h @@ -47,12 +47,17 @@ #include +#ifdef __cplusplus +#include +#include +#include +#endif + +#include "xbt/base.h" #include "xbt/sysdep.h" #include "xbt/misc.h" #include "xbt/virtu.h" -SG_BEGIN_DECL() - /*-*-* Emergency debuging: define this when the exceptions get crazy *-*-*/ #undef __EX_MAYDAY #ifdef __EX_MAYDAY @@ -76,177 +81,6 @@ SG_BEGIN_DECL() # define MAYDAY_CATCH(e) #endif /*-*-* end of debugging stuff *-*-*/ -#if defined(__EX_MCTX_MCSC__) -#include /* POSIX.1 ucontext(3) */ -#define __ex_mctx_struct ucontext_t uc; -#define __ex_mctx_save(mctx) (getcontext(&(mctx)->uc) == 0) -#define __ex_mctx_restored(mctx) /* noop */ -#define __ex_mctx_restore(mctx) (void)setcontext(&(mctx)->uc) -#elif defined(__EX_MCTX_SSJLJ__) -#include /* POSIX.1 sigjmp_buf(3) */ -#define __ex_mctx_struct sigjmp_buf jb; -#define __ex_mctx_save(mctx) (sigsetjmp((mctx)->jb, 1) == 0) -#define __ex_mctx_restored(mctx) /* noop */ -#define __ex_mctx_restore(mctx) (void)siglongjmp((mctx)->jb, 1) -#elif defined(__EX_MCTX_SJLJ__) || !defined(__EX_MCTX_CUSTOM__) || defined(__EX_MAYDAY) -#include /* ISO-C jmp_buf(3) */ -#define __ex_mctx_struct jmp_buf jb; -#define __ex_mctx_save(mctx) ( MAYDAY_SAVE(mctx) setjmp((mctx)->jb) == 0) -#define __ex_mctx_restored(mctx) /* noop */ -#define __ex_mctx_restore(mctx) ( MAYDAY_RESTORE(mctx) (void)longjmp((mctx)->jb, 1)) -#endif -/* declare the machine context type */ -typedef struct { -__ex_mctx_struct} __ex_mctx_t; - -/** @addtogroup XBT_ex - * @brief A set of macros providing exception a la C++ in ANSI C (grounding feature) - * - * This module is a small ISO-C++ style exception handling library - * for use in the ISO-C language. It allows you to use the paradigm - * of throwing and catching exceptions in order to reduce the amount - * of error handling code without hindering program robustness. - * - * This is achieved by directly transferring exceptional return codes - * (and the program control flow) from the location where the exception - * is raised (throw point) to the location where it is handled (catch - * point) -- usually from a deeply nested sub-routine to a parent - * routine. All intermediate routines no longer have to make sure that - * the exceptional return codes from sub-routines are correctly passed - * back to the parent. - * - * These features are brought to you by a modified version of the libex - * library, one of the numerous masterpiece of Ralf S. Engelschall. - * - * \htmlonly
\endhtmlonly - * - * @section XBT_ex_toc TABLE OF CONTENTS - * - * - \ref XBT_ex_intro - * - \ref XBT_ex_base - * - \ref XBT_ex_pitfalls - * - * \htmlonly
\endhtmlonly - * - * @section XBT_ex_intro DESCRIPTION - * - * In SimGrid, an exception is a triple <\a msg , \a category , \a value> - * where \a msg is a human-readable text describing the exceptional - * condition, \a code an integer describing what went wrong and \a value - * providing a sort of sub-category. (this is different in the original libex). - * - * @section XBT_ex_base BASIC USAGE - * - * \em TRY \b TRIED_BLOCK [\em TRY_CLEANUP \b CLEANUP_BLOCK] \em CATCH (variable) \b CATCH_BLOCK - * - * This is the primary syntactical construct provided. It is modeled after the - * ISO-C++ try-catch clause and should sound familiar to most of you. - * - * Any exception thrown directly from the TRIED_BLOCK block or from called - * subroutines is caught. Cleanups which must be done after this block - * (whenever an exception arose or not) should be placed into the optionnal - * CLEANUP_BLOCK. The code dealing with the exceptions when they arise should - * be placed into the (mandatory) CATCH_BLOCK. - * - * - * In absence of exception, the control flow goes into the blocks TRIED_BLOCK - * and CLEANUP_BLOCK (if present); The CATCH_BLOCK block is then ignored. - * - * When an exception is thrown, the control flow goes through the following - * blocks: TRIED_BLOCK (up to the statement throwing the exception), - * CLEANUP_BLOCK (if any) and CATCH_BLOCK. The exception is stored in a - * variable for inspection inside the CATCH_BLOCK. This variable must be - * declared in the outter scope, but its value is only valid within the - * CATCH_BLOCK block. - * - * Some notes: - * - TRY, CLEANUP and CATCH cannot be used separately, they work - * only in combination and form a language clause as a whole. - * - In contrast to the syntax of other languages (such as C++ or Jave) there - * is only one CATCH block and not multiple ones (all exceptions are - * of the same \em xbt_ex_t C type). - * - the variable of CATCH can naturally be reused in subsequent - * CATCH clauses. - * - it is possible to nest TRY clauses. - * - * The TRY block is a regular ISO-C language statement block, but - * - *
it is not - * allowed to jump into it via "goto" or longjmp(3) or out of it via "break", - * "return", "goto" or longjmp(3).
- * - * This is because there is some hidden setup and - * cleanup that needs to be done regardless of whether an exception is - * caught. Bypassing these steps will break the exception handling facility. - * The symptom are likely to be a segfault at the next exception raising point, - * ie far away from the point where you did the mistake. Finding the problem can - * reveal challenging, unfortunately. - * - * The CLEANUP and CATCH blocks are regular ISO-C language statement - * blocks without any restrictions. You are even allowed to throw (and, in the - * CATCH block, to re-throw) exceptions. - * - * There is one subtle detail you should remember about TRY blocks: - * Variables used in the CLEANUP or CATCH clauses must be declared with - * the storage class "volatile", otherwise they might contain outdated - * information if an exception is thrown. - * - * - * This is because you usually do not know which commands in the TRY - * were already successful before the exception was thrown (logically speaking) - * and because the underlying ISO-C setjmp(3) facility applies those - * restrictions (technically speaking). As a matter of fact, value changes - * between the TRY and the THROW may be discarded if you forget the - * "volatile" keyword. - * - * \section XBT_ex_pitfalls PROGRAMMING PITFALLS - * - * Exception handling is a very elegant and efficient way of dealing with - * exceptional situation. Nevertheless it requires additional discipline in - * programming and there are a few pitfalls one must be aware of. Look the - * following code which shows some pitfalls and contains many errors (assuming - * a mallocex() function which throws an exception if malloc(3) fails): - * - * \dontinclude ex.c - * \skip BAD_EXAMPLE - * \until end_of_bad_example - * - * This example raises a few issues: - * -# \b variable \b scope \n - * Variables which are used in the CLEANUP or CATCH clauses must be - * declared before the TRY clause, otherwise they only exist inside the - * TRY block. In the example above, cp1, cp2 and cp3 only exist in the - * TRY block and are invisible from the CLEANUP and CATCH - * blocks. - * -# \b variable \b initialization \n - * Variables which are used in the CLEANUP or CATCH clauses must - * be initialized before the point of the first possible THROW is - * reached. In the example above, CLEANUP would have trouble using cp3 - * if mallocex() throws a exception when allocating a TOOBIG buffer. - * -# \b volatile \b variable \n - * Variables which are used in the CLEANUP or CATCH clauses MUST BE - * DECLARED AS "volatile", otherwise they might contain outdated - * information when an exception is thrown. - * -# \b clean \b before \b catch \n - * The CLEANUP clause is not only place before the CATCH clause in - * the source code, it also occures before in the control flow. So, - * resources being cleaned up cannot be used in the CATCH block. In the - * example, c3 gets freed before the printf placed in CATCH. - * -# \b variable \b uninitialization \n - * If resources are passed out of the scope of the - * TRY/CLEANUP/CATCH construct, they naturally shouldn't get - * cleaned up. The example above does free(3) cp1 in CLEANUP although - * its value was affected to globalcontext->first, invalidating this - * pointer. - - * The following is fixed version of the code (annotated with the pitfall items - * for reference): - * - * \skip GOOD_EXAMPLE - * \until end_of_good_example - * - * @{ - */ /** @brief different kind of errors */ typedef enum { @@ -266,194 +100,42 @@ typedef enum { vm_error /**< vm error */ } xbt_errcat_t; -XBT_PUBLIC(const char *) xbt_ex_catname(xbt_errcat_t cat); +#ifdef __cplusplus +XBT_PUBLIC_CLASS xbt_ex : public std::runtime_error { +public: + xbt_ex() : std::runtime_error("") {} + xbt_ex(const char* message) : std::runtime_error(message) {} + ~xbt_ex() override; -/** @brief Structure describing an exception */ -typedef struct { - char *msg; /**< human readable message */ xbt_errcat_t category; /**< category like HTTP (what went wrong) */ int value; /**< like errno (why did it went wrong) */ /* throw point */ - char *procname; /**< Name of the process who thrown this */ + std::string procname; /**< Name of the process who thrown this */ int pid; /**< PID of the process who thrown this */ - char *file; /**< Thrown point */ + const char *file; /**< Thrown point */ int line; /**< Thrown point */ - char *func; /**< Thrown point */ + const char *func; /**< Thrown point */ /* Backtrace */ - int used; - char **bt_strings; /* only filed on display (or before the network propagation) */ - void *bt[XBT_BACKTRACE_SIZE]; -} xbt_ex_t; - -/* declare the running context type - * (that's where we get the process name for the logs and the exception storage) - * -- do not mess with it -- - */ -typedef struct xbt_running_ctx_t { - __ex_mctx_t *ctx_mctx; /* permanent machine context of enclosing try/catch */ - int ctx_caught; /* temporary flag whether exception was caught */ - volatile xbt_ex_t exception; /* temporary exception storage */ -} xbt_running_ctx_t; - -/* the static and dynamic initializers for a context structure */ -#define XBT_RUNNING_CTX_INITIALIZER \ - { NULL, 0, { /* content */ NULL, unknown_error, 0, \ - /* throw point*/ NULL, 0, NULL, 0, NULL, \ - /* backtrace */ 0, NULL, /* bt[] */ } } - -XBT_PUBLIC_DATA(const xbt_running_ctx_t) __xbt_ex_ctx_initializer; - -// #define XBT_RUNNING_CTX_INITIALIZE(ctx) (*(ctx) = __xbt_ex_ctx_initializer) - -#define XBT_RUNNING_CTX_INITIALIZE(ctx) \ - (ctx)->ctx_mctx = NULL; \ - (ctx)->ctx_caught = 0; \ - (ctx)->exception.msg = NULL; \ - (ctx)->exception.category = unknown_error; \ - (ctx)->exception.value = 0; \ - (ctx)->exception.procname = NULL; \ - (ctx)->exception.pid = 0; \ - (ctx)->exception.file = NULL; \ - (ctx)->exception.line = 0; \ - (ctx)->exception.used = 0; \ - (ctx)->exception.bt_strings = NULL; - -/* the exception context */ -typedef xbt_running_ctx_t *(*xbt_running_ctx_fetcher_t) (void); -XBT_PUBLIC_DATA(xbt_running_ctx_fetcher_t) __xbt_running_ctx_fetch; -XBT_PUBLIC( xbt_running_ctx_t *)__xbt_ex_ctx_default(void); - -/* the termination handler */ -typedef void (*ex_term_cb_t) (xbt_ex_t *); -XBT_PUBLIC_DATA(ex_term_cb_t) __xbt_ex_terminate; -XBT_PUBLIC( void )__xbt_ex_terminate_default(xbt_ex_t * e); - -/** @brief Introduce a block where exception may be dealed with - * @hideinitializer - */ -#define TRY \ - { \ - xbt_running_ctx_t *__xbt_ex_ctx_ptr = __xbt_running_ctx_fetch(); \ - int __ex_cleanup = 0; \ - __ex_mctx_t __ex_mctx_me; \ - __ex_mctx_t * __ex_mctx_en = __xbt_ex_ctx_ptr->ctx_mctx; \ - __xbt_ex_ctx_ptr->ctx_mctx = &__ex_mctx_me; \ - if (__ex_mctx_save(&__ex_mctx_me)) { \ - if (1) - -/** @brief optional(!) block for cleanup - * @hideinitializer - */ -#define TRY_CLEANUP \ - else { \ - } \ - __xbt_ex_ctx_ptr->ctx_caught = 0; \ - } else { \ - __ex_mctx_restored(&__ex_mctx_me); \ - __xbt_ex_ctx_ptr->ctx_caught = 1; \ - } \ - __xbt_ex_ctx_ptr->ctx_mctx = __ex_mctx_en; \ - __ex_cleanup = 1; \ - if (1) { \ - if (1) - -#ifndef DOXYGEN_SKIP -# ifdef __cplusplus -# define XBT_EX_T_CPLUSPLUSCAST (xbt_ex_t&) -# else -# define XBT_EX_T_CPLUSPLUSCAST -# endif + std::vector bt_strings; + std::vector bt; +}; #endif -/** @brief the block for catching (ie, deal with) an exception - * @hideinitializer - */ -#define CATCH(e) \ - DO_CATCH((e) = XBT_EX_T_CPLUSPLUSCAST __xbt_running_ctx_fetch()->exception) - -/** @brief like CATCH(e) but without argument - * @hideinitializer - * - * Useful if you only want to rethrow the exception caught, and do not want to - * bother with an unused variable. - */ -#define CATCH_ANONYMOUS DO_CATCH(0) - -#define DO_CATCH(_xbt_do_catch_set_e) \ - else { \ - } \ - if (!(__ex_cleanup)) \ - __xbt_ex_ctx_ptr->ctx_caught = 0; \ - } else { \ - if (!(__ex_cleanup)) { \ - __ex_mctx_restored(&__ex_mctx_me); \ - __xbt_ex_ctx_ptr->ctx_caught = 1; \ - } \ - } \ - __xbt_ex_ctx_ptr->ctx_mctx = __ex_mctx_en; \ - } \ - if ( !(__xbt_running_ctx_fetch()->ctx_caught) \ - || ((void)(_xbt_do_catch_set_e), \ - MAYDAY_CATCH(__xbt_running_ctx_fetch()->exception) 0)) { \ - } \ - else - -#define DO_THROW(running_ctx) \ - do { /* deal with the exception */ \ - xbt_running_ctx_t *ctx = (running_ctx); \ - if (ctx->ctx_mctx == NULL) \ - __xbt_ex_terminate((xbt_ex_t*)&(ctx->exception)); /* not catched */ \ - else \ - __ex_mctx_restore(ctx->ctx_mctx); /* catched somewhere */ \ - XBT_UNREACHABLE(); \ - } while(0) +SG_BEGIN_DECL() -/** @brief Helper macro for THROW and THROWF - * @hideinitializer - * - * @param _throw_ctx: the throwing context in which we should construct the exception - * @param c: category code (integer) - * @param v: value (integer) - * @param m: message text - * - * If called from within a TRY/CATCH construct, this exception - * is copied into the CATCH relevant variable program control flow - * is derouted to the CATCH (after the optional sg_cleanup). - * - * If no TRY/CATCH construct embeds this call, the program calls - * abort(3). - * - * The THROW can be performed everywhere, including inside TRY, - * CLEANUP and CATCH blocks. - */ +XBT_PUBLIC(const char *) xbt_ex_catname(xbt_errcat_t cat); -#define THROW_PREPARE(_throw_ctx, c, v, m) \ - /* build the exception */ \ - _throw_ctx->exception.msg = (m); \ - _throw_ctx->exception.category = (xbt_errcat_t)(c); \ - _throw_ctx->exception.value = (v); \ - _throw_ctx->exception.procname = (char*)xbt_procname(); \ - _throw_ctx->exception.pid = xbt_getpid(); \ - _throw_ctx->exception.file = (char*)__FILE__; \ - _throw_ctx->exception.line = __LINE__; \ - _throw_ctx->exception.func = (char*)__func__; \ - _throw_ctx->exception.bt_strings = NULL; \ - xbt_backtrace_current((xbt_ex_t *)&(_throw_ctx->exception)); +typedef struct xbt_ex xbt_ex_t; -#define _XBT_THROW(c, v, m) \ - do { /* change this sequence into one block */ \ - xbt_running_ctx_t *_throw_ctx = __xbt_running_ctx_fetch(); \ - THROW_PREPARE(_throw_ctx, c, v, m); \ - DO_THROW(_throw_ctx); \ - } while (0) +XBT_PUBLIC(void) xbt_throw(char* message, xbt_errcat_t errcat, int value, const char* file, int line, const char* func) XBT_ATTRIB_NORETURN; /** @brief Builds and throws an exception @hideinitializer */ -#define THROW(c, v) _XBT_THROW(c, v, NULL) +#define THROW(c, v) { xbt_throw(NULL, (xbt_errcat_t) c, v, __FILE__, __LINE__, __func__); } /** @brief Builds and throws an exception with a printf-like formatted message @hideinitializer */ -#define THROWF(c, v, ...) _XBT_THROW(c, v, bprintf(__VA_ARGS__)) +#define THROWF(c, v, ...) xbt_throw(bprintf(__VA_ARGS__), (xbt_errcat_t) c, v, __FILE__, __LINE__, __func__) #define THROW_IMPOSSIBLE \ THROWF(unknown_error, 0, "The Impossible Did Happen (yet again)") @@ -464,25 +146,6 @@ XBT_PUBLIC( void )__xbt_ex_terminate_default(xbt_ex_t * e); #define DIE_IMPOSSIBLE xbt_die("The Impossible Did Happen (yet again)") -/** @brief re-throwing of an already caught exception (ie, pass it to the upper catch block) - * @hideinitializer - */ -#define RETHROW DO_THROW(__xbt_running_ctx_fetch()) - -/** @brief like THROWF, but adding some details to the message of an existing exception - * @hideinitializer - */ -#define RETHROWF(...) \ - do { \ - char *_xbt_ex_internal_msg = __xbt_running_ctx_fetch()->exception.msg; \ - __xbt_running_ctx_fetch()->exception.msg = bprintf(__VA_ARGS__, \ - _xbt_ex_internal_msg); \ - free(_xbt_ex_internal_msg); \ - RETHROW; \ - } while (0) - -/** @brief Exception destructor */ -XBT_PUBLIC(void) xbt_ex_free(xbt_ex_t e); /** @brief The display made by an exception that is not catched */ XBT_PUBLIC(void) xbt_ex_display(xbt_ex_t * e); diff --git a/src/bindings/java/JavaContext.cpp b/src/bindings/java/JavaContext.cpp index cbe65b1e34..21af3724e5 100644 --- a/src/bindings/java/JavaContext.cpp +++ b/src/bindings/java/JavaContext.cpp @@ -74,14 +74,27 @@ JavaContext::JavaContext(std::function code, this->begin = xbt_os_sem_init(0); this->end = xbt_os_sem_init(0); - TRY { + try { this->thread = xbt_os_thread_create( nullptr, JavaContext::wrapper, this, nullptr); } - CATCH_ANONYMOUS { - RETHROWF("Failed to create context #%d. You may want to switch to Java coroutines to increase your limits (error: %s)." - "See the Install section of simgrid-java documentation (in doc/install.html) for more on coroutines.", - thread_amount); + catch (xbt_ex& ex) { + char* str = bprintf( + "Failed to create context #%d. You may want to switch to Java coroutines to increase your limits (error: %s)." + "See the Install section of simgrid-java documentation (in doc/install.html) for more on coroutines.", + thread_amount, ex.what()); + xbt_ex new_exception(str); + free(str); + new_exception.category = ex.category; + new_exception.value = ex.value; + new_exception.procname = ex.procname; + new_exception.file = ex.file; + new_exception.line = ex.line; + new_exception.func = ex.func; + new_exception.pid = ex.pid; + new_exception.bt = ex.bt; + new_exception.bt_strings = ex.bt_strings; + throw new_exception; } } else { this->thread = nullptr; diff --git a/src/bindings/java/jmsg_process.cpp b/src/bindings/java/jmsg_process.cpp index 4455dd927e..e9a59f6451 100644 --- a/src/bindings/java/jmsg_process.cpp +++ b/src/bindings/java/jmsg_process.cpp @@ -274,11 +274,11 @@ JNICALL Java_org_simgrid_msg_Process_setAutoRestart (JNIEnv *env, jobject jproce return; } - TRY { + try { MSG_process_auto_restart_set(process,auto_restart); } - CATCH (e) { - xbt_ex_free(e); + catch (xbt_ex& e) { + // Nothing to do } } @@ -291,11 +291,11 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_restart (JNIEnv *env, jobjec return; } - TRY { + try { MSG_process_restart(process); } - CATCH (e) { - xbt_ex_free(e); + catch (xbt_ex& e) { + // Nothing to do } } diff --git a/src/bindings/java/jmsg_synchro.cpp b/src/bindings/java/jmsg_synchro.cpp index ca24ac82e0..0ae09a7066 100644 --- a/src/bindings/java/jmsg_synchro.cpp +++ b/src/bindings/java/jmsg_synchro.cpp @@ -29,15 +29,12 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Mutex_init(JNIEnv * env, jobject obj } JNIEXPORT void JNICALL Java_org_simgrid_msg_Mutex_acquire(JNIEnv * env, jobject obj) { - xbt_mutex_t mutex; - - mutex = (xbt_mutex_t) (uintptr_t) env->GetLongField(obj, jsyncro_field_Mutex_bind); - xbt_ex_t e; - TRY { + xbt_mutex_t mutex = (xbt_mutex_t) (uintptr_t) env->GetLongField(obj, jsyncro_field_Mutex_bind); + try { xbt_mutex_acquire(mutex); } - CATCH(e) { - xbt_ex_free(e); + catch(xbt_ex& e) { + // Nothing to do } } diff --git a/src/bindings/java/jmsg_vm.cpp b/src/bindings/java/jmsg_vm.cpp index 7ab77aa72a..8ce32c8c01 100644 --- a/src/bindings/java/jmsg_vm.cpp +++ b/src/bindings/java/jmsg_vm.cpp @@ -128,12 +128,11 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_VM_internalmig(JNIEnv *env, jobject { msg_vm_t vm = jvm_get_native(env,jvm); msg_host_t host = jhost_get_native(env, jhost); - xbt_ex_t e; - TRY{ + try { MSG_vm_migrate(vm,host); - } CATCH(e){ - XBT_VERB("CATCH EXCEPTION MIGRATION %s",e.msg); - xbt_ex_free(e); + } + catch(xbt_ex& e){ + XBT_VERB("CATCH EXCEPTION MIGRATION %s",e.what()); jxbt_throw_host_failure(env, (char*)"during migration"); } } diff --git a/src/instr/instr_paje_values.cpp b/src/instr/instr_paje_values.cpp index 7936286965..ee6c353400 100644 --- a/src/instr/instr_paje_values.cpp +++ b/src/instr/instr_paje_values.cpp @@ -32,12 +32,10 @@ val_t PJ_value_new (const char *name, const char *color, type_t father) val_t PJ_value_get_or_new (const char *name, const char *color, type_t father) { val_t ret = 0; - xbt_ex_t e; - TRY { + try { ret = PJ_value_get(name, father); } - CATCH(e) { - xbt_ex_free(e); + catch(xbt_ex& e) { ret = PJ_value_new(name, color, father); } return ret; diff --git a/src/msg/msg_gos.cpp b/src/msg/msg_gos.cpp index 6aa14754e6..c9cd6f41a2 100644 --- a/src/msg/msg_gos.cpp +++ b/src/msg/msg_gos.cpp @@ -42,7 +42,6 @@ msg_error_t MSG_task_execute(msg_task_t task) */ msg_error_t MSG_parallel_task_execute(msg_task_t task) { - xbt_ex_t e; simdata_task_t simdata = task->simdata; simdata_process_t p_simdata = (simdata_process_t) SIMIX_process_self_get_data(); e_smx_state_t comp_state; @@ -50,8 +49,8 @@ msg_error_t MSG_parallel_task_execute(msg_task_t task) TRACE_msg_task_execute_start(task); - xbt_assert((!simdata->compute) && (task->simdata->isused == 0), - "This task is executed somewhere else. Go fix your code! %d", task->simdata->isused!=nullptr); + xbt_assert((!simdata->compute) && !task->simdata->isused, + "This task is executed somewhere else. Go fix your code!"); XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self())); @@ -60,11 +59,8 @@ msg_error_t MSG_parallel_task_execute(msg_task_t task) return MSG_OK; } - TRY { - if (msg_global->debug_multiple_use) - MSG_BT(simdata->isused, "Using Backtrace"); - else - simdata->isused = (void*)1; + try { + simdata->setUsed(); if (simdata->host_nb > 0) { simdata->compute = static_cast( @@ -88,14 +84,11 @@ msg_error_t MSG_parallel_task_execute(msg_task_t task) comp_state = simcall_execution_wait(simdata->compute); p_simdata->waiting_action = nullptr; - - if (msg_global->debug_multiple_use && simdata->isused!=0) - xbt_ex_free(*(xbt_ex_t*)simdata->isused); - simdata->isused = 0; + simdata->setNotUsed(); XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state); } - CATCH(e) { + catch (xbt_ex& e) { switch (e.category) { case cancel_error: status = MSG_TASK_CANCELED; @@ -104,10 +97,10 @@ msg_error_t MSG_parallel_task_execute(msg_task_t task) status = MSG_HOST_FAILURE; break; default: - RETHROW; + throw; } - xbt_ex_free(e); } + /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */ simdata->flops_amount = 0.0; simdata->comm = nullptr; @@ -126,16 +119,15 @@ msg_error_t MSG_parallel_task_execute(msg_task_t task) */ msg_error_t MSG_process_sleep(double nb_sec) { - xbt_ex_t e; msg_error_t status = MSG_OK; /*msg_process_t proc = MSG_process_self();*/ TRACE_msg_process_sleep_in(MSG_process_self()); - TRY { + try { simcall_process_sleep(nb_sec); } - CATCH(e) { + catch(xbt_ex& e) { switch (e.category) { case cancel_error: XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, WTF here ?"); @@ -148,9 +140,8 @@ msg_error_t MSG_process_sleep(double nb_sec) status = MSG_TASK_CANCELED; break; default: - RETHROW; + throw; } - xbt_ex_free(e); } TRACE_msg_process_sleep_out(MSG_process_self()); @@ -246,21 +237,19 @@ msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char */ msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host) { - xbt_ex_t e; msg_error_t ret = MSG_OK; XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias); - TRY { + try { ret = MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task, host, timeout); } - CATCH(e) { + catch(xbt_ex& e) { switch (e.category) { case cancel_error: /* may be thrown by MSG_mailbox_get_by_alias */ ret = MSG_HOST_FAILURE; break; default: - RETHROW; + throw; } - xbt_ex_free(e); } return ret; } @@ -299,24 +288,7 @@ static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char *al t_simdata = task->simdata; t_simdata->sender = process; t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data())->m_host; - - if (t_simdata->isused != 0) { - if (msg_global->debug_multiple_use){ - XBT_ERROR("This task is already used in there:"); - xbt_backtrace_display((xbt_ex_t*) t_simdata->isused); - XBT_ERROR("And you try to reuse it from here:"); - xbt_backtrace_display_current(); - } else { - xbt_assert(t_simdata->isused == 0, - "This task is still being used somewhere else. You cannot send it now. Go fix your code!" - "(use --cfg=msg/debug-multiple-use:on to get the backtrace of the other process)"); - } - } - - if (msg_global->debug_multiple_use) - MSG_BT(t_simdata->isused, "Using Backtrace"); - else - t_simdata->isused = (void*)1; + t_simdata->setUsed(); t_simdata->comm = nullptr; msg_global->sent_msg++; @@ -492,20 +464,16 @@ msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rat */ int MSG_comm_test(msg_comm_t comm) { - xbt_ex_t e; int finished = 0; - TRY { + try { finished = simcall_comm_test(comm->s_comm); - if (finished && comm->task_received != nullptr) { /* I am the receiver */ - if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0) - xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused); - (*comm->task_received)->simdata->isused = 0; + (*comm->task_received)->simdata->setNotUsed(); } } - CATCH(e) { + catch (xbt_ex& e) { switch (e.category) { case network_error: comm->status = MSG_TRANSFER_FAILURE; @@ -516,9 +484,8 @@ int MSG_comm_test(msg_comm_t comm) finished = 1; break; default: - RETHROW; + throw; } - xbt_ex_free(e); } return finished; @@ -533,7 +500,6 @@ int MSG_comm_test(msg_comm_t comm) */ int MSG_comm_testany(xbt_dynar_t comms) { - xbt_ex_t e; int finished_index = -1; /* create the equivalent dynar with SIMIX objects */ @@ -545,10 +511,10 @@ int MSG_comm_testany(xbt_dynar_t comms) } msg_error_t status = MSG_OK; - TRY { + try { finished_index = simcall_comm_testany(s_comms); } - CATCH(e) { + catch (xbt_ex& e) { switch (e.category) { case network_error: finished_index = e.value; @@ -559,9 +525,8 @@ int MSG_comm_testany(xbt_dynar_t comms) status = MSG_TIMEOUT; break; default: - RETHROW; + throw; } - xbt_ex_free(e); } xbt_dynar_free(&s_comms); @@ -572,9 +537,7 @@ int MSG_comm_testany(xbt_dynar_t comms) if (status == MSG_OK && comm->task_received != nullptr) { /* I am the receiver */ - if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0) - xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused); - (*comm->task_received)->simdata->isused = 0; + (*comm->task_received)->simdata->setNotUsed(); } } @@ -601,20 +564,17 @@ void MSG_comm_destroy(msg_comm_t comm) */ msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout) { - xbt_ex_t e; - TRY { + try { simcall_comm_wait(comm->s_comm, timeout); if (comm->task_received != nullptr) { /* I am the receiver */ - if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0) - xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused); - (*comm->task_received)->simdata->isused = 0; + (*comm->task_received)->simdata->setNotUsed(); } /* FIXME: these functions are not traceable */ } - CATCH(e) { + catch (xbt_ex& e) { switch (e.category) { case network_error: comm->status = MSG_TRANSFER_FAILURE; @@ -623,9 +583,8 @@ msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout) comm->status = MSG_TIMEOUT; break; default: - RETHROW; + throw; } - xbt_ex_free(e); } return comm->status; @@ -654,7 +613,6 @@ void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout) */ int MSG_comm_waitany(xbt_dynar_t comms) { - xbt_ex_t e; int finished_index = -1; /* create the equivalent dynar with SIMIX objects */ @@ -666,10 +624,10 @@ int MSG_comm_waitany(xbt_dynar_t comms) } msg_error_t status = MSG_OK; - TRY { + try { finished_index = simcall_comm_waitany(s_comms); } - CATCH(e) { + catch(xbt_ex& e) { switch (e.category) { case network_error: finished_index = e.value; @@ -680,9 +638,8 @@ int MSG_comm_waitany(xbt_dynar_t comms) status = MSG_TIMEOUT; break; default: - RETHROW; + throw; } - xbt_ex_free(e); } xbt_assert(finished_index != -1, "WaitAny returned -1"); @@ -694,9 +651,7 @@ int MSG_comm_waitany(xbt_dynar_t comms) if (comm->task_received != nullptr) { /* I am the receiver */ - if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0) - xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused); - (*comm->task_received)->simdata->isused = 0; + (*comm->task_received)->simdata->setNotUsed(); } return finished_index; @@ -811,31 +766,15 @@ msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, doubl t_simdata->sender = process; t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data())->m_host; - if (t_simdata->isused != 0) { - if (msg_global->debug_multiple_use){ - XBT_ERROR("This task is already used in there:"); - xbt_backtrace_display((xbt_ex_t*) t_simdata->isused); - XBT_ERROR("And you try to reuse it from here:"); - xbt_backtrace_display_current(); - } else { - xbt_assert(t_simdata->isused == 0, - "This task is still being used somewhere else. You cannot send it now. Go fix your code!" - " (use --cfg=msg/debug-multiple-use:on to get the backtrace of the other process)"); - } - } + t_simdata->setUsed(); - if (msg_global->debug_multiple_use) - MSG_BT(t_simdata->isused, "Using Backtrace"); - else - t_simdata->isused = (void*)1; t_simdata->comm = nullptr; msg_global->sent_msg++; p_simdata->waiting_task = task; - xbt_ex_t e; /* Try to send it by calling SIMIX network layer */ - TRY { + try { smx_synchro_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call */ comm = simcall_comm_isend(SIMIX_process_self(), mailbox,t_simdata->bytes_amount, t_simdata->rate, task, sizeof(void *), nullptr, nullptr, nullptr, task, 0); @@ -844,8 +783,7 @@ msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, doubl t_simdata->comm = static_cast(comm); simcall_comm_wait(comm, timeout); } - - CATCH(e) { + catch (xbt_ex& e) { switch (e.category) { case cancel_error: ret = MSG_HOST_FAILURE; @@ -857,14 +795,11 @@ msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, doubl ret = MSG_TIMEOUT; break; default: - RETHROW; + throw; } - xbt_ex_free(e); /* If the send failed, it is not used anymore */ - if (msg_global->debug_multiple_use && t_simdata->isused!=0) - xbt_ex_free(*(xbt_ex_t*)t_simdata->isused); - t_simdata->isused = 0; + t_simdata->setNotUsed(); } p_simdata->waiting_task = nullptr; diff --git a/src/msg/msg_mailbox.cpp b/src/msg/msg_mailbox.cpp index c1285661b3..06413a6342 100644 --- a/src/msg/msg_mailbox.cpp +++ b/src/msg/msg_mailbox.cpp @@ -90,7 +90,6 @@ msg_error_t MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, msg_task_t *task, ms msg_error_t MSG_mailbox_get_task_ext_bounded(msg_mailbox_t mailbox, msg_task_t * task, msg_host_t host, double timeout, double rate) { - xbt_ex_t e; msg_error_t ret = MSG_OK; /* We no longer support getting a task from a specific host */ if (host) @@ -106,14 +105,12 @@ msg_error_t MSG_mailbox_get_task_ext_bounded(msg_mailbox_t mailbox, msg_task_t * XBT_WARN("Asked to write the received task in a non empty struct -- proceeding."); /* Try to receive it by calling SIMIX network layer */ - TRY { + try { simcall_comm_recv(MSG_process_self(), mailbox, task, nullptr, nullptr, nullptr, nullptr, timeout, rate); XBT_DEBUG("Got task %s from %p",(*task)->name,mailbox); - if (msg_global->debug_multiple_use && (*task)->simdata->isused!=0) - xbt_ex_free(*(xbt_ex_t*)(*task)->simdata->isused); - (*task)->simdata->isused = 0; + (*task)->simdata->setNotUsed(); } - CATCH(e) { + catch (xbt_ex& e) { switch (e.category) { case cancel_error: ret = MSG_HOST_FAILURE; @@ -128,9 +125,8 @@ msg_error_t MSG_mailbox_get_task_ext_bounded(msg_mailbox_t mailbox, msg_task_t * ret = MSG_HOST_FAILURE; break; default: - RETHROW; + throw; } - xbt_ex_free(e); } if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) { diff --git a/src/msg/msg_private.h b/src/msg/msg_private.h index bb771f3ed1..1bca3acded 100644 --- a/src/msg/msg_private.h +++ b/src/msg/msg_private.h @@ -7,6 +7,7 @@ #ifndef METASIMGRID_PRIVATE_H #define METASIMGRID_PRIVATE_H +#include #include #include "simgrid/msg.h" @@ -28,42 +29,47 @@ SG_BEGIN_DECL() /**************** datatypes **********************************/ /********************************* Task **************************************/ -#define MSG_BT(ptr, m) \ - do {xbt_ex_t *_xbt_ex_t = xbt_new0(xbt_ex_t, 1); \ - /* build the exception */ \ - _xbt_ex_t->msg = (bprintf(m)); \ - _xbt_ex_t->category = (xbt_errcat_t)(0); \ - _xbt_ex_t->value = (0); \ - _xbt_ex_t->procname = (char*)xbt_procname(); \ - _xbt_ex_t->pid = xbt_getpid(); \ - _xbt_ex_t->file = (char*)__FILE__; \ - _xbt_ex_t->line = __LINE__; \ - _xbt_ex_t->func = (char*)__func__; \ - _xbt_ex_t->bt_strings = NULL; \ - xbt_backtrace_current(_xbt_ex_t); \ - ptr = _xbt_ex_t; } while(0) typedef struct simdata_task { - simgrid::simix::Exec *compute; /* SIMIX modeling of computation */ - simgrid::simix::Comm *comm; /* SIMIX modeling of communication */ - double bytes_amount; /* Data size */ - double flops_amount; /* Computation size */ - msg_process_t sender; - msg_process_t receiver; - msg_host_t source; - double priority; - double bound; /* Capping for CPU resource */ - double rate; /* Capping for network resource */ + ~simdata_task() + { + if (this->compute) + this->compute->unref(); + + /* parallel tasks only */ + xbt_free(this->host_list); + + xbt_dict_free(&this->affinity_mask_db); + } + void setUsed(); + void setNotUsed() + { + this->isused = false; + } + + simgrid::simix::Exec *compute = nullptr; /* SIMIX modeling of computation */ + simgrid::simix::Comm *comm = nullptr; /* SIMIX modeling of communication */ + double bytes_amount = 0.0; /* Data size */ + double flops_amount = 0.0; /* Computation size */ + msg_process_t sender = nullptr; + msg_process_t receiver = nullptr; + msg_host_t source = nullptr; + double priority = 0.0; + double bound = 0.0; /* Capping for CPU resource */ + double rate = 0.0; /* Capping for network resource */ /* CPU affinity database of this task */ - xbt_dict_t affinity_mask_db; /* smx_host_t host => unsigned long mask */ + xbt_dict_t affinity_mask_db = nullptr; /* smx_host_t host => unsigned long mask */ - void *isused; /* Indicates whether the task is used in SIMIX currently */ - int host_nb; /* ==0 if sequential task; parallel task if not */ + bool isused = false; /* Indicates whether the task is used in SIMIX currently */ + int host_nb = 0; /* ==0 if sequential task; parallel task if not */ /******* Parallel Tasks Only !!!! *******/ - sg_host_t *host_list; - double *flops_parallel_amount; - double *bytes_parallel_amount; + sg_host_t *host_list = nullptr; + double *flops_parallel_amount = nullptr; + double *bytes_parallel_amount = nullptr; + +private: + void reportMultipleUse() const; } s_simdata_task_t; /********************************* File **************************************/ @@ -195,4 +201,14 @@ XBT_PUBLIC(msg_process_t) MSG_process_create_with_environment( const char *name, std::function code, void *data, msg_host_t host, xbt_dict_t properties); +inline void simdata_task::setUsed() +{ + if (this->isused) + this->reportMultipleUse(); + if (msg_global->debug_multiple_use) { + // TODO, backtrace + } + this->isused = true; +} + #endif diff --git a/src/msg/msg_synchro.cpp b/src/msg/msg_synchro.cpp index d88e01f655..fa6cad5cea 100644 --- a/src/msg/msg_synchro.cpp +++ b/src/msg/msg_synchro.cpp @@ -28,17 +28,13 @@ void MSG_sem_acquire(msg_sem_t sem) { /** @brief locks on a semaphore object up until the provided timeout expires */ msg_error_t MSG_sem_acquire_timeout(msg_sem_t sem, double timeout) { - xbt_ex_t e; msg_error_t res = MSG_OK; - TRY { + try { simcall_sem_acquire_timeout(sem,timeout); - } CATCH(e) { - if (e.category == timeout_error) { - res = MSG_TIMEOUT; - xbt_ex_free(e); - } else { - RETHROW; - } + } catch(xbt_ex& e) { + if (e.category == timeout_error) + return MSG_TIMEOUT; + throw; } return res; } diff --git a/src/msg/msg_task.cpp b/src/msg/msg_task.cpp index bb76800766..a19d803800 100644 --- a/src/msg/msg_task.cpp +++ b/src/msg/msg_task.cpp @@ -19,6 +19,20 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_task, msg, "Logging specific to MSG (task)"); +void simdata_task::reportMultipleUse() const +{ + if (msg_global->debug_multiple_use){ + XBT_ERROR("This task is already used in there:"); + // TODO, backtrace + XBT_ERROR(""); + XBT_ERROR("And you try to reuse it from here:"); + xbt_backtrace_display_current(); + } else { + xbt_die("This task is still being used somewhere else. You cannot send it now. Go fix your code!" + "(use --cfg=msg/debug-multiple-use:on to get the backtrace of the other process)"); + } +} + /********************************* Task **************************************/ /** \ingroup m_task_management * \brief Creates a new #msg_task_t. @@ -37,7 +51,7 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_task, msg, "Logging specific to MSG (task)") msg_task_t MSG_task_create(const char *name, double flop_amount, double message_size, void *data) { msg_task_t task = xbt_new(s_msg_task_t, 1); - simdata_task_t simdata = xbt_new(s_simdata_task_t, 1); + simdata_task_t simdata = new s_simdata_task_t(); task->simdata = simdata; /* Task structure */ @@ -206,16 +220,8 @@ msg_error_t MSG_task_destroy(msg_task_t task) xbt_free(task->name); - if (task->simdata->compute) - task->simdata->compute->unref(); - - /* parallel tasks only */ - xbt_free(task->simdata->host_list); - - xbt_dict_free(&task->simdata->affinity_mask_db); - /* free main structures */ - xbt_free(task->simdata); + delete task->simdata; xbt_free(task); return MSG_OK; @@ -235,9 +241,7 @@ msg_error_t MSG_task_cancel(msg_task_t task) else if (task->simdata->comm) { simdata_task_t simdata = task->simdata; simcall_comm_cancel(simdata->comm); - if (msg_global->debug_multiple_use && simdata->isused!=0) - xbt_ex_free(*(xbt_ex_t*)simdata->isused); - simdata->isused = 0; + simdata->setNotUsed(); } return MSG_OK; } diff --git a/src/msg/msg_vm.cpp b/src/msg/msg_vm.cpp index b7321d7f05..351a03c86d 100644 --- a/src/msg/msg_vm.cpp +++ b/src/msg/msg_vm.cpp @@ -678,7 +678,7 @@ static int migration_tx_fun(int argc, char *argv[]) if (!skip_stage1) { double clock_prev_send = MSG_get_clock(); - TRY { + try { /* At stage 1, we do not need timeout. We have to send all the memory pages even though the duration of this * transfer exceeds the timeout value. */ XBT_VERB("Stage 1: Gonna send %llu", ramsize); @@ -692,7 +692,8 @@ static int migration_tx_fun(int argc, char *argv[]) } else if (sent > ramsize) XBT_CRITICAL("bug"); - } CATCH_ANONYMOUS { + } + catch (xbt_ex& e) { //hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data code) // Stop the dirty page tracking an return (there is no memory space to release) stop_dirty_page_tracking(ms->vm); @@ -741,11 +742,12 @@ static int migration_tx_fun(int argc, char *argv[]) sg_size_t sent = 0; double clock_prev_send = MSG_get_clock(); - TRY { + try { XBT_DEBUG("Stage 2, gonna send %llu", updated_size); sent = send_migration_data(ms->vm, ms->src_pm, ms->dst_pm, updated_size, ms->mbox, 2, stage2_round, mig_speed, mig_timeout); - } CATCH_ANONYMOUS { + } + catch (xbt_ex& e) { //hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data code) // Stop the dirty page tracking an return (there is no memory space to release) stop_dirty_page_tracking(ms->vm); @@ -784,10 +786,11 @@ static int migration_tx_fun(int argc, char *argv[]) simcall_vm_suspend(ms->vm); stop_dirty_page_tracking(ms->vm); - TRY { + try { XBT_DEBUG("Stage 3: Gonna send %f", remaining_size); send_migration_data(ms->vm, ms->src_pm, ms->dst_pm, (sg_size_t)remaining_size, ms->mbox, 3, 0, mig_speed, -1); - } CATCH_ANONYMOUS { + } + catch(xbt_ex& e) { //hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data code) // Stop the dirty page tracking an return (there is no memory space to release) simcall_vm_resume(ms->vm); diff --git a/src/simix/smx_deployment.cpp b/src/simix/smx_deployment.cpp index 1e12265a70..44ccbb8956 100644 --- a/src/simix/smx_deployment.cpp +++ b/src/simix/smx_deployment.cpp @@ -44,15 +44,15 @@ void SIMIX_launch_application(const char *file) SIMIX_init_application(); surf_parse_open(file); - TRY { + try { parse_status = surf_parse(); surf_parse_close(); xbt_assert(!parse_status, "Parse error at %s:%d", file,surf_parse_lineno); } - CATCH_ANONYMOUS { + catch (xbt_ex& e) { XBT_ERROR("Unrecoverable error at %s:%d. The full exception stack follows, in case it helps you to diagnose the problem.", file, surf_parse_lineno); - RETHROW; + throw; } } diff --git a/src/simix/smx_environment.cpp b/src/simix/smx_environment.cpp index 3d82fd9c56..372f25520a 100644 --- a/src/simix/smx_environment.cpp +++ b/src/simix/smx_environment.cpp @@ -32,19 +32,18 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_environment, simix, */ void SIMIX_create_environment(const char *file) { - xbt_ex_t e; double start = 0, end = 0; if(XBT_LOG_ISENABLED(simix_environment, xbt_log_priority_debug)) start = xbt_os_time(); - TRY { + try { parse_platform_file(file); - } CATCH (e) { - xbt_die("Error while loading %s: %s", file, e.msg); + } + catch (xbt_ex& e) { + xbt_die("Error while loading %s: %s", file, e.what()); } if(XBT_LOG_ISENABLED(simix_environment, xbt_log_priority_debug)) end = xbt_os_time(); XBT_DEBUG("PARSE TIME: %g", (end - start)); - } void SIMIX_post_create_environment(void) diff --git a/src/simix/smx_global.cpp b/src/simix/smx_global.cpp index e79e69bf0c..e242207bf4 100644 --- a/src/simix/smx_global.cpp +++ b/src/simix/smx_global.cpp @@ -217,10 +217,6 @@ void SIMIX_global_init(int *argc, char **argv) // a context object with the current context mestro): simgrid::simix::create_maestro(maestro_code); - /* context exception handlers */ - __xbt_running_ctx_fetch = &SIMIX_process_get_running_context; - __xbt_ex_terminate = &SIMIX_process_exception_terminate; - /* Prepare to display some more info when dying on Ctrl-C pressing */ signal(SIGINT, inthandler); @@ -299,15 +295,9 @@ void SIMIX_clean(void) /* Let's free maestro now */ delete simix_global->maestro_process->context; simix_global->maestro_process->context = nullptr; - xbt_free(simix_global->maestro_process->running_ctx); - simix_global->maestro_process->running_ctx = nullptr; delete simix_global->maestro_process; simix_global->maestro_process = nullptr; - /* Restore the default exception setup */ - __xbt_running_ctx_fetch = &__xbt_ex_ctx_default; - __xbt_ex_terminate = &__xbt_ex_terminate_default; - /* Finish context module and SURF */ SIMIX_context_mod_exit(); diff --git a/src/simix/smx_network.cpp b/src/simix/smx_network.cpp index 072a961a0d..266b0cd586 100644 --- a/src/simix/smx_network.cpp +++ b/src/simix/smx_network.cpp @@ -665,12 +665,22 @@ void SIMIX_comm_finish(smx_synchro_t synchro) } /* if there is an exception during a waitany or a testany, indicate the position of the failed communication */ - if (simcall->issuer->doexception) { - if (simcall->call == SIMCALL_COMM_WAITANY) { - simcall->issuer->running_ctx->exception.value = xbt_dynar_search(simcall_comm_waitany__get__comms(simcall), &synchro); + if (simcall->issuer->exception) { + // In order to modify the exception we have to rethrow it: + try { + std::rethrow_exception(simcall->issuer->exception); } - else if (simcall->call == SIMCALL_COMM_TESTANY) { - simcall->issuer->running_ctx->exception.value = xbt_dynar_search(simcall_comm_testany__get__comms(simcall), &synchro); + catch(xbt_ex& e) { + if (simcall->call == SIMCALL_COMM_WAITANY) { + e.value = xbt_dynar_search(simcall_comm_waitany__get__comms(simcall), &synchro); + } + else if (simcall->call == SIMCALL_COMM_TESTANY) { + e.value = xbt_dynar_search(simcall_comm_testany__get__comms(simcall), &synchro); + } + simcall->issuer->exception = std::make_exception_ptr(e); + } + catch(...) { + // Nothing to do } } diff --git a/src/simix/smx_private.h b/src/simix/smx_private.h index 02907e5213..333ea07d33 100644 --- a/src/simix/smx_private.h +++ b/src/simix/smx_private.h @@ -84,11 +84,18 @@ XBT_PUBLIC(void) SIMIX_clean(void); /******************************** Exceptions *********************************/ /** @brief Ask to the provided simix process to raise the provided exception */ -#define SMX_EXCEPTION(issuer, cat, val, msg) \ - if (1) { \ - smx_process_t _smx_throw_issuer = (issuer); /* evaluate only once */\ - THROW_PREPARE(_smx_throw_issuer->running_ctx, (cat), (val), xbt_strdup(msg)); \ - _smx_throw_issuer->doexception = 1; \ +#define SMX_EXCEPTION(issuer, cat, val, msg) \ + if (1) { \ + smx_process_t _smx_throw_issuer = (issuer); /* evaluate only once */ \ + xbt_ex e(msg); \ + e.category = cat; \ + e.value = val; \ + e.procname = xbt_procname(); \ + e.pid = xbt_getpid(); \ + e.file = __FILE__; \ + e.line = __LINE__; \ + e.func = __func__; \ + _smx_throw_issuer->exception = std::make_exception_ptr(e); \ } else ((void)0) /* ******************************** File ************************************ */ diff --git a/src/simix/smx_process.cpp b/src/simix/smx_process.cpp index a5f54960e0..1eb504d23d 100644 --- a/src/simix/smx_process.cpp +++ b/src/simix/smx_process.cpp @@ -126,17 +126,10 @@ void SIMIX_process_empty_trash(void) while ((process = (smx_process_t) xbt_swag_extract(simix_global->process_to_destroy))) { XBT_DEBUG("Getting rid of %p",process); - delete process->context; - - /* Free the exception allocated at creation time */ - free(process->running_ctx); xbt_dict_free(&process->properties); - xbt_fifo_free(process->comms); - xbt_dynar_free(&process->on_exit); - delete process; } } @@ -153,8 +146,6 @@ void create_maestro(std::function code) maestro->ppid = -1; maestro->name = ""; maestro->data = nullptr; - maestro->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t)); - XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx); if (!code) { maestro->context = SIMIX_context_new(std::function(), nullptr, maestro); @@ -273,13 +264,6 @@ smx_process_t SIMIX_process_create( std::move(code), simix_global->cleanup_process_function, process); - process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t)); - XBT_RUNNING_CTX_INITIALIZE(process->running_ctx); - - if(MC_is_active()){ - MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx)); - } - /* Add properties */ process->properties = properties; @@ -363,13 +347,6 @@ smx_process_t SIMIX_process_attach( process->context = simix_global->context_factory->attach( simix_global->cleanup_process_function, process); - process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t)); - XBT_RUNNING_CTX_INITIALIZE(process->running_ctx); - - if(MC_is_active()){ - MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx)); - } - /* Add properties */ process->properties = properties; @@ -450,7 +427,7 @@ void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) { process->context->iwannadie = 1; process->blocked = 0; process->suspended = 0; - process->doexception = 0; + process->exception = nullptr; /* destroy the blocking synchro if any */ if (process->waiting_synchro) { @@ -859,15 +836,16 @@ void SIMIX_process_yield(smx_process_t self) if (self->suspended) { XBT_DEBUG("Hey! I'm suspended."); - xbt_assert(!self->doexception, "Gasp! This exception may be lost by subsequent calls."); + xbt_assert(self->exception != nullptr, "Gasp! This exception may be lost by subsequent calls."); self->suspended = 0; SIMIX_process_suspend(self, self); } - if (self->doexception) { + if (self->exception != nullptr) { XBT_DEBUG("Wait, maestro left me an exception"); - self->doexception = 0; - RETHROW; + std::exception_ptr exception = std::move(self->exception); + self->exception = nullptr; + std::rethrow_exception(std::move(exception)); } if(SMPI_switch_data_segment && self->segment_index != -1){ @@ -875,16 +853,6 @@ void SIMIX_process_yield(smx_process_t self) } } -/* callback: context fetching */ -xbt_running_ctx_t *SIMIX_process_get_running_context(void) -{ - smx_process_t process = SIMIX_process_self(); - if (process) - return process->running_ctx; - else - return nullptr; -} - /* callback: termination */ void SIMIX_process_exception_terminate(xbt_ex_t * e) { diff --git a/src/simix/smx_process_private.h b/src/simix/smx_process_private.h index da6cf695fd..87c9b100c2 100644 --- a/src/simix/smx_process_private.h +++ b/src/simix/smx_process_private.h @@ -50,10 +50,9 @@ public: simgrid::xbt::string name; sg_host_t host = nullptr; /* the host on which the process is running */ smx_context_t context = nullptr; /* the context (uctx/raw/thread) that executes the user function */ - xbt_running_ctx_t *running_ctx = nullptr; // TODO, pack them - bool doexception = false; + std::exception_ptr exception; bool blocked = false; bool suspended = false; bool auto_restart = false; @@ -97,7 +96,6 @@ XBT_PRIVATE void SIMIX_process_stop(smx_process_t arg); XBT_PRIVATE void SIMIX_process_cleanup(smx_process_t arg); XBT_PRIVATE void SIMIX_process_empty_trash(void); XBT_PRIVATE void SIMIX_process_yield(smx_process_t self); -XBT_PRIVATE xbt_running_ctx_t *SIMIX_process_get_running_context(void); XBT_PRIVATE void SIMIX_process_exception_terminate(xbt_ex_t * e); XBT_PRIVATE void SIMIX_process_change_host(smx_process_t process, sg_host_t dest); diff --git a/src/smpi/colls/smpi_automatic_selector.c b/src/smpi/colls/smpi_automatic_selector.cpp similarity index 96% rename from src/smpi/colls/smpi_automatic_selector.c rename to src/smpi/colls/smpi_automatic_selector.cpp index 29455c2b80..4898c5c995 100644 --- a/src/smpi/colls/smpi_automatic_selector.c +++ b/src/smpi/colls/smpi_automatic_selector.cpp @@ -25,9 +25,8 @@ ret smpi_coll_tuned_ ## cat ## _ ## automatic(COLL_UNPAREN args)\ {\ double time1, time2, time_min=DBL_MAX;\ - volatile int min_coll=-1, global_coll=-1;\ - volatile int i;\ - xbt_ex_t ex;\ + int min_coll=-1, global_coll=-1;\ + int i;\ double buf_in, buf_out, max_min=DBL_MAX;\ for (i = 0; mpi_coll_##cat##_description[i].name; i++){\ if(!strcmp(mpi_coll_##cat##_description[i].name, "automatic"))continue;\ @@ -35,12 +34,11 @@ smpi_mpi_barrier(comm);\ TRACE_AUTO_COLL(cat)\ time1 = SIMIX_get_clock();\ - TRY{\ + try {\ ((int (*) args)\ mpi_coll_##cat##_description[i].coll) args2 ;\ }\ - CATCH(ex) {\ - xbt_ex_free(ex);\ + catch (std::exception& ex) {\ continue;\ }\ time2 = SIMIX_get_clock();\ diff --git a/src/smpi/smpi_bench.cpp b/src/smpi/smpi_bench.cpp index bbd79b3d97..e6998c378f 100644 --- a/src/smpi/smpi_bench.cpp +++ b/src/smpi/smpi_bench.cpp @@ -561,23 +561,24 @@ void smpi_shared_free(void *ptr) int smpi_shared_known_call(const char* func, const char* input) { char* loc = bprintf("%s:%s", func, input); - xbt_ex_t ex; int known = 0; if (calls==nullptr) { calls = xbt_dict_new_homogeneous(nullptr); } - TRY { + try { xbt_dict_get(calls, loc); /* Succeed or throw */ known = 1; - } - TRY_CLEANUP { xbt_free(loc); } - CATCH(ex) { + catch (xbt_ex& ex) { + xbt_free(loc); if (ex.category != not_found_error) - RETHROW; - xbt_ex_free(ex); + throw; + } + catch(...) { + xbt_free(loc); + throw; } return known; } diff --git a/src/smpi/smpi_comm.cpp b/src/smpi/smpi_comm.cpp index c13a486cc8..6d3f9a9a54 100644 --- a/src/smpi/smpi_comm.cpp +++ b/src/smpi/smpi_comm.cpp @@ -515,7 +515,7 @@ int smpi_comm_attr_delete(MPI_Comm comm, int keyval){ if(elem==nullptr) return MPI_ERR_ARG; if(elem->delete_fn!=MPI_NULL_DELETE_FN){ - void * value; + void* value = nullptr; int flag; if(smpi_comm_attr_get(comm, keyval, &value, &flag)==MPI_SUCCESS){ int ret = elem->delete_fn(comm, keyval, value, &flag); @@ -535,18 +535,17 @@ int smpi_comm_attr_get(MPI_Comm comm, int keyval, void* attr_value, int* flag){ static_cast(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast(&keyval), sizeof(int))); if(elem==nullptr) return MPI_ERR_ARG; - xbt_ex_t ex; if(comm->attributes==nullptr){ *flag=0; return MPI_SUCCESS; } - TRY { + try { *static_cast(attr_value) = xbt_dict_get_ext(comm->attributes, reinterpret_cast(&keyval), sizeof(int)); *flag=1; - } CATCH(ex) { + } + catch (xbt_ex& ex) { *flag=0; - xbt_ex_free(ex); } return MPI_SUCCESS; } @@ -559,7 +558,7 @@ int smpi_comm_attr_put(MPI_Comm comm, int keyval, void* attr_value){ if(elem==nullptr) return MPI_ERR_ARG; int flag; - void* value; + void* value = nullptr; smpi_comm_attr_get(comm, keyval, &value, &flag); if(flag!=0 && elem->delete_fn!=MPI_NULL_DELETE_FN){ int ret = elem->delete_fn(comm, keyval, value, &flag); diff --git a/src/smpi/smpi_mpi_dt.cpp b/src/smpi/smpi_mpi_dt.cpp index 4d53ee274d..9b75129f89 100644 --- a/src/smpi/smpi_mpi_dt.cpp +++ b/src/smpi/smpi_mpi_dt.cpp @@ -1570,7 +1570,7 @@ int smpi_type_attr_delete(MPI_Datatype type, int keyval){ if(elem==nullptr) return MPI_ERR_ARG; if(elem->delete_fn!=MPI_NULL_DELETE_FN){ - void * value; + void * value = nullptr; int flag; if(smpi_type_attr_get(type, keyval, &value, &flag)==MPI_SUCCESS){ int ret = elem->delete_fn(type, keyval, value, &flag); @@ -1590,18 +1590,16 @@ int smpi_type_attr_get(MPI_Datatype type, int keyval, void* attr_value, int* fla static_cast(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast(&keyval), sizeof(int))); if(elem==nullptr) return MPI_ERR_ARG; - xbt_ex_t ex; if(type->attributes==nullptr){ *flag=0; return MPI_SUCCESS; } - TRY { + try { *static_cast(attr_value) = xbt_dict_get_ext(type->attributes, reinterpret_cast(&keyval), sizeof(int)); *flag=1; } - CATCH(ex) { + catch (xbt_ex& ex) { *flag=0; - xbt_ex_free(ex); } return MPI_SUCCESS; } @@ -1614,7 +1612,7 @@ int smpi_type_attr_put(MPI_Datatype type, int keyval, void* attr_value){ if(elem==nullptr) return MPI_ERR_ARG; int flag; - void* value; + void* value = nullptr; smpi_type_attr_get(type, keyval, &value, &flag); if(flag!=0 && elem->delete_fn!=MPI_NULL_DELETE_FN){ int ret = elem->delete_fn(type, keyval, value, &flag); diff --git a/src/smpi/smpi_pmpi.cpp b/src/smpi/smpi_pmpi.cpp index 6a56ecc2e5..cf6d2f1a4a 100644 --- a/src/smpi/smpi_pmpi.cpp +++ b/src/smpi/smpi_pmpi.cpp @@ -3174,13 +3174,12 @@ int PMPI_Info_dup(MPI_Info info, MPI_Info *newinfo){ } int PMPI_Info_delete(MPI_Info info, char *key){ - xbt_ex_t e; if (info == nullptr || key==nullptr) return MPI_ERR_ARG; - TRY { + try { xbt_dict_remove(info->info_dict, key); - }CATCH(e){ - xbt_ex_free(e); + } + catch(xbt_ex& e){ return MPI_ERR_INFO_NOKEY; } return MPI_SUCCESS; diff --git a/src/surf/storage_n11.cpp b/src/surf/storage_n11.cpp index a0d281d876..20668ded2c 100644 --- a/src/surf/storage_n11.cpp +++ b/src/surf/storage_n11.cpp @@ -162,7 +162,7 @@ void StorageN11Model::updateActionsState(double /*now*/, double delta) // Update the disk usage // Update the file size // For each action of type write - volatile double current_progress = + double current_progress = delta * lmm_variable_getvalue(action->getVariable()); long int incr = current_progress; diff --git a/src/surf/surf_interface.cpp b/src/surf/surf_interface.cpp index b3de241734..32f9d99b26 100644 --- a/src/surf/surf_interface.cpp +++ b/src/surf/surf_interface.cpp @@ -474,6 +474,7 @@ double Model::next_occuring_event_lazy(double now) double Model::next_occuring_event_full(double /*now*/) { THROW_UNIMPLEMENTED; + return 0.0; } double Model::shareResourcesMaxMin(ActionList *running_actions, diff --git a/src/xbt/backtrace_linux.c b/src/xbt/backtrace_linux.c index 1c83ddb1cb..741c28f025 100644 --- a/src/xbt/backtrace_linux.c +++ b/src/xbt/backtrace_linux.c @@ -1,5 +1,5 @@ /* backtrace_linux - backtrace displaying on linux platform */ -/* This file is included by ex.c on need (have execinfo.h, popen & addrline)*/ +/* This file is included by ex.cpp on need (have execinfo.h, popen & addrline)*/ /* Copyright (c) 2008-2015. The SimGrid Team. * All rights reserved. */ @@ -11,7 +11,7 @@ #include #include -/* This file is to be included in ex.c, so the following headers are not mandatory, but it's to make sure that eclipse see them too */ +/* This file is to be included in ex.cpp, so the following headers are not mandatory, but it's to make sure that eclipse see them too */ #include "xbt/ex.h" #include "xbt/log.h" #include "xbt/str.h" @@ -43,7 +43,7 @@ struct trace_arg { static _Unwind_Reason_Code backtrace_helper (struct _Unwind_Context *ctx, void *a) { - struct trace_arg *arg = a; + struct trace_arg *arg = (struct trace_arg *) a; /* We are first called with address in the __backtrace function. Skip it. */ @@ -71,7 +71,10 @@ int xbt_backtrace_no_malloc(void **array, int size) { for(i=0; i < size; i++) array[i] = NULL; - struct trace_arg arg = { .array = array, .size = size, .cnt = -1 }; + struct trace_arg arg; + arg .array = array; + arg.size = size; + arg.cnt = -1; if (size >= 1) _Unwind_Backtrace(backtrace_helper, &arg); @@ -85,11 +88,13 @@ int xbt_backtrace_no_malloc(void **array, int size) { void xbt_backtrace_current(xbt_ex_t * e) { - e->used = backtrace((void **) e->bt, XBT_BACKTRACE_SIZE); - if (e->used == 0) { + void* bt[XBT_BACKTRACE_SIZE]; + std::size_t used = backtrace((void **) bt, XBT_BACKTRACE_SIZE); + e->bt.assign(bt, bt + used); + if (used == 0) { fprintf(stderr, "The backtrace() function failed, which probably means that the memory is exhausted. Here is a crude dump of the exception that I was trying to build:"); fprintf(stderr, "%s(%d) [%s:%d] %s", - e->procname, e->pid, e->file, e->line, e->msg); + e->procname.c_str(), e->pid, e->file, e->line, e->what()); fprintf(stderr, "Bailing out now since there is nothing I can do without a decent amount of memory. Please go fix the memleaks\n"); exit(1); } @@ -97,8 +102,6 @@ void xbt_backtrace_current(xbt_ex_t * e) void xbt_ex_setup_backtrace(xbt_ex_t * e) //FIXME: This code could be greatly improved/simplifyied with http://cairo.sourcearchive.com/documentation/1.9.4/backtrace-symbols_8c-source.html { - int i; - /* to get the backtrace from the libc */ char **backtrace_syms; @@ -122,24 +125,23 @@ void xbt_ex_setup_backtrace(xbt_ex_t * e) //FIXME: This code could be greatly im xbt_assert(e, "Backtrace not setup yet, cannot set it up for display"); - e->bt_strings = NULL; + e->bt_strings.clear(); if (xbt_binary_name == NULL) /* no binary name, nothing to do */ return; - if (e->used <= 1) + if (e->bt.empty()) return; /* ignore first one, which is xbt_backtrace_current() */ - e->used--; - memmove(e->bt, e->bt + 1, (sizeof *e->bt) * e->used); + e->bt.erase(e->bt.begin()); - backtrace_syms = backtrace_symbols(e->bt, e->used); + backtrace_syms = backtrace_symbols(e->bt.data(), e->bt.size()); /* build the commandline */ if (stat(xbt_binary_name, &stat_buf)) { /* Damn. binary not in current dir. We'll have to dig the PATH to find it */ - for (i = 0; environ[i]; i++) { + for (std::size_t i = 0; environ[i]; i++) { if (!strncmp("PATH=", environ[i], 5)) { xbt_dynar_t path = xbt_str_split(environ[i] + 5, ":"); unsigned int cpt; @@ -157,10 +159,9 @@ void xbt_ex_setup_backtrace(xbt_ex_t * e) //FIXME: This code could be greatly im xbt_dynar_free(&path); if (stat(binary_name, &stat_buf)) { /* not found */ - e->used = 1; - e->bt_strings = xbt_new(char *, 1); - - e->bt_strings[0] = bprintf("(binary '%s' not found in the PATH)", xbt_binary_name); + char* str = bprintf("(binary '%s' not found in the PATH)", xbt_binary_name); + e->bt_strings = { str }; + free(str); free(backtrace_syms); return; } @@ -170,17 +171,17 @@ void xbt_ex_setup_backtrace(xbt_ex_t * e) //FIXME: This code could be greatly im } else { binary_name = xbt_strdup(xbt_binary_name); } - int strsize=strlen(ADDR2LINE) + 25 + strlen(binary_name) + 32 * e->used; + int strsize = strlen(ADDR2LINE) + 25 + strlen(binary_name) + 32 * e->bt_strings.size(); cmd = curr = xbt_new(char, strsize); curr += snprintf(curr,strsize, "%s -f -e %s ", ADDR2LINE, binary_name); free(binary_name); - addrs = xbt_new(char *, e->used); - for (i = 0; i < e->used; i++) { + addrs = xbt_new(char *, e->bt.size()); + for (std::size_t i = 0; i < e->bt.size(); i++) { char *p; /* retrieve this address */ - XBT_DEBUG("Retrieving address number %d from '%s'", i, backtrace_syms[i]); + XBT_DEBUG("Retrieving address number %zd from '%s'", i, backtrace_syms[i]); snprintf(buff, 256, "%s", strchr(backtrace_syms[i], '[') + 1); p = strchr(buff, ']'); *p = '\0'; @@ -188,7 +189,7 @@ void xbt_ex_setup_backtrace(xbt_ex_t * e) //FIXME: This code could be greatly im addrs[i] = xbt_strdup(buff); else addrs[i] = xbt_strdup("0x0"); - XBT_DEBUG("Set up a new address: %d, '%s'(%p)", i, addrs[i], addrs[i]); + XBT_DEBUG("Set up a new address: %zd, '%s'(%p)", i, addrs[i], addrs[i]); /* Add it to the command line args */ curr += snprintf(curr,strsize, "%s ", addrs[i]); @@ -196,7 +197,7 @@ void xbt_ex_setup_backtrace(xbt_ex_t * e) //FIXME: This code could be greatly im addr_len = strlen(addrs[0]); /* parse the output and build a new backtrace */ - e->bt_strings = xbt_new(char *, e->used); + e->bt_strings.resize(e->bt.size()); XBT_VERB("Fire a first command: '%s'", cmd); pipe = popen(cmd, "r"); @@ -204,25 +205,26 @@ void xbt_ex_setup_backtrace(xbt_ex_t * e) //FIXME: This code could be greatly im xbt_die("Cannot fork addr2line to display the backtrace"); } - for (i = 0; i < e->used; i++) { - XBT_DEBUG("Looking for symbol %d, addr = '%s'", i, addrs[i]); + for (std::size_t i = 0; i < e->bt.size(); i++) { + XBT_DEBUG("Looking for symbol %zd, addr = '%s'", i, addrs[i]); if (fgets(line_func, 1024, pipe)) { line_func[strlen(line_func) - 1] = '\0'; } else { - XBT_VERB("Cannot run fgets to look for symbol %d, addr %s", i, addrs[i]); + XBT_VERB("Cannot run fgets to look for symbol %zd, addr %s", i, addrs[i]); strncpy(line_func, "???",3); } if (fgets(line_pos, 1024, pipe)) { line_pos[strlen(line_pos) - 1] = '\0'; } else { - XBT_VERB("Cannot run fgets to look for symbol %d, addr %s", i, addrs[i]); + XBT_VERB("Cannot run fgets to look for symbol %zd, addr %s", i, addrs[i]); strncpy(line_pos, backtrace_syms[i],1024); } if (strcmp("??", line_func) != 0) { XBT_DEBUG("Found static symbol %s() at %s", line_func, line_pos); - e->bt_strings[i] = - bprintf("** In %s() at %s", line_func, line_pos); + char* s = bprintf("** In %s() at %s", line_func, line_pos); + e->bt_strings[i] = s; + free(s); } else { /* Damn. The symbol is in a dynamic library. Let's get wild */ char *maps_name; @@ -274,7 +276,9 @@ void xbt_ex_setup_backtrace(xbt_ex_t * e) //FIXME: This code could be greatly im if (!found) { XBT_VERB("Problem while reading the maps file. Following backtrace will be mangled."); XBT_DEBUG("No dynamic. Static symbol: %s", backtrace_syms[i]); - e->bt_strings[i] = bprintf("** In ?? (%s)", backtrace_syms[i]); + char* s = bprintf("** In ?? (%s)", backtrace_syms[i]); + e->bt_strings[i] = s; + free(s); continue; } @@ -326,11 +330,15 @@ void xbt_ex_setup_backtrace(xbt_ex_t * e) //FIXME: This code could be greatly im /* check whether the trick worked */ if (strcmp("??", line_func)) { XBT_DEBUG("Found dynamic symbol %s() at %s", line_func, line_pos); - e->bt_strings[i] = bprintf("** In %s() at %s", line_func, line_pos); + char* s = bprintf("** In %s() at %s", line_func, line_pos); + e->bt_strings[i] = s; + free(s); } else { /* damn, nothing to do here. Let's print the raw address */ XBT_DEBUG("Dynamic symbol not found. Raw address = %s", backtrace_syms[i]); - e->bt_strings[i] = bprintf("** In ?? at %s", backtrace_syms[i]); + char* s = bprintf("** In ?? at %s", backtrace_syms[i]); + e->bt_strings[i] = s; + free(s); } } free(addrs[i]); @@ -339,17 +347,17 @@ void xbt_ex_setup_backtrace(xbt_ex_t * e) //FIXME: This code could be greatly im if (!strncmp("main", line_func, strlen("main")) || !strncmp("xbt_thread_context_wrapper", line_func, strlen("xbt_thread_context_wrapper")) || !strncmp("smx_ctx_sysv_wrapper", line_func, strlen("smx_ctx_sysv_wrapper"))) { - int j; - for (j = i + 1; j < e->used; j++) + for (std::size_t j = i + 1; j < e->bt.size(); j++) free(addrs[j]); - e->used = i + 1; if (!strncmp ("xbt_thread_context_wrapper", line_func, strlen("xbt_thread_context_wrapper"))) { - free(e->bt_strings[i]); - e->bt_strings[i] = xbt_strdup("** (in a separate thread)"); + e->bt_strings.push_back("** (in a separate thread)"); + } else { + e->bt_strings.push_back(""); } + } } pclose(pipe); diff --git a/src/xbt/config.cpp b/src/xbt/config.cpp index 6fc4d001ab..eaa8450d2f 100644 --- a/src/xbt/config.cpp +++ b/src/xbt/config.cpp @@ -605,10 +605,8 @@ on_exception: // Exit from the catch blog (and do the correct exceptio cleaning) // before attempting to THROWF. #define TRANSLATE_EXCEPTIONS(...) \ - catch(simgrid::config::missing_key_error& e) { goto on_exception; } \ - catch(...) { goto on_missing_key; } \ - on_missing_key: THROWF(not_found_error, 0, __VA_ARGS__); \ - on_exception: THROWF(not_found_error, 0, __VA_ARGS__); + catch(simgrid::config::missing_key_error& e) { THROWF(not_found_error, 0, __VA_ARGS__); abort(); } \ + catch(...) { THROWF(not_found_error, 0, __VA_ARGS__); abort(); } /** @brief Set the value of a variable, using the string representation of that value * @@ -869,14 +867,11 @@ XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests") xbt_test_add("Access to a non-existant entry"); { - xbt_ex_t e; - - TRY { + try { xbt_cfg_set_parse("color:blue"); - } CATCH(e) { + } catch(xbt_ex& e) { if (e.category != not_found_error) xbt_test_exception(e); - xbt_ex_free(e); } } xbt_cfg_free(&simgrid_config); diff --git a/src/xbt/cunit.c b/src/xbt/cunit.cpp similarity index 99% rename from src/xbt/cunit.c rename to src/xbt/cunit.cpp index 48393c4dce..1cfd7f8449 100644 --- a/src/xbt/cunit.c +++ b/src/xbt/cunit.cpp @@ -669,7 +669,7 @@ void _xbt_test_fail(const char *file, int line, const char *fmt, ...) void xbt_test_exception(xbt_ex_t e) { - _xbt_test_fail(e.file, e.line, "Exception %s raised: %s", xbt_ex_catname(e.category), e.msg); + _xbt_test_fail(e.file, e.line, "Exception %s raised: %s", xbt_ex_catname(e.category), e.what()); } void xbt_test_expect_failure(void) diff --git a/src/xbt/dict.c b/src/xbt/dict.cpp similarity index 94% rename from src/xbt/dict.c rename to src/xbt/dict.cpp index 8c98500127..f922c79137 100644 --- a/src/xbt/dict.c +++ b/src/xbt/dict.cpp @@ -8,6 +8,7 @@ #include #include +#include "xbt/dict.h" #include "xbt/ex.h" #include "xbt/log.h" #include "xbt/mallocator.h" @@ -96,7 +97,7 @@ void xbt_dict_free(xbt_dict_t * dict) } /** Returns the amount of elements in the dict */ -inline unsigned int xbt_dict_size(xbt_dict_t dict) +unsigned int xbt_dict_size(xbt_dict_t dict) { return (dict ? (unsigned int) dict->count : (unsigned int) 0); } @@ -104,9 +105,8 @@ inline unsigned int xbt_dict_size(xbt_dict_t dict) /* Expend the size of the dict */ static void xbt_dict_rehash(xbt_dict_t dict) { - const int oldsize = dict->table_size + 1; - int newsize = oldsize * 2; - int i; + const unsigned oldsize = dict->table_size + 1; + unsigned newsize = oldsize * 2; xbt_dictelm_t *currcell; xbt_dictelm_t *twincell; xbt_dictelm_t bucklet; @@ -118,7 +118,7 @@ static void xbt_dict_rehash(xbt_dict_t dict) dict->table = currcell; XBT_DEBUG("REHASH (%d->%d)", oldsize, newsize); - for (i = 0; i < oldsize; i++, currcell++) { + for (unsigned i = 0; i < oldsize; i++, currcell++) { if (!*currcell) /* empty cell */ continue; twincell = currcell + oldsize; @@ -155,7 +155,7 @@ static void xbt_dict_rehash(xbt_dict_t dict) * Set the \a data in the structure under the \a key, which can be any kind of data, as long as its length is provided * in \a key_len. */ -inline void xbt_dict_set_ext(xbt_dict_t dict, const char *key, int key_len, void *data, void_f_pvoid_t free_ctn) +void xbt_dict_set_ext(xbt_dict_t dict, const char *key, int key_len, void *data, void_f_pvoid_t free_ctn) { unsigned int hash_code = xbt_str_hash_ext(key, key_len); @@ -201,7 +201,7 @@ inline void xbt_dict_set_ext(xbt_dict_t dict, const char *key, int key_len, void * * set the \a data in the structure under the \a key, which is anull terminated string. */ -inline void xbt_dict_set(xbt_dict_t dict, const char *key, void *data, void_f_pvoid_t free_ctn) +void xbt_dict_set(xbt_dict_t dict, const char *key, void *data, void_f_pvoid_t free_ctn) { xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn); } @@ -216,7 +216,7 @@ inline void xbt_dict_set(xbt_dict_t dict, const char *key, void *data, void_f_pv * * Search the given \a key. Throws not_found_error when not found. */ -inline void *xbt_dict_get_ext(xbt_dict_t dict, const char *key, int key_len) +void *xbt_dict_get_ext(xbt_dict_t dict, const char *key, int key_len) { unsigned int hash_code = xbt_str_hash_ext(key, key_len); xbt_dictelm_t current = dict->table[hash_code & dict->table_size]; @@ -283,7 +283,7 @@ char *xbt_dict_get_elm_key(xbt_dictelm_t elm) * Search the given \a key. Throws not_found_error when not found. * Check xbt_dict_get_or_null() for a version returning NULL without exception when not found. */ -inline void *xbt_dict_get(xbt_dict_t dict, const char *key) +void *xbt_dict_get(xbt_dict_t dict, const char *key) { return xbt_dict_get_elm(dict, key)->content; } @@ -298,7 +298,7 @@ inline void *xbt_dict_get(xbt_dict_t dict, const char *key) * Search the given \a key. Throws not_found_error when not found. * Check xbt_dict_get_or_null() for a version returning NULL without exception when not found. */ -inline xbt_dictelm_t xbt_dict_get_elm(xbt_dict_t dict, const char *key) +xbt_dictelm_t xbt_dict_get_elm(xbt_dict_t dict, const char *key) { xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key); @@ -311,7 +311,7 @@ inline xbt_dictelm_t xbt_dict_get_elm(xbt_dict_t dict, const char *key) /** * \brief like xbt_dict_get(), but returning NULL when not found */ -inline void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key) +void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key) { xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key); @@ -324,7 +324,7 @@ inline void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key) /** * \brief like xbt_dict_get_elm(), but returning NULL when not found */ -inline xbt_dictelm_t xbt_dict_get_elm_or_null(xbt_dict_t dict, const char *key) +xbt_dictelm_t xbt_dict_get_elm_or_null(xbt_dict_t dict, const char *key) { unsigned int hash_code = xbt_str_hash(key); xbt_dictelm_t current = dict->table[hash_code & dict->table_size]; @@ -343,7 +343,7 @@ inline xbt_dictelm_t xbt_dict_get_elm_or_null(xbt_dict_t dict, const char *key) * * Remove the entry associated with the given \a key (throws not_found) */ -inline void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len) +void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len) { unsigned int hash_code = xbt_str_hash_ext(key, key_len); xbt_dictelm_t previous = NULL; @@ -379,7 +379,7 @@ inline void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len) * * Remove the entry associated with the given \a key */ -inline void xbt_dict_remove(xbt_dict_t dict, const char *key) +void xbt_dict_remove(xbt_dict_t dict, const char *key) { xbt_dict_remove_ext(dict, key, strlen(key)); } @@ -409,7 +409,7 @@ void xbt_dict_reset(xbt_dict_t dict) * \brief Return the number of elements in the dict. * \param dict a dictionary */ -inline int xbt_dict_length(xbt_dict_t dict) +int xbt_dict_length(xbt_dict_t dict) { return dict->count; } @@ -417,13 +417,13 @@ inline int xbt_dict_length(xbt_dict_t dict) /** @brief function to be used in xbt_dict_dump as long as the stored values are strings */ void xbt_dict_dump_output_string(void *s) { - fputs(s, stdout); + fputs((char*) s, stdout); } /** * \brief test if the dict is empty or not */ -inline int xbt_dict_is_empty(xbt_dict_t dict) +int xbt_dict_is_empty(xbt_dict_t dict) { return !dict || (xbt_dict_length(dict) == 0); } @@ -572,6 +572,7 @@ void xbt_dict_postexit(void) } #ifdef SIMGRID_TEST +#include #include "xbt.h" #include "xbt/ex.h" #include "src/internal_config.h" @@ -618,10 +619,8 @@ static void fill(xbt_dict_t * head, int homogeneous) static void search_ext(xbt_dict_t head, const char *key, const char *data) { - char *found; - xbt_test_add("Search %s", key); - found = xbt_dict_get(head, key); + char *found = (char*) xbt_dict_get(head, key); xbt_test_log("Found %s", found); if (data) { xbt_test_assert(found, "data do not match expectations: found NULL while searching for %s", data); @@ -665,17 +664,15 @@ static void traverse(xbt_dict_t head) static void search_not_found(xbt_dict_t head, const char *data) { int ok = 0; - xbt_ex_t e; - xbt_test_add("Search %s (expected not to be found)", data); - TRY { - data = xbt_dict_get(head, data); + try { + data = (const char*) xbt_dict_get(head, data); THROWF(unknown_error, 0, "Found something which shouldn't be there (%s)", data); - } CATCH(e) { + } + catch(xbt_ex& e) { if (e.category != not_found_error) xbt_test_exception(e); - xbt_ex_free(e); ok = 1; } xbt_test_assert(ok, "Exception not raised"); @@ -731,12 +728,12 @@ static void basic_test(int homogeneous) xbt_test_add("Traversal and search the empty dictionary"); head = homogeneous ? xbt_dict_new_homogeneous(&free) : xbt_dict_new(); traverse(head); - TRY { + try { debuged_remove(head, "12346"); - } CATCH(e) { + } + catch(xbt_ex& e) { if (e.category != not_found_error) xbt_test_exception(e); - xbt_ex_free(e); } xbt_dict_free(&head); @@ -781,7 +778,7 @@ static void basic_test(int homogeneous) /* RETRIEVE */ xbt_test_add("Search 123"); - data = xbt_dict_get(head, "123"); + data = (char*) xbt_dict_get(head, "123"); xbt_test_assert(data); xbt_test_assert(!strcmp("123", data)); @@ -824,12 +821,12 @@ static void remove_test(int homogeneous) fill(&head, homogeneous); count(head, 7); xbt_test_add("Remove non existing data"); - TRY { + try { debuged_remove(head, "Does not exist"); - } CATCH(e) { + } + catch(xbt_ex& e) { if (e.category != not_found_error) xbt_test_exception(e); - xbt_ex_free(e); } traverse(head); @@ -849,12 +846,12 @@ static void remove_test(int homogeneous) debuged_remove(head, "123456"); traverse(head); count(head, 3); - TRY { + try { debuged_remove(head, "12346"); - } CATCH(e) { + } + catch(xbt_ex& e) { if (e.category != not_found_error) xbt_test_exception(e); - xbt_ex_free(e); traverse(head); } debuged_remove(head, "1234"); @@ -863,12 +860,12 @@ static void remove_test(int homogeneous) traverse(head); debuged_remove(head, "123"); traverse(head); - TRY { + try { debuged_remove(head, "12346"); - } CATCH(e) { + } + catch(xbt_ex& e) { if (e.category != not_found_error) xbt_test_exception(e); - xbt_ex_free(e); } traverse(head); @@ -954,18 +951,18 @@ XBT_TEST_UNIT("crash", test_dict_crash, "Crash test") /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */ for (j = 0; j < 1000; j++) { char *data = NULL; - key = xbt_malloc(SIZEOFKEY); + key = (char*) xbt_malloc(SIZEOFKEY); do { for (k = 0; k < SIZEOFKEY - 1; k++) key[k] = rand() % ('z' - 'a') + 'a'; key[k] = '\0'; /* printf("[%d %s]\n",j,key); */ - data = xbt_dict_get_or_null(head, key); + data = (char*) xbt_dict_get_or_null(head, key); } while (data != NULL); xbt_dict_set(head, key, key, &free); - data = xbt_dict_get(head, key); + data = (char*) xbt_dict_get(head, key); xbt_test_assert(!strcmp(key, data), "Retrieved value (%s) != Injected value (%s)", key, data); count(head, j + 1); @@ -980,7 +977,7 @@ XBT_TEST_UNIT("crash", test_dict_crash, "Crash test") xbt_test_add("Fill %d elements, with keys being the number of element", NB_ELM); for (j = 0; j < NB_ELM; j++) { /* if (!(j%1000)) { printf("."); fflush(stdout); } */ - key = xbt_malloc(10); + key = (char*) xbt_malloc(10); snprintf(key,10, "%d", j); xbt_dict_set(head, key, key, &free); @@ -992,7 +989,7 @@ XBT_TEST_UNIT("crash", test_dict_crash, "Crash test") xbt_test_log("There is %d elements", i); xbt_test_add("Search my %d elements 20 times", NB_ELM); - key = xbt_malloc(10); + key = (char*) xbt_malloc(10); for (i = 0; i < 20; i++) { void *data; /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */ @@ -1007,7 +1004,7 @@ XBT_TEST_UNIT("crash", test_dict_crash, "Crash test") free(key); xbt_test_add("Remove my %d elements", NB_ELM); - key = xbt_malloc(10); + key = (char*) xbt_malloc(10); for (j = 0; j < NB_ELM; j++) { /* if (!(j%10000)) printf("."); fflush(stdout); */ snprintf(key,10, "%d", j); @@ -1029,12 +1026,12 @@ XBT_TEST_UNIT("ext", test_dict_int, "Test dictionnary with int keys") int i; for (i = 0; i < count; ++i) xbt_dict_set_ext(dict, (char*) &i, sizeof(i), (void*) (intptr_t) i, NULL); - xbt_test_assert(xbt_dict_size(dict) == count, "Bad number of elements in the dictionnary"); + xbt_test_assert(xbt_dict_size(dict) == (unsigned) count, "Bad number of elements in the dictionnary"); xbt_test_add("Check elements"); for (i = 0; i < count; ++i) { int res = (int) (intptr_t) xbt_dict_get_ext(dict, (char*) &i, sizeof(i)); - xbt_test_assert(xbt_dict_size(dict) == count, "Unexpected value at index %i, expected %i but was %i", i, i, res); + xbt_test_assert(xbt_dict_size(dict) == (unsigned) count, "Unexpected value at index %i, expected %i but was %i", i, i, res); } xbt_test_add("Free the array"); diff --git a/src/xbt/dict_private.h b/src/xbt/dict_private.h index 9d3be26d8a..0b4e57fddb 100644 --- a/src/xbt/dict_private.h +++ b/src/xbt/dict_private.h @@ -1,5 +1,5 @@ /* dict_elm - elements of generic dictionnaries */ -/* This file is not to be loaded from anywhere but dict.c */ +/* This file is not to be loaded from anywhere but dict.cpp */ /* Copyright (c) 2004-2011, 2013-2014. The SimGrid Team. * All rights reserved. */ @@ -18,6 +18,8 @@ #include "xbt/dict.h" #include "xbt/mallocator.h" +SG_BEGIN_DECL() + #define MAX_FILL_PERCENT 80 typedef struct s_xbt_het_dictelm { @@ -52,4 +54,6 @@ XBT_PRIVATE xbt_dictelm_t xbt_dictelm_new(xbt_dict_t dict, const char *key, int XBT_PRIVATE void xbt_dictelm_free(xbt_dict_t dict, xbt_dictelm_t element); XBT_PRIVATE void xbt_dictelm_set_data(xbt_dict_t dict, xbt_dictelm_t element, void *data, void_f_pvoid_t free_ctn); +SG_END_DECL() + #endif /* _XBT_DICT_PRIVATE_H_ */ diff --git a/src/xbt/dynar.c b/src/xbt/dynar.cpp similarity index 94% rename from src/xbt/dynar.c rename to src/xbt/dynar.cpp index a62e51f323..5205938f47 100644 --- a/src/xbt/dynar.c +++ b/src/xbt/dynar.cpp @@ -122,7 +122,7 @@ void xbt_dynar_free_container(xbt_dynar_t * dynar) * * \param dynar who to squeeze */ -inline void xbt_dynar_reset(xbt_dynar_t const dynar) +void xbt_dynar_reset(xbt_dynar_t const dynar) { _sanity_check_dynar(dynar); @@ -177,7 +177,7 @@ void xbt_dynar_shrink(xbt_dynar_t dynar, int empty_slots_wanted) * * kilkil a dynar and its content */ -inline void xbt_dynar_free(xbt_dynar_t * dynar) +void xbt_dynar_free(xbt_dynar_t * dynar) { if (dynar && *dynar) { xbt_dynar_reset(*dynar); @@ -196,7 +196,7 @@ void xbt_dynar_free_voidp(void *d) * * \param dynar the dynar we want to mesure */ -inline unsigned long xbt_dynar_length(const xbt_dynar_t dynar) +unsigned long xbt_dynar_length(const xbt_dynar_t dynar) { return (dynar ? (unsigned long) dynar->used : (unsigned long) 0); } @@ -206,7 +206,7 @@ inline unsigned long xbt_dynar_length(const xbt_dynar_t dynar) *\param dynar the dynat we want to check */ -inline int xbt_dynar_is_empty(const xbt_dynar_t dynar) +int xbt_dynar_is_empty(const xbt_dynar_t dynar) { return (xbt_dynar_length(dynar) == 0); } @@ -217,7 +217,7 @@ inline int xbt_dynar_is_empty(const xbt_dynar_t dynar) * \param idx index of the slot we want to retrieve * \param[out] dst where to put the result to. */ -inline void xbt_dynar_get_cpy(const xbt_dynar_t dynar, const unsigned long idx, void *const dst) +void xbt_dynar_get_cpy(const xbt_dynar_t dynar, const unsigned long idx, void *const dst) { _sanity_check_dynar(dynar); _check_inbound_idx(dynar, idx); @@ -234,7 +234,7 @@ inline void xbt_dynar_get_cpy(const xbt_dynar_t dynar, const unsigned long idx, * \warning The returned value is the actual content of the dynar. * Make a copy before fooling with it. */ -inline void *xbt_dynar_get_ptr(const xbt_dynar_t dynar, const unsigned long idx) +void *xbt_dynar_get_ptr(const xbt_dynar_t dynar, const unsigned long idx) { void *res; _sanity_check_dynar(dynar); @@ -244,7 +244,7 @@ inline void *xbt_dynar_get_ptr(const xbt_dynar_t dynar, const unsigned long idx) return res; } -inline void *xbt_dynar_set_at_ptr(const xbt_dynar_t dynar, const unsigned long idx) +void *xbt_dynar_set_at_ptr(const xbt_dynar_t dynar, const unsigned long idx) { _sanity_check_dynar(dynar); @@ -266,7 +266,7 @@ inline void *xbt_dynar_set_at_ptr(const xbt_dynar_t dynar, const unsigned long i * * If you want to free the previous content, use xbt_dynar_replace(). */ -inline void xbt_dynar_set(xbt_dynar_t dynar, const int idx, const void *const src) +void xbt_dynar_set(xbt_dynar_t dynar, const int idx, const void *const src) { memcpy(xbt_dynar_set_at_ptr(dynar, idx), src, dynar->elmsize); } @@ -329,7 +329,7 @@ void *xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar, const int idx) * Set the Nth element of a dynar, expanding the dynar if needed, and moving the previously existing value and all * subsequent ones to one position right in the dynar. */ -inline void xbt_dynar_insert_at(xbt_dynar_t const dynar, const int idx, const void *const src) +void xbt_dynar_insert_at(xbt_dynar_t const dynar, const int idx, const void *const src) { /* checks done in xbt_dynar_insert_at_ptr */ memcpy(xbt_dynar_insert_at_ptr(dynar, idx), src, dynar->elmsize); @@ -460,16 +460,13 @@ signed int xbt_dynar_search_or_negative(xbt_dynar_t const dynar, void *const ele */ int xbt_dynar_member(xbt_dynar_t const dynar, void *const elem) { - xbt_ex_t e; - - TRY { + try { xbt_dynar_search(dynar, elem); - } CATCH(e) { - if (e.category == not_found_error) { - xbt_ex_free(e); + } + catch (xbt_ex& e) { + if (e.category == not_found_error) return 0; - } - RETHROW; + throw; } return 1; } @@ -479,13 +476,13 @@ int xbt_dynar_member(xbt_dynar_t const dynar, void *const elem) * You can then use regular affectation to set its value instead of relying on the slow memcpy. This is what * xbt_dynar_push_as() does. */ -inline void *xbt_dynar_push_ptr(xbt_dynar_t const dynar) +void *xbt_dynar_push_ptr(xbt_dynar_t const dynar) { return xbt_dynar_insert_at_ptr(dynar, dynar->used); } /** @brief Add an element at the end of the dynar */ -inline void xbt_dynar_push(xbt_dynar_t const dynar, const void *const src) +void xbt_dynar_push(xbt_dynar_t const dynar, const void *const src) { /* checks done in xbt_dynar_insert_at_ptr */ memcpy(xbt_dynar_insert_at_ptr(dynar, dynar->used), src, dynar->elmsize); @@ -496,7 +493,7 @@ inline void xbt_dynar_push(xbt_dynar_t const dynar, const void *const src) * You can then use regular affectation to set its value instead of relying on the slow memcpy. This is what * xbt_dynar_pop_as() does. */ -inline void *xbt_dynar_pop_ptr(xbt_dynar_t const dynar) +void *xbt_dynar_pop_ptr(xbt_dynar_t const dynar) { _check_populated_dynar(dynar); XBT_CDEBUG(xbt_dyn, "Pop %p", (void *) dynar); @@ -505,7 +502,7 @@ inline void *xbt_dynar_pop_ptr(xbt_dynar_t const dynar) } /** @brief Get and remove the last element of the dynar */ -inline void xbt_dynar_pop(xbt_dynar_t const dynar, void *const dst) +void xbt_dynar_pop(xbt_dynar_t const dynar, void *const dst) { /* sanity checks done by remove_at */ XBT_CDEBUG(xbt_dyn, "Pop %p", (void *) dynar); @@ -516,7 +513,7 @@ inline void xbt_dynar_pop(xbt_dynar_t const dynar, void *const dst) * * This is less efficient than xbt_dynar_push() */ -inline void xbt_dynar_unshift(xbt_dynar_t const dynar, const void *const src) +void xbt_dynar_unshift(xbt_dynar_t const dynar, const void *const src) { /* sanity checks done by insert_at */ xbt_dynar_insert_at(dynar, 0, src); @@ -526,7 +523,7 @@ inline void xbt_dynar_unshift(xbt_dynar_t const dynar, const void *const src) * * This is less efficient than xbt_dynar_pop() */ -inline void xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst) +void xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst) { /* sanity checks done by remove_at */ xbt_dynar_remove_at(dynar, 0, dst); @@ -536,7 +533,7 @@ inline void xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst) * * The mapped function may change the value of the element itself, but should not mess with the structure of the dynar. */ -inline void xbt_dynar_map(const xbt_dynar_t dynar, void_f_pvoid_t const op) +void xbt_dynar_map(const xbt_dynar_t dynar, void_f_pvoid_t const op) { char *const data = (char *) dynar->data; const unsigned long elmsize = dynar->elmsize; @@ -555,7 +552,7 @@ inline void xbt_dynar_map(const xbt_dynar_t dynar, void_f_pvoid_t const op) * * This function can be used while traversing without problem. */ -inline void xbt_dynar_cursor_rm(xbt_dynar_t dynar, unsigned int *const cursor) +void xbt_dynar_cursor_rm(xbt_dynar_t dynar, unsigned int *const cursor) { xbt_dynar_remove_at(dynar, (*cursor)--, NULL); } @@ -586,7 +583,7 @@ inline void xbt_dynar_cursor_rm(xbt_dynar_t dynar, unsigned int *const cursor) * \param dynar the dynar to sort * \param compar_fn comparison function of type (int (compar_fn*) (const void*) (const void*)). */ -inline void xbt_dynar_sort(xbt_dynar_t dynar, int_f_cpvoid_cpvoid_t compar_fn) +void xbt_dynar_sort(xbt_dynar_t dynar, int_f_cpvoid_cpvoid_t compar_fn) { qsort(dynar->data, dynar->used, dynar->elmsize, compar_fn); } @@ -655,7 +652,7 @@ XBT_PUBLIC(void) xbt_dynar_three_way_partition(xbt_dynar_t const dynar, int_f_pv * * Note: The dynar won't be usable afterwards. */ -inline void *xbt_dynar_to_array(xbt_dynar_t dynar) +void *xbt_dynar_to_array(xbt_dynar_t dynar) { void *res; xbt_dynar_shrink(dynar, 1); @@ -748,13 +745,13 @@ XBT_TEST_UNIT("int", test_dynar_int, "Dynars of integers") /* 2. Traverse manually the dynar */ for (cursor = 0; cursor < NB_ELEM; cursor++) { - iptr = xbt_dynar_get_ptr(d, cursor); - xbt_test_assert(cursor == *iptr, "The retrieved value is not the same than the injected one (%u!=%d)", cursor, cpt); + iptr = (int*) xbt_dynar_get_ptr(d, cursor); + xbt_test_assert(cursor == (unsigned int) *iptr, "The retrieved value is not the same than the injected one (%u!=%d)", cursor, cpt); } /* 3. Traverse the dynar using the neat macro to that extend */ xbt_dynar_foreach(d, cursor, cpt) { - xbt_test_assert(cursor == cpt, "The retrieved value is not the same than the injected one (%u!=%d)", cursor, cpt); + xbt_test_assert(cursor == (unsigned int) cpt, "The retrieved value is not the same than the injected one (%u!=%d)", cursor, cpt); } /* end_of_traversal */ @@ -879,7 +876,7 @@ XBT_TEST_UNIT("insert",test_dynar_insert,"Using the xbt_dynar_insert and xbt_dyn /* 3. Traverse the dynar */ xbt_dynar_foreach(d, cursor, cpt) { - xbt_test_assert(cursor == cpt, "The retrieved value is not the same than the injected one (%u!=%d)", cursor, cpt); + xbt_test_assert(cursor == (unsigned int) cpt, "The retrieved value is not the same than the injected one (%u!=%d)", cursor, cpt); } /* end_of_traversal */ @@ -887,7 +884,7 @@ XBT_TEST_UNIT("insert",test_dynar_insert,"Using the xbt_dynar_insert and xbt_dyn for (cpt = 0; cpt < NB_ELEM; cpt++) xbt_dynar_set_as(d, cpt, int, cpt); xbt_dynar_foreach(d, cursor, cpt) - xbt_test_assert(cursor == cpt, "The retrieved value is not the same than the injected one (%u!=%d)", cursor, cpt); + xbt_test_assert(cursor == (unsigned int) cpt, "The retrieved value is not the same than the injected one (%u!=%d)", cursor, cpt); for (cpt = 0; cpt < NB_ELEM; cpt++) { int val; @@ -908,7 +905,7 @@ XBT_TEST_UNIT("insert",test_dynar_insert,"Using the xbt_dynar_insert and xbt_dyn /* 3. Traverse the dynar */ xbt_dynar_foreach(d, cursor, cpt) { - xbt_test_assert(cursor == cpt, "The retrieved value is not the same than the injected one (%u!=%d)", cursor, cpt); + xbt_test_assert(cursor == (unsigned) cpt, "The retrieved value is not the same than the injected one (%u!=%d)", cursor, cpt); } /* end_of_traversal */ diff --git a/src/xbt/ex.c b/src/xbt/ex.cpp similarity index 69% rename from src/xbt/ex.c rename to src/xbt/ex.cpp index e641a18469..821d1acbdf 100644 --- a/src/xbt/ex.c +++ b/src/xbt/ex.cpp @@ -58,48 +58,30 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_ex, xbt, "Exception mechanism"); -XBT_EXPORT_NO_IMPORT(const xbt_running_ctx_t) __xbt_ex_ctx_initializer = XBT_RUNNING_CTX_INITIALIZER; - -/* default __ex_ctx callback function */ -xbt_running_ctx_t *__xbt_ex_ctx_default(void) -{ - /* Don't scream: this is a default which is never used (so, yes, - there is one setjump container by running entity). - - This default gets overriden in xbt/xbt_os_thread.c so that it works in - real life and in simulation when using threads to implement the simulation - processes (ie, with pthreads and on windows). - - It also gets overriden in xbt/context.c when using ucontexts (as well as - in Java for now, but after the java overhaul, it will get cleaned out) - */ - static xbt_running_ctx_t ctx = XBT_RUNNING_CTX_INITIALIZER; - - return &ctx; -} +xbt_ex::~xbt_ex() {} /* Change raw libc symbols to file names and line numbers */ void xbt_ex_setup_backtrace(xbt_ex_t * e); void xbt_backtrace_display(xbt_ex_t * e) { + // TODO, backtrace +#if 0 xbt_ex_setup_backtrace(e); #ifdef HAVE_BACKTRACE - if (e->used == 0) { + if (e->bt_strings.empty()) { fprintf(stderr, "(backtrace not set)\n"); } else { fprintf(stderr, "Backtrace (displayed in process %s):\n", SIMIX_process_self_get_name()); - for (int i = 1; i < e->used; i++) /* no need to display "xbt_backtrace_display" */ - fprintf(stderr, "---> %s\n", e->bt_strings[i] + 4); + for (std::string const& s : e->bt_strings) /* no need to display "xbt_backtrace_display" */ + fprintf(stderr, "---> %s\n", s.c_str() + 4); } - - /* don't fool xbt_ex_free with uninitialized msg field */ - e->msg = NULL; xbt_ex_free(*e); #else XBT_ERROR("No backtrace on this arch"); #endif + #endif } /** \brief show the backtrace of the current point (lovely while debuging) */ @@ -116,6 +98,22 @@ void xbt_backtrace_display_current(void) # include "src/xbt/backtrace_dummy.c" #endif +void xbt_throw( + char* message, xbt_errcat_t errcat, int value, + const char* file, int line, const char* func) +{ + xbt_ex e(message); + free(message); + e.category = errcat; + e.value = value; + e.file = file; + e.line = line; + e.func = func; + e.procname = xbt_procname(); + e.pid = xbt_getpid(); + throw e; +} + /** @brief shows an exception content and the associated stack if available */ void xbt_ex_display(xbt_ex_t * e) { @@ -126,9 +124,9 @@ void xbt_ex_display(xbt_ex_t * e) fprintf(stderr, "** SimGrid: UNCAUGHT EXCEPTION received on %s(%d): category: %s; value: %d\n" "** %s\n" "** Thrown by %s()%s\n", - xbt_binary_name, xbt_getpid(), xbt_ex_catname(e->category), e->value, e->msg, - e->procname, thrower ? thrower : " in this process"); - XBT_CRITICAL("%s", e->msg); + xbt_binary_name, xbt_getpid(), xbt_ex_catname(e->category), e->value, e->what(), + e->procname.c_str(), thrower ? thrower : " in this process"); + XBT_CRITICAL("%s", e->what()); xbt_free(thrower); if (xbt_initialized==0 || smx_cleaned) { @@ -136,22 +134,26 @@ void xbt_ex_display(xbt_ex_t * e) return; /* Not started yet or already closing. Trying to generate a backtrace would probably fail */ } - if (!e->bt_strings) + if (e->bt_strings.empty()) xbt_ex_setup_backtrace(e); #ifdef HAVE_BACKTRACE - if (e->used && e->bt_strings) { + if (!e->bt.empty() && !e->bt_strings.empty()) { /* We have everything to build neat backtraces */ - int i; int cutpath = 0; - TRY { // We don't want to have an exception while checking how to deal with the one we already have, do we? + try { // We don't want to have an exception while checking how to deal with the one we already have, do we? cutpath = xbt_cfg_get_boolean("exception/cutpath"); - } CATCH_ANONYMOUS { } + } + catch(xbt_ex& e) { + // Nothing to do + } fprintf(stderr, "\n"); - for (i = 0; i < e->used; i++) { - + for (std::string const& s : e->bt_strings) { + if (cutpath) { + // TODO, backtrace + /* char* p = e->bt_strings[i]; xbt_str_rtrim(p, ":0123456789"); char* filename = strrchr(p, '/')+1; @@ -166,9 +168,10 @@ void xbt_ex_display(xbt_ex_t * e) fprintf(stderr, "%s %s\n", dest, filename); free(dest); + */ } else { - fprintf(stderr, "%s\n", e->bt_strings[i]); + fprintf(stderr, "%s\n", s.c_str()); } } } else @@ -178,28 +181,6 @@ void xbt_ex_display(xbt_ex_t * e) "** (no backtrace available)\n", e->func, e->file, e->line); } -/* default __ex_terminate callback function */ -void __xbt_ex_terminate_default(xbt_ex_t * e) -{ - xbt_ex_display(e); - xbt_abort(); -} - -/* the externally visible API */ -XBT_EXPORT_NO_IMPORT(xbt_running_ctx_fetcher_t) __xbt_running_ctx_fetch = &__xbt_ex_ctx_default; -XBT_EXPORT_NO_IMPORT(ex_term_cb_t) __xbt_ex_terminate = &__xbt_ex_terminate_default; - -void xbt_ex_free(xbt_ex_t e) -{ - free(e.msg); - - if (e.bt_strings) { - for (int i = 0; i < e.used; i++) - free(e.bt_strings[i]); - free(e.bt_strings); - } -} - /** \brief returns a short name for the given exception category */ const char *xbt_ex_catname(xbt_errcat_t cat) { @@ -245,44 +226,45 @@ XBT_TEST_SUITE("xbt_ex", "Exception Handling"); XBT_TEST_UNIT("controlflow", test_controlflow, "basic nested control flow") { xbt_ex_t ex; - volatile int n = 1; + int n = 1; xbt_test_add("basic nested control flow"); - TRY { + try { if (n != 1) xbt_test_fail("M1: n=%d (!= 1)", n); n++; - TRY { + try { if (n != 2) xbt_test_fail("M2: n=%d (!= 2)", n); n++; THROWF(unknown_error, 0, "something"); - } CATCH(ex) { + } + catch (xbt_ex& ex) { if (n != 3) xbt_test_fail("M3: n=%d (!= 3)", n); n++; - xbt_ex_free(ex); } n++; - TRY { + try { if (n != 5) xbt_test_fail("M2: n=%d (!= 5)", n); n++; THROWF(unknown_error, 0, "something"); - } CATCH_ANONYMOUS { + } + catch(xbt_ex& ex){ if (n != 6) xbt_test_fail("M3: n=%d (!= 6)", n); n++; - RETHROW; + throw; n++; } xbt_test_fail("MX: n=%d (shouldn't reach this point)", n); - } CATCH(ex) { + } + catch(xbt_ex& e) { if (n != 7) xbt_test_fail("M4: n=%d (!= 7)", n); n++; - xbt_ex_free(ex); } if (n != 8) xbt_test_fail("M5: n=%d (!= 8)", n); @@ -290,19 +272,17 @@ XBT_TEST_UNIT("controlflow", test_controlflow, "basic nested control flow") XBT_TEST_UNIT("value", test_value, "exception value passing") { - xbt_ex_t ex; - - TRY { + try { THROWF(unknown_error, 2, "toto"); - } CATCH(ex) { + } + catch (xbt_ex& ex) { xbt_test_add("exception value passing"); if (ex.category != unknown_error) xbt_test_fail("category=%d (!= 1)", (int)ex.category); if (ex.value != 2) xbt_test_fail("value=%d (!= 2)", ex.value); - if (strcmp(ex.msg, "toto")) - xbt_test_fail("message=%s (!= toto)", ex.msg); - xbt_ex_free(ex); + if (strcmp(ex.what(), "toto")) + xbt_test_fail("message=%s (!= toto)", ex.what()); } } @@ -312,14 +292,15 @@ XBT_TEST_UNIT("variables", test_variables, "variable value preservation") int r1; int XBT_ATTRIB_UNUSED r2; int v1; - volatile int v2; + int v2; r1 = r2 = v1 = v2 = 1234; - TRY { + try { r2 = 5678; v2 = 5678; THROWF(unknown_error, 0, "toto"); - } CATCH(ex) { + } + catch(xbt_ex& e) { xbt_test_add("variable preservation"); if (r1 != 1234) xbt_test_fail("r1=%d (!= 1234)", r1); @@ -328,85 +309,32 @@ XBT_TEST_UNIT("variables", test_variables, "variable value preservation") /* r2 is allowed to be destroyed because not volatile */ if (v2 != 5678) xbt_test_fail("v2=%d (!= 5678)", v2); - xbt_ex_free(ex); } } XBT_TEST_UNIT("cleanup", test_cleanup, "cleanup handling") { - xbt_ex_t ex; - volatile int v1; + int v1; int c; xbt_test_add("cleanup handling"); v1 = 1234; c = 0; - TRY { + try { v1 = 5678; THROWF(1, 2, "blah"); } - TRY_CLEANUP { + catch (xbt_ex& ex) { if (v1 != 5678) xbt_test_fail("v1 = %d (!= 5678)", v1); c = 1; - } CATCH(ex) { if (v1 != 5678) xbt_test_fail("v1 = %d (!= 5678)", v1); - if (!(ex.category == 1 && ex.value == 2 && !strcmp(ex.msg, "blah"))) + if (!(ex.category == 1 && ex.value == 2 && !strcmp(ex.what(), "blah"))) xbt_test_fail("unexpected exception contents"); - xbt_ex_free(ex); } if (!c) xbt_test_fail("xbt_ex_free not executed"); } - -/* - * The following is the example included in the documentation. It's a good - * idea to check its syntax even if we don't try to run it. - * And actually, it allows to put comments in the code despite doxygen. - */ -static char *mallocex(int size) -{ - return NULL; -} - -#define SMALLAMOUNT 10 -#define TOOBIG 100000000 - -typedef struct { - char *first; -} global_context_t; - -static void good_example(void) -{ - global_context_t *global_context = xbt_malloc(sizeof(global_context_t)); - - /* GOOD_EXAMPLE */ - { /*01 */ - char *volatile /*03 */ cp1 = NULL /*02 */ ; - char *volatile /*03 */ cp2 = NULL /*02 */ ; - char *volatile /*03 */ cp3 = NULL /*02 */ ; - TRY { - cp1 = mallocex(SMALLAMOUNT); - global_context->first = cp1; - cp1 = NULL /*05 give away */ ; - cp2 = mallocex(TOOBIG); - cp3 = mallocex(SMALLAMOUNT); - strncpy(cp1, "foo",3); - strncpy(cp2, "bar",3); - } - TRY_CLEANUP { /*04 */ - printf("cp3=%s", cp3 == NULL /*02 */ ? "" : cp3); - free(cp3); - free(cp2); - /*05 cp1 was given away */ - } - CATCH_ANONYMOUS { - /*05 global context untouched */ - RETHROW; - } - } - /* end_of_good_example */ -} #endif /* SIMGRID_TEST */ diff --git a/src/xbt/xbt_log_layout_format.c b/src/xbt/xbt_log_layout_format.c index 7e2c46833c..884f6379f0 100644 --- a/src/xbt/xbt_log_layout_format.c +++ b/src/xbt/xbt_log_layout_format.c @@ -157,13 +157,13 @@ static int xbt_log_layout_format_doit(xbt_log_layout_t l, xbt_log_event_t ev, co break; case 'b': /* backtrace; called %throwable in LOG4J */ case 'B': /* short backtrace; called %throwable{short} in LOG4J */ -#if HAVE_BACKTRACE && HAVE_EXECINFO_H && HAVE_POPEN && defined(ADDR2LINE) +// TODO, backtrace +#if 0 && HAVE_BACKTRACE && HAVE_EXECINFO_H && HAVE_POPEN && defined(ADDR2LINE) { - xbt_ex_t e; + xbt_ex_t e(""); e.used = backtrace((void **) e.bt, XBT_BACKTRACE_SIZE); e.bt_strings = NULL; - e.msg = NULL; xbt_ex_setup_backtrace(&e); if (*q == 'B') { show_string(e.bt_strings[1] + 8); @@ -178,7 +178,6 @@ static int xbt_log_layout_format_doit(xbt_log_layout_t l, xbt_log_event_t ev, co show_string(buff->data); xbt_strbuff_free(buff); } - xbt_ex_free(e); } #else show_string("(no backtrace on this arch)"); diff --git a/src/xbt/xbt_os_thread.c b/src/xbt/xbt_os_thread.c index 0c1ae33cd4..60e5e13a93 100644 --- a/src/xbt/xbt_os_thread.c +++ b/src/xbt/xbt_os_thread.c @@ -57,7 +57,6 @@ typedef struct xbt_os_thread_ { char *name; void *param; pvoid_f_pvoid_t start_routine; - xbt_running_ctx_t *running_ctx; void *extra_data; } s_xbt_os_thread_t; static xbt_os_thread_t main_thread = NULL; @@ -75,18 +74,10 @@ static void xbt_os_thread_free_thread_data(xbt_os_thread_t thread) { if (thread == main_thread) /* just killed main thread */ main_thread = NULL; - - free(thread->running_ctx); free(thread->name); free(thread); } -/* callback: context fetching */ -static xbt_running_ctx_t *_os_thread_get_running_ctx(void) -{ - return xbt_os_thread_self()->running_ctx; -} - /* callback: termination */ static void _os_thread_ex_terminate(xbt_ex_t * e) { @@ -109,17 +100,12 @@ void xbt_os_thread_mod_preinit(void) main_thread->name = (char *) "main"; main_thread->param = NULL; main_thread->start_routine = NULL; - main_thread->running_ctx = xbt_new(xbt_running_ctx_t, 1); main_thread->extra_data = NULL; - XBT_RUNNING_CTX_INITIALIZE(main_thread->running_ctx); if ((errcode = pthread_setspecific(xbt_self_thread_key, main_thread))) THROWF(system_error, errcode, "Impossible to set the SimGrid identity descriptor to the main thread (pthread_setspecific failed)"); - __xbt_running_ctx_fetch = _os_thread_get_running_ctx; - __xbt_ex_terminate = _os_thread_ex_terminate; - pthread_attr_init(&thread_attr); thread_mod_inited = 1; @@ -137,17 +123,12 @@ void xbt_os_thread_mod_postexit(void) // if ((errcode=pthread_key_delete(xbt_self_thread_key))) // THROWF(system_error,errcode,"pthread_key_delete failed for xbt_self_thread_key"); - free(main_thread->running_ctx); free(main_thread); main_thread = NULL; thread_mod_inited = 0; #if !HAVE_SEM_INIT xbt_os_mutex_destroy(next_sem_ID_lock); #endif - - /* Restore the default exception setup */ - __xbt_running_ctx_fetch = &__xbt_ex_ctx_default; - __xbt_ex_terminate = &__xbt_ex_terminate_default; } /** Calls pthread_atfork() if present, and raise an exception otherwise. @@ -183,8 +164,6 @@ xbt_os_thread_t xbt_os_thread_create(const char *name, pvoid_f_pvoid_t start_ro res_thread->name = xbt_strdup(name); res_thread->start_routine = start_routine; res_thread->param = param; - res_thread->running_ctx = xbt_new(xbt_running_ctx_t, 1); - XBT_RUNNING_CTX_INITIALIZE(res_thread->running_ctx); res_thread->extra_data = extra_data; int errcode = pthread_create(&(res_thread->t), &thread_attr, wrapper_start_routine, res_thread); diff --git a/src/xbt/xbt_replay.c b/src/xbt/xbt_replay.cpp similarity index 92% rename from src/xbt/xbt_replay.c rename to src/xbt/xbt_replay.cpp index 2aa0ce23dc..d9e475b8de 100644 --- a/src/xbt/xbt_replay.c +++ b/src/xbt/xbt_replay.cpp @@ -81,7 +81,7 @@ const char **xbt_replay_reader_get(xbt_replay_reader_t reader) xbt_dynar_free(&d); return xbt_replay_reader_get(reader); /* Get next line */ } - return xbt_dynar_to_array(d); + return (const char**) xbt_dynar_to_array(d); } void xbt_replay_reader_free(xbt_replay_reader_t *reader) @@ -109,7 +109,7 @@ void xbt_replay_reader_free(xbt_replay_reader_t *reader) void xbt_replay_action_register(const char *action_name, action_fun function) { char* lowername = str_tolower (action_name); - xbt_dict_set(xbt_action_funs, lowername, function, NULL); + xbt_dict_set(xbt_action_funs, lowername, (void*) function, NULL); xbt_free(lowername); } @@ -147,18 +147,17 @@ void _xbt_replay_action_exit(void) int xbt_replay_action_runner(int argc, char *argv[]) { int i; - xbt_ex_t e; if (xbt_action_fp) { // A unique trace file char **evt; while ((evt = action_get_action(argv[0]))) { char* lowername = str_tolower (evt[1]); action_fun function = (action_fun)xbt_dict_get(xbt_action_funs, lowername); xbt_free(lowername); - TRY{ + try { function((const char **)evt); - } CATCH(e) { - free(evt); - xbt_die("Replay error :\n %s", e.msg); + } + catch(xbt_ex& e) { + xbt_die("Replay error :\n %s", e.what()); } for (i=0;evt[i]!= NULL;i++) free(evt[i]); @@ -176,11 +175,11 @@ int xbt_replay_action_runner(int argc, char *argv[]) char* lowername = str_tolower (evt[1]); action_fun function = (action_fun)xbt_dict_get(xbt_action_funs, lowername); xbt_free(lowername); - TRY{ + try { function(evt); - } CATCH(e) { + } catch(xbt_ex& e) { free(evt); - xbt_die("Replay error on line %d of file %s :\n %s" , reader->linenum,reader->filename, e.msg); + xbt_die("Replay error on line %d of file %s :\n %s" , reader->linenum,reader->filename, e.what()); } } else { XBT_WARN("%s:%d: Ignore trace element not for me", reader->filename, reader->linenum); @@ -197,7 +196,7 @@ static char **action_get_action(char *name) xbt_dynar_t evt = NULL; char *evtname = NULL; - xbt_dynar_t myqueue = xbt_dict_get_or_null(xbt_action_queues, name); + xbt_dynar_t myqueue = (xbt_dynar_t) xbt_dict_get_or_null(xbt_action_queues, name); if (myqueue == NULL || xbt_dynar_is_empty(myqueue)) { // nothing stored for me. Read the file further if (xbt_action_fp == NULL) { // File closed now. There's nothing more to read. I'm out of here goto todo_done; @@ -219,11 +218,11 @@ static char **action_get_action(char *name) // if it's for me, I'm done evtname = xbt_dynar_get_as(evt, 0, char *); if (!strcasecmp(name, evtname)) { - return xbt_dynar_to_array(evt); + return (char**) xbt_dynar_to_array(evt); } else { // Else, I have to store it for the relevant colleague xbt_dynar_t otherqueue = - xbt_dict_get_or_null(xbt_action_queues, evtname); + (xbt_dynar_t) xbt_dict_get_or_null(xbt_action_queues, evtname); if (otherqueue == NULL) { // Damn. Create the queue of that guy otherqueue = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp); xbt_dict_set(xbt_action_queues, evtname, otherqueue, NULL); @@ -235,7 +234,7 @@ static char **action_get_action(char *name) } else { // Get something from my queue and return it xbt_dynar_shift(myqueue, &evt); - return xbt_dynar_to_array(evt); + return (char**) xbt_dynar_to_array(evt); } // I did all my actions for me in the file (either I closed the file, or a colleague did) diff --git a/src/xbt/xbt_str.c b/src/xbt/xbt_str.cpp similarity index 96% rename from src/xbt/xbt_str.c rename to src/xbt/xbt_str.cpp index 551aa9916c..9f817dc977 100644 --- a/src/xbt/xbt_str.c +++ b/src/xbt/xbt_str.cpp @@ -1,4 +1,4 @@ -/* xbt_str.c - various helping functions to deal with strings */ +/* xbt_str.cpp - various helping functions to deal with strings */ /* Copyright (c) 2007-2014. The SimGrid Team. * All rights reserved. */ @@ -213,7 +213,7 @@ xbt_dynar_t xbt_str_split(const char *s, const char *sep) if (*q == '\0') done = 1; - topush = xbt_malloc(q - p + 1); + topush = (char*) xbt_malloc(q - p + 1); memcpy(topush, p, q - p); topush[q - p] = '\0'; xbt_dynar_push(res, &topush); @@ -253,14 +253,14 @@ xbt_dynar_t xbt_str_split_str(const char *s, const char *sep) //if substring was not found add the entire string if (NULL == q) { v = strlen(p); - to_push = xbt_malloc(v + 1); + to_push = (char*) xbt_malloc(v + 1); memcpy(to_push, p, v); to_push[v] = '\0'; xbt_dynar_push(res, &to_push); done = 1; } else { //get the appearance - to_push = xbt_malloc(q - p + 1); + to_push = (char*) xbt_malloc(q - p + 1); memcpy(to_push, p, q - p); //add string terminator to_push[q - p] = '\0'; @@ -412,7 +412,7 @@ char *xbt_str_join(xbt_dynar_t dyn, const char *sep) } len += strlen(sep) * dyn_len; /* Do the job */ - res = xbt_malloc(len); + res = (char*) xbt_malloc(len); p = res; xbt_dynar_foreach(dyn, cpt, cursor) { if ((int) cpt < dyn_len - 1) @@ -446,7 +446,7 @@ char *xbt_str_join_array(const char *const *strs, const char *sep) len += strlen(sep) * amount_strings; /* Do the job */ - q = res = xbt_malloc(len); + q = res = (char*) xbt_malloc(len); for (i=0;strs[i];i++) { if (i!=0) { // not first loop q += snprintf(q,len, "%s%s", sep, strs[i]); @@ -570,23 +570,21 @@ XBT_TEST_UNIT("xbt_str_split_str", test_split_str, "test the function xbt_str_sp #define test_parse_error(function, name, variable, str) \ do { \ xbt_test_add(name); \ - TRY { \ + try { \ variable = function(str, "Parse error"); \ xbt_test_fail("The test '%s' did not detect the problem",name ); \ - } CATCH(e) { \ + } catch(xbt_ex& e) { \ if (e.category != arg_error) { \ xbt_test_exception(e); \ - } else { \ - xbt_ex_free(e); \ } \ } \ } while (0) #define test_parse_ok(function, name, variable, str, value) \ do { \ xbt_test_add(name); \ - TRY { \ + try { \ variable = function(str, "Parse error"); \ - } CATCH(e) { \ + } catch(xbt_ex& e) { \ xbt_test_exception(e); \ } \ xbt_test_assert(variable == value, "Fail to parse '%s'", str); \ @@ -594,7 +592,6 @@ XBT_TEST_UNIT("xbt_str_split_str", test_split_str, "test the function xbt_str_sp XBT_TEST_UNIT("xbt_str_parse", test_parse, "Test the parsing functions") { - xbt_ex_t e; int rint = -9999; test_parse_ok(xbt_str_parse_int, "Parse int", rint, "42", 42); test_parse_ok(xbt_str_parse_int, "Parse 0 as an int", rint, "0", 0); diff --git a/teshsuite/msg/CMakeLists.txt b/teshsuite/msg/CMakeLists.txt index e8a2d3f103..989b8bd93c 100644 --- a/teshsuite/msg/CMakeLists.txt +++ b/teshsuite/msg/CMakeLists.txt @@ -1,5 +1,5 @@ -foreach(x concurrent_rw get_sender host_on_off host_on_off_processes host_on_off_recv host_on_off_wait listen_async pid - process process_join storage_client_server task_destroy_cancel trace_integration) +foreach(x concurrent_rw get_sender host_on_off host_on_off_recv host_on_off_processes host_on_off_wait listen_async pid + process process_join storage_client_server trace_integration) add_executable (${x} ${x}/${x}.c) target_link_libraries(${x} simgrid) set_target_properties(${x} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${x}) @@ -8,6 +8,15 @@ foreach(x concurrent_rw get_sender host_on_off host_on_off_processes host_on_off set(teshsuite_src ${teshsuite_src} ${CMAKE_CURRENT_SOURCE_DIR}/${x}/${x}.c) endforeach() +foreach(x task_destroy_cancel) + add_executable (${x} ${x}/${x}.cpp) + target_link_libraries(${x} simgrid) + set_target_properties(${x} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${x}) + + set(tesh_files ${tesh_files} ${CMAKE_CURRENT_SOURCE_DIR}/${x}/${x}.tesh) + set(teshsuite_src ${teshsuite_src} ${CMAKE_CURRENT_SOURCE_DIR}/${x}/${x}.cpp) +endforeach() + set(teshsuite_src ${teshsuite_src} PARENT_SCOPE) set(tesh_files ${tesh_files} PARENT_SCOPE) set(xml_files ${xml_files} ${CMAKE_CURRENT_SOURCE_DIR}/trace_integration/test-hbp1.0-hbp1.0-hbp1.0.xml diff --git a/teshsuite/msg/host_on_off_recv/host_on_off_recv.c b/teshsuite/msg/host_on_off_recv/host_on_off_recv.c index 92e4ec083a..4d27e38e5d 100644 --- a/teshsuite/msg/host_on_off_recv/host_on_off_recv.c +++ b/teshsuite/msg/host_on_off_recv/host_on_off_recv.c @@ -12,57 +12,43 @@ static const char* mailbox = "comm"; static int master(int argc, char *argv[]) { - xbt_ex_t e; - TRY { - msg_host_t jupiter = MSG_host_by_name("Jupiter"); - - XBT_INFO("Master starting"); - MSG_process_sleep(0.5); - - msg_comm_t comm = NULL; - { - msg_task_t task = MSG_task_create("COMM", 0, 100000000, NULL); - comm = MSG_task_isend(task, mailbox); - } - - MSG_process_sleep(0.5); - - XBT_INFO("Turning off the slave host"); - MSG_host_off(jupiter); - - if (comm) { - MSG_comm_wait(comm, -1); - MSG_comm_destroy(comm); - } - XBT_INFO("Master has finished"); + msg_host_t jupiter = MSG_host_by_name("Jupiter"); + + XBT_INFO("Master starting"); + MSG_process_sleep(0.5); + + msg_comm_t comm = NULL; + { + msg_task_t task = MSG_task_create("COMM", 0, 100000000, NULL); + comm = MSG_task_isend(task, mailbox); } - CATCH(e) { - xbt_die("Exception caught in the master"); - return 1; + + MSG_process_sleep(0.5); + + XBT_INFO("Turning off the slave host"); + MSG_host_off(jupiter); + + if (comm) { + MSG_comm_wait(comm, -1); + MSG_comm_destroy(comm); } + XBT_INFO("Master has finished"); + return 0; } static int slave(int argc, char *argv[]) { - xbt_ex_t e; - TRY { - XBT_INFO("Slave receiving"); - msg_task_t task = NULL; - msg_error_t error = MSG_task_receive(&(task), mailbox); - if (error) { - XBT_ERROR("Error while receiving message"); - return 1; - } - - XBT_ERROR("Slave should be off already."); - return 1; - } - CATCH(e) { - XBT_ERROR("Exception caught in the slave"); + XBT_INFO("Slave receiving"); + msg_task_t task = NULL; + msg_error_t error = MSG_task_receive(&(task), mailbox); + if (error) { + XBT_ERROR("Error while receiving message"); return 1; } - return 0; + + XBT_ERROR("Slave should be off already."); + return 1; } int main(int argc, char *argv[]) diff --git a/teshsuite/msg/host_on_off_wait/host_on_off_wait.c b/teshsuite/msg/host_on_off_wait/host_on_off_wait.c index 3b84c4d35b..c32ac97dd2 100644 --- a/teshsuite/msg/host_on_off_wait/host_on_off_wait.c +++ b/teshsuite/msg/host_on_off_wait/host_on_off_wait.c @@ -10,38 +10,24 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example") static int master(int argc, char *argv[]) { - xbt_ex_t e; - TRY { - msg_host_t jupiter = MSG_host_by_name("Jupiter"); - XBT_INFO("Master waiting"); - MSG_process_sleep(1); + msg_host_t jupiter = MSG_host_by_name("Jupiter"); + XBT_INFO("Master waiting"); + MSG_process_sleep(1); + + XBT_INFO("Turning off the slave host"); + MSG_host_off(jupiter); + XBT_INFO("Master has finished"); - XBT_INFO("Turning off the slave host"); - MSG_host_off(jupiter); - XBT_INFO("Master has finished"); - } - CATCH(e) { - xbt_die("Exception caught in the master"); - return 1; - } return 0; } static int slave(int argc, char *argv[]) { - xbt_ex_t e; - TRY { - XBT_INFO("Slave waiting"); - // TODO, This should really be MSG_HOST_FAILURE - MSG_process_sleep(5); - XBT_ERROR("Slave should be off already."); - return 1; - } - CATCH(e) { - XBT_ERROR("Exception caught in the slave"); - return 1; - } - return 0; + XBT_INFO("Slave waiting"); + // TODO, This should really be MSG_HOST_FAILURE + MSG_process_sleep(5); + XBT_ERROR("Slave should be off already."); + return 1; } int main(int argc, char *argv[]) diff --git a/teshsuite/msg/task_destroy_cancel/task_destroy_cancel.c b/teshsuite/msg/task_destroy_cancel/task_destroy_cancel.cpp similarity index 95% rename from teshsuite/msg/task_destroy_cancel/task_destroy_cancel.c rename to teshsuite/msg/task_destroy_cancel/task_destroy_cancel.cpp index 929ee1a7a1..e06412a148 100644 --- a/teshsuite/msg/task_destroy_cancel/task_destroy_cancel.c +++ b/teshsuite/msg/task_destroy_cancel/task_destroy_cancel.cpp @@ -15,7 +15,6 @@ static int master(int argc, char *argv[]) double timeout = 1; const char * mailbox = "jupi"; - xbt_ex_t ex; msg_task_t task = MSG_task_create("normal", task_comp_size, task_comm_size, NULL); XBT_INFO("Sending task: \"%s\"", task->name); @@ -34,11 +33,10 @@ static int master(int argc, char *argv[]) msg_comm_t comm = MSG_task_isend(task, mailbox); XBT_INFO("Canceling task \"%s\" during comm", task->name); MSG_task_cancel(task); - TRY { + try { MSG_comm_wait(comm, -1); } - CATCH (ex) { - xbt_ex_free(ex); + catch (xbt_ex& ex) {; MSG_comm_destroy(comm); } MSG_task_destroy(task); @@ -47,11 +45,10 @@ static int master(int argc, char *argv[]) comm = MSG_task_isend(task, mailbox); XBT_INFO("Destroying task \"%s\" during comm", task->name); MSG_task_destroy(task); - TRY { + try { MSG_comm_wait(comm, -1); } - CATCH (ex) { - xbt_ex_free(ex); + catch (xbt_ex& ex) {; MSG_comm_destroy(comm); } @@ -67,7 +64,7 @@ static int master(int argc, char *argv[]) static int worker_main(int argc, char *argv[]) { - msg_task_t task = MSG_process_get_data(MSG_process_self()); + msg_task_t task = (msg_task_t) MSG_process_get_data(MSG_process_self()); msg_error_t res; XBT_INFO("Start %s", task->name); res = MSG_task_execute(task); diff --git a/teshsuite/simdag/flatifier/flatifier.cpp b/teshsuite/simdag/flatifier/flatifier.cpp index bd48f32602..bf94475198 100644 --- a/teshsuite/simdag/flatifier/flatifier.cpp +++ b/teshsuite/simdag/flatifier/flatifier.cpp @@ -40,14 +40,13 @@ static int parse_cmdline(int *timings, char **platformFile, int argc, char **arg static void create_environment(xbt_os_timer_t parse_time, const char *platformFile) { - xbt_ex_t e; - TRY { + try { xbt_os_cputimer_start(parse_time); SD_create_environment(platformFile); xbt_os_cputimer_stop(parse_time); } - CATCH(e) { - xbt_die("Error while loading %s: %s", platformFile, e.msg); + catch (std::exception& e) { + xbt_die("Error while loading %s: %s", platformFile, e.what()); } } diff --git a/teshsuite/smpi/mpich3-test/perf/allredtrace.c b/teshsuite/smpi/mpich3-test/perf/allredtrace.c index 8d7265a1b0..070271be19 100644 --- a/teshsuite/smpi/mpich3-test/perf/allredtrace.c +++ b/teshsuite/smpi/mpich3-test/perf/allredtrace.c @@ -143,7 +143,7 @@ void SetupDelay( double usec ) /* Should coordinate with the other processes - take the max? */ } -volatile double delayCounter = 0; +double delayCounter = 0; void Delay( int count ) { int i; diff --git a/teshsuite/xbt/CMakeLists.txt b/teshsuite/xbt/CMakeLists.txt index dd6887985e..3af1d9d5b7 100644 --- a/teshsuite/xbt/CMakeLists.txt +++ b/teshsuite/xbt/CMakeLists.txt @@ -8,7 +8,7 @@ foreach(x heap_bench log_large log_usage mallocator parallel_log_crashtest parma endforeach() if(HAVE_MMALLOC) - add_executable (mmalloc_test ${CMAKE_CURRENT_SOURCE_DIR}/mmalloc/mmalloc_test.c) + add_executable (mmalloc_test ${CMAKE_CURRENT_SOURCE_DIR}/mmalloc/mmalloc_test.cpp) target_link_libraries(mmalloc_test simgrid) set_target_properties(mmalloc_test PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/mmalloc) endif() @@ -16,7 +16,7 @@ endif() set(tesh_files ${tesh_files} ${CMAKE_CURRENT_SOURCE_DIR}/log_usage/log_usage_ndebug.tesh ${CMAKE_CURRENT_SOURCE_DIR}/mmalloc/mmalloc_64.tesh ${CMAKE_CURRENT_SOURCE_DIR}/mmalloc/mmalloc_32.tesh PARENT_SCOPE) -set(teshsuite_src ${teshsuite_src} ${CMAKE_CURRENT_SOURCE_DIR}/mmalloc/mmalloc_test.c PARENT_SCOPE) +set(teshsuite_src ${teshsuite_src} ${CMAKE_CURRENT_SOURCE_DIR}/mmalloc/mmalloc_test.cpp PARENT_SCOPE) foreach(x heap_bench log_large parallel_log_crashtest parmap_test) #mallocator parmap_bench ADD_TESH(tesh-xbt-${x} --setenv bindir=${CMAKE_BINARY_DIR}/teshsuite/xbt/${x} --cd ${CMAKE_HOME_DIRECTORY}/teshsuite/xbt/${x} ${x}.tesh) diff --git a/teshsuite/xbt/mmalloc/mmalloc_test.c b/teshsuite/xbt/mmalloc/mmalloc_test.cpp similarity index 90% rename from teshsuite/xbt/mmalloc/mmalloc_test.c rename to teshsuite/xbt/mmalloc/mmalloc_test.cpp index 638b776592..c5706b6fd5 100644 --- a/teshsuite/xbt/mmalloc/mmalloc_test.c +++ b/teshsuite/xbt/mmalloc/mmalloc_test.cpp @@ -22,7 +22,7 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(test,"this test"); int main(int argc, char**argv) { - void *heapA; + xbt_mheap_t heapA = nullptr; void *pointers[TESTSIZE]; xbt_init(&argc,argv); @@ -46,7 +46,7 @@ int main(int argc, char**argv) } XBT_INFO("All blocks were correctly allocated. Free every second block"); for (i = 0; i < TESTSIZE; i+=2) { - mfree(heapA,pointers[i]); + mfree(heapA, pointers[i]); } XBT_INFO("Memset every second block to zero (yeah, they are not currently allocated :)"); for (i = 0; i < TESTSIZE; i+=2) { @@ -61,15 +61,12 @@ int main(int argc, char**argv) XBT_INFO("free all blocks (each one twice, to check that double free are correctly catched)"); for (i = 0; i < TESTSIZE; i++) { - xbt_ex_t e; - int gotit = 1; - + bool gotit = false; mfree(heapA, pointers[i]); - TRY { + try { mfree(heapA, pointers[i]); - gotit = 0; - } CATCH(e) { - xbt_ex_free(e); + } catch(xbt_ex& e) { + gotit = true; } if (!gotit) xbt_die("FAIL: A double-free went undetected (for size:%d)",size_of_block(i)); @@ -77,14 +74,11 @@ int main(int argc, char**argv) XBT_INFO("free again all blocks (to really check that double free are correctly catched)"); for (i = 0; i < TESTSIZE; i++) { - xbt_ex_t e; - int gotit = 1; - - TRY { + bool gotit = false; + try { mfree(heapA, pointers[i]); - gotit = 0; - } CATCH(e) { - xbt_ex_free(e); + } catch(xbt_ex& e) { + gotit = true; } if (!gotit) xbt_die("FAIL: A double-free went undetected (for size:%d)",size_of_block(i)); diff --git a/tools/cmake/DefinePackages.cmake b/tools/cmake/DefinePackages.cmake index 1e67328249..3bb436dff1 100644 --- a/tools/cmake/DefinePackages.cmake +++ b/tools/cmake/DefinePackages.cmake @@ -215,7 +215,7 @@ set(SMPI_SRC src/smpi/colls/reduce-rab.c src/smpi/colls/scatter-ompi.c src/smpi/colls/scatter-mvapich-two-level.c - src/smpi/colls/smpi_automatic_selector.c + src/smpi/colls/smpi_automatic_selector.cpp src/smpi/colls/smpi_mpich_selector.c src/smpi/colls/smpi_intel_mpi_selector.c src/smpi/colls/smpi_openmpi_selector.c @@ -245,12 +245,12 @@ set(XBT_SRC src/xbt/automaton/automaton.c src/xbt/automaton/automatonparse_promela.c src/xbt/config.cpp - src/xbt/cunit.c - src/xbt/dict.c + src/xbt/cunit.cpp + src/xbt/dict.cpp src/xbt/dict_cursor.c src/xbt/dict_elm.c - src/xbt/dynar.c - src/xbt/ex.c + src/xbt/dynar.cpp + src/xbt/ex.cpp src/xbt/fifo.c src/xbt/graph.c src/xbt/heap.c @@ -271,8 +271,8 @@ set(XBT_SRC src/xbt/xbt_os_file.c src/xbt/xbt_os_synchro.c src/xbt/xbt_os_time.c - src/xbt/xbt_replay.c - src/xbt/xbt_str.c + src/xbt/xbt_replay.cpp + src/xbt/xbt_str.cpp src/xbt/xbt_strbuff.c src/xbt/xbt_virtu.c src/xbt_modinter.h diff --git a/tools/cmake/GCCFlags.cmake b/tools/cmake/GCCFlags.cmake index 9724561e7d..4eddcecfb7 100644 --- a/tools/cmake/GCCFlags.cmake +++ b/tools/cmake/GCCFlags.cmake @@ -112,8 +112,8 @@ if(enable_model-checking AND enable_compile_optimizations) src/xbt/mmalloc/mm.c src/xbt/log.c src/xbt/xbt_log_appender_file.c src/xbt/xbt_log_layout_format.c src/xbt/xbt_log_layout_simple.c - src/xbt/dict.c src/xbt/dict_elm.c src/xbt/dict_cursor.c - src/xbt/dynar.c src/xbt/fifo.c src/xbt/heap.c src/xbt/swag.c + src/xbt/dict.cpp src/xbt/dict_elm.c src/xbt/dict_cursor.c + src/xbt/dynar.cpp src/xbt/fifo.c src/xbt/heap.c src/xbt/swag.c src/xbt/str.c src/xbt/strbuff.c src/xbt/snprintf.c src/xbt/queue.c src/xbt/xbt_os_time.c src/xbt/xbt_os_thread.c diff --git a/tools/cmake/UnitTesting.cmake b/tools/cmake/UnitTesting.cmake index 0ec9bd0a89..41ed4544ec 100644 --- a/tools/cmake/UnitTesting.cmake +++ b/tools/cmake/UnitTesting.cmake @@ -5,12 +5,12 @@ # FILES_CONTAINING_UNITTESTS and have a pleasant day. set(FILES_CONTAINING_UNITTESTS - src/xbt/cunit.c - src/xbt/ex.c - src/xbt/dynar.c - src/xbt/dict.c + src/xbt/cunit.cpp + src/xbt/ex.cpp + src/xbt/dynar.cpp + src/xbt/dict.cpp src/xbt/swag.c - src/xbt/xbt_str.c + src/xbt/xbt_str.cpp src/xbt/xbt_strbuff.c src/xbt/config.cpp ) diff --git a/tools/cmake/test_prog/prog_stacksetup.c b/tools/cmake/test_prog/prog_stacksetup.c index 542847d17d..a9c4797c0b 100644 --- a/tools/cmake/test_prog/prog_stacksetup.c +++ b/tools/cmake/test_prog/prog_stacksetup.c @@ -19,7 +19,7 @@ union alltypes { void (*fp) (void); char *cp; }; -static volatile char *handler_addr = (char *) 0xDEAD; +static char *handler_addr = (char *) 0xDEAD; static ucontext_t uc_handler; static ucontext_t uc_main; void handler(void)