3 * $Id: track.h,v 1.4 1999/10/22 22:40:25 mdw Exp $
5 * Tracing functions for debugging
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.4 1999/10/22 22:40:25 mdw
34 * Change naming slightly. Still not documented, though.
36 * Revision 1.3 1999/05/06 19:51:36 mdw
37 * Reformatted the LGPL notice a little bit.
39 * Revision 1.2 1999/05/05 18:50:31 mdw
40 * Change licensing conditions to LGPL.
42 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
43 * Initial version of mLib
56 /*----- Options and conventions -------------------------------------------*
58 * The following macros affect the tracking system:
60 * @TRACK_ENABLE@: Enable tracking of memory allocations
61 * @TRACK_BLAME@: Register my context blocks in allocations
63 * The reason there are two switches is simple. It's often the case that a
64 * library routine allocates memory for its client. Therefore, whether we
65 * want to record the library or the client depends on how much we trust
66 * the two pieces of software. Setting @TRACK_ENABLE@ and @TRACK_BLAME@
67 * suggests that the current source file might leak memory, so we want its
68 * context markers in the list. Setting @TRACK_ENABLE@ but not
69 * @TRACK_BLAME@ suggests that we trust this code, but not the code which
70 * calls it, so we want to preserve the caller's context markers.
75 /*----- Type definitions --------------------------------------------------*/
77 /* --- A context buffer --- */
79 typedef struct track_ctx
{
80 struct track_ctx
*next
;
84 /*----- Functions provided ------------------------------------------------*/
86 /* --- @track_level@ --- *
88 * Arguments: @unsigned int l@ = tracing level for allocation messages
92 * Use: Sets the trace level for allocation messages.
95 extern void track_level(unsigned int /*l*/);
97 /* --- @track_push@ --- *
99 * Arguments: @track_ctx *ctx@ = context holder to push
103 * Use: Pushes the given context block onto the stack.
106 extern void track_push(track_ctx */
*ctx*/
);
108 /* --- @track_pop@ --- *
110 * Arguments: @track_ctx *ctx@ = context holder to pop
114 * Use: Removes the given context block from the stack.
117 extern void track_pop(track_ctx */
*ctx*/
);
119 /* --- @track_malloc@ --- *
121 * Arguments: @size_t sz@ = size requested
123 * Returns: Pointer to allocated space, or null
125 * Use: Allocates memory, and tracks how much is allocated.
128 extern void *track_malloc(size_t /*sz*/);
130 /* --- @track_free@ --- *
132 * Arguments: @void *p@ = pointer to an allocated block
136 * Use: Frees memory, and tracks how much is still allocated.
139 extern void track_free(void */
*p*/
);
141 /* --- @track_realloc@ --- *
143 * Arguments: @void *p@ = pointer to an allocated block
144 * @size_t sz@ = how big it wants to be
146 * Returns: Pointer to the new block.
148 * Use: Reallocates a block, tracking how much memory is still
152 extern void *track_realloc(void */
*p*/
, size_t /*sz*/);
154 /* --- @track_used@ --- *
158 * Returns: A count of how much memory is used currently.
160 * Use: Returns the amount of memory which the @track_@-functions
161 * above have counted as being currently allocated.
164 extern unsigned long track_used(void);
166 /* --- @track_list@ --- *
168 * Arguments: @unsigned int l@ = trace level to use
172 * Use: Traces a dump of the currently known blocks. Combined with
173 * a verbose dump of allocations and deallocations, and a
174 * good idea of which blocks were allocated where, this can
175 * be useful for locating memory leaks. It's not exactly a
179 extern void track_list(unsigned int l
);
181 /*----- Macro wrappers ----------------------------------------------------*/
183 /* --- If tracking is to be done, set it up --- */
187 # define malloc(sz) track_malloc(sz)
189 # define free(p) track_free(p)
191 # define realloc(p, sz) track_realloc(p, sz)
194 /* --- Provide a context for doing track-related things --- */
202 /* --- Handle contexts --- */
204 #if defined(TRACK_ENABLE) && defined(TRACK_BLAME)
205 # define TRACK_NCTX(name, string) track_ctx name = { 0, string }
206 # define TRACK_NPUSH(name) track_push(name)
207 # define TRACK_NPOP(name) track_pop(name)
208 # define TRACK_CTX(string) TRACK_NCTX(__track_ctx, string)
209 # define TRACK_PUSH TRACK_NPUSH(__track_ctx)
210 # define TRACK_POP TRACK_NPOP(__track_ctx)
212 # define TRACK_NCTX(name, string)
213 # define TRACK_NPUSH(name) ((void)0)
214 # define TRACK_NPOP(name) ((void)0)
215 # define TRACK_CTX(string)
216 # define TRACK_PUSH ((void)0)
217 # define TRACK_POP ((void)0)
220 /*----- That's all, folks -------------------------------------------------*/