1
1
openmpi/opal/util/malloc.c
Brian Barrett e55f99d23a * rename ompi_if to opal_if
* rename ompi_malloc to opal_malloc
* rename ompi_numtostr to opal_numtostr
* start of rename of ompi_environ to opal_environ

This commit was SVN r6332.
2005-07-04 01:36:20 +00:00

186 строки
4.0 KiB
C

/*
* 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 (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$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "ompi_config.h"
#include <stdlib.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;
/*
* Private variables
*/
static opal_output_stream_t malloc_stream = {
/* debugging */
true,
/* verbose level */
5,
/* syslog */
false, 0, NULL,
/* prefix */
"malloc_debug: ",
/* stdout */
false,
/* stderr */
true,
/* file */
false, false, NULL
};
/*
* Initialize the malloc debug interface
*/
void opal_malloc_init(void)
{
opal_malloc_output = opal_output_open(&malloc_stream);
}
/*
* Finalize the malloc debug interface
*/
void opal_malloc_finalize(void)
{
if (-1 != opal_malloc_output) {
opal_output_close(opal_malloc_output);
opal_malloc_output = -1;
}
}
/*
* Debug version of malloc
*/
void *opal_malloc(size_t size, const char *file, int line)
{
void *addr;
if (opal_malloc_debug_level > 1) {
if (size <= 0) {
opal_output(opal_malloc_output, "Request for %ld bytes (%s, %d)",
(long) size, file, line);
}
}
addr = malloc(size);
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);
}
}
return addr;
}
/*
* Debug version of calloc
*/
void *opal_calloc(size_t nmembers, size_t size, const char *file, int line)
{
void *addr;
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);
}
}
addr = calloc(nmembers, size);
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);
}
}
return addr;
}
/*
* Debug version of realloc
*/
void *opal_realloc(void *ptr, size_t size, const char *file, int line)
{
void *addr;
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);
}
}
}
addr = realloc(ptr, size);
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);
}
}
return addr;
}
/*
* Debug version of free
*/
void opal_free(void *addr, const char *file, int line)
{
if (opal_malloc_debug_level > 1 && NULL == addr) {
opal_output(opal_malloc_output, "Invalid free (%s, %d)", file, line);
} else {
free(addr);
}
}