Commit 048b63f3 authored by Per Cederqvist's avatar Per Cederqvist
Browse files

Test TRACED_ALLOCATIONS instead of DEBUG_MALLOC.

(trace_alloc_file): New function, that sets up malloc_fp.
(trace_smalloc): Write to malloc_fp.
(trace_free): Likewise.
(srealloc): Likewise.
parent ff213feb
/*
* $Id: ram-smalloc.c,v 0.26 1999/05/15 18:55:03 ceder Exp $
* $Id: ram-smalloc.c,v 0.27 1999/05/15 22:54:37 ceder Exp $
* Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996 Lysator Academic Computer Association.
*
* This file is part of the LysKOM server.
......@@ -36,14 +36,13 @@
#endif
/*
* Define DEBUG_MALLOC to get traces from smalloc, srealloc and sfree.
* Define TRACED_ALLOCATIONS to get traces from smalloc, srealloc and sfree.
* Run the resulting lyskomd under gdb. Source trace-mem.gdb. Run
* M-x resolve-trace (from handle-malloc-dump.el) on the resulting output.
*/
/* #define DEBUG_MALLOC */
static const char *
rcsid = "$Id: ram-smalloc.c,v 0.26 1999/05/15 18:55:03 ceder Exp $";
rcsid = "$Id: ram-smalloc.c,v 0.27 1999/05/15 22:54:37 ceder Exp $";
#include "rcs.h"
USE(rcsid);
......@@ -52,22 +51,46 @@ USE(rcsid);
#endif
#include <stdio.h>
#include <sys/types.h>
#include <assert.h>
#include <errno.h>
#include "exp.h"
#include "s-string.h"
#include "kom-types.h"
#include "lyskomd.h"
#include "server/smalloc.h"
#ifdef TRACED_ALLOCATIONS
# include "trace-alloc.h"
#endif
static int no_of_allocated_blocks = 0;
#ifdef DEBUG_MALLOC
#ifdef TRACED_ALLOCATIONS
static FILE *malloc_fp = NULL;
void
trace_alloc_file(const char *loc)
{
if (loc[0] == '\0')
malloc_fp = stderr;
else
malloc_fp = fopen(loc, "w");
if (malloc_fp == NULL)
restart_kom("init_malloc_fp failed: %s, %d\n", loc, errno);
fprintf(malloc_fp, "A new trace from lyskomd starts here\n");
}
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");
assert(malloc_fp != NULL);
fprintf(malloc_fp, "smalloc:\nArg: 0x%lx\nRes: 0x%lx\n",
(long)size, (long)result);
fprintf(malloc_fp, "==== end ====\n");
fflush(malloc_fp);
}
#endif
......@@ -91,19 +114,21 @@ smalloc(size_t size)
((unsigned char *) p)[size] = 0x89;
((unsigned char *) p)[size+1] = 0xA7;
#ifdef DEBUG_MALLOC
#ifdef TRACED_ALLOCATIONS
trace_smalloc(size, p);
#endif
return (void *) p;
}
#ifdef DEBUG_MALLOC
#ifdef TRACED_ALLOCATIONS
static void
trace_free(void *block)
{
printf("sfree:\nArg: 0x%lx\n", (long)block);
printf("==== end ====\n");
assert(malloc_fp != NULL);
fprintf(malloc_fp, "sfree:\nArg: 0x%lx\n", (long)block);
fprintf(malloc_fp, "==== end ====\n");
fflush(malloc_fp);
}
#endif
......@@ -114,7 +139,7 @@ sfree(void * ptr) /* it is legal to sfree a NULL pointer */
if ( ptr != NULL )
{
#ifdef DEBUG_MALLOC
#ifdef TRACED_ALLOCATIONS
trace_free(ptr);
#endif
ip = (unsigned int *) ptr;
......@@ -139,15 +164,17 @@ sfree(void * ptr) /* it is legal to sfree a NULL pointer */
}
}
#ifdef DEBUG_MALLOC
#ifdef TRACED_ALLOCATIONS
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");
assert(malloc_fp != NULL);
fprintf(malloc_fp, "srealloc:\nSize: 0x%lx\nArg: 0x%lx\nRes: 0x%lx\n",
(long)size, (long)arg, (long)result);
fprintf(malloc_fp, "==== end ====\n");
fflush(malloc_fp);
}
#endif
EXPORT void *
......@@ -192,7 +219,7 @@ 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
#ifdef TRACED_ALLOCATIONS
trace_srealloc(size, ptr, new_ptr);
#endif
......
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