2004-01-22 03:29:32 +03:00
|
|
|
/*
|
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lam_config.h"
|
|
|
|
|
2004-02-10 03:09:36 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2004-03-17 21:45:16 +03:00
|
|
|
#include "mem/malloc.h"
|
|
|
|
#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
|
|
|
|
#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
|
|
|
|
*/
|
|
|
|
int lam_malloc_debug_level = LAM_MALLOC_DEBUG_LEVEL;
|
|
|
|
int lam_malloc_output = -1;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Private variables
|
|
|
|
*/
|
|
|
|
static lam_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
|
|
|
|
};
|
|
|
|
|
2004-02-10 03:09:36 +03:00
|
|
|
/*
|
|
|
|
* Initialize the malloc debug interface
|
2004-01-22 03:29:32 +03:00
|
|
|
*/
|
|
|
|
void lam_malloc_init(void)
|
|
|
|
{
|
|
|
|
lam_malloc_output = lam_output_open(&malloc_stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-10 03:09:36 +03:00
|
|
|
/*
|
|
|
|
* Finalize the malloc debug interface
|
2004-01-22 03:29:32 +03:00
|
|
|
*/
|
|
|
|
void lam_malloc_finalize(void)
|
|
|
|
{
|
|
|
|
if (-1 != lam_malloc_output) {
|
|
|
|
lam_output_close(lam_malloc_output);
|
|
|
|
lam_malloc_output = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-10 21:58:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Debug version of malloc
|
2004-02-10 03:09:36 +03:00
|
|
|
*/
|
|
|
|
void *lam_malloc(size_t size, char *file, int line)
|
|
|
|
{
|
|
|
|
void *addr;
|
|
|
|
if (lam_malloc_debug_level > 1) {
|
|
|
|
if (size <= 0) {
|
|
|
|
lam_output(lam_malloc_output, "Request for %ld bytes (%s, %d)",
|
|
|
|
(long) size, file, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addr = malloc(size);
|
|
|
|
if (lam_malloc_debug_level > 0) {
|
|
|
|
if (NULL == addr) {
|
|
|
|
lam_output(lam_malloc_output,
|
|
|
|
"Request for %ld bytes failed (%s, %d)",
|
|
|
|
(long) size, file, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-10 21:58:55 +03:00
|
|
|
/*
|
|
|
|
* Debug version of realloc
|
|
|
|
*/
|
|
|
|
void *lam_realloc(void *ptr, size_t size, char *file, int line)
|
|
|
|
{
|
|
|
|
void *addr;
|
|
|
|
|
|
|
|
if (lam_malloc_debug_level > 1) {
|
|
|
|
if (size <= 0) {
|
|
|
|
if (NULL == ptr) {
|
|
|
|
lam_output(lam_malloc_output,
|
|
|
|
"Realloc NULL for %ld bytes (%s, %d)",
|
|
|
|
(long) size, file, line);
|
|
|
|
} else {
|
|
|
|
lam_output(lam_malloc_output, "Realloc %p for %ld bytes (%s, %d)",
|
|
|
|
ptr, (long) size, file, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addr = realloc(ptr, size);
|
|
|
|
if (lam_malloc_debug_level > 0) {
|
|
|
|
if (NULL == addr) {
|
|
|
|
lam_output(lam_malloc_output,
|
|
|
|
"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
|
|
|
*/
|
|
|
|
void lam_free(void *addr, char *file, int line)
|
|
|
|
{
|
|
|
|
if (lam_malloc_debug_level > 1 && NULL == addr) {
|
|
|
|
lam_output(lam_malloc_output, "Invalid free (%s, %d)", file, line);
|
|
|
|
} else {
|
|
|
|
free(addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-10 21:58:55 +03:00
|
|
|
|