memalloc.c revision 1.22 1 /* $NetBSD: memalloc.c,v 1.22 2000/10/21 04:37:17 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)memalloc.c 8.3 (Berkeley) 5/4/95";
43 #else
44 __RCSID("$NetBSD: memalloc.c,v 1.22 2000/10/21 04:37:17 mycroft Exp $");
45 #endif
46 #endif /* not lint */
47
48 #include <stdlib.h>
49 #include <unistd.h>
50
51 #include "shell.h"
52 #include "output.h"
53 #include "memalloc.h"
54 #include "error.h"
55 #include "machdep.h"
56 #include "mystring.h"
57
58 /*
59 * Like malloc, but returns an error when out of space.
60 */
61
62 pointer
63 ckmalloc(nbytes)
64 int nbytes;
65 {
66 pointer p;
67
68 INTOFF;
69 p = malloc(nbytes);
70 INTON;
71 if (p == NULL)
72 error("Out of space");
73 return p;
74 }
75
76
77 /*
78 * Same for realloc.
79 */
80
81 pointer
82 ckrealloc(p, nbytes)
83 pointer p;
84 int nbytes;
85 {
86
87 if ((p = realloc(p, nbytes)) == NULL)
88 error("Out of space");
89 return p;
90 }
91
92
93 /*
94 * Make a copy of a string in safe storage.
95 */
96
97 char *
98 savestr(s)
99 char *s;
100 {
101 char *p;
102
103 p = ckmalloc(strlen(s) + 1);
104 scopy(s, p);
105 return p;
106 }
107
108
109 /*
110 * Parse trees for commands are allocated in lifo order, so we use a stack
111 * to make this more efficient, and also to avoid all sorts of exception
112 * handling code to handle interrupts in the middle of a parse.
113 *
114 * The size 504 was chosen because the Ultrix malloc handles that size
115 * well.
116 */
117
118 #define MINSIZE 504 /* minimum size of a block */
119
120
121 struct stack_block {
122 struct stack_block *prev;
123 char space[MINSIZE];
124 };
125
126 struct stack_block stackbase;
127 struct stack_block *stackp = &stackbase;
128 char *stacknxt = stackbase.space;
129 int stacknleft = MINSIZE;
130 int sstrnleft;
131 int herefd = -1;
132
133
134
135 pointer
136 stalloc(nbytes)
137 int nbytes;
138 {
139 char *p;
140
141 nbytes = ALIGN(nbytes);
142 if (nbytes > stacknleft) {
143 int blocksize;
144 struct stack_block *sp;
145
146 blocksize = nbytes;
147 if (blocksize < MINSIZE)
148 blocksize = MINSIZE;
149 INTOFF;
150 sp = ckmalloc(sizeof(struct stack_block) - MINSIZE + blocksize);
151 sp->prev = stackp;
152 stacknxt = sp->space;
153 stacknleft = blocksize;
154 stackp = sp;
155 INTON;
156 }
157 p = stacknxt;
158 stacknxt += nbytes;
159 stacknleft -= nbytes;
160 return p;
161 }
162
163
164 void
165 stunalloc(p)
166 pointer p;
167 {
168 if (p == NULL) { /*DEBUG */
169 write(2, "stunalloc\n", 10);
170 abort();
171 }
172 stacknleft += stacknxt - (char *)p;
173 stacknxt = p;
174 }
175
176
177
178 void
179 setstackmark(mark)
180 struct stackmark *mark;
181 {
182 mark->stackp = stackp;
183 mark->stacknxt = stacknxt;
184 mark->stacknleft = stacknleft;
185 }
186
187
188 void
189 popstackmark(mark)
190 struct stackmark *mark;
191 {
192 struct stack_block *sp;
193
194 INTOFF;
195 while (stackp != mark->stackp) {
196 sp = stackp;
197 stackp = sp->prev;
198 ckfree(sp);
199 }
200 stacknxt = mark->stacknxt;
201 stacknleft = mark->stacknleft;
202 INTON;
203 }
204
205
206 /*
207 * When the parser reads in a string, it wants to stick the string on the
208 * stack and only adjust the stack pointer when it knows how big the
209 * string is. Stackblock (defined in stack.h) returns a pointer to a block
210 * of space on top of the stack and stackblocklen returns the length of
211 * this block. Growstackblock will grow this space by at least one byte,
212 * possibly moving it (like realloc). Grabstackblock actually allocates the
213 * part of the block that has been used.
214 */
215
216 void
217 growstackblock() {
218 char *p;
219 int newlen = ALIGN(stacknleft * 2 + 100);
220 char *oldspace = stacknxt;
221 int oldlen = stacknleft;
222 struct stack_block *sp;
223
224 if (stacknxt == stackp->space && stackp != &stackbase) {
225 INTOFF;
226 sp = stackp;
227 stackp = sp->prev;
228 sp = ckrealloc((pointer)sp, sizeof(struct stack_block) - MINSIZE + newlen);
229 sp->prev = stackp;
230 stackp = sp;
231 stacknxt = sp->space;
232 stacknleft = newlen;
233 INTON;
234 } else {
235 p = stalloc(newlen);
236 memcpy(p, oldspace, oldlen);
237 stacknxt = p; /* free the space */
238 stacknleft += newlen; /* we just allocated */
239 }
240 }
241
242
243
244 void
245 grabstackblock(len)
246 int len;
247 {
248 len = ALIGN(len);
249 stacknxt += len;
250 stacknleft -= len;
251 }
252
253
254
255 /*
256 * The following routines are somewhat easier to use that the above.
257 * The user declares a variable of type STACKSTR, which may be declared
258 * to be a register. The macro STARTSTACKSTR initializes things. Then
259 * the user uses the macro STPUTC to add characters to the string. In
260 * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
261 * grown as necessary. When the user is done, she can just leave the
262 * string there and refer to it using stackblock(). Or she can allocate
263 * the space for it using grabstackstr(). If it is necessary to allow
264 * someone else to use the stack temporarily and then continue to grow
265 * the string, the user should use grabstack to allocate the space, and
266 * then call ungrabstr(p) to return to the previous mode of operation.
267 *
268 * USTPUTC is like STPUTC except that it doesn't check for overflow.
269 * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
270 * is space for at least one character.
271 */
272
273
274 char *
275 growstackstr() {
276 int len = stackblocksize();
277 if (herefd >= 0 && len >= 1024) {
278 xwrite(herefd, stackblock(), len);
279 sstrnleft = len - 1;
280 return stackblock();
281 }
282 growstackblock();
283 sstrnleft = stackblocksize() - len - 1;
284 return stackblock() + len;
285 }
286
287
288 /*
289 * Called from CHECKSTRSPACE.
290 */
291
292 char *
293 makestrspace() {
294 int len = stackblocksize() - sstrnleft;
295 growstackblock();
296 sstrnleft = stackblocksize() - len;
297 return stackblock() + len;
298 }
299
300
301
302 void
303 ungrabstackstr(s, p)
304 char *s;
305 char *p;
306 {
307 stacknleft += stacknxt - s;
308 stacknxt = s;
309 sstrnleft = stacknleft - (p - s);
310 }
311