Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New cmake option "enable_mallocators", useful to disable mallocators.
[simgrid.git] / src / xbt / mallocator.c
index ed415a5..e6753e1 100644 (file)
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_mallocator, xbt, "Mallocators");
 
-
-/* Change to 0 to completely disable mallocators. */
-#define MALLOCATOR_IS_WANTED 1
-
 /* Mallocators and memory mess introduced by model-checking do not mix well
  * together: the mallocator will give standard memory when we are using raw
  * memory (so these blocks are killed on restore) and the contrary (so these
@@ -53,7 +49,8 @@ xbt_mallocator_t xbt_mallocator_new(int size,
   xbt_assert(new_f != NULL && free_f != NULL, "invalid parameter");
 
   m = xbt_new0(s_xbt_mallocator_t, 1);
-  XBT_VERB("Create mallocator %p", m);
+  XBT_VERB("Create mallocator %p (%s)",
+           m, MALLOCATOR_IS_ENABLED ? "enabled" : "disabled");
   m->current_size = 0;
   m->new_f = new_f;
   m->free_f = free_f;
@@ -62,13 +59,12 @@ xbt_mallocator_t xbt_mallocator_new(int size,
   if (MALLOCATOR_IS_ENABLED) {
     m->objects = xbt_new0(void *, size);
     m->max_size = size;
+    m->mutex = xbt_os_mutex_init();
   } else {
-    if (!MALLOCATOR_IS_WANTED) /* Warn to avoid to commit debugging settings */
-      XBT_WARN("Mallocator is disabled!");
     m->objects = NULL;
     m->max_size = 0;
+    m->mutex = NULL;
   }
-
   return m;
 }
 
@@ -92,6 +88,7 @@ void xbt_mallocator_free(xbt_mallocator_t m)
     m->free_f(m->objects[i]);
   }
   xbt_free(m->objects);
+  xbt_os_mutex_destroy(m->mutex);
   xbt_free(m);
 }
 
@@ -116,6 +113,7 @@ void *xbt_mallocator_get(xbt_mallocator_t m)
   void *object;
 
   if (MALLOCATOR_IS_ENABLED) {
+    xbt_os_mutex_acquire(m->mutex);
     if (m->current_size <= 0) {
       /* No object is ready yet. Create a bunch of them to try to group the
        * mallocs on the same memory pages (to help the cache lines) */
@@ -133,6 +131,7 @@ void *xbt_mallocator_get(xbt_mallocator_t m)
     /* XBT_DEBUG("Reuse an old object for mallocator %p (size:%d/%d)", */
     /*           m, m->current_size, m->max_size); */
     object = m->objects[--m->current_size];
+    xbt_os_mutex_release(m->mutex);
   } else {
     object = m->new_f();
   }
@@ -157,16 +156,23 @@ void *xbt_mallocator_get(xbt_mallocator_t m)
  */
 void xbt_mallocator_release(xbt_mallocator_t m, void *object)
 {
-  if (m->current_size < m->max_size) {
-    /* there is enough place to push the object */
-    /* XBT_DEBUG
-        ("Store deleted object in mallocator %p for further use (size:%d/%d)",
+  if (MALLOCATOR_IS_ENABLED) {
+    xbt_os_mutex_acquire(m->mutex);
+    if (m->current_size < m->max_size) {
+      /* there is enough place to push the object */
+      /* XBT_DEBUG
+         ("Store deleted object in mallocator %p for further use (size:%d/%d)",
          m, m->current_size, m->max_size); */
-    m->objects[m->current_size++] = object;
+      m->objects[m->current_size++] = object;
+      xbt_os_mutex_release(m->mutex);
+    } else {
+      xbt_os_mutex_release(m->mutex);
+      /* otherwise we don't have a choice, we must free the object */
+      /* XBT_DEBUG("Free deleted object: mallocator %p is full (size:%d/%d)", m,
+         m->current_size, m->max_size); */
+      m->free_f(object);
+    }
   } else {
-    /* otherwise we don't have a choice, we must free the object */
-    /* XBT_DEBUG("Free deleted object: mallocator %p is full (size:%d/%d)", m,
-           m->current_size, m->max_size); */
     m->free_f(object);
   }
 }