From 887351bd8cedd85d0667c5c07198af7b0f6f1708 Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <schaller@strw.leidenuniv.nl>
Date: Wed, 27 Nov 2019 12:20:09 +0000
Subject: [PATCH] Only allocate/free the core map  if we are going to print the
 CPU mask.

---
 src/engine.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/src/engine.c b/src/engine.c
index 5bbc071165..5655e4612d 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -3573,16 +3573,18 @@ void engine_config(int restart, int fof, struct engine *e,
   if (nr_cores > CPU_SETSIZE) /* Unlikely, except on e.g. SGI UV. */
     error("must allocate dynamic cpu_set_t (too many cores per node)");
 
-  char *buf = (char *)malloc((nr_cores + 1) * sizeof(char));
-  buf[nr_cores] = '\0';
-  for (int j = 0; j < nr_cores; ++j) {
-    /* Reversed bit order from convention, but same as e.g. Intel MPI's
-     * I_MPI_PIN_DOMAIN explicit mask: left-to-right, LSB-to-MSB. */
-    buf[j] = CPU_ISSET(j, entry_affinity) ? '1' : '0';
+  if (verbose && with_aff) {
+    char *buf = (char *)malloc((nr_cores + 1) * sizeof(char));
+    buf[nr_cores] = '\0';
+    for (int j = 0; j < nr_cores; ++j) {
+      /* Reversed bit order from convention, but same as e.g. Intel MPI's
+       * I_MPI_PIN_DOMAIN explicit mask: left-to-right, LSB-to-MSB. */
+      buf[j] = CPU_ISSET(j, entry_affinity) ? '1' : '0';
+    }
+    message("Affinity at entry: %s", buf);
+    free(buf);
   }
 
-  if (verbose && with_aff) message("Affinity at entry: %s", buf);
-
   int *cpuid = NULL;
   cpu_set_t cpuset;
 
@@ -4176,7 +4178,6 @@ void engine_config(int restart, int fof, struct engine *e,
   if (with_aff) {
     free(cpuid);
   }
-  free(buf);
 #endif
 
   /* Wait for the runner threads to be in place. */
-- 
GitLab