Browse Source

Check directly in macros if file descriptors are initialized before using them

Ioannis Koutras 12 years ago
parent
commit
97655dc9a9
3 changed files with 37 additions and 21 deletions
  1. 34 6
      private-include/trace.h
  2. 1 7
      src/malloc.c
  3. 2 8
      src/statistics.c

+ 34 - 6
private-include/trace.h

@@ -26,21 +26,33 @@
 #define _DMMLIB_TRACE_H_
 #include <dmmlib/config.h>
 
+#ifdef PARSE_ENV
+#include "dmmlib/dmmlib.h" /* to check if systemallocator is initialized */
+#endif /* PARSE_ENV */
+
 #ifdef WITH_MEM_TRACE
 #include <stdio.h>
 
 #ifndef PARSE_ENV
 /** Memory trace file descriptor */
 #define MEM_FD stderr
+
+/** Function for memory trace messages */
+#define MEM_TRACE(...) fprintf(MEM_FD, __VA_ARGS__)
+
 #else /* PARSE_ENV */
 /** Memory trace file descriptor macro */
 #define MEM_FD mem_fd
 /** Memory trace file descriptor */
 FILE* mem_fd;
-#endif /* PARSE_ENV */
 
 /** Function for memory trace messages */
-#define MEM_TRACE(...) fprintf(MEM_FD, __VA_ARGS__)
+#define MEM_TRACE(...) \
+    if(__builtin_expect (systemallocator.initialized, true)) { \
+        fprintf(MEM_FD, __VA_ARGS__); \
+    }
+
+#endif /* PARSE_ENV */
 
 #else /* WITH_MEM_TRACE */
 
@@ -55,15 +67,23 @@ FILE* mem_fd;
 #ifndef PARSE_ENV
 /** Statistics file descriptor */
 #define STATS_FD stderr
+
+/** Function for statistics trace messages */
+#define STATS_TRACE(...) fprintf(STATS_FD, __VA_ARGS__)
+
 #else /* PARSE_ENV */
 /** Statistics file descriptor macro */
 #define STATS_FD stats_fd
 /** Statistics file descriptor */
 FILE* stats_fd;
-#endif /* PARSE_ENV */
 
 /** Function for statistics trace messages */
-#define STATS_TRACE(...) fprintf(STATS_FD, __VA_ARGS__)
+#define STATS_TRACE(...) \
+    if(__builtin_expect (systemallocator.initialized, true)) { \
+        fprintf(STATS_FD, __VA_ARGS__); \
+    }
+
+#endif /* PARSE_ENV */
 
 #else /* WITH_STATS_TRACE */
 
@@ -78,15 +98,23 @@ FILE* stats_fd;
 #ifndef PARSE_ENV
 /** Debug file descriptor */
 #define DBG_FD stderr
+
+/** Function for debug trace messages */
+#define DBG_TRACE(...) fprintf(DBG_FD, __VA_ARGS__)
+
 #else /* PARSE_ENV */
 /** Debug file descriptor macro */
 #define DBG_FD dbg_fd
 /** Debug file descriptor */
 FILE* dbg_fd;
-#endif /* PARSE_ENV */
 
 /** Function for debug trace messages */
-#define DBG_TRACE(...) fprintf(DBG_FD, __VA_ARGS__)
+#define DBG_TRACE(...) \
+    if(__builtin_expect (systemallocator.initialized, true)) { \
+        fprintf(DBG_FD, __VA_ARGS__); \
+    }
+
+#endif /* PARSE_ENV */
 
 #else /* WITH_DEBUG */
 

+ 1 - 7
src/malloc.c

@@ -143,13 +143,7 @@ void * malloc(size_t size) {
         }
     }
 
-#if defined PARSE_ENV && defined WITH_MEM_TRACE
-    if(__builtin_expect (systemallocator.initialized, true)) {
-#endif /* PARSE_ENV && WITH_MEM_TRACE */
-        MEM_TRACE("dmmlib - m %p %zu\n", ptr, size);
-#if defined PARSE_ENV && defined WITH_MEM_TRACE
-    }
-#endif /* PARSE_ENV && WITH_MEM_TRACE */
+    MEM_TRACE("dmmlib - m %p %zu\n", ptr, size);
     
     return ptr;
 }

+ 2 - 8
src/statistics.c

@@ -62,16 +62,10 @@ void update_stats
         dmm_stats->num_free++;
     }
 
-#if defined PARSE_ENV && defined WITH_STATS_TRACE
-    if(__builtin_expect (systemallocator.initialized, true)) {
-#endif /* PARSE_ENV && WITH_STATS_TRACE */
-        STATS_TRACE("dmmlib - ms all %zu\n", dmm_stats->total_mem_allocated);
+    STATS_TRACE("dmmlib - ms all %zu\n", dmm_stats->total_mem_allocated);
 #ifdef REQUEST_SIZE_INFO
-        STATS_TRACE("dmmlib - ms req %zu\n", dmm_stats->total_mem_requested);
+    STATS_TRACE("dmmlib - ms req %zu\n", dmm_stats->total_mem_requested);
 #endif /* REQUEST_SIZE_INFO */
-#if defined PARSE_ENV && defined WITH_STATS_TRACE
-    }
-#endif /* PARSE_ENV && WITH_STATS_TRACE */
 
 }