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