3 * $Id: track.h,v 1.3 1999/05/06 19:51:36 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.3 1999/05/06 19:51:36 mdw
34 * Reformatted the LGPL notice a little bit.
36 * Revision 1.2 1999/05/05 18:50:31 mdw
37 * Change licensing conditions to LGPL.
39 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
40 * Initial version of mLib
53 /*----- Options and conventions -------------------------------------------*
55 * The following macros affect the tracking system:
57 * @TRACK_ENABLE@: Enable tracking of memory allocations
58 * @TRACK_BLAME@: Register my context blocks in allocations
60 * The reason there are two switches is simple. It's often the case that a
61 * library routine allocates memory for its client. Therefore, whether we
62 * want to record the library or the client depends on how much we trust
63 * the two pieces of software. Setting @TRACK_ENABLE@ and @TRACK_BLAME@
64 * suggests that the current source file might leak memory, so we want its
65 * context markers in the list. Setting @TRACK_ENABLE@ but not
66 * @TRACK_BLAME@ suggests that we trust this code, but not the code which
67 * calls it, so we want to preserve the caller's context markers.
72 /*----- Type definitions --------------------------------------------------*/
74 /* --- A context buffer --- */
76 typedef struct track_ctx {
77 struct track_ctx *next;
81 /*----- Functions provided ------------------------------------------------*/
83 /* --- @track_setLevel@ --- *
85 * Arguments: @unsigned int l@ = tracing level for allocation messages
89 * Use: Sets the trace level for allocation messages.
92 extern void track_setLevel(unsigned int /*l*/);
94 /* --- @track_pushContext@ --- *
96 * Arguments: @track_ctx *ctx@ = context holder to push
100 * Use: Pushes the given context block onto the stack.
103 extern void track_pushContext(track_ctx */*ctx*/);
105 /* --- @track_popContext@ --- *
107 * Arguments: @track_ctx *ctx@ = context holder to pop
111 * Use: Removes the given context block from the stack.
114 extern void track_popContext(track_ctx */*ctx*/);
116 /* --- @track_malloc@ --- *
118 * Arguments: @size_t sz@ = size requested
120 * Returns: Pointer to allocated space, or null
122 * Use: Allocates memory, and tracks how much is allocated.
125 extern void *track_malloc(size_t /*sz*/);
127 /* --- @track_free@ --- *
129 * Arguments: @void *p@ = pointer to an allocated block
133 * Use: Frees memory, and tracks how much is still allocated.
136 extern void track_free(void */*p*/);
138 /* --- @track_realloc@ --- *
140 * Arguments: @void *p@ = pointer to an allocated block
141 * @size_t sz@ = how big it wants to be
143 * Returns: Pointer to the new block.
145 * Use: Reallocates a block, tracking how much memory is still
149 extern void *track_realloc(void */*p*/, size_t /*sz*/);
151 /* --- @track_used@ --- *
155 * Returns: A count of how much memory is used currently.
157 * Use: Returns the amount of memory which the @track_@-functions
158 * above have counted as being currently allocated.
161 extern unsigned long track_used(void);
163 /* --- @track_list@ --- *
165 * Arguments: @unsigned int l@ = trace level to use
169 * Use: Traces a dump of the currently known blocks. Combined with
170 * a verbose dump of allocations and deallocations, and a
171 * good idea of which blocks were allocated where, this can
172 * be useful for locating memory leaks. It's not exactly a
176 extern void track_list(unsigned int l);
178 /*----- Macro wrappers ----------------------------------------------------*/
181 /* --- If tracking is to be done, set it up --- */
185 # define malloc(sz) track_malloc(sz)
187 # define free(p) track_free(p)
189 # define realloc(p, sz) track_realloc(p, sz)
192 /* --- Provide a context for doing track-related things --- */
200 /* --- Handle contexts --- */
202 #if defined(TRACK_ENABLE) && defined(TRACK_BLAME)
203 # define TRACK_NCTX(name, string) track__context name = { 0, string }
204 # define TRACK_NPUSH(name) track_pushContext(name)
205 # define TRACK_NPOP(name) track_popContext(name)
206 # define TRACK_CTX(string) TRACK_NCTX(track__localContext, string)
207 # define TRACK_PUSH TRACK_NPUSH(track__localContext)
208 # define TRACK_POP TRACK_NPOP(track__localContext)
210 # define TRACK_NCTX(name, string)
211 # define TRACK_NPUSH(name) ((void)0)
212 # define TRACK_NPOP(name) ((void)0)
213 # define TRACK_CTX(string)
214 # define TRACK_PUSH ((void)0)
215 # define TRACK_POP ((void)0)
218 /*----- That's all, folks -------------------------------------------------*/