xref: /netbsd-src/bin/sh/memalloc.c (revision 001c68bd94f75ce9270b69227c4199fbf34ee396)
1 /*	$NetBSD: memalloc.c,v 1.27 2003/01/22 20:36:04 dsl 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.27 2003/01/22 20:36:04 dsl 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(int nbytes)
64 {
65 	pointer p;
66 
67 	p = malloc(nbytes);
68 	if (p == NULL)
69 		error("Out of space");
70 	return p;
71 }
72 
73 
74 /*
75  * Same for realloc.
76  */
77 
78 pointer
79 ckrealloc(pointer p, int nbytes)
80 {
81 	p = realloc(p, nbytes);
82 	if (p == NULL)
83 		error("Out of space");
84 	return p;
85 }
86 
87 
88 /*
89  * Make a copy of a string in safe storage.
90  */
91 
92 char *
93 savestr(const char *s)
94 {
95 	char *p;
96 
97 	p = ckmalloc(strlen(s) + 1);
98 	scopy(s, p);
99 	return p;
100 }
101 
102 
103 /*
104  * Parse trees for commands are allocated in lifo order, so we use a stack
105  * to make this more efficient, and also to avoid all sorts of exception
106  * handling code to handle interrupts in the middle of a parse.
107  *
108  * The size 504 was chosen because the Ultrix malloc handles that size
109  * well.
110  */
111 
112 #define MINSIZE 504		/* minimum size of a block */
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 struct stackmark *markp;
122 char *stacknxt = stackbase.space;
123 int stacknleft = MINSIZE;
124 int sstrnleft;
125 int herefd = -1;
126 
127 pointer
128 stalloc(int nbytes)
129 {
130 	char *p;
131 
132 	nbytes = SHELL_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(pointer p)
157 {
158 	if (p == NULL) {		/*DEBUG */
159 		write(2, "stunalloc\n", 10);
160 		abort();
161 	}
162 	stacknleft += stacknxt - (char *)p;
163 	stacknxt = p;
164 }
165 
166 
167 
168 void
169 setstackmark(struct stackmark *mark)
170 {
171 	mark->stackp = stackp;
172 	mark->stacknxt = stacknxt;
173 	mark->stacknleft = stacknleft;
174 	mark->marknext = markp;
175 	markp = mark;
176 }
177 
178 
179 void
180 popstackmark(struct stackmark *mark)
181 {
182 	struct stack_block *sp;
183 
184 	INTOFF;
185 	markp = mark->marknext;
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(void)
209 {
210 	int newlen = SHELL_ALIGN(stacknleft * 2 + 100);
211 
212 	if (stacknxt == stackp->space && stackp != &stackbase) {
213 		struct stack_block *oldstackp;
214 		struct stackmark *xmark;
215 		struct stack_block *sp;
216 
217 		INTOFF;
218 		oldstackp = stackp;
219 		sp = stackp;
220 		stackp = sp->prev;
221 		sp = ckrealloc((pointer)sp,
222 		    sizeof(struct stack_block) - MINSIZE + newlen);
223 		sp->prev = stackp;
224 		stackp = sp;
225 		stacknxt = sp->space;
226 		stacknleft = newlen;
227 
228 		/*
229 		 * Stack marks pointing to the start of the old block
230 		 * must be relocated to point to the new block
231 		 */
232 		xmark = markp;
233 		while (xmark != NULL && xmark->stackp == oldstackp) {
234 			xmark->stackp = stackp;
235 			xmark->stacknxt = stacknxt;
236 			xmark->stacknleft = stacknleft;
237 			xmark = xmark->marknext;
238 		}
239 		INTON;
240 	} else {
241 		char *oldspace = stacknxt;
242 		int oldlen = stacknleft;
243 		char *p = stalloc(newlen);
244 
245 		(void)memcpy(p, oldspace, oldlen);
246 		stacknxt = p;			/* free the space */
247 		stacknleft += newlen;		/* we just allocated */
248 	}
249 }
250 
251 void
252 grabstackblock(int len)
253 {
254 	len = SHELL_ALIGN(len);
255 	stacknxt += len;
256 	stacknleft -= len;
257 }
258 
259 /*
260  * The following routines are somewhat easier to use than the above.
261  * The user declares a variable of type STACKSTR, which may be declared
262  * to be a register.  The macro STARTSTACKSTR initializes things.  Then
263  * the user uses the macro STPUTC to add characters to the string.  In
264  * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
265  * grown as necessary.  When the user is done, she can just leave the
266  * string there and refer to it using stackblock().  Or she can allocate
267  * the space for it using grabstackstr().  If it is necessary to allow
268  * someone else to use the stack temporarily and then continue to grow
269  * the string, the user should use grabstack to allocate the space, and
270  * then call ungrabstr(p) to return to the previous mode of operation.
271  *
272  * USTPUTC is like STPUTC except that it doesn't check for overflow.
273  * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
274  * is space for at least one character.
275  */
276 
277 char *
278 growstackstr(void)
279 {
280 	int len = stackblocksize();
281 	if (herefd >= 0 && len >= 1024) {
282 		xwrite(herefd, stackblock(), len);
283 		sstrnleft = len - 1;
284 		return stackblock();
285 	}
286 	growstackblock();
287 	sstrnleft = stackblocksize() - len - 1;
288 	return stackblock() + len;
289 }
290 
291 /*
292  * Called from CHECKSTRSPACE.
293  */
294 
295 char *
296 makestrspace(void)
297 {
298 	int len = stackblocksize() - sstrnleft;
299 	growstackblock();
300 	sstrnleft = stackblocksize() - len;
301 	return stackblock() + len;
302 }
303 
304 void
305 ungrabstackstr(char *s, char *p)
306 {
307 	stacknleft += stacknxt - s;
308 	stacknxt = s;
309 	sstrnleft = stacknleft - (p - s);
310 
311 }
312