Rev 2714 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2714 | Rev 2782 | ||
---|---|---|---|
1 | /* $NetBSD: memalloc.c,v 1.23 2000/11/01 19:56:01 christos Exp $ */ |
1 | /* $NetBSD: memalloc.c,v 1.23 2000/11/01 19:56:01 christos Exp $ */ |
2 | 2 | ||
3 | /*- |
3 | /*- |
4 | * Copyright (c) 1991, 1993 |
4 | * Copyright (c) 1991, 1993 |
5 | * The Regents of the University of California. All rights reserved. |
5 | * The Regents of the University of California. All rights reserved. |
6 | * |
6 | * |
7 | * This code is derived from software contributed to Berkeley by |
7 | * This code is derived from software contributed to Berkeley by |
8 | * Kenneth Almquist. |
8 | * Kenneth Almquist. |
9 | * |
9 | * |
10 | * Redistribution and use in source and binary forms, with or without |
10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions |
11 | * modification, are permitted provided that the following conditions |
12 | * are met: |
12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright |
13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. |
14 | * notice, this list of conditions and the following disclaimer. |
15 | * 2. Redistributions in binary form must reproduce the above copyright |
15 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in the |
16 | * notice, this list of conditions and the following disclaimer in the |
17 | * documentation and/or other materials provided with the distribution. |
17 | * documentation and/or other materials provided with the distribution. |
18 | * 3. All advertising materials mentioning features or use of this software |
18 | * 3. All advertising materials mentioning features or use of this software |
19 | * must display the following acknowledgement: |
19 | * must display the following acknowledgement: |
20 | * This product includes software developed by the University of |
20 | * This product includes software developed by the University of |
21 | * California, Berkeley and its contributors. |
21 | * California, Berkeley and its contributors. |
22 | * 4. Neither the name of the University nor the names of 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 |
23 | * may be used to endorse or promote products derived from this software |
24 | * without specific prior written permission. |
24 | * without specific prior written permission. |
25 | * |
25 | * |
26 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
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 |
27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
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 |
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 |
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 |
35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
36 | * SUCH DAMAGE. |
36 | * SUCH DAMAGE. |
37 | */ |
37 | */ |
38 | 38 | ||
39 | #include <sys/cdefs.h> |
39 | #include <sys/cdefs.h> |
40 | #ifndef lint |
40 | #ifndef lint |
41 | #if 0 |
41 | #if 0 |
42 | static char sccsid[] = "@(#)memalloc.c 8.3 (Berkeley) 5/4/95"; |
42 | static char sccsid[] = "@(#)memalloc.c 8.3 (Berkeley) 5/4/95"; |
43 | #else |
43 | #else |
44 | __RCSID("$NetBSD: memalloc.c,v 1.23 2000/11/01 19:56:01 christos Exp $"); |
44 | __RCSID("$NetBSD: memalloc.c,v 1.23 2000/11/01 19:56:01 christos Exp $"); |
45 | #endif |
45 | #endif |
46 | #endif /* not lint */ |
46 | #endif /* not lint */ |
47 | 47 | ||
48 | #include <stdlib.h> |
48 | #include <stdlib.h> |
49 | #include <unistd.h> |
49 | #include <unistd.h> |
50 | 50 | ||
51 | #include "shell.h" |
51 | #include "shell.h" |
52 | #include "output.h" |
52 | #include "output.h" |
53 | #include "memalloc.h" |
53 | #include "memalloc.h" |
54 | #include "error.h" |
54 | #include "error.h" |
55 | #include "machdep.h" |
55 | #include "machdep.h" |
56 | #include "mystring.h" |
56 | #include "mystring.h" |
57 | 57 | ||
58 | /* |
58 | /* |
59 | * Like malloc, but returns an error when out of space. |
59 | * Like malloc, but returns an error when out of space. |
60 | */ |
60 | */ |
61 | 61 | ||
62 | pointer |
62 | pointer |
63 | ckmalloc(nbytes) |
63 | ckmalloc(nbytes) |
64 | int nbytes; |
64 | int nbytes; |
65 | { |
65 | { |
66 | pointer p; |
66 | pointer p; |
67 | 67 | ||
68 | INTOFF; |
68 | INTOFF; |
69 | p = malloc(nbytes); |
69 | p = malloc(nbytes); |
70 | INTON; |
70 | INTON; |
71 | if (p == NULL) |
71 | if (p == NULL) |
72 | error("Out of space"); |
72 | error("Out of space"); |
73 | return p; |
73 | return p; |
74 | } |
74 | } |
75 | 75 | ||
76 | 76 | ||
77 | /* |
77 | /* |
78 | * Same for realloc. |
78 | * Same for realloc. |
79 | */ |
79 | */ |
80 | 80 | ||
81 | pointer |
81 | pointer |
82 | ckrealloc(p, nbytes) |
82 | ckrealloc(p, nbytes) |
83 | pointer p; |
83 | pointer p; |
84 | int nbytes; |
84 | int nbytes; |
85 | { |
85 | { |
86 | 86 | ||
87 | if ((p = realloc(p, nbytes)) == NULL) |
87 | if ((p = realloc(p, nbytes)) == NULL) |
88 | error("Out of space"); |
88 | error("Out of space"); |
89 | return p; |
89 | return p; |
90 | } |
90 | } |
91 | 91 | ||
92 | 92 | ||
93 | /* |
93 | /* |
94 | * Make a copy of a string in safe storage. |
94 | * Make a copy of a string in safe storage. |
95 | */ |
95 | */ |
96 | 96 | ||
97 | char * |
97 | char * |
98 | savestr(s) |
98 | savestr(s) |
99 | char *s; |
99 | char *s; |
100 | { |
100 | { |
101 | char *p; |
101 | char *p; |
102 | 102 | ||
103 | p = ckmalloc(strlen(s) + 1); |
103 | p = ckmalloc(strlen(s) + 1); |
104 | scopy(s, p); |
104 | scopy(s, p); |
105 | return p; |
105 | return p; |
106 | } |
106 | } |
107 | 107 | ||
108 | 108 | ||
109 | /* |
109 | /* |
110 | * Parse trees for commands are allocated in lifo order, so we use a stack |
110 | * Parse trees for commands are allocated in lifo order, so we use a stack |
111 | * to make this more efficient, and also to avoid all sorts of exception |
111 | * to make this more efficient, and also to avoid all sorts of exception |
112 | * handling code to handle interrupts in the middle of a parse. |
112 | * handling code to handle interrupts in the middle of a parse. |
113 | * |
113 | * |
114 | * The size 504 was chosen because the Ultrix malloc handles that size |
114 | * The size 504 was chosen because the Ultrix malloc handles that size |
115 | * well. |
115 | * well. |
116 | */ |
116 | */ |
117 | 117 | ||
118 | #define MINSIZE 504 /* minimum size of a block */ |
118 | #define MINSIZE 504 /* minimum size of a block */ |
119 | 119 | ||
120 | 120 | ||
121 | struct stack_block { |
121 | struct stack_block { |
122 | struct stack_block *prev; |
122 | struct stack_block *prev; |
123 | char space[MINSIZE]; |
123 | char space[MINSIZE]; |
124 | }; |
124 | }; |
125 | 125 | ||
126 | struct stack_block stackbase; |
126 | struct stack_block stackbase; |
127 | struct stack_block *stackp = &stackbase; |
127 | struct stack_block *stackp = &stackbase; |
128 | struct stackmark *markp; |
128 | struct stackmark *markp; |
129 | char *stacknxt = stackbase.space; |
129 | char *stacknxt = stackbase.space; |
130 | int stacknleft = MINSIZE; |
130 | int stacknleft = MINSIZE; |
131 | int sstrnleft; |
131 | int sstrnleft; |
132 | int herefd = -1; |
132 | int herefd = -1; |
133 | 133 | ||
134 | 134 | ||
135 | 135 | ||
136 | pointer |
136 | pointer |
137 | stalloc(nbytes) |
137 | stalloc(nbytes) |
138 | int nbytes; |
138 | int nbytes; |
139 | { |
139 | { |
140 | char *p; |
140 | char *p; |
141 | 141 | ||
142 | nbytes = ALIGN(nbytes); |
142 | nbytes = ALIGN(nbytes); |
143 | if (nbytes > stacknleft) { |
143 | if (nbytes > stacknleft) { |
144 | int blocksize; |
144 | int blocksize; |
145 | struct stack_block *sp; |
145 | struct stack_block *sp; |
146 | 146 | ||
147 | blocksize = nbytes; |
147 | blocksize = nbytes; |
148 | if (blocksize < MINSIZE) |
148 | if (blocksize < MINSIZE) |
149 | blocksize = MINSIZE; |
149 | blocksize = MINSIZE; |
150 | INTOFF; |
150 | INTOFF; |
151 | sp = ckmalloc(sizeof(struct stack_block) - MINSIZE + blocksize); |
151 | sp = ckmalloc(sizeof(struct stack_block) - MINSIZE + blocksize); |
152 | sp->prev = stackp; |
152 | sp->prev = stackp; |
153 | stacknxt = sp->space; |
153 | stacknxt = sp->space; |
154 | stacknleft = blocksize; |
154 | stacknleft = blocksize; |
155 | stackp = sp; |
155 | stackp = sp; |
156 | INTON; |
156 | INTON; |
157 | } |
157 | } |
158 | p = stacknxt; |
158 | p = stacknxt; |
159 | stacknxt += nbytes; |
159 | stacknxt += nbytes; |
160 | stacknleft -= nbytes; |
160 | stacknleft -= nbytes; |
161 | return p; |
161 | return p; |
162 | } |
162 | } |
163 | 163 | ||
164 | 164 | ||
165 | void |
165 | void |
166 | stunalloc(p) |
166 | stunalloc(p) |
167 | pointer p; |
167 | pointer p; |
168 | { |
168 | { |
169 | if (p == NULL) { /*DEBUG */ |
169 | if (p == NULL) { /*DEBUG */ |
170 | write(2, "stunalloc\n", 10); |
170 | write(2, "stunalloc\n", 10); |
171 | abort(); |
171 | abort(); |
172 | } |
172 | } |
173 | stacknleft += stacknxt - (char *)p; |
173 | stacknleft += stacknxt - (char *)p; |
174 | stacknxt = p; |
174 | stacknxt = p; |
175 | } |
175 | } |
176 | 176 | ||
177 | 177 | ||
178 | 178 | ||
179 | void |
179 | void |
180 | setstackmark(mark) |
180 | setstackmark(mark) |
181 | struct stackmark *mark; |
181 | struct stackmark *mark; |
182 | { |
182 | { |
183 | mark->stackp = stackp; |
183 | mark->stackp = stackp; |
184 | mark->stacknxt = stacknxt; |
184 | mark->stacknxt = stacknxt; |
185 | mark->stacknleft = stacknleft; |
185 | mark->stacknleft = stacknleft; |
186 | mark->marknext = markp; |
186 | mark->marknext = markp; |
187 | markp = mark; |
187 | markp = mark; |
188 | } |
188 | } |
189 | 189 | ||
190 | 190 | ||
191 | void |
191 | void |
192 | popstackmark(mark) |
192 | popstackmark(mark) |
193 | struct stackmark *mark; |
193 | struct stackmark *mark; |
194 | { |
194 | { |
195 | struct stack_block *sp; |
195 | struct stack_block *sp; |
196 | 196 | ||
197 | INTOFF; |
197 | INTOFF; |
198 | markp = mark->marknext; |
198 | markp = mark->marknext; |
199 | while (stackp != mark->stackp) { |
199 | while (stackp != mark->stackp) { |
200 | sp = stackp; |
200 | sp = stackp; |
201 | stackp = sp->prev; |
201 | stackp = sp->prev; |
202 | ckfree(sp); |
202 | ckfree(sp); |
203 | } |
203 | } |
204 | stacknxt = mark->stacknxt; |
204 | stacknxt = mark->stacknxt; |
205 | stacknleft = mark->stacknleft; |
205 | stacknleft = mark->stacknleft; |
206 | INTON; |
206 | INTON; |
207 | } |
207 | } |
208 | 208 | ||
209 | 209 | ||
210 | /* |
210 | /* |
211 | * When the parser reads in a string, it wants to stick the string on the |
211 | * When the parser reads in a string, it wants to stick the string on the |
212 | * stack and only adjust the stack pointer when it knows how big the |
212 | * stack and only adjust the stack pointer when it knows how big the |
213 | * string is. Stackblock (defined in stack.h) returns a pointer to a block |
213 | * string is. Stackblock (defined in stack.h) returns a pointer to a block |
214 | * of space on top of the stack and stackblocklen returns the length of |
214 | * of space on top of the stack and stackblocklen returns the length of |
215 | * this block. Growstackblock will grow this space by at least one byte, |
215 | * this block. Growstackblock will grow this space by at least one byte, |
216 | * possibly moving it (like realloc). Grabstackblock actually allocates the |
216 | * possibly moving it (like realloc). Grabstackblock actually allocates the |
217 | * part of the block that has been used. |
217 | * part of the block that has been used. |
218 | */ |
218 | */ |
219 | 219 | ||
220 | void |
220 | void |
221 | growstackblock() { |
221 | growstackblock() { |
222 | char *p; |
222 | char *p; |
223 | int newlen = ALIGN(stacknleft * 2 + 100); |
223 | int newlen = ALIGN(stacknleft * 2 + 100); |
224 | char *oldspace = stacknxt; |
224 | char *oldspace = stacknxt; |
225 | int oldlen = stacknleft; |
225 | int oldlen = stacknleft; |
226 | struct stack_block *sp; |
226 | struct stack_block *sp; |
227 | struct stack_block *oldstackp; |
227 | struct stack_block *oldstackp; |
228 | 228 | ||
229 | if (stacknxt == stackp->space && stackp != &stackbase) { |
229 | if (stacknxt == stackp->space && stackp != &stackbase) { |
230 | INTOFF; |
230 | INTOFF; |
231 | oldstackp = stackp; |
231 | oldstackp = stackp; |
232 | sp = stackp; |
232 | sp = stackp; |
233 | stackp = sp->prev; |
233 | stackp = sp->prev; |
234 | sp = ckrealloc((pointer)sp, sizeof(struct stack_block) - MINSIZE + newlen); |
234 | sp = ckrealloc((pointer)sp, sizeof(struct stack_block) - MINSIZE + newlen); |
235 | sp->prev = stackp; |
235 | sp->prev = stackp; |
236 | stackp = sp; |
236 | stackp = sp; |
237 | stacknxt = sp->space; |
237 | stacknxt = sp->space; |
238 | stacknleft = newlen; |
238 | stacknleft = newlen; |
239 | { |
239 | { |
240 | /* Stack marks pointing to the start of the old block |
240 | /* Stack marks pointing to the start of the old block |
241 | * must be relocated to point to the new block |
241 | * must be relocated to point to the new block |
242 | */ |
242 | */ |
243 | struct stackmark *xmark; |
243 | struct stackmark *xmark; |
244 | xmark = markp; |
244 | xmark = markp; |
245 | while (xmark != NULL && xmark->stackp == oldstackp) { |
245 | while (xmark != NULL && xmark->stackp == oldstackp) { |
246 | xmark->stackp = stackp; |
246 | xmark->stackp = stackp; |
247 | xmark->stacknxt = stacknxt; |
247 | xmark->stacknxt = stacknxt; |
248 | xmark->stacknleft = stacknleft; |
248 | xmark->stacknleft = stacknleft; |
249 | xmark = xmark->marknext; |
249 | xmark = xmark->marknext; |
250 | } |
250 | } |
251 | } |
251 | } |
252 | INTON; |
252 | INTON; |
253 | } else { |
253 | } else { |
254 | p = stalloc(newlen); |
254 | p = stalloc(newlen); |
255 | memcpy(p, oldspace, oldlen); |
255 | memcpy(p, oldspace, oldlen); |
256 | stacknxt = p; /* free the space */ |
256 | stacknxt = p; /* free the space */ |
257 | stacknleft += newlen; /* we just allocated */ |
257 | stacknleft += newlen; /* we just allocated */ |
258 | } |
258 | } |
259 | } |
259 | } |
260 | 260 | ||
261 | 261 | ||
262 | 262 | ||
263 | void |
263 | void |
264 | grabstackblock(len) |
264 | grabstackblock(len) |
265 | int len; |
265 | int len; |
266 | { |
266 | { |
267 | len = ALIGN(len); |
267 | len = ALIGN(len); |
268 | stacknxt += len; |
268 | stacknxt += len; |
269 | stacknleft -= len; |
269 | stacknleft -= len; |
270 | } |
270 | } |
271 | 271 | ||
272 | 272 | ||
273 | 273 | ||
274 | /* |
274 | /* |
275 | * The following routines are somewhat easier to use that the above. |
275 | * The following routines are somewhat easier to use that the above. |
276 | * The user declares a variable of type STACKSTR, which may be declared |
276 | * The user declares a variable of type STACKSTR, which may be declared |
277 | * to be a register. The macro STARTSTACKSTR initializes things. Then |
277 | * to be a register. The macro STARTSTACKSTR initializes things. Then |
278 | * the user uses the macro STPUTC to add characters to the string. In |
278 | * the user uses the macro STPUTC to add characters to the string. In |
279 | * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is |
279 | * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is |
280 | * grown as necessary. When the user is done, she can just leave the |
280 | * grown as necessary. When the user is done, she can just leave the |
281 | * string there and refer to it using stackblock(). Or she can allocate |
281 | * string there and refer to it using stackblock(). Or she can allocate |
282 | * the space for it using grabstackstr(). If it is necessary to allow |
282 | * the space for it using grabstackstr(). If it is necessary to allow |
283 | * someone else to use the stack temporarily and then continue to grow |
283 | * someone else to use the stack temporarily and then continue to grow |
284 | * the string, the user should use grabstack to allocate the space, and |
284 | * the string, the user should use grabstack to allocate the space, and |
285 | * then call ungrabstr(p) to return to the previous mode of operation. |
285 | * then call ungrabstr(p) to return to the previous mode of operation. |
286 | * |
286 | * |
287 | * USTPUTC is like STPUTC except that it doesn't check for overflow. |
287 | * USTPUTC is like STPUTC except that it doesn't check for overflow. |
288 | * CHECKSTACKSPACE can be called before USTPUTC to ensure that there |
288 | * CHECKSTACKSPACE can be called before USTPUTC to ensure that there |
289 | * is space for at least one character. |
289 | * is space for at least one character. |
290 | */ |
290 | */ |
291 | 291 | ||
292 | 292 | ||
293 | char * |
293 | char * |
294 | growstackstr() { |
294 | growstackstr() { |
295 | int len = stackblocksize(); |
295 | int len = stackblocksize(); |
296 | if (herefd >= 0 && len >= 1024) { |
296 | if (herefd >= 0 && len >= 1024) { |
297 | xwrite(herefd, stackblock(), len); |
297 | xwrite(herefd, stackblock(), len); |
298 | sstrnleft = len - 1; |
298 | sstrnleft = len - 1; |
299 | return stackblock(); |
299 | return stackblock(); |
300 | } |
300 | } |
301 | growstackblock(); |
301 | growstackblock(); |
302 | sstrnleft = stackblocksize() - len - 1; |
302 | sstrnleft = stackblocksize() - len - 1; |
303 | return stackblock() + len; |
303 | return stackblock() + len; |
304 | } |
304 | } |
305 | 305 | ||
306 | 306 | ||
307 | /* |
307 | /* |
308 | * Called from CHECKSTRSPACE. |
308 | * Called from CHECKSTRSPACE. |
309 | */ |
309 | */ |
310 | 310 | ||
311 | char * |
311 | char * |
312 | makestrspace() { |
312 | makestrspace() { |
313 | int len = stackblocksize() - sstrnleft; |
313 | int len = stackblocksize() - sstrnleft; |
314 | growstackblock(); |
314 | growstackblock(); |
315 | sstrnleft = stackblocksize() - len; |
315 | sstrnleft = stackblocksize() - len; |
316 | return stackblock() + len; |
316 | return stackblock() + len; |
317 | } |
317 | } |
318 | 318 | ||
319 | 319 | ||
320 | 320 | ||
321 | void |
321 | void |
322 | ungrabstackstr(s, p) |
322 | ungrabstackstr(s, p) |
323 | char *s; |
323 | char *s; |
324 | char *p; |
324 | char *p; |
325 | { |
325 | { |
326 | stacknleft += stacknxt - s; |
326 | stacknleft += stacknxt - s; |
327 | stacknxt = s; |
327 | stacknxt = s; |
328 | sstrnleft = stacknleft - (p - s); |
328 | sstrnleft = stacknleft - (p - s); |
329 | } |
329 | } |
330 | 330 |