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