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