Imported from ../bash-2.05b.tar.gz.
[platform/upstream/bash.git] / lib / malloc / trace.c
1 /* trace.c - tracing functions for malloc */
2
3 /* Copyright (C) 2001 Free Software Foundation, Inc.
4
5    This file is part of GNU Bash, the Bourne Again SHell.
6
7    Bash is free software; you can redistribute it and/or modify it under
8    the terms of the GNU General Public License as published by the Free
9    Software Foundation; either version 2, or (at your option) any later
10    version.
11
12    Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13    WARRANTY; without even the implied warranty of MERCHANTABILITY or
14    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15    for more details.
16
17    You should have received a copy of the GNU General Public License along
18    with Bash; see the file COPYING.  If not, write to the Free Software
19    Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #include <stdio.h>
25
26 #include "imalloc.h"
27
28 extern int malloc_trace;
29
30 static int _mtrace_verbose = 0;
31
32 #ifdef MALLOC_TRACE
33
34 FILE *_mtrace_fp = NULL;
35 extern char _malloc_trace_buckets[];
36
37 void
38 mtrace_alloc (tag, mem, size, file, line)
39      const char *tag;
40      PTR_T mem;
41      size_t size;
42      const char *file;
43      int line;
44 {
45   if (_mtrace_fp == NULL)
46     _mtrace_fp = stderr;
47
48   if (_mtrace_verbose)
49     fprintf (_mtrace_fp, "alloc: %s: %p (%d bytes) from '%s:%d'\n",
50                 tag, mem, size, file ? file : "unknown", line);
51   else
52     fprintf (_mtrace_fp, "alloc:%p:%d:%s:%d\n",
53                 mem, size, file ? file : "unknown", line);
54 }
55
56 void
57 mtrace_free (mem, size, file, line)
58      PTR_T mem;
59      int size;
60      const char *file;
61      int line;
62 {
63   if (_mtrace_fp == NULL)
64     _mtrace_fp = stderr;
65
66   if (_mtrace_verbose)
67     fprintf (_mtrace_fp, "free: %p (%d bytes) from '%s:%d'\n",
68                 mem, size, file ? file : "unknown", line);
69   else
70     fprintf (_mtrace_fp, "free:%p:%d:%s:%d\n",
71                 mem, size, file ? file : "unknown", line);
72 }
73 #endif /* MALLOC_TRACE */
74
75 int
76 malloc_set_trace (n)
77      int n;
78 {
79   int old;
80
81   old = malloc_trace;
82   malloc_trace = n;
83   _mtrace_verbose = (n > 1);
84   return old;
85 }
86
87 void
88 malloc_set_tracefp (fp)
89      FILE *fp;
90 {
91 #ifdef MALLOC_TRACE
92   _mtrace_fp = fp ? fp : stderr;
93 #endif
94 }
95
96 void
97 malloc_trace_bin (n)
98      int n;
99 {
100 #ifdef MALLOC_TRACE
101   _malloc_trace_buckets[n] = 1;
102 #endif
103 }