3 * $Id: utils.h,v 1.4 1998/01/12 16:46:52 mdw Exp $
5 * Miscellaneous useful bits of code.
7 * (c) 1998 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of `become'
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.4 1998/01/12 16:46:52 mdw
35 * Revision 1.3 1997/08/20 16:25:37 mdw
36 * Add some simple `malloc' tracking.
38 * Revision 1.2 1997/08/04 10:24:26 mdw
39 * Sources placed under CVS control.
41 * Revision 1.1 1997/07/21 13:47:42 mdw
53 /*----- Required header files ---------------------------------------------*/
58 /*----- Storing and retrieving numbers ------------------------------------*
60 * These use big-endian conventions, because they seem more usual in network
61 * applications. I actually think that little-endian is more sensible...
65 ((((unsigned char)(p)[0] & 0xFF) << 24) | \
66 (((unsigned char)(p)[1] & 0xFF) << 16) | \
67 (((unsigned char)(p)[2] & 0xFF) << 8) | \
68 (((unsigned char)(p)[3] & 0xFF) << 0))
70 #define store32(p, v) \
71 ((p)[0] = ((unsigned long)(v) >> 24) & 0xFF, \
72 (p)[1] = ((unsigned long)(v) >> 16) & 0xFF, \
73 (p)[2] = ((unsigned long)(v) >> 8) & 0xFF, \
74 (p)[3] = ((unsigned long)(v) >> 0) & 0xFF)
76 /* --- Little-endian versions (for MD5) --- */
79 ((((unsigned char)(p)[0] & 0xFF) << 0) | \
80 (((unsigned char)(p)[1] & 0xFF) << 8) | \
81 (((unsigned char)(p)[2] & 0xFF) << 16) | \
82 (((unsigned char)(p)[3] & 0xFF) << 24))
84 #define store32_l(p, v) \
85 ((p)[0] = ((unsigned long)(v) >> 0) & 0xFF, \
86 (p)[1] = ((unsigned long)(v) >> 8) & 0xFF, \
87 (p)[2] = ((unsigned long)(v) >> 16) & 0xFF, \
88 (p)[3] = ((unsigned long)(v) >> 24) & 0xFF)
90 /*----- Other macros ------------------------------------------------------*/
94 * Arguments: @obj@ = some object
96 * Use: Writes zero bytes over the object.
99 #define burn(obj) ((void)memset(&obj, 0, sizeof(obj)))
101 /*----- Program name handling ---------------------------------------------*/
107 * Returns: Pointer to the program name.
109 * Use: Returns the program name.
112 extern const char *quis(void);
116 * Arguments: @const char *p@ = pointer to program name
120 * Use: Tells the utils library what the program's name is.
123 extern void ego(const char */*p*/);
125 /*----- Error reporting ---------------------------------------------------*/
129 * Arguments: @const char *f@ = a @printf@-style format string
130 * @...@ = other arguments
134 * Use: Reports an error.
137 extern void moan(const char */*f*/, ...);
141 * Arguments: @const char *f@ = a @printf@-style format string
142 * @...@ = other arguments
146 * Use: Reports an error and hari-kiris. Like @moan@ above, only
150 extern void die(const char */*f*/, ...);
152 /*----- Trace messages ----------------------------------------------------*/
154 #if !defined(NDEBUG) && !defined(TRACING)
162 * Arguments: @unsigned int lvl@ = trace level for output
163 * @const char *f@ = a @printf@-style format string
164 * @...@ = other arguments
168 * Use: Reports a message to the trace output.
171 extern void trace(unsigned int /*lvl*/, const char */*f*/, ...);
173 /* --- @traceblk@ --- *
175 * Arguments: @unsigned int lvl@ = trace level for output
176 * @const char *hdr@ = some header string to write
177 * @const void *blk@ = pointer to a block of memory to dump
178 * @size_t sz@ = size of the block of memory
182 * Use: Dumps the contents of a block to the trace output.
185 extern void traceblk(unsigned int /*lvl*/, const char */*hdr*/,
186 const void */*blk*/, size_t /*sz*/);
188 /* --- @traceon@ --- *
190 * Arguments: @FILE *fp@ = a file to trace on
191 * @unsigned int lvl@ = trace level to set
195 * Use: Enables tracing to a file.
198 extern void traceon(FILE */*fp*/, unsigned int /*lvl*/);
200 /* --- @tracesetlvl@ --- *
202 * Arguments: @unsigned int lvl@ = trace level to set
206 * Use: Sets the tracing level.
209 extern void tracesetlvl(unsigned int /*lvl*/);
211 /* --- @tracing@ --- *
215 * Returns: Zero if not tracing, tracing level if tracing.
217 * Use: Informs the caller whether tracing is enabled.
220 extern unsigned int tracing(void);
224 /* --- Some hacky macros --- */
228 # define IF_TRACING(lvl, x) if ((lvl) & tracing()) x
231 # define IF_TRACING(lvl, x)
234 /*----- Memory management functions ---------------------------------------*/
236 /* --- @xmalloc@ --- *
238 * Arguments: @size_t sz@ = size of block to allocate
240 * Returns: Pointer to allocated block.
242 * Use: Allocates memory. If the memory isn't available, we don't
243 * hang aroung long enough for a normal function return.
246 extern void *xmalloc(size_t /*sz*/);
248 /* --- @xstrdup@ --- *
250 * Arguments: @const char *s@ = pointer to a string
252 * Returns: Pointer to a copy of the string.
254 * Use: Copies a string (like @strdup@ would, if it existed).
257 extern char *xstrdup(const char */*s*/);
259 /* --- @xrealloc@ --- *
261 * Arguments: @void *p@ = pointer to a block of memory
262 * @size_t sz@ = new size desired for the block
264 * Returns: Pointer to the resized memory block (which is almost
265 * certainly not in the same place any more).
267 * Use: Resizes a memory block.
270 extern void *xrealloc(void */*p*/, size_t /*sz*/);
272 /*----- Simple memory use tracking ----------------------------------------*/
278 /* --- @track_malloc@ --- *
280 * Arguments: @size_t sz@ = size requested
282 * Returns: Pointer to allocated space, or null
284 * Use: Allocates memory, and tracks how much is allocated.
287 extern void *track_malloc(size_t /*sz*/);
289 /* --- @track_free@ --- *
291 * Arguments: @void *p@ = pointer to an allocated block
295 * Use: Frees memory, and tracks how much is still allocated.
298 extern void track_free(void */*p*/);
300 /* --- @track_realloc@ --- *
302 * Arguments: @void *p@ = pointer to an allocated block
303 * @size_t sz@ = how big it wants to be
305 * Returns: Pointer to the new block.
307 * Use: Reallocates a block, tracking how much memory is still
311 extern void *track_realloc(void */*p*/, size_t /*sz*/);
313 /* --- @track_memused@ --- *
317 * Returns: A count of how much memory is used currently.
319 * Use: Returns the amount of memory which the @track_@-functions
320 * above have counted as being currently allocated.
323 extern unsigned long track_memused(void);
325 /* --- @track_memlist@ --- *
331 * Use: Dumps a list of allocated blocks to standard output.
334 extern void track_memlist(void);
337 #define malloc(sz) track_malloc(sz)
340 #define free(p) track_free(p)
343 #define realloc(p, sz) track_realloc(p, sz)
347 /*----- That's all, folks -------------------------------------------------*/