chiark / gitweb /
Add some simple `malloc' tracking.
[become] / src / utils.h
1 /* -*-c-*-
2  *
3  * $Id: utils.h,v 1.3 1997/08/20 16:25:37 mdw Exp $
4  *
5  * Miscellaneous useful bits of code.
6  *
7  * (c) 1997 Mark Wooding
8  */
9
10 /*----- Licensing notice --------------------------------------------------*
11  *
12  * This file is part of `become'
13  *
14  * `Become' is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * `Become' 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 General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with `become'; if not, write to the Free Software Foundation,
26  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 /*----- Revision history --------------------------------------------------*
30  *
31  * $Log: utils.h,v $
32  * Revision 1.3  1997/08/20 16:25:37  mdw
33  * Add some simple `malloc' tracking.
34  *
35  * Revision 1.2  1997/08/04 10:24:26  mdw
36  * Sources placed under CVS control.
37  *
38  * Revision 1.1  1997/07/21  13:47:42  mdw
39  * Initial revision
40  *
41  */
42
43 #ifndef UTILS_H
44 #define UTILS_H
45
46 #ifdef __cplusplus
47   extern "C" {
48 #endif
49
50 /*----- Required header files ---------------------------------------------*/
51
52 #include <stddef.h>
53 #include <stdlib.h>
54
55 /*----- Storing and retrieving numbers ------------------------------------*
56  *
57  * These use big-endian conventions, because they seem more usual in network
58  * applications.  I actually think that little-endian is more sensible...
59  */
60
61 #define load32(p)                                                       \
62   ((((unsigned char)(p)[0] & 0xFF) << 24) |                             \
63    (((unsigned char)(p)[1] & 0xFF) << 16) |                             \
64    (((unsigned char)(p)[2] & 0xFF) <<  8) |                             \
65    (((unsigned char)(p)[3] & 0xFF) <<  0))
66
67 #define store32(p, v)                                                   \
68   ((p)[0] = ((unsigned long)(v) >> 24) & 0xFF,                          \
69    (p)[1] = ((unsigned long)(v) >> 16) & 0xFF,                          \
70    (p)[2] = ((unsigned long)(v) >>  8) & 0xFF,                          \
71    (p)[3] = ((unsigned long)(v) >>  0) & 0xFF)
72
73 /* --- Little-endian versions (for MD5) --- */
74
75 #define load32_l(p)                                                     \
76   ((((unsigned char)(p)[0] & 0xFF) <<  0) |                             \
77    (((unsigned char)(p)[1] & 0xFF) <<  8) |                             \
78    (((unsigned char)(p)[2] & 0xFF) << 16) |                             \
79    (((unsigned char)(p)[3] & 0xFF) << 24))
80
81 #define store32_l(p, v)                                                 \
82   ((p)[0] = ((unsigned long)(v) >>  0) & 0xFF,                          \
83    (p)[1] = ((unsigned long)(v) >>  8) & 0xFF,                          \
84    (p)[2] = ((unsigned long)(v) >> 16) & 0xFF,                          \
85    (p)[3] = ((unsigned long)(v) >> 24) & 0xFF)
86
87 /*----- Other macros ------------------------------------------------------*/
88
89 /* --- @burn@ --- *
90  *
91  * Arguments:   @obj@ = some object
92  *
93  * Use:         Writes zero bytes over the object.
94  */
95
96 #define burn(obj) ((void)memset(&obj, 0, sizeof(obj)))
97
98 /*----- Program name handling ---------------------------------------------*/
99
100 /* --- @quis@ --- *
101  *
102  * Arguments:   ---
103  *
104  * Returns:     Pointer to the program name.
105  *
106  * Use:         Returns the program name.
107  */
108
109 extern const char *quis(void);
110
111 /* --- @ego@ --- *
112  *
113  * Arguments:   @const char *p@ = pointer to program name
114  *
115  * Returns:     ---
116  *
117  * Use:         Tells the utils library what the program's name is.
118  */
119
120 extern void ego(const char */*p*/);
121
122 /*----- Error reporting ---------------------------------------------------*/
123
124 /* --- @moan@ --- *
125  *
126  * Arguments:   @const char *f@ = a @printf@-style format string
127  *              @...@ = other arguments
128  *
129  * Returns:     ---
130  *
131  * Use:         Reports an error.
132  */
133
134 extern void moan(const char */*f*/, ...);
135
136 /* --- @die@ --- *
137  *
138  * Arguments:   @const char *f@ = a @printf@-style format string
139  *              @...@ = other arguments
140  *
141  * Returns:     Never.
142  *
143  * Use:         Reports an error and hari-kiris.  Like @moan@ above, only
144  *              more permanent.
145  */
146
147 extern void die(const char */*f*/, ...);
148
149 /*----- Trace messages ----------------------------------------------------*/
150
151 #if !defined(NDEBUG) && !defined(TRACING)
152 #  define TRACING
153 #endif
154
155 #ifdef TRACING
156
157 /* --- @trace@ --- *
158  *
159  * Arguments:   @unsigned int lvl@ = trace level for output
160  *              @const char *f@ = a @printf@-style format string
161  *              @...@ = other arguments
162  *
163  * Returns:     ---
164  *
165  * Use:         Reports a message to the trace output.
166  */
167
168 extern void trace(unsigned int /*lvl*/, const char */*f*/, ...);
169
170 /* --- @traceblk@ --- *
171  *
172  * Arguments:   @unsigned int lvl@ = trace level for output
173  *              @const char *hdr@ = some header string to write
174  *              @const void *blk@ = pointer to a block of memory to dump
175  *              @size_t sz@ = size of the block of memory
176  *
177  * Returns:     ---
178  *
179  * Use:         Dumps the contents of a block to the trace output.
180  */
181
182 extern void traceblk(unsigned int /*lvl*/, const char */*hdr*/,
183                      const void */*blk*/, size_t /*sz*/);
184
185 /* --- @traceon@ --- *
186  *
187  * Arguments:   @FILE *fp@ = a file to trace on
188  *              @unsigned int lvl@ = trace level to set
189  *
190  * Returns:     ---
191  *
192  * Use:         Enables tracing to a file.
193  */
194
195 extern void traceon(FILE */*fp*/, unsigned int /*lvl*/);
196
197 /* --- @tracesetlvl@ --- *
198  *
199  * Arguments:   @unsigned int lvl@ = trace level to set
200  *
201  * Returns:     ---
202  *
203  * Use:         Sets the tracing level.
204  */
205
206 extern void tracesetlvl(unsigned int /*lvl*/);
207
208 /* --- @tracing@ --- *
209  *
210  * Arguments:   ---
211  *
212  * Returns:     Zero if not tracing, tracing level if tracing.
213  *
214  * Use:         Informs the caller whether tracing is enabled.
215  */
216
217 extern unsigned int tracing(void);
218
219 #endif
220
221 /* --- Some hacky macros --- */
222
223 #ifdef TRACING
224 #  define T(x) x
225 #  define IF_TRACING(lvl, x) if ((lvl) & tracing()) x
226 #else
227 #  define T(x)
228 #  define IF_TRACING(lvl, x)
229 #endif
230
231 /*----- Memory management functions ---------------------------------------*/
232
233 /* --- @xmalloc@ --- *
234  *
235  * Arguments:   @size_t sz@ = size of block to allocate
236  *
237  * Returns:     Pointer to allocated block.
238  *
239  * Use:         Allocates memory.  If the memory isn't available, we don't
240  *              hang aroung long enough for a normal function return.
241  */
242
243 extern void *xmalloc(size_t /*sz*/);
244
245 /* --- @xstrdup@ --- *
246  *
247  * Arguments:   @const char *s@ = pointer to a string
248  *
249  * Returns:     Pointer to a copy of the string.
250  *
251  * Use:         Copies a string (like @strdup@ would, if it existed).
252  */
253
254 extern char *xstrdup(const char */*s*/);
255
256 /* --- @xrealloc@ --- *
257  *
258  * Arguments:   @void *p@ = pointer to a block of memory
259  *              @size_t sz@ = new size desired for the block
260  *
261  * Returns:     Pointer to the resized memory block (which is almost
262  *              certainly not in the same place any more).
263  *
264  * Use:         Resizes a memory block.
265  */
266
267 extern void *xrealloc(void */*p*/, size_t /*sz*/);
268
269 /*----- Simple memory use tracking ----------------------------------------*/
270
271 #undef TRACK_MALLOC
272
273 #ifdef TRACK_MALLOC
274
275 /* --- @track_malloc@ --- *
276  *
277  * Arguments:   @size_t sz@ = size requested
278  *
279  * Returns:     Pointer to allocated space, or null
280  *
281  * Use:         Allocates memory, and tracks how much is allocated.
282  */
283
284 extern void *track_malloc(size_t /*sz*/);
285
286 /* --- @track_free@ --- *
287  *
288  * Arguments:   @void *p@ = pointer to an allocated block
289  *
290  * Returns:     ---
291  *
292  * Use:         Frees memory, and tracks how much is still allocated.
293  */
294
295 extern void track_free(void */*p*/);
296
297 /* --- @track_realloc@ --- *
298  *
299  * Arguments:   @void *p@ = pointer to an allocated block
300  *              @size_t sz@ = how big it wants to be
301  *
302  * Returns:     Pointer to the new block.
303  *
304  * Use:         Reallocates a block, tracking how much memory is still
305  *              available.
306  */
307
308 extern void *track_realloc(void */*p*/, size_t /*sz*/);
309
310 /* --- @track_memused@ --- *
311  *
312  * Arguments:   ---
313  *
314  * Returns:     A count of how much memory is used currently.
315  *
316  * Use:         Returns the amount of memory which the @track_@-functions
317  *              above have counted as being currently allocated.
318  */
319
320 extern unsigned long track_memused(void);
321
322 /* --- @track_memlist@ --- *
323  *
324  * Arguments:   ---
325  *
326  * Returns:     ---
327  *
328  * Use:         Dumps a list of allocated blocks to standard output.
329  */
330
331 extern void track_memlist(void);
332
333 #undef malloc
334 #define malloc(sz) track_malloc(sz)
335
336 #undef free
337 #define free(p) track_free(p)
338
339 #undef realloc
340 #define realloc(p, sz) track_realloc(p, sz)
341
342 #endif
343
344 /*----- That's all, folks -------------------------------------------------*/
345
346 #ifdef __cplusplus
347   }
348 #endif
349
350 #endif