Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Initialize the dict module on need, w/o relying on lib constructors
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Mon, 20 Feb 2023 08:51:18 +0000 (09:51 +0100)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Tue, 21 Feb 2023 14:16:28 +0000 (15:16 +0100)
src/xbt/dict.cpp
src/xbt/xbt_main.cpp
src/xbt/xbt_modinter.h

index a155e19..614d6b7 100644 (file)
@@ -23,6 +23,23 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict, xbt, "Dictionaries provide the same fu
 
 constexpr int MAX_FILL_PERCENT = 80;
 
+/** Handle the dict mallocators init/fini cycle. */
+static void xbt_dict_postexit()
+{
+  if (dict_elm_mallocator != nullptr) {
+    xbt_mallocator_free(dict_elm_mallocator);
+    dict_elm_mallocator = nullptr;
+  }
+}
+static void xbt_dict_preinit()
+{
+  if (dict_elm_mallocator == nullptr) {
+    dict_elm_mallocator =
+        xbt_mallocator_new(256, dict_elm_mallocator_new_f, dict_elm_mallocator_free_f, dict_elm_mallocator_reset_f);
+    atexit(xbt_dict_postexit);
+  }
+}
+
 /**
  * @brief Constructor
  * @param free_ctn function to call with (@a data as argument) when @a data is removed from the dictionary
@@ -34,7 +51,7 @@ constexpr int MAX_FILL_PERCENT = 80;
  */
 xbt_dict_t xbt_dict_new_homogeneous(void_f_pvoid_t free_ctn)
 {
-  xbt_dict_preinit();
+  xbt_dict_preinit(); // Make sure that the module is intialized
 
   xbt_dict_t dict;
 
@@ -304,27 +321,3 @@ int xbt_dict_is_empty(const_xbt_dict_t dict)
 {
   return not dict || (xbt_dict_length(dict) == 0);
 }
-
-/**
- * Create the dict mallocators.
- * This is an internal XBT function called during the lib initialization.
- * It can be used several times to recreate the mallocator, for example when you switch to MC mode
- */
-void xbt_dict_preinit()
-{
-  if (dict_elm_mallocator == nullptr)
-    dict_elm_mallocator = xbt_mallocator_new(256, dict_elm_mallocator_new_f, dict_elm_mallocator_free_f,
-      dict_elm_mallocator_reset_f);
-}
-
-/**
- * Destroy the dict mallocators.
- * This is an internal XBT function during the lib initialization
- */
-void xbt_dict_postexit()
-{
-  if (dict_elm_mallocator != nullptr) {
-    xbt_mallocator_free(dict_elm_mallocator);
-    dict_elm_mallocator = nullptr;
-  }
-}
index ee40cfc..a985d2f 100644 (file)
@@ -64,7 +64,6 @@ XBT_ATTRIB_NOINLINE void sthread_disable()
 
 static void xbt_preinit()
 {
-  xbt_dict_preinit();
   atexit(xbt_postexit);
 }
 
@@ -73,7 +72,6 @@ static void xbt_postexit()
   if (not cfg_dbg_clean_atexit)
     return;
   xbt_initialized--;
-  xbt_dict_postexit();
   xbt_log_postexit();
 }
 
index 6eee62f..bd3625e 100644 (file)
@@ -15,9 +15,6 @@ SG_BEGIN_DECL
 
 void xbt_log_postexit(void);
 
-void xbt_dict_preinit(void);
-void xbt_dict_postexit(void);
-
 extern int xbt_initialized;
 
 SG_END_DECL