1*5e01dafbSagc /* $NetBSD: defs.h,v 1.3 2009/06/30 02:44:52 agc Exp $ */ 22f245829Sagc 32f245829Sagc /* 42f245829Sagc * Copyright (c) 1999-2005 Alistair Crooks. All rights reserved. 52f245829Sagc * 62f245829Sagc * Redistribution and use in source and binary forms, with or without 72f245829Sagc * modification, are permitted provided that the following conditions 82f245829Sagc * are met: 92f245829Sagc * 1. Redistributions of source code must retain the above copyright 102f245829Sagc * notice, this list of conditions and the following disclaimer. 112f245829Sagc * 2. Redistributions in binary form must reproduce the above copyright 122f245829Sagc * notice, this list of conditions and the following disclaimer in the 132f245829Sagc * documentation and/or other materials provided with the distribution. 142f245829Sagc * 3. The name of the author may not be used to endorse or promote 152f245829Sagc * products derived from this software without specific prior written 162f245829Sagc * permission. 172f245829Sagc * 182f245829Sagc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 192f245829Sagc * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 202f245829Sagc * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 212f245829Sagc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 222f245829Sagc * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 232f245829Sagc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 242f245829Sagc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 252f245829Sagc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 262f245829Sagc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 272f245829Sagc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 282f245829Sagc * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 292f245829Sagc */ 302f245829Sagc #ifndef DEFS_H_ 312f245829Sagc #define DEFS_H_ 322f245829Sagc 332f245829Sagc #include "config.h" 342f245829Sagc 352f245829Sagc #include <sys/types.h> 362f245829Sagc #include <sys/param.h> 372f245829Sagc 382f245829Sagc #ifdef HAVE_STDINT_H 392f245829Sagc #include <stdint.h> 402f245829Sagc #endif 412f245829Sagc 422f245829Sagc #include <stdio.h> 432f245829Sagc #include <stdlib.h> 442f245829Sagc #include <string.h> 452f245829Sagc 462f245829Sagc #define NEWARRAY(type,ptr,size,where,action) do { \ 472f245829Sagc if ((ptr = (type *) calloc(sizeof(type), (unsigned)(size))) == NULL) { \ 482f245829Sagc (void) fprintf(stderr, "%s: can't allocate %lu bytes\n", \ 492f245829Sagc where, (unsigned long)(size * sizeof(type))); \ 502f245829Sagc action; \ 512f245829Sagc } \ 522f245829Sagc } while( /* CONSTCOND */ 0) 532f245829Sagc 542f245829Sagc #define RENEW(type,ptr,size,where,action) do { \ 552f245829Sagc type *_newptr; \ 562f245829Sagc if ((_newptr = (type *) realloc(ptr, sizeof(type) * (size))) == NULL) { \ 572f245829Sagc (void) fprintf(stderr, "%s: can't realloc %lu bytes\n", \ 582f245829Sagc where, (unsigned long)(size * sizeof(type))); \ 592f245829Sagc action; \ 602f245829Sagc } else { \ 612f245829Sagc ptr = _newptr; \ 622f245829Sagc } \ 632f245829Sagc } while( /* CONSTCOND */ 0) 642f245829Sagc 652f245829Sagc #define NEW(type, ptr, where, action) NEWARRAY(type, ptr, 1, where, action) 662f245829Sagc 672f245829Sagc #define FREE(ptr) (void) free(ptr) 682f245829Sagc 692f245829Sagc #define ALLOC(type, v, size, c, init, incr, where, action) do { \ 702f245829Sagc uint32_t _newsize = size; \ 712f245829Sagc if (size == 0) { \ 722f245829Sagc _newsize = init; \ 732f245829Sagc NEWARRAY(type, v, _newsize, where ": new", action); \ 742f245829Sagc } else if (c == size) { \ 752f245829Sagc _newsize = size + incr; \ 762f245829Sagc RENEW(type, v, _newsize, where ": renew", action); \ 772f245829Sagc } \ 782f245829Sagc size = _newsize; \ 792f245829Sagc } while( /* CONSTCOND */ 0) 802f245829Sagc 81b6364952Sagc #define USE_ARG(x) /*LINTED*/(void)&(x) 822f245829Sagc 832f245829Sagc #define DEFINE_ARRAY(name, type) \ 842f245829Sagc typedef struct name { \ 852f245829Sagc uint32_t c; \ 862f245829Sagc uint32_t size; \ 872f245829Sagc type *v; \ 882f245829Sagc } name 892f245829Sagc 902f245829Sagc #ifndef ABS 912f245829Sagc #define ABS(a) (((a) < 0) ? -(a) : (a)) 922f245829Sagc #endif 932f245829Sagc 942f245829Sagc #endif /* !DEFS_H_ */ 95