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