xref: /netbsd-src/bin/sh/memalloc.c (revision fdecd6a253f999ae92b139670d9e15cc9df4497c)
1 /*	$NetBSD: memalloc.c,v 1.20 1997/07/04 21:02:08 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.20 1997/07/04 21:02:08 christos Exp $");
45 #endif
46 #endif /* not lint */
47 
48 #include "shell.h"
49 #include "output.h"
50 #include "memalloc.h"
51 #include "error.h"
52 #include "machdep.h"
53 #include "mystring.h"
54 #include <stdlib.h>
55 #include <unistd.h>
56 
57 /*
58  * Like malloc, but returns an error when out of space.
59  */
60 
61 pointer
62 ckmalloc(nbytes)
63 	int nbytes;
64 {
65 	pointer p;
66 
67 	if ((p = malloc(nbytes)) == NULL)
68 		error("Out of space");
69 	return p;
70 }
71 
72 
73 /*
74  * Same for realloc.
75  */
76 
77 pointer
78 ckrealloc(p, nbytes)
79 	pointer p;
80 	int nbytes;
81 {
82 
83 	if ((p = realloc(p, nbytes)) == NULL)
84 		error("Out of space");
85 	return p;
86 }
87 
88 
89 /*
90  * Make a copy of a string in safe storage.
91  */
92 
93 char *
94 savestr(s)
95 	char *s;
96 	{
97 	char *p;
98 
99 	p = ckmalloc(strlen(s) + 1);
100 	scopy(s, p);
101 	return p;
102 }
103 
104 
105 /*
106  * Parse trees for commands are allocated in lifo order, so we use a stack
107  * to make this more efficient, and also to avoid all sorts of exception
108  * handling code to handle interrupts in the middle of a parse.
109  *
110  * The size 504 was chosen because the Ultrix malloc handles that size
111  * well.
112  */
113 
114 #define MINSIZE 504		/* minimum size of a block */
115 
116 
117 struct stack_block {
118 	struct stack_block *prev;
119 	char space[MINSIZE];
120 };
121 
122 struct stack_block stackbase;
123 struct stack_block *stackp = &stackbase;
124 char *stacknxt = stackbase.space;
125 int stacknleft = MINSIZE;
126 int sstrnleft;
127 int herefd = -1;
128 
129 
130 
131 pointer
132 stalloc(nbytes)
133 	int nbytes;
134 {
135 	char *p;
136 
137 	nbytes = 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 }
182 
183 
184 void
185 popstackmark(mark)
186 	struct stackmark *mark;
187 	{
188 	struct stack_block *sp;
189 
190 	INTOFF;
191 	while (stackp != mark->stackp) {
192 		sp = stackp;
193 		stackp = sp->prev;
194 		ckfree(sp);
195 	}
196 	stacknxt = mark->stacknxt;
197 	stacknleft = mark->stacknleft;
198 	INTON;
199 }
200 
201 
202 /*
203  * When the parser reads in a string, it wants to stick the string on the
204  * stack and only adjust the stack pointer when it knows how big the
205  * string is.  Stackblock (defined in stack.h) returns a pointer to a block
206  * of space on top of the stack and stackblocklen returns the length of
207  * this block.  Growstackblock will grow this space by at least one byte,
208  * possibly moving it (like realloc).  Grabstackblock actually allocates the
209  * part of the block that has been used.
210  */
211 
212 void
213 growstackblock() {
214 	char *p;
215 	int newlen = ALIGN(stacknleft * 2 + 100);
216 	char *oldspace = stacknxt;
217 	int oldlen = stacknleft;
218 	struct stack_block *sp;
219 
220 	if (stacknxt == stackp->space && stackp != &stackbase) {
221 		INTOFF;
222 		sp = stackp;
223 		stackp = sp->prev;
224 		sp = ckrealloc((pointer)sp, sizeof(struct stack_block) - MINSIZE + newlen);
225 		sp->prev = stackp;
226 		stackp = sp;
227 		stacknxt = sp->space;
228 		stacknleft = newlen;
229 		INTON;
230 	} else {
231 		p = stalloc(newlen);
232 		memcpy(p, oldspace, oldlen);
233 		stacknxt = p;			/* free the space */
234 		stacknleft += newlen;		/* we just allocated */
235 	}
236 }
237 
238 
239 
240 void
241 grabstackblock(len)
242 	int len;
243 {
244 	len = ALIGN(len);
245 	stacknxt += len;
246 	stacknleft -= len;
247 }
248 
249 
250 
251 /*
252  * The following routines are somewhat easier to use that the above.
253  * The user declares a variable of type STACKSTR, which may be declared
254  * to be a register.  The macro STARTSTACKSTR initializes things.  Then
255  * the user uses the macro STPUTC to add characters to the string.  In
256  * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
257  * grown as necessary.  When the user is done, she can just leave the
258  * string there and refer to it using stackblock().  Or she can allocate
259  * the space for it using grabstackstr().  If it is necessary to allow
260  * someone else to use the stack temporarily and then continue to grow
261  * the string, the user should use grabstack to allocate the space, and
262  * then call ungrabstr(p) to return to the previous mode of operation.
263  *
264  * USTPUTC is like STPUTC except that it doesn't check for overflow.
265  * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
266  * is space for at least one character.
267  */
268 
269 
270 char *
271 growstackstr() {
272 	int len = stackblocksize();
273 	if (herefd >= 0 && len >= 1024) {
274 		xwrite(herefd, stackblock(), len);
275 		sstrnleft = len - 1;
276 		return stackblock();
277 	}
278 	growstackblock();
279 	sstrnleft = stackblocksize() - len - 1;
280 	return stackblock() + len;
281 }
282 
283 
284 /*
285  * Called from CHECKSTRSPACE.
286  */
287 
288 char *
289 makestrspace() {
290 	int len = stackblocksize() - sstrnleft;
291 	growstackblock();
292 	sstrnleft = stackblocksize() - len;
293 	return stackblock() + len;
294 }
295 
296 
297 
298 void
299 ungrabstackstr(s, p)
300 	char *s;
301 	char *p;
302 	{
303 	stacknleft += stacknxt - s;
304 	stacknxt = s;
305 	sstrnleft = stacknleft - (p - s);
306 }
307