1 #ifndef MPT_DBMALLOC_H
2 #define MPT_DBMALLOC_H
3 
4 
5 /*
6  * mpatrol
7  * A library for controlling and tracing dynamic memory allocations.
8  * Copyright (C) 1997-2002 Graeme S. Roy <graeme.roy@analog.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the Free
22  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23  * MA 02111-1307, USA.
24  */
25 
26 
27 /*
28  * Dbmalloc-compatible interface.  Implements Dbmalloc functions using
29  * mpatrol.  Dbmalloc is copyright (C) 1990-1992 Conor P. Cahill.
30  */
31 
32 
33 /*
34  * $Id: dbmalloc.h,v 1.15 2002/01/08 20:05:10 graeme Exp $
35  */
36 
37 
38 /*
39  * This file provides Dbmalloc-compatible functions which are built on top
40  * of the mpatrol library.  They are compatible with the last known public
41  * release of Dbmalloc (patch level 14), but only the Dbmalloc-specific
42  * functions are defined here, leaving the overriding of standard functions
43  * up to the mpatrol library.  As the mpatrol library does not currently
44  * override the C library string functions and the X toolkit heap allocation
45  * functions, neither does this file.
46  *
47  * The dbmallopt() function does not support the setting of all of the
48  * Dbmalloc options.  In fact, most of them do not make sense when applied
49  * to the mpatrol library.  Some of them have slightly changed behaviour
50  * due to the mapping process and some of them cannot be implemented due to
51  * the mpatrol library having been initialised beforehand.
52  *
53  * The malloc_dump() function does not support the full recognition of the
54  * MALLOC_DETAIL option in that it does not display the additional columns
55  * and summary that the Dbmalloc library does.  This is because this would
56  * make no sense when applied to the mpatrol library, but it does still
57  * affect whether freed allocations are shown in the listing (although the
58  * details for such allocations are slightly different, and there are no
59  * entries displayed for free memory blocks).
60  *
61  * The output for the malloc_dump() and malloc_list() functions is almost
62  * identical to that of the Dbmalloc library except for a slight change in
63  * the pointer format when displaying the address of each memory allocation.
64  * The stack information is obtained differently as well, since the mpatrol
65  * library records symbolic stack tracebacks for each memory allocation.
66  * As a result, malloc_enter() and malloc_leave() do nothing and the return
67  * address in a stack frame is displayed if no associated symbol name, file
68  * and line number could be determined.  Parentheses are not printed at the
69  * end of symbol names so that they can be processed properly by a C++
70  * demangler if necessary.  Passing a file descriptor of 0 to malloc_dump()
71  * or malloc_list() results in the output being sent to the mpatrol log
72  * file.
73  *
74  * The malloc_size() and malloc_mark() functions do not give an error
75  * message if the pointer passed in does not correspond to a heap
76  * allocation.  Neither of these functions automatically perform an
77  * integrity check of the heap.  Note that the malloc_chain_check()
78  * function will never return a non-zero value - it always terminates with
79  * an error message in the mpatrol log file whenever it detects heap
80  * corruption.  As a result, the malloc_abort() function is not used.
81  *
82  * This file is initialised via the mpatrol library's initialiser function
83  * feature, which means that if the __mp_init_dbmalloc() function is noted
84  * by the mpatrol symbol manager then it will be called when the mpatrol
85  * library is being initialised.  If this feature is not supported then the
86  * dbmallinit() function must be called as early on as possible, otherwise
87  * this file will not be initialised until one of its functions are called.
88  */
89 
90 
91 #include <mpatrol.h>
92 
93 
94 /* Commands for dbmallopt().  Some of them are ignored as they have no
95  * meaning when used with the mpatrol library.
96  */
97 
98 #define MALLOC_WARN      100
99 #define MALLOC_FATAL     101
100 #define MALLOC_ERRFILE   102
101 #define MALLOC_CKCHAIN   103
102 #define MALLOC_FILLAREA  104
103 #define MALLOC_LOWFRAG   105
104 #define MALLOC_CKDATA    106
105 #define MALLOC_REUSE     107
106 #define MALLOC_SHOWLINKS 108
107 #define MALLOC_DETAIL    109
108 #define MALLOC_FREEMARK  110
109 #define MALLOC_ZERO      111
110 
111 
112 /* The settings for the MALLOC_WARN and MALLOC_FATAL options.
113  */
114 
115 #define M_HANDLE_IGNORE 0
116 #define M_HANDLE_ABORT  1
117 #define M_HANDLE_EXIT   2
118 #define M_HANDLE_CORE   3
119 #define M_HANDLE_DUMP   128
120 
121 
122 /* The errors reported by the library.  None of these are actually used
123  * in this implementation and are here in case user code relies on them.
124  */
125 
126 #define M_CODE_CHAIN_BROKE  MP_ET_MAX
127 #define M_CODE_NO_END       MP_ET_MAX
128 #define M_CODE_BAD_PTR      MP_ET_NOTALL
129 #define M_CODE_BAD_MAGIC    MP_ET_NOTALL
130 #define M_CODE_BAD_CONNECT  MP_ET_MAX
131 #define M_CODE_OVERRUN      MP_ET_ALLOVF
132 #define M_CODE_REUSE        MP_ET_FRDCOR
133 #define M_CODE_NOT_INUSE    MP_ET_PRVFRD
134 #define M_CODE_NOMORE_MEM   MP_ET_OUTMEM
135 #define M_CODE_OUTOF_BOUNDS MP_ET_RNGOVF
136 #define M_CODE_FREELIST_BAD MP_ET_MAX
137 #define M_CODE_NOBOUND      MP_ET_MAX
138 #define M_CODE_STK_NOCUR    MP_ET_MAX
139 #define M_CODE_STK_BADFUNC  MP_ET_MAX
140 #define M_CODE_UNDERRUN     MP_ET_ALLOVF
141 #define M_CODE_FREEMARK     MP_ET_FREMRK
142 #define M_CODE_ZERO_ALLOC   MP_ET_ALLZER
143 
144 
145 /* The union used to supply a command argument to dbmallopt().
146  */
147 
148 union dbmalloptarg
149 {
150     long i;    /* integer value */
151     char *str; /* string value */
152 };
153 
154 
155 #ifndef NDEBUG
156 
157 #define malloc_errno __mp_errno
158 
159 #define dbmallinit() __mp_init_dbmalloc()
160 #define dbmallopt(c, v) __mpt_dbmallocoption((c), (v))
161 #define malloc_perror(s) __mpt_dbmallocperror(s)
162 #define malloc_chain_check(f) __mpt_dbmallocchaincheck((f), MP_FUNCNAME, \
163                                                        __FILE__, __LINE__)
164 #define malloc_dump(f) __mpt_dbmallocdump(f)
165 #define malloc_list(f, l, u) __mpt_dbmalloclist((f), (l), (u))
166 #define malloc_inuse(h) __mpt_dbmallocinuse(h)
167 #define malloc_size(p) __mpt_dbmallocsize(p)
168 #define malloc_mark(p) (void) __mp_setmark(p)
169 #define malloc_enter(f) ((void) 0)
170 #define malloc_leave(f) ((void) 0)
171 
172 
173 #ifdef __cplusplus
174 extern "C"
175 {
176 #endif /* __cplusplus */
177 
178 
179 int __mpt_dbmallocoption(int, union dbmalloptarg *);
180 void __mpt_dbmallocperror(MP_CONST char *);
181 int __mpt_dbmallocchaincheck(int, MP_CONST char *, MP_CONST char *,
182                              unsigned long);
183 void __mpt_dbmallocdump(int);
184 void __mpt_dbmalloclist(int, unsigned long, unsigned long);
185 unsigned long __mpt_dbmallocinuse(unsigned long *);
186 size_t __mpt_dbmallocsize(MP_CONST void *);
187 void __mp_init_dbmalloc(void);
188 
189 
190 static MP_VOLATILE void *__mpt_init_dbmalloc = (void *) __mp_init_dbmalloc;
191 
192 
193 #ifdef __cplusplus
194 }
195 #endif /* __cplusplus */
196 
197 #else /* NDEBUG */
198 
199 #define malloc_errno __mp_errno
200 
201 #define dbmallinit() ((void) 0)
202 #define dbmallopt(c, v) ((int) 1)
203 #define malloc_perror(s) ((void) 0)
204 #define malloc_chain_check(f) ((int) 0)
205 #define malloc_dump(f) ((void) 0)
206 #define malloc_list(f, l, u) ((void) 0)
207 #define malloc_inuse(h) (*(h) = 0, (unsigned long) 0)
208 #define malloc_size(p) ((size_t) -1)
209 #define malloc_mark(p) ((void) 0)
210 #define malloc_enter(f) ((void) 0)
211 #define malloc_leave(f) ((void) 0)
212 
213 #endif /* NDEBUG */
214 
215 
216 #endif /* MPT_DBMALLOC_H */
217