defs.h revision 1.1 1 1.1 agc /* $NetBSD: defs.h,v 1.1 2007/04/15 15:22:44 agc Exp $ */
2 1.1 agc
3 1.1 agc /*
4 1.1 agc * Copyright (c) 1999-2005 Alistair 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. The name of the author may not be used to endorse or promote
15 1.1 agc * products derived from this software without specific prior written
16 1.1 agc * permission.
17 1.1 agc *
18 1.1 agc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 1.1 agc * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 1.1 agc * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 1.1 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 1.1 agc * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 1.1 agc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.1 agc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 1.1 agc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 1.1 agc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 1.1 agc * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.1 agc */
30 1.1 agc #ifndef DEFS_H_
31 1.1 agc #define DEFS_H_
32 1.1 agc
33 1.1 agc #include <sys/types.h>
34 1.1 agc #include <sys/param.h>
35 1.1 agc
36 1.1 agc #ifdef HAVE_STDINT_H
37 1.1 agc #include <stdint.h>
38 1.1 agc #endif
39 1.1 agc
40 1.1 agc #include <stdio.h>
41 1.1 agc #include <stdlib.h>
42 1.1 agc #include <string.h>
43 1.1 agc
44 1.1 agc #define NEWARRAY(type,ptr,size,where,action) do { \
45 1.1 agc if ((ptr = (type *) calloc(sizeof(type), (unsigned)(size))) == NULL) { \
46 1.1 agc (void) fprintf(stderr, "%s: can't allocate %lu bytes\n", \
47 1.1 agc where, (unsigned long)(size * sizeof(type))); \
48 1.1 agc action; \
49 1.1 agc } \
50 1.1 agc } while( /* CONSTCOND */ 0)
51 1.1 agc
52 1.1 agc #define RENEW(type,ptr,size,where,action) do { \
53 1.1 agc type *_newptr; \
54 1.1 agc if ((_newptr = (type *) realloc(ptr, sizeof(type) * (size))) == NULL) { \
55 1.1 agc (void) fprintf(stderr, "%s: can't realloc %lu bytes\n", \
56 1.1 agc where, (unsigned long)(size * sizeof(type))); \
57 1.1 agc action; \
58 1.1 agc } else { \
59 1.1 agc ptr = _newptr; \
60 1.1 agc } \
61 1.1 agc } while( /* CONSTCOND */ 0)
62 1.1 agc
63 1.1 agc #define NEW(type, ptr, where, action) NEWARRAY(type, ptr, 1, where, action)
64 1.1 agc
65 1.1 agc #define FREE(ptr) (void) free(ptr)
66 1.1 agc
67 1.1 agc #define ALLOC(type, v, size, c, init, incr, where, action) do { \
68 1.1 agc uint32_t _newsize = size; \
69 1.1 agc if (size == 0) { \
70 1.1 agc _newsize = init; \
71 1.1 agc NEWARRAY(type, v, _newsize, where ": new", action); \
72 1.1 agc } else if (c == size) { \
73 1.1 agc _newsize = size + incr; \
74 1.1 agc RENEW(type, v, _newsize, where ": renew", action); \
75 1.1 agc } \
76 1.1 agc size = _newsize; \
77 1.1 agc } while( /* CONSTCOND */ 0)
78 1.1 agc
79 1.1 agc /* (void) memset(&v[size], 0x0, sizeof(type) * incr); \*/
80 1.1 agc
81 1.1 agc #define DEFINE_ARRAY(name, type) \
82 1.1 agc typedef struct name { \
83 1.1 agc uint32_t c; \
84 1.1 agc uint32_t size; \
85 1.1 agc type *v; \
86 1.1 agc } name
87 1.1 agc
88 1.1 agc #endif /* !DEFS_H_ */
89