2004-01-22 03:29:32 +03:00
|
|
|
/*
|
2005-11-05 22:57:48 +03:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
|
|
|
|
* University Research and Technology
|
|
|
|
* Corporation. All rights reserved.
|
2008-03-18 06:00:08 +03:00
|
|
|
* Copyright (c) 2004-2008 The University of Tennessee and The University
|
2005-11-05 22:57:48 +03:00
|
|
|
* of Tennessee Research Foundation. All rights
|
|
|
|
* reserved.
|
2015-06-24 06:59:57 +03:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
2004-11-28 23:09:25 +03:00
|
|
|
* University of Stuttgart. All rights reserved.
|
2005-03-24 15:43:37 +03:00
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
2004-11-22 04:38:40 +03:00
|
|
|
* $COPYRIGHT$
|
2015-06-24 06:59:57 +03:00
|
|
|
*
|
2004-11-22 04:38:40 +03:00
|
|
|
* Additional copyrights may follow
|
2015-06-24 06:59:57 +03:00
|
|
|
*
|
2004-01-22 03:29:32 +03:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal_config.h"
|
2004-01-22 03:29:32 +03:00
|
|
|
|
2004-02-10 03:09:36 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2005-07-04 05:36:20 +04:00
|
|
|
#include "opal/util/malloc.h"
|
2005-07-04 03:31:27 +04:00
|
|
|
#include "opal/util/output.h"
|
2004-01-22 03:29:32 +03:00
|
|
|
|
|
|
|
|
2004-02-10 03:09:36 +03:00
|
|
|
/*
|
|
|
|
* Undefine "malloc" and "free"
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(malloc)
|
|
|
|
#undef malloc
|
|
|
|
#endif
|
2004-03-27 17:53:42 +03:00
|
|
|
#if defined(calloc)
|
|
|
|
#undef calloc
|
|
|
|
#endif
|
2004-02-10 03:09:36 +03:00
|
|
|
#if defined(free)
|
|
|
|
#undef free
|
|
|
|
#endif
|
2004-02-10 23:55:27 +03:00
|
|
|
#if defined(realloc)
|
|
|
|
#undef realloc
|
|
|
|
#endif
|
2004-02-10 03:09:36 +03:00
|
|
|
|
2004-01-22 03:29:32 +03:00
|
|
|
/*
|
|
|
|
* Public variables
|
|
|
|
*/
|
2005-07-04 05:36:20 +04:00
|
|
|
int opal_malloc_debug_level = OPAL_MALLOC_DEBUG_LEVEL;
|
|
|
|
int opal_malloc_output = -1;
|
2004-01-22 03:29:32 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Private variables
|
|
|
|
*/
|
2005-09-09 14:46:54 +04:00
|
|
|
static opal_output_stream_t malloc_stream;
|
|
|
|
|
2004-01-22 03:29:32 +03:00
|
|
|
|
2004-02-10 03:09:36 +03:00
|
|
|
/*
|
|
|
|
* Initialize the malloc debug interface
|
2004-01-22 03:29:32 +03:00
|
|
|
*/
|
2005-07-04 05:36:20 +04:00
|
|
|
void opal_malloc_init(void)
|
2004-01-22 03:29:32 +03:00
|
|
|
{
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2005-09-09 14:46:54 +04:00
|
|
|
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);
|
2009-05-07 00:11:28 +04:00
|
|
|
#endif /* OPAL_ENABLE_DEBUG */
|
2004-01-22 03:29:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-10 03:09:36 +03:00
|
|
|
/*
|
|
|
|
* Finalize the malloc debug interface
|
2004-01-22 03:29:32 +03:00
|
|
|
*/
|
2005-07-04 05:36:20 +04:00
|
|
|
void opal_malloc_finalize(void)
|
2004-01-22 03:29:32 +03:00
|
|
|
{
|
2008-03-18 06:00:08 +03:00
|
|
|
if (-1 != opal_malloc_output) {
|
|
|
|
opal_output_close(opal_malloc_output);
|
|
|
|
opal_malloc_output = -1;
|
|
|
|
OBJ_DESTRUCT(&malloc_stream);
|
|
|
|
}
|
2004-01-22 03:29:32 +03:00
|
|
|
}
|
|
|
|
|
2004-02-10 21:58:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Debug version of malloc
|
2004-02-10 03:09:36 +03:00
|
|
|
*/
|
2005-07-04 05:36:20 +04:00
|
|
|
void *opal_malloc(size_t size, const char *file, int line)
|
2004-02-10 03:09:36 +03:00
|
|
|
{
|
|
|
|
void *addr;
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2005-07-04 05:36:20 +04:00
|
|
|
if (opal_malloc_debug_level > 1) {
|
2004-02-10 03:09:36 +03:00
|
|
|
if (size <= 0) {
|
2015-06-24 06:59:57 +03:00
|
|
|
opal_output(opal_malloc_output, "Request for %ld bytes (%s, %d)",
|
2008-03-18 06:00:08 +03:00
|
|
|
(long) size, file, line);
|
2004-02-10 03:09:36 +03:00
|
|
|
}
|
|
|
|
}
|
2009-05-07 00:11:28 +04:00
|
|
|
#endif /* OPAL_ENABLE_DEBUG */
|
2008-03-18 06:00:08 +03:00
|
|
|
|
2004-02-10 03:09:36 +03:00
|
|
|
addr = malloc(size);
|
2008-03-18 06:00:08 +03:00
|
|
|
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2005-07-04 05:36:20 +04:00
|
|
|
if (opal_malloc_debug_level > 0) {
|
2004-02-10 03:09:36 +03:00
|
|
|
if (NULL == addr) {
|
2015-06-24 06:59:57 +03:00
|
|
|
opal_output(opal_malloc_output,
|
2008-03-18 06:00:08 +03:00
|
|
|
"Request for %ld bytes failed (%s, %d)",
|
|
|
|
(long) size, file, line);
|
2004-02-10 03:09:36 +03:00
|
|
|
}
|
|
|
|
}
|
2009-05-07 00:11:28 +04:00
|
|
|
#endif /* OPAL_ENABLE_DEBUG */
|
2004-02-10 03:09:36 +03:00
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-27 17:53:42 +03:00
|
|
|
/*
|
|
|
|
* Debug version of calloc
|
|
|
|
*/
|
2005-07-04 05:36:20 +04:00
|
|
|
void *opal_calloc(size_t nmembers, size_t size, const char *file, int line)
|
2004-03-27 17:53:42 +03:00
|
|
|
{
|
|
|
|
void *addr;
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2005-07-04 05:36:20 +04:00
|
|
|
if (opal_malloc_debug_level > 1) {
|
2004-03-27 17:53:42 +03:00
|
|
|
if (size <= 0) {
|
2008-03-18 06:00:08 +03:00
|
|
|
opal_output(opal_malloc_output,
|
2015-06-24 06:59:57 +03:00
|
|
|
"Request for %ld zeroed elements of size %ld (%s, %d)",
|
2008-03-18 06:00:08 +03:00
|
|
|
(long) nmembers, (long) size, file, line);
|
2004-03-27 17:53:42 +03:00
|
|
|
}
|
|
|
|
}
|
2009-05-07 00:11:28 +04:00
|
|
|
#endif /* OPAL_ENABLE_DEBUG */
|
2004-03-27 17:53:42 +03:00
|
|
|
addr = calloc(nmembers, size);
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2005-07-04 05:36:20 +04:00
|
|
|
if (opal_malloc_debug_level > 0) {
|
2004-03-27 17:53:42 +03:00
|
|
|
if (NULL == addr) {
|
2015-06-24 06:59:57 +03:00
|
|
|
opal_output(opal_malloc_output,
|
2008-03-18 06:00:08 +03:00
|
|
|
"Request for %ld zeroed elements of size %ld failed (%s, %d)",
|
|
|
|
(long) nmembers, (long) size, file, line);
|
2004-03-27 17:53:42 +03:00
|
|
|
}
|
|
|
|
}
|
2009-05-07 00:11:28 +04:00
|
|
|
#endif /* OPAL_ENABLE_DEBUG */
|
2004-03-27 17:53:42 +03:00
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-10 21:58:55 +03:00
|
|
|
/*
|
|
|
|
* Debug version of realloc
|
|
|
|
*/
|
2005-07-04 05:36:20 +04:00
|
|
|
void *opal_realloc(void *ptr, size_t size, const char *file, int line)
|
2004-02-10 21:58:55 +03:00
|
|
|
{
|
|
|
|
void *addr;
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2005-07-04 05:36:20 +04:00
|
|
|
if (opal_malloc_debug_level > 1) {
|
2004-02-10 21:58:55 +03:00
|
|
|
if (size <= 0) {
|
2008-03-18 06:00:08 +03:00
|
|
|
if (NULL == ptr) {
|
2015-06-24 06:59:57 +03:00
|
|
|
opal_output(opal_malloc_output,
|
|
|
|
"Realloc NULL for %ld bytes (%s, %d)",
|
2008-03-18 06:00:08 +03:00
|
|
|
(long) size, file, line);
|
|
|
|
} else {
|
2015-06-24 06:59:57 +03:00
|
|
|
opal_output(opal_malloc_output, "Realloc %p for %ld bytes (%s, %d)",
|
2008-03-18 06:00:08 +03:00
|
|
|
ptr, (long) size, file, line);
|
|
|
|
}
|
2004-02-10 21:58:55 +03:00
|
|
|
}
|
|
|
|
}
|
2009-05-07 00:11:28 +04:00
|
|
|
#endif /* OPAL_ENABLE_DEBUG */
|
2004-02-10 21:58:55 +03:00
|
|
|
addr = realloc(ptr, size);
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2005-07-04 05:36:20 +04:00
|
|
|
if (opal_malloc_debug_level > 0) {
|
2004-02-10 21:58:55 +03:00
|
|
|
if (NULL == addr) {
|
2015-06-24 06:59:57 +03:00
|
|
|
opal_output(opal_malloc_output,
|
2008-03-18 06:00:08 +03:00
|
|
|
"Realloc %p for %ld bytes failed (%s, %d)",
|
|
|
|
ptr, (long) size, file, line);
|
2004-02-10 21:58:55 +03:00
|
|
|
}
|
|
|
|
}
|
2009-05-07 00:11:28 +04:00
|
|
|
#endif /* OPAL_ENABLE_DEBUG */
|
2004-02-10 21:58:55 +03:00
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Debug version of free
|
2004-02-10 03:09:36 +03:00
|
|
|
*/
|
2005-07-04 05:36:20 +04:00
|
|
|
void opal_free(void *addr, const char *file, int line)
|
2004-02-10 03:09:36 +03:00
|
|
|
{
|
2008-03-18 06:00:08 +03:00
|
|
|
free(addr);
|
2004-02-10 03:09:36 +03:00
|
|
|
}
|
|
|
|
|
2008-03-18 06:00:08 +03:00
|
|
|
void opal_malloc_debug(int level)
|
|
|
|
{
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2008-03-18 06:00:08 +03:00
|
|
|
opal_malloc_debug_level = level;
|
2009-05-07 00:11:28 +04:00
|
|
|
#endif /* OPAL_ENABLE_DEBUG */
|
2008-03-18 06:00:08 +03:00
|
|
|
}
|