memalloc.c revision 45618
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
39static char sccsid[] = "@(#)memalloc.c	8.3 (Berkeley) 5/4/95";
40#endif
41static const char rcsid[] =
42	"$Id: memalloc.c,v 1.12 1998/09/13 19:24:57 tegge Exp $";
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 "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
59pointer
60ckmalloc(nbytes)
61	int nbytes;
62{
63	pointer p;
64
65	if ((p = malloc(nbytes)) == NULL)
66		error("Out of space");
67	return p;
68}
69
70
71/*
72 * Same for realloc.
73 */
74
75pointer
76ckrealloc(p, nbytes)
77	pointer p;
78	int nbytes;
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
90char *
91savestr(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
114struct stack_block {
115	struct stack_block *prev;
116	char space[MINSIZE];
117};
118
119struct stack_block stackbase;
120struct stack_block *stackp = &stackbase;
121char *stacknxt = stackbase.space;
122int stacknleft = MINSIZE;
123int sstrnleft;
124int herefd = -1;
125
126
127
128pointer
129stalloc(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 +
144		    blocksize);
145		sp->prev = stackp;
146		stacknxt = sp->space;
147		stacknleft = blocksize;
148		stackp = sp;
149		INTON;
150	}
151	p = stacknxt;
152	stacknxt += nbytes;
153	stacknleft -= nbytes;
154	return p;
155}
156
157
158void
159stunalloc(p)
160	pointer p;
161{
162	if (p == NULL) {		/*DEBUG */
163		write(2, "stunalloc\n", 10);
164		abort();
165	}
166	stacknleft += stacknxt - (char *)p;
167	stacknxt = p;
168}
169
170
171
172void
173setstackmark(mark)
174	struct stackmark *mark;
175{
176	mark->stackp = stackp;
177	mark->stacknxt = stacknxt;
178	mark->stacknleft = stacknleft;
179}
180
181
182void
183popstackmark(mark)
184	struct stackmark *mark;
185{
186	struct stack_block *sp;
187
188	INTOFF;
189	while (stackp != mark->stackp) {
190		if (stackp == NULL) {
191			write(2, "Oops, stackp deleted\n", 21);
192			abort();
193		}
194		sp = stackp;
195		stackp = sp->prev;
196		ckfree(sp);
197	}
198	stacknxt = mark->stacknxt;
199	stacknleft = mark->stacknleft;
200	INTON;
201}
202
203
204/*
205 * When the parser reads in a string, it wants to stick the string on the
206 * stack and only adjust the stack pointer when it knows how big the
207 * string is.  Stackblock (defined in stack.h) returns a pointer to a block
208 * of space on top of the stack and stackblocklen returns the length of
209 * this block.  Growstackblock will grow this space by at least one byte,
210 * possibly moving it (like realloc).  Grabstackblock actually allocates the
211 * part of the block that has been used.
212 */
213
214void
215growstackblock()
216{
217	char *p;
218	int newlen;
219	char *oldspace;
220	int oldlen;
221	struct stack_block *sp;
222
223	newlen = ALIGN(stacknleft * 2 + 100);
224	oldspace = stacknxt;
225	oldlen = stacknleft;
226
227	if (stacknxt == stackp->space && stackp != &stackbase) {
228		INTOFF;
229		sp = stackp;
230		stackp = sp->prev;
231		sp = ckrealloc((pointer)sp, sizeof(struct stack_block) -
232		    MINSIZE + newlen);
233		sp->prev = stackp;
234		stackp = sp;
235		stacknxt = sp->space;
236		stacknleft = newlen;
237		INTON;
238	} else {
239		p = stalloc(newlen);
240		memcpy(p, oldspace, oldlen);
241		stacknxt = p;			/* free the space */
242		stacknleft += newlen;		/* we just allocated */
243	}
244}
245
246
247
248void
249grabstackblock(len)
250	int len;
251{
252	len = ALIGN(len);
253	stacknxt += len;
254	stacknleft -= len;
255}
256
257
258
259/*
260 * The following routines are somewhat easier to use that 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
278char *
279growstackstr()
280{
281	int len;
282
283	len = stackblocksize();
284	if (herefd >= 0 && len >= 1024) {
285		xwrite(herefd, stackblock(), len);
286		sstrnleft = len - 1;
287		return stackblock();
288	}
289	growstackblock();
290	sstrnleft = stackblocksize() - len - 1;
291	return stackblock() + len;
292}
293
294
295/*
296 * Called from CHECKSTRSPACE.
297 */
298
299char *
300makestrspace()
301{
302	int len;
303
304	len = stackblocksize() - sstrnleft;
305	growstackblock();
306	sstrnleft = stackblocksize() - len;
307	return stackblock() + len;
308}
309
310
311
312void
313ungrabstackstr(s, p)
314	char *s;
315	char *p;
316{
317	stacknleft += stacknxt - s;
318	stacknxt = s;
319	sstrnleft = stacknleft - (p - s);
320}
321