memalloc.c revision 1.11 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";*/
39 static char *rcsid = "$Id: memalloc.c,v 1.11 1994/12/23 13:21:01 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
55 pointer
56 ckmalloc(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
71 pointer
72 ckrealloc(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
87 char *
88 savestr(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
105 #define MINSIZE 512 /* minimum size of a block */
106
107
108 struct stack_block {
109 struct stack_block *prev;
110 char space[MINSIZE];
111 };
112
113 struct stack_block stackbase;
114 struct stack_block *stackp = &stackbase;
115 char *stacknxt = stackbase.space;
116 int stacknleft = MINSIZE;
117 int sstrnleft;
118 int herefd = -1;
119
120
121
122 pointer
123 stalloc(nbytes)
124 int nbytes;
125 {
126 register char *p;
127 int roundbytes;
128
129 roundbytes = ALIGN(nbytes);
130 if (roundbytes > stacknleft) {
131 int blocksize;
132 struct stack_block *sp;
133
134 blocksize = roundbytes;
135 if (blocksize < MINSIZE)
136 blocksize = MINSIZE;
137 INTOFF;
138 sp = ckmalloc(sizeof(struct stack_block) - MINSIZE + blocksize);
139 memmove(sp->space, stacknxt, nbytes);
140 sp->prev = stackp;
141 stacknxt = sp->space;
142 stacknleft = blocksize;
143 stackp = sp;
144 INTON;
145 }
146 p = stacknxt;
147 stacknxt += roundbytes;
148 stacknleft -= roundbytes;
149 return p;
150 }
151
152
153 void
154 stunalloc(p)
155 pointer p;
156 {
157 if (p == NULL) { /*DEBUG */
158 write(2, "stunalloc\n", 10);
159 abort();
160 }
161 stacknleft += stacknxt - (char *)p;
162 stacknxt = p;
163 }
164
165
166
167 void
168 setstackmark(mark)
169 struct stackmark *mark;
170 {
171 mark->stackp = stackp;
172 mark->stacknxt = stacknxt;
173 mark->stacknleft = stacknleft;
174 }
175
176
177 void
178 popstackmark(mark)
179 struct stackmark *mark;
180 {
181 struct stack_block *sp;
182
183 INTOFF;
184 while (stackp != mark->stackp) {
185 sp = stackp;
186 stackp = sp->prev;
187 ckfree(sp);
188 }
189 stacknxt = mark->stacknxt;
190 stacknleft = mark->stacknleft;
191 INTON;
192 }
193
194
195 /*
196 * When the parser reads in a string, it wants to stick the string on the
197 * stack and only adjust the stack pointer when it knows how big the
198 * string is. Stackblock (defined in stack.h) returns a pointer to a block
199 * of space on top of the stack and stackblocklen returns the length of
200 * this block. Growstackblock will grow this space by at least one byte,
201 * possibly moving it (like realloc). Grabstackblock actually allocates the
202 * part of the block that has been used.
203 */
204
205 void
206 growstackblock() {
207 char *p;
208 int newlen = stacknleft * 2 + 128;
209 char *oldspace = stacknxt;
210 int oldlen = stacknleft;
211 struct stack_block *sp;
212
213 if (stacknxt == stackp->space && stackp != &stackbase) {
214 INTOFF;
215 sp = stackp;
216 stackp = sp->prev;
217 sp = ckrealloc((pointer)sp, sizeof(struct stack_block) - MINSIZE + newlen);
218 sp->prev = stackp;
219 stackp = sp;
220 stacknxt = sp->space;
221 stacknleft = newlen;
222 INTON;
223 } else {
224 p = stalloc(newlen);
225 memcpy(p, oldspace, oldlen);
226 stacknxt = p; /* free the space */
227 stacknleft += newlen; /* we just allocated */
228 }
229 }
230
231
232
233 void
234 grabstackblock(len)
235 int len;
236 {
237 len = ALIGN(len);
238 stacknxt += len;
239 stacknleft -= len;
240 }
241
242
243
244 /*
245 * The following routines are somewhat easier to use that the above.
246 * The user declares a variable of type STACKSTR, which may be declared
247 * to be a register. The macro STARTSTACKSTR initializes things. Then
248 * the user uses the macro STPUTC to add characters to the string. In
249 * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
250 * grown as necessary. When the user is done, she can just leave the
251 * string there and refer to it using stackblock(). Or she can allocate
252 * the space for it using grabstackstr(). If it is necessary to allow
253 * someone else to use the stack temporarily and then continue to grow
254 * the string, the user should use grabstack to allocate the space, and
255 * then call ungrabstr(p) to return to the previous mode of operation.
256 *
257 * USTPUTC is like STPUTC except that it doesn't check for overflow.
258 * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
259 * is space for at least one character.
260 */
261
262
263 char *
264 growstackstr() {
265 int len = stackblocksize();
266 if (herefd >= 0 && len >= 1024) {
267 xwrite(herefd, stackblock(), len);
268 sstrnleft = len - 1;
269 return stackblock();
270 }
271 growstackblock();
272 sstrnleft = stackblocksize() - len - 1;
273 return stackblock() + len;
274 }
275
276
277 /*
278 * Called from CHECKSTRSPACE.
279 */
280
281 char *
282 makestrspace() {
283 int len = stackblocksize() - sstrnleft;
284 growstackblock();
285 sstrnleft = stackblocksize() - len;
286 return stackblock() + len;
287 }
288
289
290
291 void
292 ungrabstackstr(s, p)
293 char *s;
294 char *p;
295 {
296 stacknleft += stacknxt - s;
297 stacknxt = s;
298 sstrnleft = stacknleft - (p - s);
299 }
300