1 /* obstack.c - subroutines used implicitly by object stack macros
2 Copyright (C) 1988-1994, 1996-1999, 2000-2002 Free Software Foundation, Inc.
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation,
15 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 /* NOTE BEFORE MODIFYING THIS FILE: This version number must be
24 incremented whenever callers compiled using an old obstack.h can no
25 longer properly call the functions in this obstack.c. */
26 #define OBSTACK_INTERFACE_VERSION 1
28 /* Comment out all this code if we are using the GNU C Library, and are not
29 actually compiling the library itself, and the installed library
30 supports the same library interface we do. This code is part of the GNU
31 C Library, but also included in many other GNU distributions. Compiling
32 and linking in this code is a waste when using the GNU C library
33 (especially if it is a shared library). Rather than having every GNU
34 program understand `configure --with-gnu-libc' and omit the object
35 files, it is simpler to just do this in the source for each such file. */
37 #include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */
38 #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
39 # include <gnu-versions.h>
40 # if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION
45 #if defined _LIBC && defined USE_IN_LIBIO
52 # if defined __STDC__ && __STDC__
53 # define POINTER void *
55 # define POINTER char *
58 /* Determine default alignment. */
59 struct fooalign
{char x
; double d
;};
60 # define DEFAULT_ALIGNMENT \
61 ((PTR_INT_TYPE) ((char *) &((struct fooalign *) 0)->d - (char *) 0))
62 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
63 But in fact it might be less smart and round addresses to as much as
64 DEFAULT_ROUNDING. So we prepare for it to do that. */
65 union fooround
{long x
; double d
;};
66 # define DEFAULT_ROUNDING (sizeof (union fooround))
68 /* When we copy a long block of data, this is the unit to do it with.
69 On some machines, copying successive ints does not work;
70 in such a case, redefine COPYING_UNIT to `long' (if that works)
71 or `char' as a last resort. */
73 # define COPYING_UNIT int
77 /* The functions allocating more room by calling `obstack_chunk_alloc'
78 jump to the handler pointed to by `obstack_alloc_failed_handler'.
79 This can be set to a user defined function which should either
80 abort gracefully or use longjump - but shouldn't return. This
81 variable by default points to the internal function
83 # if defined __STDC__ && __STDC__
84 static void print_and_abort (void);
85 void (*obstack_alloc_failed_handler
) (void) = print_and_abort
;
87 static void print_and_abort ();
88 void (*obstack_alloc_failed_handler
) () = print_and_abort
;
91 /* Exit value used when `print_and_abort' is used. */
92 # if defined __GNU_LIBRARY__ || defined HAVE_STDLIB_H
96 # define EXIT_FAILURE 1
98 int obstack_exit_failure
= EXIT_FAILURE
;
100 /* The non-GNU-C macros copy the obstack into this global variable
101 to avoid multiple evaluation. */
103 struct obstack
*_obstack
;
105 /* Define a macro that either calls functions with the traditional malloc/free
106 calling interface, or calls functions with the mmalloc/mfree interface
107 (that adds an extra first argument), based on the state of use_extra_arg.
108 For free, do not use ?:, since some compilers, like the MIPS compilers,
109 do not allow (expr) ? void : void. */
111 # if defined __STDC__ && __STDC__
112 # define CALL_CHUNKFUN(h, size) \
113 (((h) -> use_extra_arg) \
114 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
115 : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
117 # define CALL_FREEFUN(h, old_chunk) \
119 if ((h) -> use_extra_arg) \
120 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
122 (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
125 # define CALL_CHUNKFUN(h, size) \
126 (((h) -> use_extra_arg) \
127 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
128 : (*(struct _obstack_chunk *(*) ()) (h)->chunkfun) ((size)))
130 # define CALL_FREEFUN(h, old_chunk) \
132 if ((h) -> use_extra_arg) \
133 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
135 (*(void (*) ()) (h)->freefun) ((old_chunk)); \
140 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
141 Objects start on multiples of ALIGNMENT (0 means use default).
142 CHUNKFUN is the function to use to allocate chunks,
143 and FREEFUN the function to free them.
145 Return nonzero if successful, calls obstack_alloc_failed_handler if
149 _obstack_begin (h
, size
, alignment
, chunkfun
, freefun
)
153 # if defined __STDC__ && __STDC__
154 POINTER (*chunkfun
) (long);
155 void (*freefun
) (void *);
157 POINTER (*chunkfun
) ();
161 register struct _obstack_chunk
*chunk
; /* points to new chunk */
164 alignment
= (int) DEFAULT_ALIGNMENT
;
166 /* Default size is what GNU malloc can fit in a 4096-byte block. */
168 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
169 Use the values for range checking, because if range checking is off,
170 the extra bytes won't be missed terribly, but if range checking is on
171 and we used a larger request, a whole extra 4096 bytes would be
174 These number are irrelevant to the new GNU malloc. I suspect it is
175 less sensitive to the size of the request. */
176 int extra
= ((((12 + DEFAULT_ROUNDING
- 1) & ~(DEFAULT_ROUNDING
- 1))
177 + 4 + DEFAULT_ROUNDING
- 1)
178 & ~(DEFAULT_ROUNDING
- 1));
182 # if defined __STDC__ && __STDC__
183 h
->chunkfun
= (struct _obstack_chunk
* (*)(void *, long)) chunkfun
;
184 h
->freefun
= (void (*) (void *, struct _obstack_chunk
*)) freefun
;
186 h
->chunkfun
= (struct _obstack_chunk
* (*)()) chunkfun
;
187 h
->freefun
= freefun
;
189 h
->chunk_size
= size
;
190 h
->alignment_mask
= alignment
- 1;
191 h
->use_extra_arg
= 0;
193 chunk
= h
->chunk
= CALL_CHUNKFUN (h
, h
-> chunk_size
);
195 (*obstack_alloc_failed_handler
) ();
196 h
->next_free
= h
->object_base
= chunk
->contents
;
197 h
->chunk_limit
= chunk
->limit
198 = (char *) chunk
+ h
->chunk_size
;
200 /* The initial chunk now contains no empty object. */
201 h
->maybe_empty_object
= 0;
207 _obstack_begin_1 (h
, size
, alignment
, chunkfun
, freefun
, arg
)
211 # if defined __STDC__ && __STDC__
212 POINTER (*chunkfun
) (POINTER
, long);
213 void (*freefun
) (POINTER
, POINTER
);
215 POINTER (*chunkfun
) ();
220 register struct _obstack_chunk
*chunk
; /* points to new chunk */
223 alignment
= (int) DEFAULT_ALIGNMENT
;
225 /* Default size is what GNU malloc can fit in a 4096-byte block. */
227 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
228 Use the values for range checking, because if range checking is off,
229 the extra bytes won't be missed terribly, but if range checking is on
230 and we used a larger request, a whole extra 4096 bytes would be
233 These number are irrelevant to the new GNU malloc. I suspect it is
234 less sensitive to the size of the request. */
235 int extra
= ((((12 + DEFAULT_ROUNDING
- 1) & ~(DEFAULT_ROUNDING
- 1))
236 + 4 + DEFAULT_ROUNDING
- 1)
237 & ~(DEFAULT_ROUNDING
- 1));
241 # if defined __STDC__ && __STDC__
242 h
->chunkfun
= (struct _obstack_chunk
* (*)(void *,long)) chunkfun
;
243 h
->freefun
= (void (*) (void *, struct _obstack_chunk
*)) freefun
;
245 h
->chunkfun
= (struct _obstack_chunk
* (*)()) chunkfun
;
246 h
->freefun
= freefun
;
248 h
->chunk_size
= size
;
249 h
->alignment_mask
= alignment
- 1;
251 h
->use_extra_arg
= 1;
253 chunk
= h
->chunk
= CALL_CHUNKFUN (h
, h
-> chunk_size
);
255 (*obstack_alloc_failed_handler
) ();
256 h
->next_free
= h
->object_base
= chunk
->contents
;
257 h
->chunk_limit
= chunk
->limit
258 = (char *) chunk
+ h
->chunk_size
;
260 /* The initial chunk now contains no empty object. */
261 h
->maybe_empty_object
= 0;
266 /* Allocate a new current chunk for the obstack *H
267 on the assumption that LENGTH bytes need to be added
268 to the current object, or a new object of length LENGTH allocated.
269 Copies any partial object from the end of the old chunk
270 to the beginning of the new one. */
273 _obstack_newchunk (h
, length
)
277 register struct _obstack_chunk
*old_chunk
= h
->chunk
;
278 register struct _obstack_chunk
*new_chunk
;
279 register long new_size
;
280 register long obj_size
= h
->next_free
- h
->object_base
;
285 /* Compute size for new chunk. */
286 new_size
= (obj_size
+ length
) + (obj_size
>> 3) + h
->alignment_mask
+ 100;
287 if (new_size
< h
->chunk_size
)
288 new_size
= h
->chunk_size
;
290 /* Allocate and initialize the new chunk. */
291 new_chunk
= CALL_CHUNKFUN (h
, new_size
);
293 (*obstack_alloc_failed_handler
) ();
294 h
->chunk
= new_chunk
;
295 new_chunk
->prev
= old_chunk
;
296 new_chunk
->limit
= h
->chunk_limit
= (char *) new_chunk
+ new_size
;
298 /* Compute an aligned object_base in the new chunk */
300 __INT_TO_PTR ((__PTR_TO_INT (new_chunk
->contents
) + h
->alignment_mask
)
301 & ~ (h
->alignment_mask
));
303 /* Move the existing object to the new chunk.
304 Word at a time is fast and is safe if the object
305 is sufficiently aligned. */
306 if (h
->alignment_mask
+ 1 >= DEFAULT_ALIGNMENT
)
308 for (i
= obj_size
/ sizeof (COPYING_UNIT
) - 1;
310 ((COPYING_UNIT
*)object_base
)[i
]
311 = ((COPYING_UNIT
*)h
->object_base
)[i
];
312 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
313 but that can cross a page boundary on a machine
314 which does not do strict alignment for COPYING_UNITS. */
315 already
= obj_size
/ sizeof (COPYING_UNIT
) * sizeof (COPYING_UNIT
);
319 /* Copy remaining bytes one by one. */
320 for (i
= already
; i
< obj_size
; i
++)
321 object_base
[i
] = h
->object_base
[i
];
323 /* If the object just copied was the only data in OLD_CHUNK,
324 free that chunk and remove it from the chain.
325 But not if that chunk might contain an empty object. */
326 if (h
->object_base
== old_chunk
->contents
&& ! h
->maybe_empty_object
)
328 new_chunk
->prev
= old_chunk
->prev
;
329 CALL_FREEFUN (h
, old_chunk
);
332 h
->object_base
= object_base
;
333 h
->next_free
= h
->object_base
+ obj_size
;
334 /* The new chunk certainly contains no empty object yet. */
335 h
->maybe_empty_object
= 0;
338 /* Return nonzero if object OBJ has been allocated from obstack H.
339 This is here for debugging.
340 If you use it in a program, you are probably losing. */
342 # if defined __STDC__ && __STDC__
343 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
344 obstack.h because it is just for debugging. */
345 int _obstack_allocated_p (struct obstack
*h
, POINTER obj
);
349 _obstack_allocated_p (h
, obj
)
353 register struct _obstack_chunk
*lp
; /* below addr of any objects in this chunk */
354 register struct _obstack_chunk
*plp
; /* point to previous chunk if any */
357 /* We use >= rather than > since the object cannot be exactly at
358 the beginning of the chunk but might be an empty object exactly
359 at the end of an adjacent chunk. */
360 while (lp
!= 0 && ((POINTER
) lp
>= obj
|| (POINTER
) (lp
)->limit
< obj
))
368 /* Free objects in obstack H, including OBJ and everything allocate
369 more recently than OBJ. If OBJ is zero, free everything in H. */
373 /* This function has two names with identical definitions.
374 This is the first one, called from non-ANSI code. */
377 _obstack_free (h
, obj
)
381 register struct _obstack_chunk
*lp
; /* below addr of any objects in this chunk */
382 register struct _obstack_chunk
*plp
; /* point to previous chunk if any */
385 /* We use >= because there cannot be an object at the beginning of a chunk.
386 But there can be an empty object at that address
387 at the end of another chunk. */
388 while (lp
!= 0 && ((POINTER
) lp
>= obj
|| (POINTER
) (lp
)->limit
< obj
))
391 CALL_FREEFUN (h
, lp
);
393 /* If we switch chunks, we can't tell whether the new current
394 chunk contains an empty object, so assume that it may. */
395 h
->maybe_empty_object
= 1;
399 h
->object_base
= h
->next_free
= (char *) (obj
);
400 h
->chunk_limit
= lp
->limit
;
404 /* obj is not in any of the chunks! */
408 /* This function is used from ANSI code. */
411 obstack_free (h
, obj
)
415 register struct _obstack_chunk
*lp
; /* below addr of any objects in this chunk */
416 register struct _obstack_chunk
*plp
; /* point to previous chunk if any */
419 /* We use >= because there cannot be an object at the beginning of a chunk.
420 But there can be an empty object at that address
421 at the end of another chunk. */
422 while (lp
!= 0 && ((POINTER
) lp
>= obj
|| (POINTER
) (lp
)->limit
< obj
))
425 CALL_FREEFUN (h
, lp
);
427 /* If we switch chunks, we can't tell whether the new current
428 chunk contains an empty object, so assume that it may. */
429 h
->maybe_empty_object
= 1;
433 h
->object_base
= h
->next_free
= (char *) (obj
);
434 h
->chunk_limit
= lp
->limit
;
438 /* obj is not in any of the chunks! */
443 _obstack_memory_used (h
)
446 register struct _obstack_chunk
* lp
;
447 register int nbytes
= 0;
449 for (lp
= h
->chunk
; lp
!= 0; lp
= lp
->prev
)
451 nbytes
+= lp
->limit
- (char *) lp
;
456 /* Define the error handler. */
458 # include <libintl.h>
460 # include "gettext.h"
462 # define _(msgid) gettext (msgid)
464 # if defined _LIBC && defined USE_IN_LIBIO
465 # include <libio/iolibio.h>
466 # define fputs(s, f) _IO_fputs (s, f)
469 # ifndef __attribute__
470 /* This feature is available in gcc versions 2.5 and later. */
471 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
472 # define __attribute__(Spec) /* empty */
477 __attribute__ ((noreturn
))
480 /* Don't change any of these strings. Yes, it would be possible to add
481 the newline to the string and use fputs or so. But this must not
482 happen because the "memory exhausted" message appears in other places
483 like this and the translation should be reused instead of creating
484 a very similar string which requires a separate translation. */
485 # if defined _LIBC && defined USE_IN_LIBIO
486 if (_IO_fwide (stderr
, 0) > 0)
487 __fwprintf (stderr
, L
"%s\n", _("memory exhausted"));
490 fprintf (stderr
, "%s\n", _("memory exhausted"));
491 exit (obstack_exit_failure
);
495 /* These are now turned off because the applications do not use it
496 and it uses bcopy via obstack_grow, which causes trouble on sysV. */
498 /* Now define the functional versions of the obstack macros.
499 Define them to simply use the corresponding macros to do the job. */
501 # if defined __STDC__ && __STDC__
502 /* These function definitions do not work with non-ANSI preprocessors;
503 they won't pass through the macro names in parentheses. */
505 /* The function names appear in parentheses in order to prevent
506 the macro-definitions of the names from being expanded there. */
508 POINTER (obstack_base
) (obstack
)
509 struct obstack
*obstack
;
511 return obstack_base (obstack
);
514 POINTER (obstack_next_free
) (obstack
)
515 struct obstack
*obstack
;
517 return obstack_next_free (obstack
);
520 int (obstack_object_size
) (obstack
)
521 struct obstack
*obstack
;
523 return obstack_object_size (obstack
);
526 int (obstack_room
) (obstack
)
527 struct obstack
*obstack
;
529 return obstack_room (obstack
);
532 int (obstack_make_room
) (obstack
, length
)
533 struct obstack
*obstack
;
536 return obstack_make_room (obstack
, length
);
539 void (obstack_grow
) (obstack
, data
, length
)
540 struct obstack
*obstack
;
544 obstack_grow (obstack
, data
, length
);
547 void (obstack_grow0
) (obstack
, data
, length
)
548 struct obstack
*obstack
;
552 obstack_grow0 (obstack
, data
, length
);
555 void (obstack_1grow
) (obstack
, character
)
556 struct obstack
*obstack
;
559 obstack_1grow (obstack
, character
);
562 void (obstack_blank
) (obstack
, length
)
563 struct obstack
*obstack
;
566 obstack_blank (obstack
, length
);
569 void (obstack_1grow_fast
) (obstack
, character
)
570 struct obstack
*obstack
;
573 obstack_1grow_fast (obstack
, character
);
576 void (obstack_blank_fast
) (obstack
, length
)
577 struct obstack
*obstack
;
580 obstack_blank_fast (obstack
, length
);
583 POINTER (obstack_finish
) (obstack
)
584 struct obstack
*obstack
;
586 return obstack_finish (obstack
);
589 POINTER (obstack_alloc
) (obstack
, length
)
590 struct obstack
*obstack
;
593 return obstack_alloc (obstack
, length
);
596 POINTER (obstack_copy
) (obstack
, address
, length
)
597 struct obstack
*obstack
;
598 const POINTER address
;
601 return obstack_copy (obstack
, address
, length
);
604 POINTER (obstack_copy0
) (obstack
, address
, length
)
605 struct obstack
*obstack
;
606 const POINTER address
;
609 return obstack_copy0 (obstack
, address
, length
);
612 # endif /* __STDC__ */
616 #endif /* !ELIDE_CODE */