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