2004-01-22 03:29:32 +03:00
|
|
|
/*
|
2004-11-22 04:38:40 +03:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University.
|
|
|
|
* All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The Trustees of the University of Tennessee.
|
|
|
|
* All rights reserved.
|
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-01-22 03:29:32 +03:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
#include "ompi_config.h"
|
2004-01-22 03:29:32 +03:00
|
|
|
|
2004-02-10 03:09:36 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2004-06-15 23:07:45 +04:00
|
|
|
#include "util/malloc.h"
|
2004-03-17 21:45:16 +03:00
|
|
|
#include "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
|
|
|
|
*/
|
2004-06-07 19:33:53 +04:00
|
|
|
int ompi_malloc_debug_level = OMPI_MALLOC_DEBUG_LEVEL;
|
|
|
|
int ompi_malloc_output = -1;
|
2004-01-22 03:29:32 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Private variables
|
|
|
|
*/
|
2004-06-07 19:33:53 +04:00
|
|
|
static ompi_output_stream_t malloc_stream = {
|
2004-01-22 03:29:32 +03:00
|
|
|
/* debugging */
|
|
|
|
true,
|
|
|
|
/* verbose level */
|
|
|
|
5,
|
|
|
|
/* syslog */
|
|
|
|
false, 0, NULL,
|
|
|
|
/* prefix */
|
|
|
|
"malloc_debug: ",
|
|
|
|
/* stdout */
|
|
|
|
false,
|
|
|
|
/* stderr */
|
|
|
|
true,
|
|
|
|
/* file */
|
|
|
|
false, false, NULL
|
|
|
|
};
|
|
|
|
|
2004-02-10 03:09:36 +03:00
|
|
|
/*
|
|
|
|
* Initialize the malloc debug interface
|
2004-01-22 03:29:32 +03:00
|
|
|
*/
|
2004-06-07 19:33:53 +04:00
|
|
|
void ompi_malloc_init(void)
|
2004-01-22 03:29:32 +03:00
|
|
|
{
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_malloc_output = ompi_output_open(&malloc_stream);
|
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
|
|
|
*/
|
2004-06-07 19:33:53 +04:00
|
|
|
void ompi_malloc_finalize(void)
|
2004-01-22 03:29:32 +03:00
|
|
|
{
|
2004-06-07 19:33:53 +04:00
|
|
|
if (-1 != ompi_malloc_output) {
|
|
|
|
ompi_output_close(ompi_malloc_output);
|
|
|
|
ompi_malloc_output = -1;
|
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
|
|
|
*/
|
2004-06-07 19:33:53 +04:00
|
|
|
void *ompi_malloc(size_t size, char *file, int line)
|
2004-02-10 03:09:36 +03:00
|
|
|
{
|
|
|
|
void *addr;
|
2004-06-07 19:33:53 +04:00
|
|
|
if (ompi_malloc_debug_level > 1) {
|
2004-02-10 03:09:36 +03:00
|
|
|
if (size <= 0) {
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_output(ompi_malloc_output, "Request for %ld bytes (%s, %d)",
|
2004-02-10 03:09:36 +03:00
|
|
|
(long) size, file, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addr = malloc(size);
|
2004-06-07 19:33:53 +04:00
|
|
|
if (ompi_malloc_debug_level > 0) {
|
2004-02-10 03:09:36 +03:00
|
|
|
if (NULL == addr) {
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_output(ompi_malloc_output,
|
2004-02-10 03:09:36 +03:00
|
|
|
"Request for %ld bytes failed (%s, %d)",
|
|
|
|
(long) size, file, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-27 17:53:42 +03:00
|
|
|
/*
|
|
|
|
* Debug version of calloc
|
|
|
|
*/
|
2004-06-07 19:33:53 +04:00
|
|
|
void *ompi_calloc(size_t nmembers, size_t size, char *file, int line)
|
2004-03-27 17:53:42 +03:00
|
|
|
{
|
|
|
|
void *addr;
|
2004-06-07 19:33:53 +04:00
|
|
|
if (ompi_malloc_debug_level > 1) {
|
2004-03-27 17:53:42 +03:00
|
|
|
if (size <= 0) {
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_output(ompi_malloc_output,
|
2004-03-27 17:53:42 +03:00
|
|
|
"Request for %ld zeroed elements of size %ld (%s, %d)",
|
|
|
|
(long) nmembers, (long) size, file, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addr = calloc(nmembers, size);
|
2004-06-07 19:33:53 +04:00
|
|
|
if (ompi_malloc_debug_level > 0) {
|
2004-03-27 17:53:42 +03:00
|
|
|
if (NULL == addr) {
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_output(ompi_malloc_output,
|
2004-03-27 17:53:42 +03:00
|
|
|
"Request for %ld zeroed elements of size %ld failed (%s, %d)",
|
|
|
|
(long) nmembers, (long) size, file, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-10 21:58:55 +03:00
|
|
|
/*
|
|
|
|
* Debug version of realloc
|
|
|
|
*/
|
2004-06-07 19:33:53 +04:00
|
|
|
void *ompi_realloc(void *ptr, size_t size, char *file, int line)
|
2004-02-10 21:58:55 +03:00
|
|
|
{
|
|
|
|
void *addr;
|
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
if (ompi_malloc_debug_level > 1) {
|
2004-02-10 21:58:55 +03:00
|
|
|
if (size <= 0) {
|
|
|
|
if (NULL == ptr) {
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_output(ompi_malloc_output,
|
2004-02-10 21:58:55 +03:00
|
|
|
"Realloc NULL for %ld bytes (%s, %d)",
|
|
|
|
(long) size, file, line);
|
|
|
|
} else {
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_output(ompi_malloc_output, "Realloc %p for %ld bytes (%s, %d)",
|
2004-02-10 21:58:55 +03:00
|
|
|
ptr, (long) size, file, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addr = realloc(ptr, size);
|
2004-06-07 19:33:53 +04:00
|
|
|
if (ompi_malloc_debug_level > 0) {
|
2004-02-10 21:58:55 +03:00
|
|
|
if (NULL == addr) {
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_output(ompi_malloc_output,
|
2004-02-10 21:58:55 +03:00
|
|
|
"Realloc %p for %ld bytes failed (%s, %d)",
|
|
|
|
ptr, (long) size, file, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Debug version of free
|
2004-02-10 03:09:36 +03:00
|
|
|
*/
|
2004-06-07 19:33:53 +04:00
|
|
|
void ompi_free(void *addr, char *file, int line)
|
2004-02-10 03:09:36 +03:00
|
|
|
{
|
2004-06-07 19:33:53 +04:00
|
|
|
if (ompi_malloc_debug_level > 1 && NULL == addr) {
|
|
|
|
ompi_output(ompi_malloc_output, "Invalid free (%s, %d)", file, line);
|
2004-02-10 03:09:36 +03:00
|
|
|
} else {
|
|
|
|
free(addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-10 21:58:55 +03:00
|
|
|
|