This source file includes following definitions.
- opal_malloc_finalize
- opal_malloc_init
- opal_malloc_init
- opal_malloc
- opal_calloc
- opal_realloc
- opal_free
- opal_malloc_debug
   1 
   2 
   3 
   4 
   5 
   6 
   7 
   8 
   9 
  10 
  11 
  12 
  13 
  14 
  15 
  16 
  17 
  18 
  19 
  20 
  21 
  22 #include "opal_config.h"
  23 
  24 #include <stdlib.h>
  25 
  26 #include "opal/util/malloc.h"
  27 #include "opal/util/output.h"
  28 #include "opal/runtime/opal.h"
  29 
  30 
  31 
  32 
  33 
  34 
  35 #if defined(malloc)
  36 #undef malloc
  37 #endif
  38 #if defined(calloc)
  39 #undef calloc
  40 #endif
  41 #if defined(free)
  42 #undef free
  43 #endif
  44 #if defined(realloc)
  45 #undef realloc
  46 #endif
  47 
  48 
  49 
  50 
  51 int opal_malloc_debug_level = OPAL_MALLOC_DEBUG_LEVEL;
  52 int opal_malloc_output = -1;
  53 
  54 
  55 
  56 
  57 
  58 static opal_output_stream_t malloc_stream;
  59 
  60 
  61 #if OPAL_ENABLE_DEBUG
  62 
  63 
  64 
  65 static void opal_malloc_finalize(void)
  66 {
  67     if (-1 != opal_malloc_output) {
  68         opal_output_close(opal_malloc_output);
  69         opal_malloc_output = -1;
  70         OBJ_DESTRUCT(&malloc_stream);
  71     }
  72 }
  73 
  74 
  75 
  76 
  77 void opal_malloc_init(void)
  78 {
  79     OBJ_CONSTRUCT(&malloc_stream, opal_output_stream_t);
  80     malloc_stream.lds_is_debugging = true;
  81     malloc_stream.lds_verbose_level = 5;
  82     malloc_stream.lds_prefix = "malloc debug: ";
  83     malloc_stream.lds_want_stderr = true;
  84     opal_malloc_output = opal_output_open(&malloc_stream);
  85     opal_finalize_register_cleanup (opal_malloc_finalize);
  86 }
  87 #else
  88 void opal_malloc_init (void)
  89 {
  90 }
  91 #endif  
  92 
  93 
  94 
  95 
  96 void *opal_malloc(size_t size, const char *file, int line)
  97 {
  98     void *addr;
  99 #if OPAL_ENABLE_DEBUG
 100     if (opal_malloc_debug_level > 1) {
 101         if (size <= 0) {
 102             opal_output(opal_malloc_output, "Request for %ld bytes (%s, %d)",
 103                         (long) size, file, line);
 104         }
 105     }
 106 #endif 
 107 
 108     addr = malloc(size);
 109 
 110 #if OPAL_ENABLE_DEBUG
 111     if (opal_malloc_debug_level > 0) {
 112         if (NULL == addr) {
 113             opal_output(opal_malloc_output,
 114                         "Request for %ld bytes failed (%s, %d)",
 115                         (long) size, file, line);
 116         }
 117     }
 118 #endif  
 119     return addr;
 120 }
 121 
 122 
 123 
 124 
 125 
 126 void *opal_calloc(size_t nmembers, size_t size, const char *file, int line)
 127 {
 128     void *addr;
 129 #if OPAL_ENABLE_DEBUG
 130     if (opal_malloc_debug_level > 1) {
 131         if (size <= 0) {
 132             opal_output(opal_malloc_output,
 133                         "Request for %ld zeroed elements of size %ld (%s, %d)",
 134                         (long) nmembers, (long) size, file, line);
 135         }
 136     }
 137 #endif  
 138     addr = calloc(nmembers, size);
 139 #if OPAL_ENABLE_DEBUG
 140     if (opal_malloc_debug_level > 0) {
 141         if (NULL == addr) {
 142             opal_output(opal_malloc_output,
 143                         "Request for %ld zeroed elements of size %ld failed (%s, %d)",
 144                         (long) nmembers, (long) size, file, line);
 145         }
 146     }
 147 #endif  
 148     return addr;
 149 }
 150 
 151 
 152 
 153 
 154 
 155 void *opal_realloc(void *ptr, size_t size, const char *file, int line)
 156 {
 157     void *addr;
 158 #if OPAL_ENABLE_DEBUG
 159     if (opal_malloc_debug_level > 1) {
 160         if (size <= 0) {
 161             if (NULL == ptr) {
 162                 opal_output(opal_malloc_output,
 163                             "Realloc NULL for %ld bytes (%s, %d)",
 164                             (long) size, file, line);
 165             } else {
 166                 opal_output(opal_malloc_output, "Realloc %p for %ld bytes (%s, %d)",
 167                             ptr, (long) size, file, line);
 168             }
 169         }
 170     }
 171 #endif  
 172     addr = realloc(ptr, size);
 173 #if OPAL_ENABLE_DEBUG
 174     if (opal_malloc_debug_level > 0) {
 175         if (NULL == addr) {
 176             opal_output(opal_malloc_output,
 177                         "Realloc %p for %ld bytes failed (%s, %d)",
 178                         ptr, (long) size, file, line);
 179         }
 180     }
 181 #endif  
 182     return addr;
 183 }
 184 
 185 
 186 
 187 
 188 
 189 void opal_free(void *addr, const char *file, int line)
 190 {
 191     free(addr);
 192 }
 193 
 194 void opal_malloc_debug(int level)
 195 {
 196 #if OPAL_ENABLE_DEBUG
 197     opal_malloc_debug_level = level;
 198 #endif  
 199 }