1 /* obstack.c - subroutines used implicitly by object stack macros
2 Copyright (C) 1988, 89, 90, 91, 92, 93, 94 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* NOTE BEFORE MODIFYING THIS FILE: This version number must be
21 incremented whenever callers compiled using an old obstack.h can no
22 longer properly call the functions in this obstack.c. */
23 #define OBSTACK_INTERFACE_VERSION 1
25 /* Comment out all this code if we are using the GNU C Library, and are not
26 actually compiling the library itself, and the installed library
27 supports the same library interface we do. This code is part of the GNU
28 C Library, but also included in many other GNU distributions. Compiling
29 and linking in this code is a waste when using the GNU C library
30 (especially if it is a shared library). Rather than having every GNU
31 program understand `configure --with-gnu-libc' and omit the object
32 files, it is simpler to just do this in the source for each such file. */
34 #include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */
35 #if !defined (_LIBC) && defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
36 #include <gnu-versions.h>
37 #if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION
46 #if defined (__STDC__) && __STDC__
47 #define POINTER void *
49 #define POINTER char *
52 /* Determine default alignment. */
53 struct fooalign {char x; double d;};
54 #define DEFAULT_ALIGNMENT \
55 ((PTR_INT_TYPE) ((char *)&((struct fooalign *) 0)->d - (char *)0))
56 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
57 But in fact it might be less smart and round addresses to as much as
58 DEFAULT_ROUNDING. So we prepare for it to do that. */
59 union fooround {long x; double d;};
60 #define DEFAULT_ROUNDING (sizeof (union fooround))
62 /* When we copy a long block of data, this is the unit to do it with.
63 On some machines, copying successive ints does not work;
64 in such a case, redefine COPYING_UNIT to `long' (if that works)
65 or `char' as a last resort. */
67 #define COPYING_UNIT int
70 /* The non-GNU-C macros copy the obstack into this global variable
71 to avoid multiple evaluation. */
73 struct obstack *_obstack;
75 /* Define a macro that either calls functions with the traditional malloc/free
76 calling interface, or calls functions with the mmalloc/mfree interface
77 (that adds an extra first argument), based on the state of use_extra_arg.
78 For free, do not use ?:, since some compilers, like the MIPS compilers,
79 do not allow (expr) ? void : void. */
81 #define CALL_CHUNKFUN(h, size) \
82 (((h) -> use_extra_arg) \
83 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
84 : (*(h)->chunkfun) ((size)))
86 #define CALL_FREEFUN(h, old_chunk) \
88 if ((h) -> use_extra_arg) \
89 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
91 (*(h)->freefun) ((old_chunk)); \
95 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
96 Objects start on multiples of ALIGNMENT (0 means use default).
97 CHUNKFUN is the function to use to allocate chunks,
98 and FREEFUN the function to free them.
100 Return nonzero if successful, zero if out of memory.
101 To recover from an out of memory error,
102 free up some memory, then call this again. */
105 _obstack_begin (h, size, alignment, chunkfun, freefun)
109 POINTER (*chunkfun) ();
112 register struct _obstack_chunk* chunk; /* points to new chunk */
115 alignment = DEFAULT_ALIGNMENT;
117 /* Default size is what GNU malloc can fit in a 4096-byte block. */
119 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
120 Use the values for range checking, because if range checking is off,
121 the extra bytes won't be missed terribly, but if range checking is on
122 and we used a larger request, a whole extra 4096 bytes would be
125 These number are irrelevant to the new GNU malloc. I suspect it is
126 less sensitive to the size of the request. */
127 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
128 + 4 + DEFAULT_ROUNDING - 1)
129 & ~(DEFAULT_ROUNDING - 1));
133 h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
134 h->freefun = freefun;
135 h->chunk_size = size;
136 h->alignment_mask = alignment - 1;
137 h->use_extra_arg = 0;
139 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
146 h->next_free = h->object_base = chunk->contents;
147 h->chunk_limit = chunk->limit
148 = (char *) chunk + h->chunk_size;
150 /* The initial chunk now contains no empty object. */
151 h->maybe_empty_object = 0;
156 _obstack_begin_1 (h, size, alignment, chunkfun, freefun, arg)
160 POINTER (*chunkfun) ();
164 register struct _obstack_chunk* chunk; /* points to new chunk */
167 alignment = DEFAULT_ALIGNMENT;
169 /* Default size is what GNU malloc can fit in a 4096-byte block. */
171 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
172 Use the values for range checking, because if range checking is off,
173 the extra bytes won't be missed terribly, but if range checking is on
174 and we used a larger request, a whole extra 4096 bytes would be
177 These number are irrelevant to the new GNU malloc. I suspect it is
178 less sensitive to the size of the request. */
179 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
180 + 4 + DEFAULT_ROUNDING - 1)
181 & ~(DEFAULT_ROUNDING - 1));
185 h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
186 h->freefun = freefun;
187 h->chunk_size = size;
188 h->alignment_mask = alignment - 1;
190 h->use_extra_arg = 1;
192 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
199 h->next_free = h->object_base = chunk->contents;
200 h->chunk_limit = chunk->limit
201 = (char *) chunk + h->chunk_size;
203 /* The initial chunk now contains no empty object. */
204 h->maybe_empty_object = 0;
208 /* Allocate a new current chunk for the obstack *H
209 on the assumption that LENGTH bytes need to be added
210 to the current object, or a new object of length LENGTH allocated.
211 Copies any partial object from the end of the old chunk
212 to the beginning of the new one. */
215 _obstack_newchunk (h, length)
219 register struct _obstack_chunk* old_chunk = h->chunk;
220 register struct _obstack_chunk* new_chunk;
221 register long new_size;
222 register int obj_size = h->next_free - h->object_base;
226 /* Compute size for new chunk. */
227 new_size = (obj_size + length) + (obj_size >> 3) + 100;
228 if (new_size < h->chunk_size)
229 new_size = h->chunk_size;
231 /* Allocate and initialize the new chunk. */
232 new_chunk = CALL_CHUNKFUN (h, new_size);
239 h->chunk = new_chunk;
240 new_chunk->prev = old_chunk;
241 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
243 /* Move the existing object to the new chunk.
244 Word at a time is fast and is safe if the object
245 is sufficiently aligned. */
246 if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
248 for (i = obj_size / sizeof (COPYING_UNIT) - 1;
250 ((COPYING_UNIT *)new_chunk->contents)[i]
251 = ((COPYING_UNIT *)h->object_base)[i];
252 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
253 but that can cross a page boundary on a machine
254 which does not do strict alignment for COPYING_UNITS. */
255 already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
259 /* Copy remaining bytes one by one. */
260 for (i = already; i < obj_size; i++)
261 new_chunk->contents[i] = h->object_base[i];
263 /* If the object just copied was the only data in OLD_CHUNK,
264 free that chunk and remove it from the chain.
265 But not if that chunk might contain an empty object. */
266 if (h->object_base == old_chunk->contents && ! h->maybe_empty_object)
268 new_chunk->prev = old_chunk->prev;
269 CALL_FREEFUN (h, old_chunk);
272 h->object_base = new_chunk->contents;
273 h->next_free = h->object_base + obj_size;
274 /* The new chunk certainly contains no empty object yet. */
275 h->maybe_empty_object = 0;
278 /* Return nonzero if object OBJ has been allocated from obstack H.
279 This is here for debugging.
280 If you use it in a program, you are probably losing. */
282 #if defined (__STDC__) && __STDC__
283 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
284 obstack.h because it is just for debugging. */
285 int _obstack_allocated_p (struct obstack *h, POINTER obj);
289 _obstack_allocated_p (h, obj)
293 register struct _obstack_chunk* lp; /* below addr of any objects in this chunk */
294 register struct _obstack_chunk* plp; /* point to previous chunk if any */
297 /* We use >= rather than > since the object cannot be exactly at
298 the beginning of the chunk but might be an empty object exactly
299 at the end of an adjacent chunk. */
300 while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
308 /* Free objects in obstack H, including OBJ and everything allocate
309 more recently than OBJ. If OBJ is zero, free everything in H. */
313 /* This function has two names with identical definitions.
314 This is the first one, called from non-ANSI code. */
317 _obstack_free (h, obj)
321 register struct _obstack_chunk* lp; /* below addr of any objects in this chunk */
322 register struct _obstack_chunk* plp; /* point to previous chunk if any */
325 /* We use >= because there cannot be an object at the beginning of a chunk.
326 But there can be an empty object at that address
327 at the end of another chunk. */
328 while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
331 CALL_FREEFUN (h, lp);
333 /* If we switch chunks, we can't tell whether the new current
334 chunk contains an empty object, so assume that it may. */
335 h->maybe_empty_object = 1;
339 h->object_base = h->next_free = (char *)(obj);
340 h->chunk_limit = lp->limit;
344 /* obj is not in any of the chunks! */
348 /* This function is used from ANSI code. */
351 obstack_free (h, obj)
355 register struct _obstack_chunk* lp; /* below addr of any objects in this chunk */
356 register struct _obstack_chunk* plp; /* point to previous chunk if any */
359 /* We use >= because there cannot be an object at the beginning of a chunk.
360 But there can be an empty object at that address
361 at the end of another chunk. */
362 while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
365 CALL_FREEFUN (h, lp);
367 /* If we switch chunks, we can't tell whether the new current
368 chunk contains an empty object, so assume that it may. */
369 h->maybe_empty_object = 1;
373 h->object_base = h->next_free = (char *)(obj);
374 h->chunk_limit = lp->limit;
378 /* obj is not in any of the chunks! */
383 /* These are now turned off because the applications do not use it
384 and it uses bcopy via obstack_grow, which causes trouble on sysV. */
386 /* Now define the functional versions of the obstack macros.
387 Define them to simply use the corresponding macros to do the job. */
389 #if defined (__STDC__) && __STDC__
390 /* These function definitions do not work with non-ANSI preprocessors;
391 they won't pass through the macro names in parentheses. */
393 /* The function names appear in parentheses in order to prevent
394 the macro-definitions of the names from being expanded there. */
396 POINTER (obstack_base) (obstack)
397 struct obstack *obstack;
399 return obstack_base (obstack);
402 POINTER (obstack_next_free) (obstack)
403 struct obstack *obstack;
405 return obstack_next_free (obstack);
408 int (obstack_object_size) (obstack)
409 struct obstack *obstack;
411 return obstack_object_size (obstack);
414 int (obstack_room) (obstack)
415 struct obstack *obstack;
417 return obstack_room (obstack);
420 void (obstack_grow) (obstack, pointer, length)
421 struct obstack *obstack;
425 obstack_grow (obstack, pointer, length);
428 void (obstack_grow0) (obstack, pointer, length)
429 struct obstack *obstack;
433 obstack_grow0 (obstack, pointer, length);
436 void (obstack_1grow) (obstack, character)
437 struct obstack *obstack;
440 obstack_1grow (obstack, character);
443 void (obstack_blank) (obstack, length)
444 struct obstack *obstack;
447 obstack_blank (obstack, length);
450 void (obstack_1grow_fast) (obstack, character)
451 struct obstack *obstack;
454 obstack_1grow_fast (obstack, character);
457 void (obstack_blank_fast) (obstack, length)
458 struct obstack *obstack;
461 obstack_blank_fast (obstack, length);
464 POINTER (obstack_finish) (obstack)
465 struct obstack *obstack;
467 return obstack_finish (obstack);
470 POINTER (obstack_alloc) (obstack, length)
471 struct obstack *obstack;
474 return obstack_alloc (obstack, length);
477 POINTER (obstack_copy) (obstack, pointer, length)
478 struct obstack *obstack;
482 return obstack_copy (obstack, pointer, length);
485 POINTER (obstack_copy0) (obstack, pointer, length)
486 struct obstack *obstack;
490 return obstack_copy0 (obstack, pointer, length);
493 #endif /* __STDC__ */
497 #endif /* !ELIDE_CODE */