root/opal/util/malloc.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. opal_malloc_finalize
  2. opal_malloc_init
  3. opal_malloc_init
  4. opal_malloc
  5. opal_calloc
  6. opal_realloc
  7. opal_free
  8. opal_malloc_debug

   1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
   2 /*
   3  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
   4  *                         University Research and Technology
   5  *                         Corporation.  All rights reserved.
   6  * Copyright (c) 2004-2008 The University of Tennessee and The University
   7  *                         of Tennessee Research Foundation.  All rights
   8  *                         reserved.
   9  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
  10  *                         University of Stuttgart.  All rights reserved.
  11  * Copyright (c) 2004-2005 The Regents of the University of California.
  12  *                         All rights reserved.
  13  * Copyright (c) 2018      Triad National Security, LLC. All rights
  14  *                         reserved.
  15  * $COPYRIGHT$
  16  *
  17  * Additional copyrights may follow
  18  *
  19  * $HEADER$
  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  * Undefine "malloc" and "free"
  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  * Public variables
  50  */
  51 int opal_malloc_debug_level = OPAL_MALLOC_DEBUG_LEVEL;
  52 int opal_malloc_output = -1;
  53 
  54 
  55 /*
  56  * Private variables
  57  */
  58 static opal_output_stream_t malloc_stream;
  59 
  60 
  61 #if OPAL_ENABLE_DEBUG
  62 /*
  63  * Finalize the malloc debug interface
  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  * Initialize the malloc debug interface
  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  /* OPAL_ENABLE_DEBUG */
  92 
  93 /*
  94  * Debug version of malloc
  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 /* OPAL_ENABLE_DEBUG */
 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  /* OPAL_ENABLE_DEBUG */
 119     return addr;
 120 }
 121 
 122 
 123 /*
 124  * Debug version of calloc
 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  /* OPAL_ENABLE_DEBUG */
 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  /* OPAL_ENABLE_DEBUG */
 148     return addr;
 149 }
 150 
 151 
 152 /*
 153  * Debug version of realloc
 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  /* OPAL_ENABLE_DEBUG */
 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  /* OPAL_ENABLE_DEBUG */
 182     return addr;
 183 }
 184 
 185 
 186 /*
 187  * Debug version of free
 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  /* OPAL_ENABLE_DEBUG */
 199 }

/* [<][>][^][v][top][bottom][index][help] */