3 * $Id: track.h,v 1.2 1999/05/05 18:50:31 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 Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.2 1999/05/05 18:50:31 mdw
33 * Change licensing conditions to LGPL.
35 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
36 * Initial version of mLib
49 /*----- Options and conventions -------------------------------------------*
51 * The following macros affect the tracking system:
53 * @TRACK_ENABLE@: Enable tracking of memory allocations
54 * @TRACK_BLAME@: Register my context blocks in allocations
56 * The reason there are two switches is simple. It's often the case that a
57 * library routine allocates memory for its client. Therefore, whether we
58 * want to record the library or the client depends on how much we trust
59 * the two pieces of software. Setting @TRACK_ENABLE@ and @TRACK_BLAME@
60 * suggests that the current source file might leak memory, so we want its
61 * context markers in the list. Setting @TRACK_ENABLE@ but not
62 * @TRACK_BLAME@ suggests that we trust this code, but not the code which
63 * calls it, so we want to preserve the caller's context markers.
68 /*----- Type definitions --------------------------------------------------*/
70 /* --- A context buffer --- */
72 typedef struct track_ctx {
73 struct track_ctx *next;
77 /*----- Functions provided ------------------------------------------------*/
79 /* --- @track_setLevel@ --- *
81 * Arguments: @unsigned int l@ = tracing level for allocation messages
85 * Use: Sets the trace level for allocation messages.
88 extern void track_setLevel(unsigned int /*l*/);
90 /* --- @track_pushContext@ --- *
92 * Arguments: @track_ctx *ctx@ = context holder to push
96 * Use: Pushes the given context block onto the stack.
99 extern void track_pushContext(track_ctx */*ctx*/);
101 /* --- @track_popContext@ --- *
103 * Arguments: @track_ctx *ctx@ = context holder to pop
107 * Use: Removes the given context block from the stack.
110 extern void track_popContext(track_ctx */*ctx*/);
112 /* --- @track_malloc@ --- *
114 * Arguments: @size_t sz@ = size requested
116 * Returns: Pointer to allocated space, or null
118 * Use: Allocates memory, and tracks how much is allocated.
121 extern void *track_malloc(size_t /*sz*/);
123 /* --- @track_free@ --- *
125 * Arguments: @void *p@ = pointer to an allocated block
129 * Use: Frees memory, and tracks how much is still allocated.
132 extern void track_free(void */*p*/);
134 /* --- @track_realloc@ --- *
136 * Arguments: @void *p@ = pointer to an allocated block
137 * @size_t sz@ = how big it wants to be
139 * Returns: Pointer to the new block.
141 * Use: Reallocates a block, tracking how much memory is still
145 extern void *track_realloc(void */*p*/, size_t /*sz*/);
147 /* --- @track_used@ --- *
151 * Returns: A count of how much memory is used currently.
153 * Use: Returns the amount of memory which the @track_@-functions
154 * above have counted as being currently allocated.
157 extern unsigned long track_used(void);
159 /* --- @track_list@ --- *
161 * Arguments: @unsigned int l@ = trace level to use
165 * Use: Traces a dump of the currently known blocks. Combined with
166 * a verbose dump of allocations and deallocations, and a
167 * good idea of which blocks were allocated where, this can
168 * be useful for locating memory leaks. It's not exactly a
172 extern void track_list(unsigned int l);
174 /*----- Macro wrappers ----------------------------------------------------*/
177 /* --- If tracking is to be done, set it up --- */
181 # define malloc(sz) track_malloc(sz)
183 # define free(p) track_free(p)
185 # define realloc(p, sz) track_realloc(p, sz)
188 /* --- Provide a context for doing track-related things --- */
196 /* --- Handle contexts --- */
198 #if defined(TRACK_ENABLE) && defined(TRACK_BLAME)
199 # define TRACK_NCTX(name, string) track__context name = { 0, string }
200 # define TRACK_NPUSH(name) track_pushContext(name)
201 # define TRACK_NPOP(name) track_popContext(name)
202 # define TRACK_CTX(string) TRACK_NCTX(track__localContext, string)
203 # define TRACK_PUSH TRACK_NPUSH(track__localContext)
204 # define TRACK_POP TRACK_NPOP(track__localContext)
206 # define TRACK_NCTX(name, string)
207 # define TRACK_NPUSH(name) ((void)0)
208 # define TRACK_NPOP(name) ((void)0)
209 # define TRACK_CTX(string)
210 # define TRACK_PUSH ((void)0)
211 # define TRACK_POP ((void)0)
214 /*----- That's all, folks -------------------------------------------------*/