xref: /netbsd-src/bin/sh/memalloc.c (revision 37b34d511dea595d3ba03a661cf3b775038ea5f8)
1 /*	$NetBSD: memalloc.c,v 1.25 2002/10/07 14:26:49 christos 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.25 2002/10/07 14:26:49 christos 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 	p = malloc(nbytes);
69 	if (p == NULL)
70 		error("Out of space");
71 	return p;
72 }
73 
74 
75 /*
76  * Same for realloc.
77  */
78 
79 pointer
80 ckrealloc(p, nbytes)
81 	pointer p;
82 	int nbytes;
83 {
84 	p = realloc(p, nbytes);
85 	if (p == NULL)
86 		error("Out of space");
87 	return p;
88 }
89 
90 
91 /*
92  * Make a copy of a string in safe storage.
93  */
94 
95 char *
96 savestr(s)
97 	char *s;
98 {
99 	char *p;
100 
101 	p = ckmalloc(strlen(s) + 1);
102 	scopy(s, p);
103 	return p;
104 }
105 
106 
107 /*
108  * Parse trees for commands are allocated in lifo order, so we use a stack
109  * to make this more efficient, and also to avoid all sorts of exception
110  * handling code to handle interrupts in the middle of a parse.
111  *
112  * The size 504 was chosen because the Ultrix malloc handles that size
113  * well.
114  */
115 
116 #define MINSIZE 504		/* minimum size of a block */
117 
118 struct stack_block {
119 	struct stack_block *prev;
120 	char space[MINSIZE];
121 };
122 
123 struct stack_block stackbase;
124 struct stack_block *stackp = &stackbase;
125 struct stackmark *markp;
126 char *stacknxt = stackbase.space;
127 int stacknleft = MINSIZE;
128 int sstrnleft;
129 int herefd = -1;
130 
131 pointer
132 stalloc(nbytes)
133 	int nbytes;
134 {
135 	char *p;
136 
137 	nbytes = SHELL_ALIGN(nbytes);
138 	if (nbytes > stacknleft) {
139 		int blocksize;
140 		struct stack_block *sp;
141 
142 		blocksize = nbytes;
143 		if (blocksize < MINSIZE)
144 			blocksize = MINSIZE;
145 		INTOFF;
146 		sp = ckmalloc(sizeof(struct stack_block) - MINSIZE + blocksize);
147 		sp->prev = stackp;
148 		stacknxt = sp->space;
149 		stacknleft = blocksize;
150 		stackp = sp;
151 		INTON;
152 	}
153 	p = stacknxt;
154 	stacknxt += nbytes;
155 	stacknleft -= nbytes;
156 	return p;
157 }
158 
159 
160 void
161 stunalloc(p)
162 	pointer p;
163 {
164 	if (p == NULL) {		/*DEBUG */
165 		write(2, "stunalloc\n", 10);
166 		abort();
167 	}
168 	stacknleft += stacknxt - (char *)p;
169 	stacknxt = p;
170 }
171 
172 
173 
174 void
175 setstackmark(mark)
176 	struct stackmark *mark;
177 {
178 	mark->stackp = stackp;
179 	mark->stacknxt = stacknxt;
180 	mark->stacknleft = stacknleft;
181 	mark->marknext = markp;
182 	markp = mark;
183 }
184 
185 
186 void
187 popstackmark(mark)
188 	struct stackmark *mark;
189 {
190 	struct stack_block *sp;
191 
192 	INTOFF;
193 	markp = mark->marknext;
194 	while (stackp != mark->stackp) {
195 		sp = stackp;
196 		stackp = sp->prev;
197 		ckfree(sp);
198 	}
199 	stacknxt = mark->stacknxt;
200 	stacknleft = mark->stacknleft;
201 	INTON;
202 }
203 
204 
205 /*
206  * When the parser reads in a string, it wants to stick the string on the
207  * stack and only adjust the stack pointer when it knows how big the
208  * string is.  Stackblock (defined in stack.h) returns a pointer to a block
209  * of space on top of the stack and stackblocklen returns the length of
210  * this block.  Growstackblock will grow this space by at least one byte,
211  * possibly moving it (like realloc).  Grabstackblock actually allocates the
212  * part of the block that has been used.
213  */
214 
215 void
216 growstackblock()
217 {
218 	int newlen = SHELL_ALIGN(stacknleft * 2 + 100);
219 
220 	if (stacknxt == stackp->space && stackp != &stackbase) {
221 		struct stack_block *oldstackp;
222 		struct stackmark *xmark;
223 		struct stack_block *sp;
224 
225 		INTOFF;
226 		oldstackp = stackp;
227 		sp = stackp;
228 		stackp = sp->prev;
229 		sp = ckrealloc((pointer)sp,
230 		    sizeof(struct stack_block) - MINSIZE + newlen);
231 		sp->prev = stackp;
232 		stackp = sp;
233 		stacknxt = sp->space;
234 		stacknleft = newlen;
235 
236 		/*
237 		 * Stack marks pointing to the start of the old block
238 		 * must be relocated to point to the new block
239 		 */
240 		xmark = markp;
241 		while (xmark != NULL && xmark->stackp == oldstackp) {
242 			xmark->stackp = stackp;
243 			xmark->stacknxt = stacknxt;
244 			xmark->stacknleft = stacknleft;
245 			xmark = xmark->marknext;
246 		}
247 		INTON;
248 	} else {
249 		char *oldspace = stacknxt;
250 		int oldlen = stacknleft;
251 		char *p = stalloc(newlen);
252 
253 		(void)memcpy(p, oldspace, oldlen);
254 		stacknxt = p;			/* free the space */
255 		stacknleft += newlen;		/* we just allocated */
256 	}
257 }
258 
259 void
260 grabstackblock(len)
261 	int len;
262 {
263 	len = SHELL_ALIGN(len);
264 	stacknxt += len;
265 	stacknleft -= len;
266 }
267 
268 /*
269  * The following routines are somewhat easier to use that the above.
270  * The user declares a variable of type STACKSTR, which may be declared
271  * to be a register.  The macro STARTSTACKSTR initializes things.  Then
272  * the user uses the macro STPUTC to add characters to the string.  In
273  * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
274  * grown as necessary.  When the user is done, she can just leave the
275  * string there and refer to it using stackblock().  Or she can allocate
276  * the space for it using grabstackstr().  If it is necessary to allow
277  * someone else to use the stack temporarily and then continue to grow
278  * the string, the user should use grabstack to allocate the space, and
279  * then call ungrabstr(p) to return to the previous mode of operation.
280  *
281  * USTPUTC is like STPUTC except that it doesn't check for overflow.
282  * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
283  * is space for at least one character.
284  */
285 
286 char *
287 growstackstr()
288 {
289 	int len = stackblocksize();
290 	if (herefd >= 0 && len >= 1024) {
291 		xwrite(herefd, stackblock(), len);
292 		sstrnleft = len - 1;
293 		return stackblock();
294 	}
295 	growstackblock();
296 	sstrnleft = stackblocksize() - len - 1;
297 	return stackblock() + len;
298 }
299 
300 /*
301  * Called from CHECKSTRSPACE.
302  */
303 
304 char *
305 makestrspace()
306 {
307 	int len = stackblocksize() - sstrnleft;
308 	growstackblock();
309 	sstrnleft = stackblocksize() - len;
310 	return stackblock() + len;
311 }
312 
313 void
314 ungrabstackstr(s, p)
315 	char *s;
316 	char *p;
317 {
318 	stacknleft += stacknxt - s;
319 	stacknxt = s;
320 	sstrnleft = stacknleft - (p - s);
321 
322 }
323