1*5b91f35fSagc /* $NetBSD: defs.h,v 1.2 2009/12/06 17:43:05 agc Exp $ */ 291c29c74Sagc 391c29c74Sagc /*- 491c29c74Sagc * Copyright (c) 2009 The NetBSD Foundation, Inc. 591c29c74Sagc * All rights reserved. 691c29c74Sagc * 791c29c74Sagc * This code is derived from software contributed to The NetBSD Foundation 891c29c74Sagc * by Alistair Crooks (agc@NetBSD.org) 991c29c74Sagc * 1091c29c74Sagc * Redistribution and use in source and binary forms, with or without 1191c29c74Sagc * modification, are permitted provided that the following conditions 1291c29c74Sagc * are met: 1391c29c74Sagc * 1. Redistributions of source code must retain the above copyright 1491c29c74Sagc * notice, this list of conditions and the following disclaimer. 1591c29c74Sagc * 2. Redistributions in binary form must reproduce the above copyright 1691c29c74Sagc * notice, this list of conditions and the following disclaimer in the 1791c29c74Sagc * documentation and/or other materials provided with the distribution. 1891c29c74Sagc * 1991c29c74Sagc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2091c29c74Sagc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2191c29c74Sagc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2291c29c74Sagc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2391c29c74Sagc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2491c29c74Sagc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2591c29c74Sagc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2691c29c74Sagc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2791c29c74Sagc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2891c29c74Sagc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2991c29c74Sagc * POSSIBILITY OF SUCH DAMAGE. 3091c29c74Sagc */ 3191c29c74Sagc #ifndef DEFS_H_ 3291c29c74Sagc #define DEFS_H_ 3391c29c74Sagc 3491c29c74Sagc #include <sys/types.h> 3591c29c74Sagc #include <sys/param.h> 3691c29c74Sagc 3791c29c74Sagc #ifdef HAVE_INTTYPES_H 3891c29c74Sagc #include <inttypes.h> 3991c29c74Sagc #endif 4091c29c74Sagc 4191c29c74Sagc #ifdef HAVE_STDINT_H 4291c29c74Sagc #include <stdint.h> 4391c29c74Sagc #endif 4491c29c74Sagc 4591c29c74Sagc #include <stdio.h> 4691c29c74Sagc #include <stdlib.h> 4791c29c74Sagc #include <string.h> 4891c29c74Sagc 4991c29c74Sagc #define NEWARRAY(type,ptr,size,where,action) do { \ 50*5b91f35fSagc if ((ptr = calloc(sizeof(type), (unsigned)(size))) == NULL) { \ 5191c29c74Sagc (void) fprintf(stderr, "%s: can't allocate %lu bytes\n", \ 5291c29c74Sagc where, (unsigned long)(size * sizeof(type))); \ 5391c29c74Sagc action; \ 5491c29c74Sagc } \ 5591c29c74Sagc } while( /* CONSTCOND */ 0) 5691c29c74Sagc 5791c29c74Sagc #define RENEW(type,ptr,size,where,action) do { \ 5891c29c74Sagc type *_newptr; \ 59*5b91f35fSagc _newptr = realloc(ptr, (size_t)(sizeof(type) * (size))); \ 60*5b91f35fSagc if (_newptr == NULL) { \ 6191c29c74Sagc (void) fprintf(stderr, "%s: can't realloc %lu bytes\n", \ 6291c29c74Sagc where, (unsigned long)(size * sizeof(type))); \ 6391c29c74Sagc action; \ 6491c29c74Sagc } else { \ 6591c29c74Sagc ptr = _newptr; \ 6691c29c74Sagc } \ 6791c29c74Sagc } while( /* CONSTCOND */ 0) 6891c29c74Sagc 6991c29c74Sagc #define NEW(type, ptr, where, action) NEWARRAY(type, ptr, 1, where, action) 7091c29c74Sagc 7191c29c74Sagc #define FREE(ptr) (void) free(ptr) 7291c29c74Sagc 7391c29c74Sagc #define ALLOC(type, v, size, c, init, incr, where, action) do { \ 7491c29c74Sagc uint32_t _newsize = size; \ 7591c29c74Sagc if (size == 0) { \ 7691c29c74Sagc _newsize = init; \ 7791c29c74Sagc NEWARRAY(type, v, _newsize, where ": new", action); \ 7891c29c74Sagc } else if (c == size) { \ 7991c29c74Sagc _newsize = size + incr; \ 8091c29c74Sagc RENEW(type, v, _newsize, where ": renew", action); \ 8191c29c74Sagc } \ 8291c29c74Sagc size = _newsize; \ 8391c29c74Sagc } while( /* CONSTCOND */ 0) 8491c29c74Sagc 8591c29c74Sagc #define DEFINE_ARRAY(name, type) \ 8691c29c74Sagc typedef struct name { \ 8791c29c74Sagc uint32_t c; \ 8891c29c74Sagc uint32_t size; \ 8991c29c74Sagc type *v; \ 9091c29c74Sagc } name 9191c29c74Sagc 9291c29c74Sagc #endif /* !DEFS_H_ */ 93