Deleted Added
full compact
memalloc.c (39137) memalloc.c (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

--- 25 unchanged lines hidden (view full) ---

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[] =
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

--- 25 unchanged lines hidden (view full) ---

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.11 1998/09/10 14:51:06 cracauer Exp $";
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"

--- 21 unchanged lines hidden (view full) ---

72 * Same for realloc.
73 */
74
75pointer
76ckrealloc(p, nbytes)
77 pointer p;
78 int nbytes;
79{
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"

--- 21 unchanged lines hidden (view full) ---

72 * Same for realloc.
73 */
74
75pointer
76ckrealloc(p, nbytes)
77 pointer p;
78 int nbytes;
79{
80
81 if ((p = realloc(p, nbytes)) == NULL)
82 error("Out of space");
83 return p;
84}
85
86
87/*
88 * Make a copy of a string in safe storage.
89 */
90
91char *
92savestr(s)
93 char *s;
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;
94 {
93{
95 char *p;
96
97 p = ckmalloc(strlen(s) + 1);
98 scopy(s, p);
99 return p;
100}
101
102

--- 33 unchanged lines hidden (view full) ---

136 if (nbytes > stacknleft) {
137 int blocksize;
138 struct stack_block *sp;
139
140 blocksize = nbytes;
141 if (blocksize < MINSIZE)
142 blocksize = MINSIZE;
143 INTOFF;
94 char *p;
95
96 p = ckmalloc(strlen(s) + 1);
97 scopy(s, p);
98 return p;
99}
100
101

--- 33 unchanged lines hidden (view full) ---

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;
144 sp = ckmalloc(sizeof(struct stack_block) - MINSIZE + blocksize);
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;
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 {
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;
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 {
175{
176 mark->stackp = stackp;
177 mark->stacknxt = stacknxt;
178 mark->stacknleft = stacknleft;
179}
180
181
182void
183popstackmark(mark)
184 struct stackmark *mark;
176 mark->stackp = stackp;
177 mark->stacknxt = stacknxt;
178 mark->stacknleft = stacknleft;
179}
180
181
182void
183popstackmark(mark)
184 struct stackmark *mark;
185 {
185{
186 struct stack_block *sp;
187
188 INTOFF;
189 while (stackp != mark->stackp) {
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 }
190 sp = stackp;
191 stackp = sp->prev;
192 ckfree(sp);
193 }
194 stacknxt = mark->stacknxt;
195 stacknleft = mark->stacknleft;
196 INTON;
197}

--- 5 unchanged lines hidden (view full) ---

203 * string is. Stackblock (defined in stack.h) returns a pointer to a block
204 * of space on top of the stack and stackblocklen returns the length of
205 * this block. Growstackblock will grow this space by at least one byte,
206 * possibly moving it (like realloc). Grabstackblock actually allocates the
207 * part of the block that has been used.
208 */
209
210void
194 sp = stackp;
195 stackp = sp->prev;
196 ckfree(sp);
197 }
198 stacknxt = mark->stacknxt;
199 stacknleft = mark->stacknleft;
200 INTON;
201}

--- 5 unchanged lines hidden (view full) ---

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
211growstackblock() {
215growstackblock()
216{
212 char *p;
217 char *p;
213 int newlen = ALIGN(stacknleft * 2 + 100);
214 char *oldspace = stacknxt;
215 int oldlen = stacknleft;
218 int newlen;
219 char *oldspace;
220 int oldlen;
216 struct stack_block *sp;
217
221 struct stack_block *sp;
222
223 newlen = ALIGN(stacknleft * 2 + 100);
224 oldspace = stacknxt;
225 oldlen = stacknleft;
226
218 if (stacknxt == stackp->space && stackp != &stackbase) {
219 INTOFF;
220 sp = stackp;
221 stackp = sp->prev;
227 if (stacknxt == stackp->space && stackp != &stackbase) {
228 INTOFF;
229 sp = stackp;
230 stackp = sp->prev;
222 sp = ckrealloc((pointer)sp, sizeof(struct stack_block) - MINSIZE + newlen);
231 sp = ckrealloc((pointer)sp, sizeof(struct stack_block) -
232 MINSIZE + newlen);
223 sp->prev = stackp;
224 stackp = sp;
225 stacknxt = sp->space;
226 stacknleft = newlen;
227 INTON;
228 } else {
229 p = stalloc(newlen);
230 memcpy(p, oldspace, oldlen);

--- 30 unchanged lines hidden (view full) ---

261 *
262 * USTPUTC is like STPUTC except that it doesn't check for overflow.
263 * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
264 * is space for at least one character.
265 */
266
267
268char *
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);

--- 30 unchanged lines hidden (view full) ---

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 *
269growstackstr() {
270 int len = stackblocksize();
279growstackstr()
280{
281 int len;
282
283 len = stackblocksize();
271 if (herefd >= 0 && len >= 1024) {
272 xwrite(herefd, stackblock(), len);
273 sstrnleft = len - 1;
274 return stackblock();
275 }
276 growstackblock();
277 sstrnleft = stackblocksize() - len - 1;
278 return stackblock() + len;
279}
280
281
282/*
283 * Called from CHECKSTRSPACE.
284 */
285
286char *
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 *
287makestrspace() {
288 int len = stackblocksize() - sstrnleft;
300makestrspace()
301{
302 int len;
303
304 len = stackblocksize() - sstrnleft;
289 growstackblock();
290 sstrnleft = stackblocksize() - len;
291 return stackblock() + len;
292}
293
294
295
296void
297ungrabstackstr(s, p)
298 char *s;
299 char *p;
305 growstackblock();
306 sstrnleft = stackblocksize() - len;
307 return stackblock() + len;
308}
309
310
311
312void
313ungrabstackstr(s, p)
314 char *s;
315 char *p;
300 {
316{
301 stacknleft += stacknxt - s;
302 stacknxt = s;
303 sstrnleft = stacknleft - (p - s);
304}
317 stacknleft += stacknxt - s;
318 stacknxt = s;
319 sstrnleft = stacknleft - (p - s);
320}