1/* $NetBSD: defs.h,v 1.4 2006/04/17 16:44:42 thorpej 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 <sys/types.h> 34#include <sys/param.h> 35 36#ifdef HAVE_STDINT_H 37#include <stdint.h> 38#endif 39 40#include <stdio.h> 41#include <stdlib.h> 42#include <string.h> 43 44#define NEWARRAY(type,ptr,size,where,action) do { \ 45 if ((ptr = (type *) calloc(sizeof(type), (unsigned)(size))) == NULL) { \ 46 (void) fprintf(stderr, "%s: can't allocate %lu bytes\n", \ 47 where, (unsigned long)(size * sizeof(type))); \ 48 action; \ 49 } \ 50} while( /* CONSTCOND */ 0) 51 52#define RENEW(type,ptr,size,where,action) do { \ 53 type *_newptr; \ 54 if ((_newptr = (type *) realloc(ptr, sizeof(type) * (size))) == NULL) { \ 55 (void) fprintf(stderr, "%s: can't realloc %lu bytes\n", \ 56 where, (unsigned long)(size * sizeof(type))); \ 57 action; \ 58 } else { \ 59 ptr = _newptr; \ 60 } \ 61} while( /* CONSTCOND */ 0) 62 63#define NEW(type, ptr, where, action) NEWARRAY(type, ptr, 1, where, action) 64 65#define FREE(ptr) (void) free(ptr) 66 67#define ALLOC(type, v, size, c, init, incr, where, action) do { \ 68 uint32_t _newsize = size; \ 69 if (size == 0) { \ 70 _newsize = init; \ 71 NEWARRAY(type, v, _newsize, where ": new", action); \ 72 } else if (c == size) { \ 73 _newsize = size + incr; \ 74 RENEW(type, v, _newsize, where ": renew", action); \ 75 } \ 76 size = _newsize; \ 77} while( /* CONSTCOND */ 0) 78 79/* (void) memset(&v[size], 0x0, sizeof(type) * incr); \*/ 80 81#define DEFINE_ARRAY(name, type) \ 82typedef struct name { \ 83 uint32_t c; \ 84 uint32_t size; \ 85 type *v; \ 86} name 87 88#endif /* !DEFS_H_ */ 89