Commit | Line | Data |
---|---|---|
1e64b3ba JH |
1 | /*- |
2 | * Copyright (c) 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | * | |
33 | * @(#)mem.h 8.2 (Berkeley) 12/19/93 | |
34 | */ | |
35 | ||
36 | /* Increase the size of a malloc'd buffer. Two versions, one that | |
37 | * returns, one that jumps to an error label. | |
38 | */ | |
39 | #define BINC_GOTO(sp, lp, llen, nlen) { \ | |
40 | if ((nlen) > llen && binc(sp, &(lp), &(llen), nlen)) \ | |
41 | goto binc_err; \ | |
42 | } | |
43 | #define BINC_RET(sp, lp, llen, nlen) { \ | |
44 | if ((nlen) > llen && binc(sp, &(lp), &(llen), nlen)) \ | |
45 | return (1); \ | |
46 | } | |
47 | ||
48 | /* | |
49 | * Get some temporary space, preferably from the global temporary buffer, | |
50 | * from a malloc'd buffer otherwise. Two versions, one that returns, one | |
51 | * that jumps to an error label. | |
52 | */ | |
53 | #define GET_SPACE_GOTO(sp, bp, blen, nlen) { \ | |
54 | GS *__gp = (sp)->gp; \ | |
55 | if (F_ISSET(__gp, G_TMP_INUSE)) { \ | |
56 | bp = NULL; \ | |
57 | blen = 0; \ | |
58 | BINC_GOTO(sp, bp, blen, nlen); \ | |
59 | } else { \ | |
60 | BINC_GOTO(sp, __gp->tmp_bp, __gp->tmp_blen, nlen); \ | |
61 | bp = __gp->tmp_bp; \ | |
62 | blen = __gp->tmp_blen; \ | |
63 | F_SET(__gp, G_TMP_INUSE); \ | |
64 | } \ | |
65 | } | |
66 | #define GET_SPACE_RET(sp, bp, blen, nlen) { \ | |
67 | GS *__gp = (sp)->gp; \ | |
68 | if (F_ISSET(__gp, G_TMP_INUSE)) { \ | |
69 | bp = NULL; \ | |
70 | blen = 0; \ | |
71 | BINC_RET(sp, bp, blen, nlen); \ | |
72 | } else { \ | |
73 | BINC_RET(sp, __gp->tmp_bp, __gp->tmp_blen, nlen); \ | |
74 | bp = __gp->tmp_bp; \ | |
75 | blen = __gp->tmp_blen; \ | |
76 | F_SET(__gp, G_TMP_INUSE); \ | |
77 | } \ | |
78 | } | |
79 | ||
80 | /* | |
81 | * Add space to a GET_SPACE returned buffer. Two versions, one that | |
82 | * returns, one that jumps to an error label. | |
83 | */ | |
84 | #define ADD_SPACE_GOTO(sp, bp, blen, nlen) { \ | |
85 | GS *__gp = (sp)->gp; \ | |
86 | if (bp == __gp->tmp_bp) { \ | |
87 | F_CLR(__gp, G_TMP_INUSE); \ | |
88 | BINC_GOTO(sp, __gp->tmp_bp, __gp->tmp_blen, nlen); \ | |
89 | bp = __gp->tmp_bp; \ | |
90 | blen = __gp->tmp_blen; \ | |
91 | F_SET(__gp, G_TMP_INUSE); \ | |
92 | } else \ | |
93 | BINC_GOTO(sp, bp, blen, nlen); \ | |
94 | } | |
95 | #define ADD_SPACE_RET(sp, bp, blen, nlen) { \ | |
96 | GS *__gp = (sp)->gp; \ | |
97 | if (bp == __gp->tmp_bp) { \ | |
98 | F_CLR(__gp, G_TMP_INUSE); \ | |
99 | BINC_RET(sp, __gp->tmp_bp, __gp->tmp_blen, nlen); \ | |
100 | bp = __gp->tmp_bp; \ | |
101 | blen = __gp->tmp_blen; \ | |
102 | F_SET(__gp, G_TMP_INUSE); \ | |
103 | } else \ | |
104 | BINC_RET(sp, bp, blen, nlen); \ | |
105 | } | |
106 | ||
107 | /* Free memory, optionally making pointers unusable. */ | |
108 | #ifdef DEBUG | |
109 | #define FREE(p, sz) { \ | |
110 | memset(p, 0xff, sz); \ | |
111 | free(p); \ | |
112 | } | |
113 | #else | |
114 | #define FREE(p, sz) free(p); | |
115 | #endif | |
116 | ||
117 | /* Free a GET_SPACE returned buffer. */ | |
118 | #define FREE_SPACE(sp, bp, blen) { \ | |
119 | if (bp == sp->gp->tmp_bp) \ | |
120 | F_CLR(sp->gp, G_TMP_INUSE); \ | |
121 | else \ | |
122 | FREE(bp, blen); \ | |
123 | } | |
124 | ||
125 | /* | |
126 | * Malloc a buffer, casting the return pointer. Various versions. | |
127 | * | |
128 | * !!! | |
129 | * The cast should be unnecessary, malloc(3) and friends return void *'s, | |
130 | * which is all we need. However, some systems that nvi needs to run on | |
131 | * don't do it right yet, resulting in the compiler printing out roughly | |
132 | * a million warnings. After awhile, it seemed easier to put the casts | |
133 | * in instead of explaining it all the time. | |
134 | */ | |
135 | #define CALLOC_NOMSG(sp, p, cast, nmemb, size) { \ | |
136 | p = (cast)calloc(nmemb, size); \ | |
137 | } | |
138 | #define CALLOC(sp, p, cast, nmemb, size) { \ | |
139 | if ((p = (cast)calloc(nmemb, size)) == NULL) \ | |
140 | msgq(sp, M_SYSERR, NULL); \ | |
141 | } | |
142 | #define CALLOC_RET(sp, p, cast, nmemb, size) { \ | |
143 | if ((p = (cast)calloc(nmemb, size)) == NULL) { \ | |
144 | msgq(sp, M_SYSERR, NULL); \ | |
145 | return (1); \ | |
146 | } \ | |
147 | } | |
148 | #define MALLOC_NOMSG(sp, p, cast, size) { \ | |
149 | p = (cast)malloc(size); \ | |
150 | } | |
151 | #define MALLOC(sp, p, cast, size) { \ | |
152 | if ((p = (cast)malloc(size)) == NULL) \ | |
153 | msgq(sp, M_SYSERR, NULL); \ | |
154 | } | |
155 | #define MALLOC_RET(sp, p, cast, size) { \ | |
156 | if ((p = (cast)malloc(size)) == NULL) { \ | |
157 | msgq(sp, M_SYSERR, NULL); \ | |
158 | return (1); \ | |
159 | } \ | |
160 | } | |
161 | #define REALLOC(sp, p, cast, size) { \ | |
162 | if ((p = (cast)realloc(p, size)) == NULL) \ | |
163 | msgq(sp, M_SYSERR, NULL); \ | |
164 | } | |
165 | ||
166 | int binc __P((SCR *, void *, size_t *, size_t)); |