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 (__STDC__) && __STDC__
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 #define CALL_CHUNKFUN(h, size) \
108 (((h) -> use_extra_arg) \
109 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
110 : (*(struct _obstack_chunk *(*) ()) (h)->chunkfun) ((size)))
112 #define CALL_FREEFUN(h, old_chunk) \
114 if ((h) -> use_extra_arg) \
115 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
117 (*(void (*) ()) (h)->freefun) ((old_chunk)); \
121 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
122 Objects start on multiples of ALIGNMENT (0 means use default).
123 CHUNKFUN is the function to use to allocate chunks,
124 and FREEFUN the function to free them.
126 Return nonzero if successful, zero if out of memory.
127 To recover from an out of memory error,
128 free up some memory, then call this again. */
131 _obstack_begin (h
, size
, alignment
, chunkfun
, freefun
)
135 POINTER (*chunkfun
) ();
138 register struct _obstack_chunk
*chunk
; /* points to new chunk */
141 alignment
= DEFAULT_ALIGNMENT
;
143 /* Default size is what GNU malloc can fit in a 4096-byte block. */
145 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
146 Use the values for range checking, because if range checking is off,
147 the extra bytes won't be missed terribly, but if range checking is on
148 and we used a larger request, a whole extra 4096 bytes would be
151 These number are irrelevant to the new GNU malloc. I suspect it is
152 less sensitive to the size of the request. */
153 int extra
= ((((12 + DEFAULT_ROUNDING
- 1) & ~(DEFAULT_ROUNDING
- 1))
154 + 4 + DEFAULT_ROUNDING
- 1)
155 & ~(DEFAULT_ROUNDING
- 1));
159 h
->chunkfun
= (struct _obstack_chunk
* (*)()) chunkfun
;
160 h
->freefun
= freefun
;
161 h
->chunk_size
= size
;
162 h
->alignment_mask
= alignment
- 1;
163 h
->use_extra_arg
= 0;
165 chunk
= h
->chunk
= CALL_CHUNKFUN (h
, h
-> chunk_size
);
167 (*obstack_alloc_failed_handler
) ();
168 h
->next_free
= h
->object_base
= chunk
->contents
;
169 h
->chunk_limit
= chunk
->limit
170 = (char *) chunk
+ h
->chunk_size
;
172 /* The initial chunk now contains no empty object. */
173 h
->maybe_empty_object
= 0;
179 _obstack_begin_1 (h
, size
, alignment
, chunkfun
, freefun
, arg
)
183 POINTER (*chunkfun
) ();
187 register struct _obstack_chunk
*chunk
; /* points to new chunk */
190 alignment
= DEFAULT_ALIGNMENT
;
192 /* Default size is what GNU malloc can fit in a 4096-byte block. */
194 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
195 Use the values for range checking, because if range checking is off,
196 the extra bytes won't be missed terribly, but if range checking is on
197 and we used a larger request, a whole extra 4096 bytes would be
200 These number are irrelevant to the new GNU malloc. I suspect it is
201 less sensitive to the size of the request. */
202 int extra
= ((((12 + DEFAULT_ROUNDING
- 1) & ~(DEFAULT_ROUNDING
- 1))
203 + 4 + DEFAULT_ROUNDING
- 1)
204 & ~(DEFAULT_ROUNDING
- 1));
208 h
->chunkfun
= (struct _obstack_chunk
* (*)()) chunkfun
;
209 h
->freefun
= freefun
;
210 h
->chunk_size
= size
;
211 h
->alignment_mask
= alignment
- 1;
213 h
->use_extra_arg
= 1;
215 chunk
= h
->chunk
= CALL_CHUNKFUN (h
, h
-> chunk_size
);
217 (*obstack_alloc_failed_handler
) ();
218 h
->next_free
= h
->object_base
= chunk
->contents
;
219 h
->chunk_limit
= chunk
->limit
220 = (char *) chunk
+ h
->chunk_size
;
222 /* The initial chunk now contains no empty object. */
223 h
->maybe_empty_object
= 0;
228 /* Allocate a new current chunk for the obstack *H
229 on the assumption that LENGTH bytes need to be added
230 to the current object, or a new object of length LENGTH allocated.
231 Copies any partial object from the end of the old chunk
232 to the beginning of the new one. */
235 _obstack_newchunk (h
, length
)
239 register struct _obstack_chunk
*old_chunk
= h
->chunk
;
240 register struct _obstack_chunk
*new_chunk
;
241 register long new_size
;
242 register int obj_size
= h
->next_free
- h
->object_base
;
246 /* Compute size for new chunk. */
247 new_size
= (obj_size
+ length
) + (obj_size
>> 3) + 100;
248 if (new_size
< h
->chunk_size
)
249 new_size
= h
->chunk_size
;
251 /* Allocate and initialize the new chunk. */
252 new_chunk
= CALL_CHUNKFUN (h
, new_size
);
254 (*obstack_alloc_failed_handler
) ();
255 h
->chunk
= new_chunk
;
256 new_chunk
->prev
= old_chunk
;
257 new_chunk
->limit
= h
->chunk_limit
= (char *) new_chunk
+ new_size
;
259 /* Move the existing object to the new chunk.
260 Word at a time is fast and is safe if the object
261 is sufficiently aligned. */
262 if (h
->alignment_mask
+ 1 >= DEFAULT_ALIGNMENT
)
264 for (i
= obj_size
/ sizeof (COPYING_UNIT
) - 1;
266 ((COPYING_UNIT
*)new_chunk
->contents
)[i
]
267 = ((COPYING_UNIT
*)h
->object_base
)[i
];
268 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
269 but that can cross a page boundary on a machine
270 which does not do strict alignment for COPYING_UNITS. */
271 already
= obj_size
/ sizeof (COPYING_UNIT
) * sizeof (COPYING_UNIT
);
275 /* Copy remaining bytes one by one. */
276 for (i
= already
; i
< obj_size
; i
++)
277 new_chunk
->contents
[i
] = h
->object_base
[i
];
279 /* If the object just copied was the only data in OLD_CHUNK,
280 free that chunk and remove it from the chain.
281 But not if that chunk might contain an empty object. */
282 if (h
->object_base
== old_chunk
->contents
&& ! h
->maybe_empty_object
)
284 new_chunk
->prev
= old_chunk
->prev
;
285 CALL_FREEFUN (h
, old_chunk
);
288 h
->object_base
= new_chunk
->contents
;
289 h
->next_free
= h
->object_base
+ obj_size
;
290 /* The new chunk certainly contains no empty object yet. */
291 h
->maybe_empty_object
= 0;
294 /* Return nonzero if object OBJ has been allocated from obstack H.
295 This is here for debugging.
296 If you use it in a program, you are probably losing. */
298 #if defined (__STDC__) && __STDC__
299 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
300 obstack.h because it is just for debugging. */
301 int _obstack_allocated_p (struct obstack
*h
, POINTER obj
);
305 _obstack_allocated_p (h
, obj
)
309 register struct _obstack_chunk
*lp
; /* below addr of any objects in this chunk */
310 register struct _obstack_chunk
*plp
; /* point to previous chunk if any */
313 /* We use >= rather than > since the object cannot be exactly at
314 the beginning of the chunk but might be an empty object exactly
315 at the end of an adjacent chunk. */
316 while (lp
!= 0 && ((POINTER
) lp
>= obj
|| (POINTER
) (lp
)->limit
< obj
))
324 /* Free objects in obstack H, including OBJ and everything allocate
325 more recently than OBJ. If OBJ is zero, free everything in H. */
329 /* This function has two names with identical definitions.
330 This is the first one, called from non-ANSI code. */
333 _obstack_free (h
, obj
)
337 register struct _obstack_chunk
*lp
; /* below addr of any objects in this chunk */
338 register struct _obstack_chunk
*plp
; /* point to previous chunk if any */
341 /* We use >= because there cannot be an object at the beginning of a chunk.
342 But there can be an empty object at that address
343 at the end of another chunk. */
344 while (lp
!= 0 && ((POINTER
) lp
>= obj
|| (POINTER
) (lp
)->limit
< obj
))
347 CALL_FREEFUN (h
, lp
);
349 /* If we switch chunks, we can't tell whether the new current
350 chunk contains an empty object, so assume that it may. */
351 h
->maybe_empty_object
= 1;
355 h
->object_base
= h
->next_free
= (char *) (obj
);
356 h
->chunk_limit
= lp
->limit
;
360 /* obj is not in any of the chunks! */
364 /* This function is used from ANSI code. */
367 obstack_free (h
, obj
)
371 register struct _obstack_chunk
*lp
; /* below addr of any objects in this chunk */
372 register struct _obstack_chunk
*plp
; /* point to previous chunk if any */
375 /* We use >= because there cannot be an object at the beginning of a chunk.
376 But there can be an empty object at that address
377 at the end of another chunk. */
378 while (lp
!= 0 && ((POINTER
) lp
>= obj
|| (POINTER
) (lp
)->limit
< obj
))
381 CALL_FREEFUN (h
, lp
);
383 /* If we switch chunks, we can't tell whether the new current
384 chunk contains an empty object, so assume that it may. */
385 h
->maybe_empty_object
= 1;
389 h
->object_base
= h
->next_free
= (char *) (obj
);
390 h
->chunk_limit
= lp
->limit
;
394 /* obj is not in any of the chunks! */
399 _obstack_memory_used (h
)
402 register struct _obstack_chunk
* lp
;
403 register int nbytes
= 0;
405 for (lp
= h
->chunk
; lp
!= 0; lp
= lp
->prev
)
407 nbytes
+= lp
->limit
- (char *) lp
;
412 /* Define the error handler. */
414 # ifdef HAVE_LIBINTL_H
415 # include <libintl.h>
417 # define _(Str) gettext (Str)
420 # define _(Str) (Str)
427 fputs (_("memory exhausted\n"), stderr
);
428 exit (obstack_exit_failure
);
432 /* These are now turned off because the applications do not use it
433 and it uses bcopy via obstack_grow, which causes trouble on sysV. */
435 /* Now define the functional versions of the obstack macros.
436 Define them to simply use the corresponding macros to do the job. */
438 #if defined (__STDC__) && __STDC__
439 /* These function definitions do not work with non-ANSI preprocessors;
440 they won't pass through the macro names in parentheses. */
442 /* The function names appear in parentheses in order to prevent
443 the macro-definitions of the names from being expanded there. */
445 POINTER (obstack_base
) (obstack
)
446 struct obstack
*obstack
;
448 return obstack_base (obstack
);
451 POINTER (obstack_next_free
) (obstack
)
452 struct obstack
*obstack
;
454 return obstack_next_free (obstack
);
457 int (obstack_object_size
) (obstack
)
458 struct obstack
*obstack
;
460 return obstack_object_size (obstack
);
463 int (obstack_room
) (obstack
)
464 struct obstack
*obstack
;
466 return obstack_room (obstack
);
469 int (obstack_make_room
) (obstack
, length
)
470 struct obstack
*obstack
;
473 return obstack_make_room (obstack
, length
);
476 void (obstack_grow
) (obstack
, pointer
, length
)
477 struct obstack
*obstack
;
481 obstack_grow (obstack
, pointer
, length
);
484 void (obstack_grow0
) (obstack
, pointer
, length
)
485 struct obstack
*obstack
;
489 obstack_grow0 (obstack
, pointer
, length
);
492 void (obstack_1grow
) (obstack
, character
)
493 struct obstack
*obstack
;
496 obstack_1grow (obstack
, character
);
499 void (obstack_blank
) (obstack
, length
)
500 struct obstack
*obstack
;
503 obstack_blank (obstack
, length
);
506 void (obstack_1grow_fast
) (obstack
, character
)
507 struct obstack
*obstack
;
510 obstack_1grow_fast (obstack
, character
);
513 void (obstack_blank_fast
) (obstack
, length
)
514 struct obstack
*obstack
;
517 obstack_blank_fast (obstack
, length
);
520 POINTER (obstack_finish
) (obstack
)
521 struct obstack
*obstack
;
523 return obstack_finish (obstack
);
526 POINTER (obstack_alloc
) (obstack
, length
)
527 struct obstack
*obstack
;
530 return obstack_alloc (obstack
, length
);
533 POINTER (obstack_copy
) (obstack
, pointer
, length
)
534 struct obstack
*obstack
;
538 return obstack_copy (obstack
, pointer
, length
);
541 POINTER (obstack_copy0
) (obstack
, pointer
, length
)
542 struct obstack
*obstack
;
546 return obstack_copy0 (obstack
, pointer
, length
);
549 #endif /* __STDC__ */
553 #endif /* !ELIDE_CODE */