Commit 2cbfa9be authored by Per Cederqvist's avatar Per Cederqvist
Browse files

Memory leak hunting utilities.

parent 6752359a
Sat Sep 21 02:12:24 1991 Per Cederqvist (ceder at lysator)
* ram-smalloc.c: Added support for memory leak finding.
* trace-mem.gdb, handle-malloc-dump.el: Useful for finding memory
leaks in the server. Compile ram-smalloc.c with DEBUG_MALLOC
defined.
* mux.c (mux_close): Fixed memory leak.
* ramkomd.c (main): Print how many blocks isc allocates.
* ramkomd.c: ip_client_port and ip_mux_port are local to ramkomd.c.
Tue Sep 17 23:04:39 1991 Per Cederqvist (ceder at lysator)
......
/*
* $Id: ram-smalloc.c,v 0.6 1991/09/15 10:29:31 linus Exp $
* $Id: ram-smalloc.c,v 0.7 1991/09/21 12:19:24 ceder Exp $
* Copyright (C) 1991 Lysator Academic Computer Association.
*
* This file is part of the LysKOM server.
......@@ -30,7 +30,14 @@
* TEST VERSION by ceder
*/
static char *rcsid = "$Id: ram-smalloc.c,v 0.6 1991/09/15 10:29:31 linus Exp $";
/*
* Define DEBUG_MALLOC to get traces from smalloc, srealloc and sfree.
* Run the resulting lyskomd under gdb. Source trace-mem.gdb. Run
* handle-malloc-dump.el on the resulting output.
*/
/* #define DEBUG_MALLOC */
static char *rcsid = "$Id: ram-smalloc.c,v 0.7 1991/09/21 12:19:24 ceder Exp $";
#include <stdio.h>
......@@ -45,6 +52,16 @@ static char *rcsid = "$Id: ram-smalloc.c,v 0.6 1991/09/15 10:29:31 linus Exp $";
static int no_of_allocated_blocks = 0;
#ifdef DEBUG_MALLOC
static void
trace_smalloc(size_t size,
void *result)
{
printf("smalloc:\nArg: 0x%lx\nRes: 0x%lx\n", (long)size, (long)result);
printf("==== end ====\n");
}
#endif
/*
* "safe" malloc. Handles the case when malloc returns NULL.
* smalloc cannot fail.
......@@ -64,10 +81,22 @@ smalloc(size_t size)
*p++ = size;
((unsigned char *) p)[size] = 0x89;
((unsigned char *) p)[size+1] = 0xA7;
#ifdef DEBUG_MALLOC
trace_smalloc(size, p);
#endif
return (void *) p;
}
#ifdef DEBUG_MALLOC
static void
trace_free(void *block)
{
printf("sfree:\nArg: 0x%lx\n", (long)block);
printf("==== end ====\n");
}
#endif
EXPORT void
sfree(void * ptr) /* it is legal to sfree a NULL pointer */
......@@ -81,6 +110,9 @@ sfree(void * ptr) /* it is legal to sfree a NULL pointer */
if ( ptr != NULL )
{
#ifdef DEBUG_MALLOC
trace_free(ptr);
#endif
ip = (unsigned int *) ptr;
ip -= 2;
switch (*ip)
......@@ -103,6 +135,17 @@ sfree(void * ptr) /* it is legal to sfree a NULL pointer */
}
}
#ifdef DEBUG_MALLOC
static void
trace_srealloc(size_t size,
void *arg,
void *result)
{
printf("srealloc:\nSize: 0x%lx\nArg: 0x%lx\nRes: 0x%lx\n",
(long)size, (long)arg, (long)result);
printf("==== end ====\n");
}
#endif
EXPORT void *
srealloc(void * ptr, size_t size) /* Never fails. It is legal to */
{ /* realloc the NULL ptr. */
......@@ -144,7 +187,11 @@ srealloc(void * ptr, size_t size) /* Never fails. It is legal to */
((unsigned char *) new_ptr)[size] = 0x89;
((unsigned char *) new_ptr)[size+1] = 0xA7;
#ifdef DEBUG_MALLOC
trace_srealloc(size, ptr, new_ptr);
#endif
return (void *) new_ptr;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment