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