3 * $Id: utils.h,v 1.3 1997/08/20 16:25:37 mdw Exp $
5 * Miscellaneous useful bits of code.
7 * (c) 1997 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.3 1997/08/20 16:25:37 mdw
33 * Add some simple `malloc' tracking.
35 * Revision 1.2 1997/08/04 10:24:26 mdw
36 * Sources placed under CVS control.
38 * Revision 1.1 1997/07/21 13:47:42 mdw
50 /*----- Required header files ---------------------------------------------*/
55 /*----- Storing and retrieving numbers ------------------------------------*
57 * These use big-endian conventions, because they seem more usual in network
58 * applications. I actually think that little-endian is more sensible...
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))
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)
73 /* --- Little-endian versions (for MD5) --- */
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))
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)
87 /*----- Other macros ------------------------------------------------------*/
91 * Arguments: @obj@ = some object
93 * Use: Writes zero bytes over the object.
96 #define burn(obj) ((void)memset(&obj, 0, sizeof(obj)))
98 /*----- Program name handling ---------------------------------------------*/
104 * Returns: Pointer to the program name.
106 * Use: Returns the program name.
109 extern const char *quis(void);
113 * Arguments: @const char *p@ = pointer to program name
117 * Use: Tells the utils library what the program's name is.
120 extern void ego(const char */
*p*/
);
122 /*----- Error reporting ---------------------------------------------------*/
126 * Arguments: @const char *f@ = a @printf@-style format string
127 * @...@ = other arguments
131 * Use: Reports an error.
134 extern void moan(const char */
*f*/
, ...);
138 * Arguments: @const char *f@ = a @printf@-style format string
139 * @...@ = other arguments
143 * Use: Reports an error and hari-kiris. Like @moan@ above, only
147 extern void die(const char */
*f*/
, ...);
149 /*----- Trace messages ----------------------------------------------------*/
151 #if !defined(NDEBUG) && !defined(TRACING)
159 * Arguments: @unsigned int lvl@ = trace level for output
160 * @const char *f@ = a @printf@-style format string
161 * @...@ = other arguments
165 * Use: Reports a message to the trace output.
168 extern void trace(unsigned int /*lvl*/, const char */
*f*/
, ...);
170 /* --- @traceblk@ --- *
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
179 * Use: Dumps the contents of a block to the trace output.
182 extern void traceblk(unsigned int /*lvl*/, const char */
*hdr*/
,
183 const void */
*blk*/
, size_t /*sz*/);
185 /* --- @traceon@ --- *
187 * Arguments: @FILE *fp@ = a file to trace on
188 * @unsigned int lvl@ = trace level to set
192 * Use: Enables tracing to a file.
195 extern void traceon(FILE */
*fp*/
, unsigned int /*lvl*/);
197 /* --- @tracesetlvl@ --- *
199 * Arguments: @unsigned int lvl@ = trace level to set
203 * Use: Sets the tracing level.
206 extern void tracesetlvl(unsigned int /*lvl*/);
208 /* --- @tracing@ --- *
212 * Returns: Zero if not tracing, tracing level if tracing.
214 * Use: Informs the caller whether tracing is enabled.
217 extern unsigned int tracing(void);
221 /* --- Some hacky macros --- */
225 # define IF_TRACING(lvl, x) if ((lvl) & tracing()) x
228 # define IF_TRACING(lvl, x)
231 /*----- Memory management functions ---------------------------------------*/
233 /* --- @xmalloc@ --- *
235 * Arguments: @size_t sz@ = size of block to allocate
237 * Returns: Pointer to allocated block.
239 * Use: Allocates memory. If the memory isn't available, we don't
240 * hang aroung long enough for a normal function return.
243 extern void *xmalloc(size_t /*sz*/);
245 /* --- @xstrdup@ --- *
247 * Arguments: @const char *s@ = pointer to a string
249 * Returns: Pointer to a copy of the string.
251 * Use: Copies a string (like @strdup@ would, if it existed).
254 extern char *xstrdup(const char */
*s*/
);
256 /* --- @xrealloc@ --- *
258 * Arguments: @void *p@ = pointer to a block of memory
259 * @size_t sz@ = new size desired for the block
261 * Returns: Pointer to the resized memory block (which is almost
262 * certainly not in the same place any more).
264 * Use: Resizes a memory block.
267 extern void *xrealloc(void */
*p*/
, size_t /*sz*/);
269 /*----- Simple memory use tracking ----------------------------------------*/
275 /* --- @track_malloc@ --- *
277 * Arguments: @size_t sz@ = size requested
279 * Returns: Pointer to allocated space, or null
281 * Use: Allocates memory, and tracks how much is allocated.
284 extern void *track_malloc(size_t /*sz*/);
286 /* --- @track_free@ --- *
288 * Arguments: @void *p@ = pointer to an allocated block
292 * Use: Frees memory, and tracks how much is still allocated.
295 extern void track_free(void */
*p*/
);
297 /* --- @track_realloc@ --- *
299 * Arguments: @void *p@ = pointer to an allocated block
300 * @size_t sz@ = how big it wants to be
302 * Returns: Pointer to the new block.
304 * Use: Reallocates a block, tracking how much memory is still
308 extern void *track_realloc(void */
*p*/
, size_t /*sz*/);
310 /* --- @track_memused@ --- *
314 * Returns: A count of how much memory is used currently.
316 * Use: Returns the amount of memory which the @track_@-functions
317 * above have counted as being currently allocated.
320 extern unsigned long track_memused(void);
322 /* --- @track_memlist@ --- *
328 * Use: Dumps a list of allocated blocks to standard output.
331 extern void track_memlist(void);
334 #define malloc(sz) track_malloc(sz)
337 #define free(p) track_free(p)
340 #define realloc(p, sz) track_realloc(p, sz)
344 /*----- That's all, folks -------------------------------------------------*/