1 /* $NetBSD: defs.h,v 1.3 2009/06/30 02:44:52 agc Exp $ */ 2 3 /* 4 * Copyright (c) 1999-2005 Alistair Crooks. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote 15 * products derived from this software without specific prior written 16 * permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 24 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 #ifndef DEFS_H_ 31 #define DEFS_H_ 32 33 #include "config.h" 34 35 #include <sys/types.h> 36 #include <sys/param.h> 37 38 #ifdef HAVE_STDINT_H 39 #include <stdint.h> 40 #endif 41 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 46 #define NEWARRAY(type,ptr,size,where,action) do { \ 47 if ((ptr = (type *) calloc(sizeof(type), (unsigned)(size))) == NULL) { \ 48 (void) fprintf(stderr, "%s: can't allocate %lu bytes\n", \ 49 where, (unsigned long)(size * sizeof(type))); \ 50 action; \ 51 } \ 52 } while( /* CONSTCOND */ 0) 53 54 #define RENEW(type,ptr,size,where,action) do { \ 55 type *_newptr; \ 56 if ((_newptr = (type *) realloc(ptr, sizeof(type) * (size))) == NULL) { \ 57 (void) fprintf(stderr, "%s: can't realloc %lu bytes\n", \ 58 where, (unsigned long)(size * sizeof(type))); \ 59 action; \ 60 } else { \ 61 ptr = _newptr; \ 62 } \ 63 } while( /* CONSTCOND */ 0) 64 65 #define NEW(type, ptr, where, action) NEWARRAY(type, ptr, 1, where, action) 66 67 #define FREE(ptr) (void) free(ptr) 68 69 #define ALLOC(type, v, size, c, init, incr, where, action) do { \ 70 uint32_t _newsize = size; \ 71 if (size == 0) { \ 72 _newsize = init; \ 73 NEWARRAY(type, v, _newsize, where ": new", action); \ 74 } else if (c == size) { \ 75 _newsize = size + incr; \ 76 RENEW(type, v, _newsize, where ": renew", action); \ 77 } \ 78 size = _newsize; \ 79 } while( /* CONSTCOND */ 0) 80 81 #define USE_ARG(x) /*LINTED*/(void)&(x) 82 83 #define DEFINE_ARRAY(name, type) \ 84 typedef struct name { \ 85 uint32_t c; \ 86 uint32_t size; \ 87 type *v; \ 88 } name 89 90 #ifndef ABS 91 #define ABS(a) (((a) < 0) ? -(a) : (a)) 92 #endif 93 94 #endif /* !DEFS_H_ */ 95