Merge release-5-0 into master
[alexxy/gromacs.git] / src / gromacs / utility / smalloc.c
index 3ec2536083b9f48cce715c1360ec5476921821f7..6b45a99a82be08246483cfa8eb7035a6d6cfbf4a 100644 (file)
  * To help us fund GROMACS development, we humbly ask that you cite
  * the research papers on the package. Check out http://www.gromacs.org.
  */
+#include "gmxpre.h"
+
 #include "gromacs/utility/smalloc.h"
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
+#include "config.h"
 
 #include <errno.h>
 #include <stdio.h>
 #include <dmalloc.h>
 #endif
 
-#include "gromacs/legacyheaders/gmx_fatal.h"
+#include "thread_mpi/threads.h"
 
+#include "gromacs/utility/fatalerror.h"
 #ifdef PRINT_ALLOC_KB
-#include "gromacs/legacyheaders/network.h"
+#include "gromacs/utility/basenetwork.h"
 #include "gromacs/utility/gmxmpi.h"
 #endif
 
-#ifdef DEBUG
-#include "thread_mpi/threads.h"
+static gmx_bool            g_bOverAllocDD     = FALSE;
+static tMPI_Thread_mutex_t g_over_alloc_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
 
+#ifdef DEBUG
 static void log_action(int bMal, const char *what, const char *file, int line,
                        int nelem, int size, void *ptr)
 {
@@ -287,7 +289,6 @@ void *save_malloc_aligned(const char *name, const char *file, int line,
         }
 #endif
 
-        allocate_fail = FALSE; /* stop compiler warnings */
 #ifdef HAVE_POSIX_MEMALIGN
         allocate_fail = (0 != posix_memalign(&malloced, alignment, nelem*elsize));
 #elif defined HAVE_MEMALIGN
@@ -356,3 +357,24 @@ void save_free_aligned(const char *name, const char *file, int line, void *ptr)
 #endif
     }
 }
+
+void set_over_alloc_dd(gmx_bool set)
+{
+    tMPI_Thread_mutex_lock(&g_over_alloc_mutex);
+    /* we just make sure that we don't set this at the same time.
+       We don't worry too much about reading this rarely-set variable */
+    g_bOverAllocDD = set;
+    tMPI_Thread_mutex_unlock(&g_over_alloc_mutex);
+}
+
+int over_alloc_dd(int n)
+{
+    if (g_bOverAllocDD)
+    {
+        return OVER_ALLOC_FAC*n + 100;
+    }
+    else
+    {
+        return n;
+    }
+}