defs.h revision 1.2 1 1.2 simonb /* $NetBSD: defs.h,v 1.2 1999/12/06 22:50:46 simonb Exp $ */
2 1.1 agc
3 1.1 agc /*
4 1.1 agc * Copyright 1999 Alistair G. Crooks. All rights reserved.
5 1.1 agc *
6 1.1 agc * Redistribution and use in source and binary forms, with or without
7 1.1 agc * modification, are permitted provided that the following conditions
8 1.1 agc * are met:
9 1.1 agc * 1. Redistributions of source code must retain the above copyright
10 1.1 agc * notice, this list of conditions and the following disclaimer.
11 1.1 agc * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 agc * notice, this list of conditions and the following disclaimer in the
13 1.1 agc * documentation and/or other materials provided with the distribution.
14 1.1 agc * 3. All advertising materials mentioning features or use of this software
15 1.1 agc * must display the following acknowledgement:
16 1.1 agc * This product includes software developed by Alistair G. Crooks.
17 1.1 agc * 4. The name of the author may not be used to endorse or promote
18 1.1 agc * products derived from this software without specific prior written
19 1.1 agc * permission.
20 1.1 agc *
21 1.1 agc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 1.1 agc * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 1.1 agc * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 1.1 agc * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 1.1 agc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1 agc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.1 agc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 1.1 agc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 1.1 agc * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 agc */
33 1.1 agc #ifndef DEFS_H_
34 1.1 agc #define DEFS_H_
35 1.1 agc
36 1.1 agc #define NEWARRAY(type,ptr,size,action) do { \
37 1.1 agc if ((ptr = (type *) calloc(sizeof(type), size)) == (type *) NULL) { \
38 1.2 simonb warn("can't allocate %ld bytes", (long)(size * sizeof(type))); \
39 1.1 agc action; \
40 1.1 agc } \
41 1.1 agc } while( /* CONSTCOND */ 0)
42 1.1 agc
43 1.1 agc #define RENEW(type,ptr,size,action) do { \
44 1.1 agc if ((ptr = (type *) realloc(ptr, sizeof(type) * size)) == (type *) NULL) { \
45 1.2 simonb warn("can't realloc %ld bytes", (long)(size * sizeof(type))); \
46 1.1 agc action; \
47 1.1 agc } \
48 1.1 agc } while( /* CONSTCOND */ 0)
49 1.1 agc
50 1.1 agc #define NEW(type, ptr, action) NEWARRAY(type, ptr, 1, action)
51 1.1 agc
52 1.1 agc #define FREE(ptr) (void) free(ptr)
53 1.1 agc
54 1.1 agc #ifndef MIN
55 1.1 agc #define MIN(a,b) (((a) < (b)) ? (a) : (b))
56 1.1 agc #endif
57 1.1 agc
58 1.1 agc #ifndef MAX
59 1.1 agc #define MAX(a,b) (((a) > (b)) ? (a) : (b))
60 1.1 agc #endif
61 1.1 agc
62 1.1 agc #define STRNCPY(to, from, size) do { \
63 1.1 agc (void) strncpy(to, from, size); \
64 1.1 agc to[(size) - 1] = 0; \
65 1.1 agc } while( /* CONSTCOND */ 0)
66 1.1 agc
67 1.1 agc #endif /* !DEFS_H_ */
68