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