1 /* obstack.c - subroutines used implicitly by object stack macros
2 Copyright (C) 1988,89,90,91,92,93,94,96 Free Software Foundation, Inc.
4 This file is part of the GNU C Library. Its master source is NOT part of
5 the C library, however. The master source lives in /gd/gnu/lib.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 /* NOTE BEFORE MODIFYING THIS FILE: This version number must be
25 incremented whenever callers compiled using an old obstack.h can no
26 longer properly call the functions in this obstack.c. */
27 #define OBSTACK_INTERFACE_VERSION 1
29 /* Comment out all this code if we are using the GNU C Library, and are not
30 actually compiling the library itself, and the installed library
31 supports the same library interface we do. This code is part of the GNU
32 C Library, but also included in many other GNU distributions. Compiling
33 and linking in this code is a waste when using the GNU C library
34 (especially if it is a shared library). Rather than having every GNU
35 program understand `configure --with-gnu-libc' and omit the object
36 files, it is simpler to just do this in the source for each such file. */
38 #include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */
39 #if !defined (_LIBC) && defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
40 #include <gnu-versions.h>
41 #if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION
50 #if defined (__STDC__) && __STDC__
51 #define POINTER void *
53 #define POINTER char *
56 /* Determine default alignment. */
57 struct fooalign
{char x
; double d
;};
58 #define DEFAULT_ALIGNMENT \
59 ((PTR_INT_TYPE) ((char *) &((struct fooalign *) 0)->d - (char *) 0))
60 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
61 But in fact it might be less smart and round addresses to as much as
62 DEFAULT_ROUNDING. So we prepare for it to do that. */
63 union fooround
{long x
; double d
;};
64 #define DEFAULT_ROUNDING (sizeof (union fooround))
66 /* When we copy a long block of data, this is the unit to do it with.
67 On some machines, copying successive ints does not work;
68 in such a case, redefine COPYING_UNIT to `long' (if that works)
69 or `char' as a last resort. */
71 #define COPYING_UNIT int
75 /* The functions allocating more room by calling `obstack_chunk_alloc'
76 jump to the handler pointed to by `obstack_alloc_failed_handler'.
77 This variable by default points to the internal function
79 #if defined (__STDC__) && __STDC__
80 static void print_and_abort (void);
81 void (*obstack_alloc_failed_handler
) (void) = print_and_abort
;
83 static void print_and_abort ();
84 void (*obstack_alloc_failed_handler
) () = print_and_abort
;
87 /* Exit value used when `print_and_abort' is used. */
88 #if defined __GNU_LIBRARY__ || defined HAVE_STDLIB_H
92 #define EXIT_FAILURE 1
94 int obstack_exit_failure
= EXIT_FAILURE
;
96 /* The non-GNU-C macros copy the obstack into this global variable
97 to avoid multiple evaluation. */
99 struct obstack
*_obstack
;
101 /* Define a macro that either calls functions with the traditional malloc/free
102 calling interface, or calls functions with the mmalloc/mfree interface
103 (that adds an extra first argument), based on the state of use_extra_arg.
104 For free, do not use ?:, since some compilers, like the MIPS compilers,
105 do not allow (expr) ? void : void. */
107 #if defined (__STDC__) && __STDC__
108 #define CALL_CHUNKFUN(h, size) \
109 (((h) -> use_extra_arg) \
110 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
111 : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
113 #define CALL_FREEFUN(h, old_chunk) \
115 if ((h) -> use_extra_arg) \
116 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
118 (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
121 #define CALL_CHUNKFUN(h, size) \
122 (((h) -> use_extra_arg) \
123 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
124 : (*(struct _obstack_chunk *(*) ()) (h)->chunkfun) ((size)))
126 #define CALL_FREEFUN(h, old_chunk) \
128 if ((h) -> use_extra_arg) \
129 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
131 (*(void (*) ()) (h)->freefun) ((old_chunk)); \
136 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
137 Objects start on multiples of ALIGNMENT (0 means use default).
138 CHUNKFUN is the function to use to allocate chunks,
139 and FREEFUN the function to free them.
141 Return nonzero if successful, zero if out of memory.
142 To recover from an out of memory error,
143 free up some memory, then call this again. */
146 _obstack_begin (h
, size
, alignment
, chunkfun
, freefun
)
150 #if defined (__STDC__) && __STDC__
151 POINTER (*chunkfun
) (long);
152 void (*freefun
) (void *);
154 POINTER (*chunkfun
) ();
158 register struct _obstack_chunk
*chunk
; /* points to new chunk */
161 alignment
= DEFAULT_ALIGNMENT
;
163 /* Default size is what GNU malloc can fit in a 4096-byte block. */
165 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
166 Use the values for range checking, because if range checking is off,
167 the extra bytes won't be missed terribly, but if range checking is on
168 and we used a larger request, a whole extra 4096 bytes would be
171 These number are irrelevant to the new GNU malloc. I suspect it is
172 less sensitive to the size of the request. */
173 int extra
= ((((12 + DEFAULT_ROUNDING
- 1) & ~(DEFAULT_ROUNDING
- 1))
174 + 4 + DEFAULT_ROUNDING
- 1)
175 & ~(DEFAULT_ROUNDING
- 1));
179 #if defined (__STDC__) && __STDC__
180 h
->chunkfun
= (struct _obstack_chunk
* (*)(void *, long)) chunkfun
;
181 h
->freefun
= (void (*) (void *, struct _obstack_chunk
*)) freefun
;
183 h
->chunkfun
= (struct _obstack_chunk
* (*)()) chunkfun
;
184 h
->freefun
= freefun
;
186 h
->chunk_size
= size
;
187 h
->alignment_mask
= alignment
- 1;
188 h
->use_extra_arg
= 0;
190 chunk
= h
->chunk
= CALL_CHUNKFUN (h
, h
-> chunk_size
);
192 (*obstack_alloc_failed_handler
) ();
193 h
->next_free
= h
->object_base
= chunk
->contents
;
194 h
->chunk_limit
= chunk
->limit
195 = (char *) chunk
+ h
->chunk_size
;
197 /* The initial chunk now contains no empty object. */
198 h
->maybe_empty_object
= 0;
204 _obstack_begin_1 (h
, size
, alignment
, chunkfun
, freefun
, arg
)
208 #if defined (__STDC__) && __STDC__
209 POINTER (*chunkfun
) (POINTER
, long);
210 void (*freefun
) (POINTER
, POINTER
);
212 POINTER (*chunkfun
) ();
217 register struct _obstack_chunk
*chunk
; /* points to new chunk */
220 alignment
= DEFAULT_ALIGNMENT
;
222 /* Default size is what GNU malloc can fit in a 4096-byte block. */
224 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
225 Use the values for range checking, because if range checking is off,
226 the extra bytes won't be missed terribly, but if range checking is on
227 and we used a larger request, a whole extra 4096 bytes would be
230 These number are irrelevant to the new GNU malloc. I suspect it is
231 less sensitive to the size of the request. */
232 int extra
= ((((12 + DEFAULT_ROUNDING
- 1) & ~(DEFAULT_ROUNDING
- 1))
233 + 4 + DEFAULT_ROUNDING
- 1)
234 & ~(DEFAULT_ROUNDING
- 1));
238 #if defined(__STDC__) && __STDC__
239 h
->chunkfun
= (struct _obstack_chunk
* (*)(void *,long)) chunkfun
;
240 h
->freefun
= (void (*) (void *, struct _obstack_chunk
*)) freefun
;
242 h
->chunkfun
= (struct _obstack_chunk
* (*)()) chunkfun
;
243 h
->freefun
= freefun
;
245 h
->chunk_size
= size
;
246 h
->alignment_mask
= alignment
- 1;
248 h
->use_extra_arg
= 1;
250 chunk
= h
->chunk
= CALL_CHUNKFUN (h
, h
-> chunk_size
);
252 (*obstack_alloc_failed_handler
) ();
253 h
->next_free
= h
->object_base
= chunk
->contents
;
254 h
->chunk_limit
= chunk
->limit
255 = (char *) chunk
+ h
->chunk_size
;
257 /* The initial chunk now contains no empty object. */
258 h
->maybe_empty_object
= 0;
263 /* Allocate a new current chunk for the obstack *H
264 on the assumption that LENGTH bytes need to be added
265 to the current object, or a new object of length LENGTH allocated.
266 Copies any partial object from the end of the old chunk
267 to the beginning of the new one. */
270 _obstack_newchunk (h
, length
)
274 register struct _obstack_chunk
*old_chunk
= h
->chunk
;
275 register struct _obstack_chunk
*new_chunk
;
276 register long new_size
;
277 register int obj_size
= h
->next_free
- h
->object_base
;
281 /* Compute size for new chunk. */
282 new_size
= (obj_size
+ length
) + (obj_size
>> 3) + 100;
283 if (new_size
< h
->chunk_size
)
284 new_size
= h
->chunk_size
;
286 /* Allocate and initialize the new chunk. */
287 new_chunk
= CALL_CHUNKFUN (h
, new_size
);
289 (*obstack_alloc_failed_handler
) ();
290 h
->chunk
= new_chunk
;
291 new_chunk
->prev
= old_chunk
;
292 new_chunk
->limit
= h
->chunk_limit
= (char *) new_chunk
+ new_size
;
294 /* Move the existing object to the new chunk.
295 Word at a time is fast and is safe if the object
296 is sufficiently aligned. */
297 if (h
->alignment_mask
+ 1 >= DEFAULT_ALIGNMENT
)
299 for (i
= obj_size
/ sizeof (COPYING_UNIT
) - 1;
301 ((COPYING_UNIT
*)new_chunk
->contents
)[i
]
302 = ((COPYING_UNIT
*)h
->object_base
)[i
];
303 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
304 but that can cross a page boundary on a machine
305 which does not do strict alignment for COPYING_UNITS. */
306 already
= obj_size
/ sizeof (COPYING_UNIT
) * sizeof (COPYING_UNIT
);
310 /* Copy remaining bytes one by one. */
311 for (i
= already
; i
< obj_size
; i
++)
312 new_chunk
->contents
[i
] = h
->object_base
[i
];
314 /* If the object just copied was the only data in OLD_CHUNK,
315 free that chunk and remove it from the chain.
316 But not if that chunk might contain an empty object. */
317 if (h
->object_base
== old_chunk
->contents
&& ! h
->maybe_empty_object
)
319 new_chunk
->prev
= old_chunk
->prev
;
320 CALL_FREEFUN (h
, old_chunk
);
323 h
->object_base
= new_chunk
->contents
;
324 h
->next_free
= h
->object_base
+ obj_size
;
325 /* The new chunk certainly contains no empty object yet. */
326 h
->maybe_empty_object
= 0;
329 /* Return nonzero if object OBJ has been allocated from obstack H.
330 This is here for debugging.
331 If you use it in a program, you are probably losing. */
333 #if defined (__STDC__) && __STDC__
334 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
335 obstack.h because it is just for debugging. */
336 int _obstack_allocated_p (struct obstack
*h
, POINTER obj
);
340 _obstack_allocated_p (h
, obj
)
344 register struct _obstack_chunk
*lp
; /* below addr of any objects in this chunk */
345 register struct _obstack_chunk
*plp
; /* point to previous chunk if any */
348 /* We use >= rather than > since the object cannot be exactly at
349 the beginning of the chunk but might be an empty object exactly
350 at the end of an adjacent chunk. */
351 while (lp
!= 0 && ((POINTER
) lp
>= obj
|| (POINTER
) (lp
)->limit
< obj
))
359 /* Free objects in obstack H, including OBJ and everything allocate
360 more recently than OBJ. If OBJ is zero, free everything in H. */
364 /* This function has two names with identical definitions.
365 This is the first one, called from non-ANSI code. */
368 _obstack_free (h
, obj
)
372 register struct _obstack_chunk
*lp
; /* below addr of any objects in this chunk */
373 register struct _obstack_chunk
*plp
; /* point to previous chunk if any */
376 /* We use >= because there cannot be an object at the beginning of a chunk.
377 But there can be an empty object at that address
378 at the end of another chunk. */
379 while (lp
!= 0 && ((POINTER
) lp
>= obj
|| (POINTER
) (lp
)->limit
< obj
))
382 CALL_FREEFUN (h
, lp
);
384 /* If we switch chunks, we can't tell whether the new current
385 chunk contains an empty object, so assume that it may. */
386 h
->maybe_empty_object
= 1;
390 h
->object_base
= h
->next_free
= (char *) (obj
);
391 h
->chunk_limit
= lp
->limit
;
395 /* obj is not in any of the chunks! */
399 /* This function is used from ANSI code. */
402 obstack_free (h
, obj
)
406 register struct _obstack_chunk
*lp
; /* below addr of any objects in this chunk */
407 register struct _obstack_chunk
*plp
; /* point to previous chunk if any */
410 /* We use >= because there cannot be an object at the beginning of a chunk.
411 But there can be an empty object at that address
412 at the end of another chunk. */
413 while (lp
!= 0 && ((POINTER
) lp
>= obj
|| (POINTER
) (lp
)->limit
< obj
))
416 CALL_FREEFUN (h
, lp
);
418 /* If we switch chunks, we can't tell whether the new current
419 chunk contains an empty object, so assume that it may. */
420 h
->maybe_empty_object
= 1;
424 h
->object_base
= h
->next_free
= (char *) (obj
);
425 h
->chunk_limit
= lp
->limit
;
429 /* obj is not in any of the chunks! */
434 _obstack_memory_used (h
)
437 register struct _obstack_chunk
* lp
;
438 register int nbytes
= 0;
440 for (lp
= h
->chunk
; lp
!= 0; lp
= lp
->prev
)
442 nbytes
+= lp
->limit
- (char *) lp
;
447 /* Define the error handler. */
449 # ifdef HAVE_LIBINTL_H
450 # include <libintl.h>
452 # define _(Str) gettext (Str)
455 # define _(Str) (Str)
462 fputs (_("memory exhausted\n"), stderr
);
463 exit (obstack_exit_failure
);
467 /* These are now turned off because the applications do not use it
468 and it uses bcopy via obstack_grow, which causes trouble on sysV. */
470 /* Now define the functional versions of the obstack macros.
471 Define them to simply use the corresponding macros to do the job. */
473 #if defined (__STDC__) && __STDC__
474 /* These function definitions do not work with non-ANSI preprocessors;
475 they won't pass through the macro names in parentheses. */
477 /* The function names appear in parentheses in order to prevent
478 the macro-definitions of the names from being expanded there. */
480 POINTER (obstack_base
) (obstack
)
481 struct obstack
*obstack
;
483 return obstack_base (obstack
);
486 POINTER (obstack_next_free
) (obstack
)
487 struct obstack
*obstack
;
489 return obstack_next_free (obstack
);
492 int (obstack_object_size
) (obstack
)
493 struct obstack
*obstack
;
495 return obstack_object_size (obstack
);
498 int (obstack_room
) (obstack
)
499 struct obstack
*obstack
;
501 return obstack_room (obstack
);
504 int (obstack_make_room
) (obstack
, length
)
505 struct obstack
*obstack
;
508 return obstack_make_room (obstack
, length
);
511 void (obstack_grow
) (obstack
, pointer
, length
)
512 struct obstack
*obstack
;
516 obstack_grow (obstack
, pointer
, length
);
519 void (obstack_grow0
) (obstack
, pointer
, length
)
520 struct obstack
*obstack
;
524 obstack_grow0 (obstack
, pointer
, length
);
527 void (obstack_1grow
) (obstack
, character
)
528 struct obstack
*obstack
;
531 obstack_1grow (obstack
, character
);
534 void (obstack_blank
) (obstack
, length
)
535 struct obstack
*obstack
;
538 obstack_blank (obstack
, length
);
541 void (obstack_1grow_fast
) (obstack
, character
)
542 struct obstack
*obstack
;
545 obstack_1grow_fast (obstack
, character
);
548 void (obstack_blank_fast
) (obstack
, length
)
549 struct obstack
*obstack
;
552 obstack_blank_fast (obstack
, length
);
555 POINTER (obstack_finish
) (obstack
)
556 struct obstack
*obstack
;
558 return obstack_finish (obstack
);
561 POINTER (obstack_alloc
) (obstack
, length
)
562 struct obstack
*obstack
;
565 return obstack_alloc (obstack
, length
);
568 POINTER (obstack_copy
) (obstack
, pointer
, length
)
569 struct obstack
*obstack
;
573 return obstack_copy (obstack
, pointer
, length
);
576 POINTER (obstack_copy0
) (obstack
, pointer
, length
)
577 struct obstack
*obstack
;
581 return obstack_copy0 (obstack
, pointer
, length
);
584 #endif /* __STDC__ */
588 #endif /* !ELIDE_CODE */