1 /* $NetBSD: defs.h,v 1.7 2011/08/31 16:25:00 plunky Exp $ */ 2 3 /* 4 * Copyright (c) 1999 Alistair G. 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 #define NEWARRAY(type,ptr,size,action) do { \ 34 if ((ptr = (type *) calloc(sizeof(type), size)) == NULL) { \ 35 warn("can't allocate %ld bytes", (long)(size * sizeof(type))); \ 36 action; \ 37 } \ 38 } while( /* CONSTCOND */ 0) 39 40 #define RENEW(type,ptr,size,action) do { \ 41 if ((ptr = (type *) realloc(ptr, sizeof(type) * size)) == NULL) { \ 42 warn("can't realloc %ld bytes", (long)(size * sizeof(type))); \ 43 action; \ 44 } \ 45 } while( /* CONSTCOND */ 0) 46 47 #define NEW(type, ptr, action) NEWARRAY(type, ptr, 1, action) 48 49 #define FREE(ptr) (void) free(ptr) 50 51 #ifndef MIN 52 #define MIN(a,b) (((a) < (b)) ? (a) : (b)) 53 #endif 54 55 #ifndef MAX 56 #define MAX(a,b) (((a) > (b)) ? (a) : (b)) 57 #endif 58 59 #endif /* !DEFS_H_ */ 60