1 /* obstack.c - subroutines used implicitly by object stack macros
2 Copyright (C) 1988-1994, 1996-2001, 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library. Its master source is NOT part of
4 the C library, however. The master source lives in /gd/gnu/lib.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
31 /* NOTE BEFORE MODIFYING THIS FILE: This version number must be
32 incremented whenever callers compiled using an old obstack.h can no
33 longer properly call the functions in this obstack.c. */
34 #define OBSTACK_INTERFACE_VERSION 1
36 /* Comment out all this code if we are using the GNU C Library, and are not
37 actually compiling the library itself, and the installed library
38 supports the same library interface we do. This code is part of the GNU
39 C Library, but also included in many other GNU distributions. Compiling
40 and linking in this code is a waste when using the GNU C library
41 (especially if it is a shared library). Rather than having every GNU
42 program understand `configure --with-gnu-libc' and omit the object
43 files, it is simpler to just do this in the source for each such file. */
45 #include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */
46 #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
47 # include <gnu-versions.h>
48 # if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION
53 #if defined _LIBC && defined USE_IN_LIBIO
60 # if defined __STDC__ && __STDC__
61 # define POINTER void *
63 # define POINTER char *
66 /* Determine default alignment. */
67 struct fooalign
{char x
; double d
;};
68 # define DEFAULT_ALIGNMENT \
69 ((PTR_INT_TYPE) ((char *) &((struct fooalign *) 0)->d - (char *) 0))
70 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
71 But in fact it might be less smart and round addresses to as much as
72 DEFAULT_ROUNDING. So we prepare for it to do that. */
73 union fooround
{long x
; double d
;};
74 # define DEFAULT_ROUNDING (sizeof (union fooround))
76 /* When we copy a long block of data, this is the unit to do it with.
77 On some machines, copying successive ints does not work;
78 in such a case, redefine COPYING_UNIT to `long' (if that works)
79 or `char' as a last resort. */
81 # define COPYING_UNIT int
85 /* The functions allocating more room by calling `obstack_chunk_alloc'
86 jump to the handler pointed to by `obstack_alloc_failed_handler'.
87 This can be set to a user defined function which should either
88 abort gracefully or use longjump - but shouldn't return. This
89 variable by default points to the internal function
91 # if defined __STDC__ && __STDC__
92 static void print_and_abort (void);
93 void (*obstack_alloc_failed_handler
) (void) = print_and_abort
;
95 static void print_and_abort ();
96 void (*obstack_alloc_failed_handler
) () = print_and_abort
;
99 /* Exit value used when `print_and_abort' is used. */
100 # if defined __GNU_LIBRARY__ || defined HAVE_STDLIB_H
103 # ifndef EXIT_FAILURE
104 # define EXIT_FAILURE 1
106 int obstack_exit_failure
= EXIT_FAILURE
;
108 /* The non-GNU-C macros copy the obstack into this global variable
109 to avoid multiple evaluation. */
111 struct obstack
*_obstack
;
113 /* Define a macro that either calls functions with the traditional malloc/free
114 calling interface, or calls functions with the mmalloc/mfree interface
115 (that adds an extra first argument), based on the state of use_extra_arg.
116 For free, do not use ?:, since some compilers, like the MIPS compilers,
117 do not allow (expr) ? void : void. */
119 # if defined __STDC__ && __STDC__
120 # define CALL_CHUNKFUN(h, size) \
121 (((h) -> use_extra_arg) \
122 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
123 : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
125 # define CALL_FREEFUN(h, old_chunk) \
127 if ((h) -> use_extra_arg) \
128 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
130 (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
133 # define CALL_CHUNKFUN(h, size) \
134 (((h) -> use_extra_arg) \
135 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
136 : (*(struct _obstack_chunk *(*) ()) (h)->chunkfun) ((size)))
138 # define CALL_FREEFUN(h, old_chunk) \
140 if ((h) -> use_extra_arg) \
141 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
143 (*(void (*) ()) (h)->freefun) ((old_chunk)); \
148 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
149 Objects start on multiples of ALIGNMENT (0 means use default).
150 CHUNKFUN is the function to use to allocate chunks,
151 and FREEFUN the function to free them.
153 Return nonzero if successful, calls obstack_alloc_failed_handler if
157 _obstack_begin (h
, size
, alignment
, chunkfun
, freefun
)
161 # if defined __STDC__ && __STDC__
162 POINTER (*chunkfun
) (long);
163 void (*freefun
) (void *);
165 POINTER (*chunkfun
) ();
169 register struct _obstack_chunk
*chunk
; /* points to new chunk */
172 alignment
= (int) DEFAULT_ALIGNMENT
;
174 /* Default size is what GNU malloc can fit in a 4096-byte block. */
176 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
177 Use the values for range checking, because if range checking is off,
178 the extra bytes won't be missed terribly, but if range checking is on
179 and we used a larger request, a whole extra 4096 bytes would be
182 These number are irrelevant to the new GNU malloc. I suspect it is
183 less sensitive to the size of the request. */
184 int extra
= ((((12 + DEFAULT_ROUNDING
- 1) & ~(DEFAULT_ROUNDING
- 1))
185 + 4 + DEFAULT_ROUNDING
- 1)
186 & ~(DEFAULT_ROUNDING
- 1));
190 # if defined __STDC__ && __STDC__
191 h
->chunkfun
= (struct _obstack_chunk
* (*)(void *, long)) chunkfun
;
192 h
->freefun
= (void (*) (void *, struct _obstack_chunk
*)) freefun
;
194 h
->chunkfun
= (struct _obstack_chunk
* (*)()) chunkfun
;
195 h
->freefun
= freefun
;
197 h
->chunk_size
= size
;
198 h
->alignment_mask
= alignment
- 1;
199 h
->use_extra_arg
= 0;
201 chunk
= h
->chunk
= CALL_CHUNKFUN (h
, h
-> chunk_size
);
203 (*obstack_alloc_failed_handler
) ();
204 h
->next_free
= h
->object_base
= chunk
->contents
;
205 h
->chunk_limit
= chunk
->limit
206 = (char *) chunk
+ h
->chunk_size
;
208 /* The initial chunk now contains no empty object. */
209 h
->maybe_empty_object
= 0;
215 _obstack_begin_1 (h
, size
, alignment
, chunkfun
, freefun
, arg
)
219 # if defined __STDC__ && __STDC__
220 POINTER (*chunkfun
) (POINTER
, long);
221 void (*freefun
) (POINTER
, POINTER
);
223 POINTER (*chunkfun
) ();
228 register struct _obstack_chunk
*chunk
; /* points to new chunk */
231 alignment
= (int) DEFAULT_ALIGNMENT
;
233 /* Default size is what GNU malloc can fit in a 4096-byte block. */
235 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
236 Use the values for range checking, because if range checking is off,
237 the extra bytes won't be missed terribly, but if range checking is on
238 and we used a larger request, a whole extra 4096 bytes would be
241 These number are irrelevant to the new GNU malloc. I suspect it is
242 less sensitive to the size of the request. */
243 int extra
= ((((12 + DEFAULT_ROUNDING
- 1) & ~(DEFAULT_ROUNDING
- 1))
244 + 4 + DEFAULT_ROUNDING
- 1)
245 & ~(DEFAULT_ROUNDING
- 1));
249 # if defined __STDC__ && __STDC__
250 h
->chunkfun
= (struct _obstack_chunk
* (*)(void *,long)) chunkfun
;
251 h
->freefun
= (void (*) (void *, struct _obstack_chunk
*)) freefun
;
253 h
->chunkfun
= (struct _obstack_chunk
* (*)()) chunkfun
;
254 h
->freefun
= freefun
;
256 h
->chunk_size
= size
;
257 h
->alignment_mask
= alignment
- 1;
259 h
->use_extra_arg
= 1;
261 chunk
= h
->chunk
= CALL_CHUNKFUN (h
, h
-> chunk_size
);
263 (*obstack_alloc_failed_handler
) ();
264 h
->next_free
= h
->object_base
= chunk
->contents
;
265 h
->chunk_limit
= chunk
->limit
266 = (char *) chunk
+ h
->chunk_size
;
268 /* The initial chunk now contains no empty object. */
269 h
->maybe_empty_object
= 0;
274 /* Allocate a new current chunk for the obstack *H
275 on the assumption that LENGTH bytes need to be added
276 to the current object, or a new object of length LENGTH allocated.
277 Copies any partial object from the end of the old chunk
278 to the beginning of the new one. */
281 _obstack_newchunk (h
, length
)
285 register struct _obstack_chunk
*old_chunk
= h
->chunk
;
286 register struct _obstack_chunk
*new_chunk
;
287 register long new_size
;
288 register long obj_size
= h
->next_free
- h
->object_base
;
293 /* Compute size for new chunk. */
294 new_size
= (obj_size
+ length
) + (obj_size
>> 3) + h
->alignment_mask
+ 100;
295 if (new_size
< h
->chunk_size
)
296 new_size
= h
->chunk_size
;
298 /* Allocate and initialize the new chunk. */
299 new_chunk
= CALL_CHUNKFUN (h
, new_size
);
301 (*obstack_alloc_failed_handler
) ();
302 h
->chunk
= new_chunk
;
303 new_chunk
->prev
= old_chunk
;
304 new_chunk
->limit
= h
->chunk_limit
= (char *) new_chunk
+ new_size
;
306 /* Compute an aligned object_base in the new chunk */
308 __INT_TO_PTR ((__PTR_TO_INT (new_chunk
->contents
) + h
->alignment_mask
)
309 & ~ (h
->alignment_mask
));
311 /* Move the existing object to the new chunk.
312 Word at a time is fast and is safe if the object
313 is sufficiently aligned. */
314 if (h
->alignment_mask
+ 1 >= DEFAULT_ALIGNMENT
)
316 for (i
= obj_size
/ sizeof (COPYING_UNIT
) - 1;
318 ((COPYING_UNIT
*)object_base
)[i
]
319 = ((COPYING_UNIT
*)h
->object_base
)[i
];
320 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
321 but that can cross a page boundary on a machine
322 which does not do strict alignment for COPYING_UNITS. */
323 already
= obj_size
/ sizeof (COPYING_UNIT
) * sizeof (COPYING_UNIT
);
327 /* Copy remaining bytes one by one. */
328 for (i
= already
; i
< obj_size
; i
++)
329 object_base
[i
] = h
->object_base
[i
];
331 /* If the object just copied was the only data in OLD_CHUNK,
332 free that chunk and remove it from the chain.
333 But not if that chunk might contain an empty object. */
334 if (h
->object_base
== old_chunk
->contents
&& ! h
->maybe_empty_object
)
336 new_chunk
->prev
= old_chunk
->prev
;
337 CALL_FREEFUN (h
, old_chunk
);
340 h
->object_base
= object_base
;
341 h
->next_free
= h
->object_base
+ obj_size
;
342 /* The new chunk certainly contains no empty object yet. */
343 h
->maybe_empty_object
= 0;
346 libc_hidden_def (_obstack_newchunk
)
349 /* Return nonzero if object OBJ has been allocated from obstack H.
350 This is here for debugging.
351 If you use it in a program, you are probably losing. */
353 # if defined __STDC__ && __STDC__
354 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
355 obstack.h because it is just for debugging. */
356 int _obstack_allocated_p (struct obstack
*h
, POINTER obj
);
360 _obstack_allocated_p (h
, obj
)
364 register struct _obstack_chunk
*lp
; /* below addr of any objects in this chunk */
365 register struct _obstack_chunk
*plp
; /* point to previous chunk if any */
368 /* We use >= rather than > since the object cannot be exactly at
369 the beginning of the chunk but might be an empty object exactly
370 at the end of an adjacent chunk. */
371 while (lp
!= 0 && ((POINTER
) lp
>= obj
|| (POINTER
) (lp
)->limit
< obj
))
379 /* Free objects in obstack H, including OBJ and everything allocate
380 more recently than OBJ. If OBJ is zero, free everything in H. */
384 /* This function has two names with identical definitions.
385 This is the first one, called from non-ANSI code. */
388 _obstack_free (h
, obj
)
392 register struct _obstack_chunk
*lp
; /* below addr of any objects in this chunk */
393 register struct _obstack_chunk
*plp
; /* point to previous chunk if any */
396 /* We use >= because there cannot be an object at the beginning of a chunk.
397 But there can be an empty object at that address
398 at the end of another chunk. */
399 while (lp
!= 0 && ((POINTER
) lp
>= obj
|| (POINTER
) (lp
)->limit
< obj
))
402 CALL_FREEFUN (h
, lp
);
404 /* If we switch chunks, we can't tell whether the new current
405 chunk contains an empty object, so assume that it may. */
406 h
->maybe_empty_object
= 1;
410 h
->object_base
= h
->next_free
= (char *) (obj
);
411 h
->chunk_limit
= lp
->limit
;
415 /* obj is not in any of the chunks! */
419 /* This function is used from ANSI code. */
422 strong_alias (_obstack_free
, obstack_free
)
426 obstack_free (h
, obj
)
430 register struct _obstack_chunk
*lp
; /* below addr of any objects in this chunk */
431 register struct _obstack_chunk
*plp
; /* point to previous chunk if any */
434 /* We use >= because there cannot be an object at the beginning of a chunk.
435 But there can be an empty object at that address
436 at the end of another chunk. */
437 while (lp
!= 0 && ((POINTER
) lp
>= obj
|| (POINTER
) (lp
)->limit
< obj
))
440 CALL_FREEFUN (h
, lp
);
442 /* If we switch chunks, we can't tell whether the new current
443 chunk contains an empty object, so assume that it may. */
444 h
->maybe_empty_object
= 1;
448 h
->object_base
= h
->next_free
= (char *) (obj
);
449 h
->chunk_limit
= lp
->limit
;
453 /* obj is not in any of the chunks! */
459 _obstack_memory_used (h
)
462 register struct _obstack_chunk
* lp
;
463 register int nbytes
= 0;
465 for (lp
= h
->chunk
; lp
!= 0; lp
= lp
->prev
)
467 nbytes
+= lp
->limit
- (char *) lp
;
472 /* Define the error handler. */
474 # if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
475 # include <libintl.h>
477 # define _(Str) gettext (Str)
480 # define _(Str) (Str)
484 # include <libio/iolibio.h>
487 # ifndef __attribute__
488 /* This feature is available in gcc versions 2.5 and later. */
489 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
490 # define __attribute__(Spec) /* empty */
495 __attribute__ ((noreturn
))
498 /* Don't change any of these strings. Yes, it would be possible to add
499 the newline to the string and use fputs or so. But this must not
500 happen because the "memory exhausted" message appears in other places
501 like this and the translation should be reused instead of creating
502 a very similar string which requires a separate translation. */
503 # if defined _LIBC && defined USE_IN_LIBIO
504 if (_IO_fwide (stderr
, 0) > 0)
505 __fwprintf (stderr
, L
"%s\n", _("memory exhausted"));
508 fprintf (stderr
, "%s\n", _("memory exhausted"));
509 exit (obstack_exit_failure
);
513 /* These are now turned off because the applications do not use it
514 and it uses bcopy via obstack_grow, which causes trouble on sysV. */
516 /* Now define the functional versions of the obstack macros.
517 Define them to simply use the corresponding macros to do the job. */
519 # if defined __STDC__ && __STDC__
520 /* These function definitions do not work with non-ANSI preprocessors;
521 they won't pass through the macro names in parentheses. */
523 /* The function names appear in parentheses in order to prevent
524 the macro-definitions of the names from being expanded there. */
526 POINTER (obstack_base
) (obstack
)
527 struct obstack
*obstack
;
529 return obstack_base (obstack
);
532 POINTER (obstack_next_free
) (obstack
)
533 struct obstack
*obstack
;
535 return obstack_next_free (obstack
);
538 int (obstack_object_size
) (obstack
)
539 struct obstack
*obstack
;
541 return obstack_object_size (obstack
);
544 int (obstack_room
) (obstack
)
545 struct obstack
*obstack
;
547 return obstack_room (obstack
);
550 int (obstack_make_room
) (obstack
, length
)
551 struct obstack
*obstack
;
554 return obstack_make_room (obstack
, length
);
557 void (obstack_grow
) (obstack
, data
, length
)
558 struct obstack
*obstack
;
562 obstack_grow (obstack
, data
, length
);
565 void (obstack_grow0
) (obstack
, data
, length
)
566 struct obstack
*obstack
;
570 obstack_grow0 (obstack
, data
, length
);
573 void (obstack_1grow
) (obstack
, character
)
574 struct obstack
*obstack
;
577 obstack_1grow (obstack
, character
);
580 void (obstack_blank
) (obstack
, length
)
581 struct obstack
*obstack
;
584 obstack_blank (obstack
, length
);
587 void (obstack_1grow_fast
) (obstack
, character
)
588 struct obstack
*obstack
;
591 obstack_1grow_fast (obstack
, character
);
594 void (obstack_blank_fast
) (obstack
, length
)
595 struct obstack
*obstack
;
598 obstack_blank_fast (obstack
, length
);
601 POINTER (obstack_finish
) (obstack
)
602 struct obstack
*obstack
;
604 return obstack_finish (obstack
);
607 POINTER (obstack_alloc
) (obstack
, length
)
608 struct obstack
*obstack
;
611 return obstack_alloc (obstack
, length
);
614 POINTER (obstack_copy
) (obstack
, address
, length
)
615 struct obstack
*obstack
;
616 const POINTER address
;
619 return obstack_copy (obstack
, address
, length
);
622 POINTER (obstack_copy0
) (obstack
, address
, length
)
623 struct obstack
*obstack
;
624 const POINTER address
;
627 return obstack_copy0 (obstack
, address
, length
);
630 # endif /* __STDC__ */
634 #endif /* !ELIDE_CODE */