1 /* obstack.c - subroutines used implicitly by object stack macros
3 Copyright (C) 1988-1994, 1996, 1997, 1998, 1999, 2000, 2001, 2002
4 Free Software Foundation, Inc.
6 This file is part of the GNU C Library. Its master source is NOT part of
7 the C library, however. The master source lives in /gd/gnu/lib.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software Foundation,
21 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
29 /* NOTE BEFORE MODIFYING THIS FILE: This version number must be
30 incremented whenever callers compiled using an old obstack.h can no
31 longer properly call the functions in this obstack.c. */
32 #define OBSTACK_INTERFACE_VERSION 1
34 /* Comment out all this code if we are using the GNU C Library, and are not
35 actually compiling the library itself, and the installed library
36 supports the same library interface we do. This code is part of the GNU
37 C Library, but also included in many other GNU distributions. Compiling
38 and linking in this code is a waste when using the GNU C library
39 (especially if it is a shared library). Rather than having every GNU
40 program understand `configure --with-gnu-libc' and omit the object
41 files, it is simpler to just do this in the source for each such file. */
43 #include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */
44 #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
45 # include <gnu-versions.h>
46 # if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION
51 #if defined _LIBC && defined USE_IN_LIBIO
59 # define POINTER void *
61 # define POINTER char *
64 /* Determine default alignment. */
65 struct fooalign
{char x
; double d
;};
66 # define DEFAULT_ALIGNMENT \
67 ((PTR_INT_TYPE) ((char *) &((struct fooalign *) 0)->d - (char *) 0))
68 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
69 But in fact it might be less smart and round addresses to as much as
70 DEFAULT_ROUNDING. So we prepare for it to do that. */
71 union fooround
{long x
; double d
;};
72 # define DEFAULT_ROUNDING (sizeof (union fooround))
74 /* When we copy a long block of data, this is the unit to do it with.
75 On some machines, copying successive ints does not work;
76 in such a case, redefine COPYING_UNIT to `long' (if that works)
77 or `char' as a last resort. */
79 # define COPYING_UNIT int
83 /* The functions allocating more room by calling `obstack_chunk_alloc'
84 jump to the handler pointed to by `obstack_alloc_failed_handler'.
85 This can be set to a user defined function which should either
86 abort gracefully or use longjump - but shouldn't return. This
87 variable by default points to the internal function
89 # if PROTOTYPES || (defined __STDC__ && __STDC__)
90 static void print_and_abort (void);
91 void (*obstack_alloc_failed_handler
) (void) = print_and_abort
;
93 static void print_and_abort ();
94 void (*obstack_alloc_failed_handler
) () = print_and_abort
;
97 /* Exit value used when `print_and_abort' is used. */
98 # if defined __GNU_LIBRARY__ || defined HAVE_STDLIB_H
101 # ifndef EXIT_FAILURE
102 # define EXIT_FAILURE 1
104 int obstack_exit_failure
= EXIT_FAILURE
;
106 /* The non-GNU-C macros copy the obstack into this global variable
107 to avoid multiple evaluation. */
109 struct obstack
*_obstack
;
111 /* Define a macro that either calls functions with the traditional malloc/free
112 calling interface, or calls functions with the mmalloc/mfree interface
113 (that adds an extra first argument), based on the state of use_extra_arg.
114 For free, do not use ?:, since some compilers, like the MIPS compilers,
115 do not allow (expr) ? void : void. */
117 # if PROTOTYPES || (defined __STDC__ && __STDC__)
118 # define CALL_CHUNKFUN(h, size) \
119 (((h) -> use_extra_arg) \
120 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
121 : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
123 # define CALL_FREEFUN(h, old_chunk) \
125 if ((h) -> use_extra_arg) \
126 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
128 (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
131 # define CALL_CHUNKFUN(h, size) \
132 (((h) -> use_extra_arg) \
133 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
134 : (*(struct _obstack_chunk *(*) ()) (h)->chunkfun) ((size)))
136 # define CALL_FREEFUN(h, old_chunk) \
138 if ((h) -> use_extra_arg) \
139 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
141 (*(void (*) ()) (h)->freefun) ((old_chunk)); \
146 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
147 Objects start on multiples of ALIGNMENT (0 means use default).
148 CHUNKFUN is the function to use to allocate chunks,
149 and FREEFUN the function to free them.
151 Return nonzero if successful, calls obstack_alloc_failed_handler if
155 _obstack_begin (h
, size
, alignment
, chunkfun
, freefun
)
159 # if PROTOTYPES || (defined __STDC__ && __STDC__)
160 POINTER (*chunkfun
) (long);
161 void (*freefun
) (void *);
163 POINTER (*chunkfun
) ();
167 register struct _obstack_chunk
*chunk
; /* points to new chunk */
170 alignment
= (int) DEFAULT_ALIGNMENT
;
172 /* Default size is what GNU malloc can fit in a 4096-byte block. */
174 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
175 Use the values for range checking, because if range checking is off,
176 the extra bytes won't be missed terribly, but if range checking is on
177 and we used a larger request, a whole extra 4096 bytes would be
180 These number are irrelevant to the new GNU malloc. I suspect it is
181 less sensitive to the size of the request. */
182 int extra
= ((((12 + DEFAULT_ROUNDING
- 1) & ~(DEFAULT_ROUNDING
- 1))
183 + 4 + DEFAULT_ROUNDING
- 1)
184 & ~(DEFAULT_ROUNDING
- 1));
188 # if PROTOTYPES || (defined __STDC__ && __STDC__)
189 h
->chunkfun
= (struct _obstack_chunk
* (*)(void *, long)) chunkfun
;
190 h
->freefun
= (void (*) (void *, struct _obstack_chunk
*)) freefun
;
192 h
->chunkfun
= (struct _obstack_chunk
* (*)()) chunkfun
;
193 h
->freefun
= freefun
;
195 h
->chunk_size
= size
;
196 h
->alignment_mask
= alignment
- 1;
197 h
->use_extra_arg
= 0;
199 chunk
= h
->chunk
= CALL_CHUNKFUN (h
, h
-> chunk_size
);
201 (*obstack_alloc_failed_handler
) ();
202 h
->next_free
= h
->object_base
= chunk
->contents
;
203 h
->chunk_limit
= chunk
->limit
204 = (char *) chunk
+ h
->chunk_size
;
206 /* The initial chunk now contains no empty object. */
207 h
->maybe_empty_object
= 0;
213 _obstack_begin_1 (h
, size
, alignment
, chunkfun
, freefun
, arg
)
217 # if PROTOTYPES || (defined __STDC__ && __STDC__)
218 POINTER (*chunkfun
) (POINTER
, long);
219 void (*freefun
) (POINTER
, POINTER
);
221 POINTER (*chunkfun
) ();
226 register struct _obstack_chunk
*chunk
; /* points to new chunk */
229 alignment
= (int) DEFAULT_ALIGNMENT
;
231 /* Default size is what GNU malloc can fit in a 4096-byte block. */
233 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
234 Use the values for range checking, because if range checking is off,
235 the extra bytes won't be missed terribly, but if range checking is on
236 and we used a larger request, a whole extra 4096 bytes would be
239 These number are irrelevant to the new GNU malloc. I suspect it is
240 less sensitive to the size of the request. */
241 int extra
= ((((12 + DEFAULT_ROUNDING
- 1) & ~(DEFAULT_ROUNDING
- 1))
242 + 4 + DEFAULT_ROUNDING
- 1)
243 & ~(DEFAULT_ROUNDING
- 1));
247 # if PROTOTYPES || (defined __STDC__ && __STDC__)
248 h
->chunkfun
= (struct _obstack_chunk
* (*)(void *,long)) chunkfun
;
249 h
->freefun
= (void (*) (void *, struct _obstack_chunk
*)) freefun
;
251 h
->chunkfun
= (struct _obstack_chunk
* (*)()) chunkfun
;
252 h
->freefun
= freefun
;
254 h
->chunk_size
= size
;
255 h
->alignment_mask
= alignment
- 1;
257 h
->use_extra_arg
= 1;
259 chunk
= h
->chunk
= CALL_CHUNKFUN (h
, h
-> chunk_size
);
261 (*obstack_alloc_failed_handler
) ();
262 h
->next_free
= h
->object_base
= chunk
->contents
;
263 h
->chunk_limit
= chunk
->limit
264 = (char *) chunk
+ h
->chunk_size
;
266 /* The initial chunk now contains no empty object. */
267 h
->maybe_empty_object
= 0;
272 /* Allocate a new current chunk for the obstack *H
273 on the assumption that LENGTH bytes need to be added
274 to the current object, or a new object of length LENGTH allocated.
275 Copies any partial object from the end of the old chunk
276 to the beginning of the new one. */
279 _obstack_newchunk (h
, length
)
283 register struct _obstack_chunk
*old_chunk
= h
->chunk
;
284 register struct _obstack_chunk
*new_chunk
;
285 register long new_size
;
286 register long obj_size
= h
->next_free
- h
->object_base
;
291 /* Compute size for new chunk. */
292 new_size
= (obj_size
+ length
) + (obj_size
>> 3) + h
->alignment_mask
+ 100;
293 if (new_size
< h
->chunk_size
)
294 new_size
= h
->chunk_size
;
296 /* Allocate and initialize the new chunk. */
297 new_chunk
= CALL_CHUNKFUN (h
, new_size
);
299 (*obstack_alloc_failed_handler
) ();
300 h
->chunk
= new_chunk
;
301 new_chunk
->prev
= old_chunk
;
302 new_chunk
->limit
= h
->chunk_limit
= (char *) new_chunk
+ new_size
;
304 /* Compute an aligned object_base in the new chunk */
306 __INT_TO_PTR ((__PTR_TO_INT (new_chunk
->contents
) + h
->alignment_mask
)
307 & ~ (h
->alignment_mask
));
309 /* Move the existing object to the new chunk.
310 Word at a time is fast and is safe if the object
311 is sufficiently aligned. */
312 if (h
->alignment_mask
+ 1 >= DEFAULT_ALIGNMENT
)
314 for (i
= obj_size
/ sizeof (COPYING_UNIT
) - 1;
316 ((COPYING_UNIT
*)object_base
)[i
]
317 = ((COPYING_UNIT
*)h
->object_base
)[i
];
318 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
319 but that can cross a page boundary on a machine
320 which does not do strict alignment for COPYING_UNITS. */
321 already
= obj_size
/ sizeof (COPYING_UNIT
) * sizeof (COPYING_UNIT
);
325 /* Copy remaining bytes one by one. */
326 for (i
= already
; i
< obj_size
; i
++)
327 object_base
[i
] = h
->object_base
[i
];
329 /* If the object just copied was the only data in OLD_CHUNK,
330 free that chunk and remove it from the chain.
331 But not if that chunk might contain an empty object. */
332 if (h
->object_base
== old_chunk
->contents
&& ! h
->maybe_empty_object
)
334 new_chunk
->prev
= old_chunk
->prev
;
335 CALL_FREEFUN (h
, old_chunk
);
338 h
->object_base
= object_base
;
339 h
->next_free
= h
->object_base
+ obj_size
;
340 /* The new chunk certainly contains no empty object yet. */
341 h
->maybe_empty_object
= 0;
344 /* Return nonzero if object OBJ has been allocated from obstack H.
345 This is here for debugging.
346 If you use it in a program, you are probably losing. */
348 # if PROTOTYPES || (defined __STDC__ && __STDC__)
349 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
350 obstack.h because it is just for debugging. */
351 int _obstack_allocated_p (struct obstack
*h
, POINTER obj
);
355 _obstack_allocated_p (h
, obj
)
359 register struct _obstack_chunk
*lp
; /* below addr of any objects in this chunk */
360 register struct _obstack_chunk
*plp
; /* point to previous chunk if any */
363 /* We use >= rather than > since the object cannot be exactly at
364 the beginning of the chunk but might be an empty object exactly
365 at the end of an adjacent chunk. */
366 while (lp
!= 0 && ((POINTER
) lp
>= obj
|| (POINTER
) (lp
)->limit
< obj
))
374 /* Free objects in obstack H, including OBJ and everything allocate
375 more recently than OBJ. If OBJ is zero, free everything in H. */
379 /* This function has two names with identical definitions.
380 This is the first one, called from non-ANSI code. */
383 _obstack_free (h
, obj
)
387 register struct _obstack_chunk
*lp
; /* below addr of any objects in this chunk */
388 register struct _obstack_chunk
*plp
; /* point to previous chunk if any */
391 /* We use >= because there cannot be an object at the beginning of a chunk.
392 But there can be an empty object at that address
393 at the end of another chunk. */
394 while (lp
!= 0 && ((POINTER
) lp
>= obj
|| (POINTER
) (lp
)->limit
< obj
))
397 CALL_FREEFUN (h
, lp
);
399 /* If we switch chunks, we can't tell whether the new current
400 chunk contains an empty object, so assume that it may. */
401 h
->maybe_empty_object
= 1;
405 h
->object_base
= h
->next_free
= (char *) (obj
);
406 h
->chunk_limit
= lp
->limit
;
410 /* obj is not in any of the chunks! */
414 /* This function is used from ANSI code. */
417 obstack_free (h
, obj
)
421 register struct _obstack_chunk
*lp
; /* below addr of any objects in this chunk */
422 register struct _obstack_chunk
*plp
; /* point to previous chunk if any */
425 /* We use >= because there cannot be an object at the beginning of a chunk.
426 But there can be an empty object at that address
427 at the end of another chunk. */
428 while (lp
!= 0 && ((POINTER
) lp
>= obj
|| (POINTER
) (lp
)->limit
< obj
))
431 CALL_FREEFUN (h
, lp
);
433 /* If we switch chunks, we can't tell whether the new current
434 chunk contains an empty object, so assume that it may. */
435 h
->maybe_empty_object
= 1;
439 h
->object_base
= h
->next_free
= (char *) (obj
);
440 h
->chunk_limit
= lp
->limit
;
444 /* obj is not in any of the chunks! */
449 _obstack_memory_used (h
)
452 register struct _obstack_chunk
* lp
;
453 register int nbytes
= 0;
455 for (lp
= h
->chunk
; lp
!= 0; lp
= lp
->prev
)
457 nbytes
+= lp
->limit
- (char *) lp
;
462 /* Define the error handler. */
464 # if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
465 # include <libintl.h>
467 # define _(Str) gettext (Str)
470 # define _(Str) (Str)
473 # if defined _LIBC && defined USE_IN_LIBIO
474 # include <libio/iolibio.h>
475 # define fputs(s, f) _IO_fputs (s, f)
478 # ifndef __attribute__
479 /* This feature is available in gcc versions 2.5 and later. */
480 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
481 # define __attribute__(Spec) /* empty */
486 __attribute__ ((noreturn
))
489 /* Don't change any of these strings. Yes, it would be possible to add
490 the newline to the string and use fputs or so. But this must not
491 happen because the "memory exhausted" message appears in other places
492 like this and the translation should be reused instead of creating
493 a very similar string which requires a separate translation. */
494 # if defined _LIBC && defined USE_IN_LIBIO
495 if (_IO_fwide (stderr
, 0) > 0)
496 __fwprintf (stderr
, L
"%s\n", _("memory exhausted"));
499 fprintf (stderr
, "%s\n", _("memory exhausted"));
500 exit (obstack_exit_failure
);
504 /* These are now turned off because the applications do not use it
505 and it uses bcopy via obstack_grow, which causes trouble on sysV. */
507 /* Now define the functional versions of the obstack macros.
508 Define them to simply use the corresponding macros to do the job. */
510 # if PROTOTYPES || (defined __STDC__ && __STDC__)
511 /* These function definitions do not work with non-ANSI preprocessors;
512 they won't pass through the macro names in parentheses. */
514 /* The function names appear in parentheses in order to prevent
515 the macro-definitions of the names from being expanded there. */
517 POINTER (obstack_base
) (obstack
)
518 struct obstack
*obstack
;
520 return obstack_base (obstack
);
523 POINTER (obstack_next_free
) (obstack
)
524 struct obstack
*obstack
;
526 return obstack_next_free (obstack
);
529 int (obstack_object_size
) (obstack
)
530 struct obstack
*obstack
;
532 return obstack_object_size (obstack
);
535 int (obstack_room
) (obstack
)
536 struct obstack
*obstack
;
538 return obstack_room (obstack
);
541 int (obstack_make_room
) (obstack
, length
)
542 struct obstack
*obstack
;
545 return obstack_make_room (obstack
, length
);
548 void (obstack_grow
) (obstack
, data
, length
)
549 struct obstack
*obstack
;
553 obstack_grow (obstack
, data
, length
);
556 void (obstack_grow0
) (obstack
, data
, length
)
557 struct obstack
*obstack
;
561 obstack_grow0 (obstack
, data
, length
);
564 void (obstack_1grow
) (obstack
, character
)
565 struct obstack
*obstack
;
568 obstack_1grow (obstack
, character
);
571 void (obstack_blank
) (obstack
, length
)
572 struct obstack
*obstack
;
575 obstack_blank (obstack
, length
);
578 void (obstack_1grow_fast
) (obstack
, character
)
579 struct obstack
*obstack
;
582 obstack_1grow_fast (obstack
, character
);
585 void (obstack_blank_fast
) (obstack
, length
)
586 struct obstack
*obstack
;
589 obstack_blank_fast (obstack
, length
);
592 POINTER (obstack_finish
) (obstack
)
593 struct obstack
*obstack
;
595 return obstack_finish (obstack
);
598 POINTER (obstack_alloc
) (obstack
, length
)
599 struct obstack
*obstack
;
602 return obstack_alloc (obstack
, length
);
605 POINTER (obstack_copy
) (obstack
, address
, length
)
606 struct obstack
*obstack
;
607 const POINTER address
;
610 return obstack_copy (obstack
, address
, length
);
613 POINTER (obstack_copy0
) (obstack
, address
, length
)
614 struct obstack
*obstack
;
615 const POINTER address
;
618 return obstack_copy0 (obstack
, address
, length
);
621 # endif /* PROTOTYPES || (defined __STDC__ && __STDC__) */
625 #endif /* !ELIDE_CODE */