xref: /freebsd/bin/sh/memalloc.c (revision 2ba1b30bf50f04ab147452cfba7a3b9899f1d59c)
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 #endif /* not lint */
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44 
45 #include <sys/param.h>
46 #include "shell.h"
47 #include "output.h"
48 #include "memalloc.h"
49 #include "error.h"
50 #include "mystring.h"
51 #include "expand.h"
52 #include <stdlib.h>
53 #include <unistd.h>
54 
55 /*
56  * Like malloc, but returns an error when out of space.
57  */
58 
59 pointer
60 ckmalloc(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(pointer p, int nbytes)
76 {
77 	if ((p = realloc(p, nbytes)) == NULL)
78 		error("Out of space");
79 	return p;
80 }
81 
82 
83 /*
84  * Make a copy of a string in safe storage.
85  */
86 
87 char *
88 savestr(char *s)
89 {
90 	char *p;
91 
92 	p = ckmalloc(strlen(s) + 1);
93 	scopy(s, p);
94 	return p;
95 }
96 
97 
98 /*
99  * Parse trees for commands are allocated in lifo order, so we use a stack
100  * to make this more efficient, and also to avoid all sorts of exception
101  * handling code to handle interrupts in the middle of a parse.
102  *
103  * The size 496 was chosen because with 16-byte alignment the total size
104  * for the allocated block is 512.
105  */
106 
107 #define MINSIZE 496		/* minimum size of a block. */
108 
109 
110 struct stack_block {
111 	struct stack_block *prev;
112 	/* Data follows */
113 };
114 #define SPACE(sp)	((char*)(sp) + ALIGN(sizeof(struct stack_block)))
115 
116 STATIC struct stack_block *stackp;
117 STATIC struct stackmark *markp;
118 char *stacknxt;
119 int stacknleft;
120 int sstrnleft;
121 int herefd = -1;
122 
123 
124 static void
125 stnewblock(int nbytes)
126 {
127 	struct stack_block *sp;
128 	int allocsize;
129 
130 	if (nbytes < MINSIZE)
131 		nbytes = MINSIZE;
132 
133 	allocsize = ALIGN(sizeof(struct stack_block)) + ALIGN(nbytes);
134 
135 	INTOFF;
136 	sp = ckmalloc(allocsize);
137 	sp->prev = stackp;
138 	stacknxt = SPACE(sp);
139 	stacknleft = allocsize - (stacknxt - (char*)sp);
140 	stackp = sp;
141 	INTON;
142 }
143 
144 
145 pointer
146 stalloc(int nbytes)
147 {
148 	char *p;
149 
150 	nbytes = ALIGN(nbytes);
151 	if (nbytes > stacknleft)
152 		stnewblock(nbytes);
153 	p = stacknxt;
154 	stacknxt += nbytes;
155 	stacknleft -= nbytes;
156 	return p;
157 }
158 
159 
160 void
161 stunalloc(pointer p)
162 {
163 	if (p == NULL) {		/*DEBUG */
164 		write(STDERR_FILENO, "stunalloc\n", 10);
165 		abort();
166 	}
167 	stacknleft += stacknxt - (char *)p;
168 	stacknxt = p;
169 }
170 
171 
172 
173 void
174 setstackmark(struct stackmark *mark)
175 {
176 	mark->stackp = stackp;
177 	mark->stacknxt = stacknxt;
178 	mark->stacknleft = stacknleft;
179 	mark->marknext = markp;
180 	markp = mark;
181 }
182 
183 
184 void
185 popstackmark(struct stackmark *mark)
186 {
187 	struct stack_block *sp;
188 
189 	INTOFF;
190 	markp = mark->marknext;
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(void)
214 {
215 	char *p;
216 	int newlen;
217 	char *oldspace;
218 	int oldlen;
219 	struct stack_block *sp;
220 	struct stack_block *oldstackp;
221 	struct stackmark *xmark;
222 
223 	newlen = (stacknleft == 0) ? MINSIZE : stacknleft * 2 + 100;
224 	newlen = ALIGN(newlen);
225 	oldspace = stacknxt;
226 	oldlen = stacknleft;
227 
228 	if (stackp != NULL && stacknxt == SPACE(stackp)) {
229 		INTOFF;
230 		oldstackp = stackp;
231 		stackp = oldstackp->prev;
232 		sp = ckrealloc((pointer)oldstackp, newlen);
233 		sp->prev = stackp;
234 		stackp = sp;
235 		stacknxt = SPACE(sp);
236 		stacknleft = newlen - (stacknxt - (char*)sp);
237 
238 		/*
239 		 * Stack marks pointing to the start of the old block
240 		 * must be relocated to point to the new block
241 		 */
242 		xmark = markp;
243 		while (xmark != NULL && xmark->stackp == oldstackp) {
244 			xmark->stackp = stackp;
245 			xmark->stacknxt = stacknxt;
246 			xmark->stacknleft = stacknleft;
247 			xmark = xmark->marknext;
248 		}
249 		INTON;
250 	} else {
251 		p = stalloc(newlen);
252 		if (oldlen != 0)
253 			memcpy(p, oldspace, oldlen);
254 		stunalloc(p);
255 	}
256 }
257 
258 
259 
260 void
261 grabstackblock(int len)
262 {
263 	len = ALIGN(len);
264 	stacknxt += len;
265 	stacknleft -= len;
266 }
267 
268 
269 
270 /*
271  * The following routines are somewhat easier to use that the above.
272  * The user declares a variable of type STACKSTR, which may be declared
273  * to be a register.  The macro STARTSTACKSTR initializes things.  Then
274  * the user uses the macro STPUTC to add characters to the string.  In
275  * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
276  * grown as necessary.  When the user is done, she can just leave the
277  * string there and refer to it using stackblock().  Or she can allocate
278  * the space for it using grabstackstr().  If it is necessary to allow
279  * someone else to use the stack temporarily and then continue to grow
280  * the string, the user should use grabstack to allocate the space, and
281  * then call ungrabstr(p) to return to the previous mode of operation.
282  *
283  * USTPUTC is like STPUTC except that it doesn't check for overflow.
284  * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
285  * is space for at least one character.
286  */
287 
288 
289 char *
290 growstackstr(void)
291 {
292 	int len;
293 
294 	len = stackblocksize();
295 	if (herefd >= 0 && len >= 1024) {
296 		xwrite(herefd, stackblock(), len);
297 		sstrnleft = len - 1;
298 		return stackblock();
299 	}
300 	growstackblock();
301 	sstrnleft = stackblocksize() - len - 1;
302 	return stackblock() + len;
303 }
304 
305 
306 /*
307  * Called from CHECKSTRSPACE.
308  */
309 
310 char *
311 makestrspace(void)
312 {
313 	int len;
314 
315 	len = stackblocksize() - sstrnleft;
316 	growstackblock();
317 	sstrnleft = stackblocksize() - len;
318 	return stackblock() + len;
319 }
320 
321 
322 
323 void
324 ungrabstackstr(char *s, char *p)
325 {
326 	stacknleft += stacknxt - s;
327 	stacknxt = s;
328 	sstrnleft = stacknleft - (p - s);
329 }
330