pick: Update from 2.0.1 to 2.0.2
[termux-packages] / packages / elfutils / obstack.h
CommitLineData
cf310881
FF
1/* obstack.h - object stack macros
2 Copyright (C) 1988-1994,1996-1999,2003,2004,2005,2009,2011,2012
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5
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.
10
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.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20/* Summary:
21
22All the apparent functions defined here are macros. The idea
23is that you would use these pre-tested macros to solve a
24very specific set of problems, and they would run fast.
25Caution: no side-effects in arguments please!! They may be
26evaluated MANY times!!
27
28These macros operate a stack of objects. Each object starts life
29small, and may grow to maturity. (Consider building a word syllable
30by syllable.) An object can move while it is growing. Once it has
31been "finished" it never changes address again. So the "top of the
32stack" is typically an immature growing object, while the rest of the
33stack is of mature, fixed size and fixed address objects.
34
35These routines grab large chunks of memory, using a function you
36supply, called `obstack_chunk_alloc'. On occasion, they free chunks,
37by calling `obstack_chunk_free'. You must define them and declare
38them before using any obstack macros.
39
40Each independent stack is represented by a `struct obstack'.
41Each of the obstack macros expects a pointer to such a structure
42as the first argument.
43
44One motivation for this package is the problem of growing char strings
45in symbol tables. Unless you are "fascist pig with a read-only mind"
46--Gosper's immortal quote from HAKMEM item 154, out of context--you
47would not like to put any arbitrary upper limit on the length of your
48symbols.
49
50In practice this often means you will build many short symbols and a
51few long symbols. At the time you are reading a symbol you don't know
52how long it is. One traditional method is to read a symbol into a
53buffer, realloc()ating the buffer every time you try to read a symbol
54that is longer than the buffer. This is beaut, but you still will
55want to copy the symbol from the buffer to a more permanent
56symbol-table entry say about half the time.
57
58With obstacks, you can work differently. Use one obstack for all symbol
59names. As you read a symbol, grow the name in the obstack gradually.
60When the name is complete, finalize it. Then, if the symbol exists already,
61free the newly read name.
62
63The way we do this is to take a large chunk, allocating memory from
64low addresses. When you want to build a symbol in the chunk you just
65add chars above the current "high water mark" in the chunk. When you
66have finished adding chars, because you got to the end of the symbol,
67you know how long the chars are, and you can create a new object.
68Mostly the chars will not burst over the highest address of the chunk,
69because you would typically expect a chunk to be (say) 100 times as
70long as an average object.
71
72In case that isn't clear, when we have enough chars to make up
73the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed)
74so we just point to it where it lies. No moving of chars is
75needed and this is the second win: potentially long strings need
76never be explicitly shuffled. Once an object is formed, it does not
77change its address during its lifetime.
78
79When the chars burst over a chunk boundary, we allocate a larger
80chunk, and then copy the partly formed object from the end of the old
81chunk to the beginning of the new larger chunk. We then carry on
82accreting characters to the end of the object as we normally would.
83
84A special macro is provided to add a single char at a time to a
85growing object. This allows the use of register variables, which
86break the ordinary 'growth' macro.
87
88Summary:
89 We allocate large chunks.
90 We carve out one object at a time from the current chunk.
91 Once carved, an object never moves.
92 We are free to append data of any size to the currently
93 growing object.
94 Exactly one object is growing in an obstack at any one time.
95 You can run one obstack per control block.
96 You may have as many control blocks as you dare.
97 Because of the way we do it, you can `unwind' an obstack
98 back to a previous state. (You may remove objects much
99 as you would with a stack.)
100*/
101
102
103/* Don't do the contents of this file more than once. */
104
105#ifndef _OBSTACK_H
106#define _OBSTACK_H 1
107
108#ifdef __cplusplus
109extern "C" {
110#endif
111\f
112/* We need the type of a pointer subtraction. If __PTRDIFF_TYPE__ is
113 defined, as with GNU C, use that; that way we don't pollute the
114 namespace with <stddef.h>'s symbols. Otherwise, include <stddef.h>
115 and use ptrdiff_t. */
116
117#ifdef __PTRDIFF_TYPE__
118# define PTR_INT_TYPE __PTRDIFF_TYPE__
119#else
120# include <stddef.h>
121# define PTR_INT_TYPE ptrdiff_t
122#endif
123
124/* If B is the base of an object addressed by P, return the result of
125 aligning P to the next multiple of A + 1. B and P must be of type
126 char *. A + 1 must be a power of 2. */
127
128#define __BPTR_ALIGN(B, P, A) ((B) + (((P) - (B) + (A)) & ~(A)))
129
130/* Similiar to _BPTR_ALIGN (B, P, A), except optimize the common case
131 where pointers can be converted to integers, aligned as integers,
132 and converted back again. If PTR_INT_TYPE is narrower than a
133 pointer (e.g., the AS/400), play it safe and compute the alignment
134 relative to B. Otherwise, use the faster strategy of computing the
135 alignment relative to 0. */
136
137#define __PTR_ALIGN(B, P, A) \
138 __BPTR_ALIGN (sizeof (PTR_INT_TYPE) < sizeof (void *) ? (B) : (char *) 0, \
139 P, A)
140
141#include <string.h>
142
143struct _obstack_chunk /* Lives at front of each chunk. */
144{
145 char *limit; /* 1 past end of this chunk */
146 struct _obstack_chunk *prev; /* address of prior chunk or NULL */
147 char contents[4]; /* objects begin here */
148};
149
150struct obstack /* control current object in current chunk */
151{
152 long chunk_size; /* preferred size to allocate chunks in */
153 struct _obstack_chunk *chunk; /* address of current struct obstack_chunk */
154 char *object_base; /* address of object we are building */
155 char *next_free; /* where to add next char to current object */
156 char *chunk_limit; /* address of char after current chunk */
157 union
158 {
159 PTR_INT_TYPE tempint;
160 void *tempptr;
161 } temp; /* Temporary for some macros. */
162 int alignment_mask; /* Mask of alignment for each object. */
163 /* These prototypes vary based on `use_extra_arg', and we use
164 casts to the prototypeless function type in all assignments,
165 but having prototypes here quiets -Wstrict-prototypes. */
166 struct _obstack_chunk *(*chunkfun) (void *, long);
167 void (*freefun) (void *, struct _obstack_chunk *);
168 void *extra_arg; /* first arg for chunk alloc/dealloc funcs */
169 unsigned use_extra_arg:1; /* chunk alloc/dealloc funcs take extra arg */
170 unsigned maybe_empty_object:1;/* There is a possibility that the current
171 chunk contains a zero-length object. This
172 prevents freeing the chunk if we allocate
173 a bigger chunk to replace it. */
174 unsigned alloc_failed:1; /* No longer used, as we now call the failed
175 handler on error, but retained for binary
176 compatibility. */
177};
178
179static void _obstack_newchunk (struct obstack *h, int length);
180
181/* Exit value used when `print_and_abort' is used. */
182extern int obstack_exit_failure;
183\f
184/* Pointer to beginning of object being allocated or to be allocated next.
185 Note that this might not be the final address of the object
186 because a new chunk might be needed to hold the final size. */
187
188#define obstack_base(h) ((void *) (h)->object_base)
189
190/* Size for allocating ordinary chunks. */
191
192#define obstack_chunk_size(h) ((h)->chunk_size)
193
194/* Pointer to next byte not yet allocated in current chunk. */
195
196#define obstack_next_free(h) ((h)->next_free)
197
198/* Mask specifying low bits that should be clear in address of an object. */
199
200#define obstack_alignment_mask(h) ((h)->alignment_mask)
201
202/* To prevent prototype warnings provide complete argument list. */
203#define obstack_init(h) \
204 _obstack_begin ((h), 0, 0, \
205 (void *(*) (long)) obstack_chunk_alloc, \
206 (void (*) (void *)) obstack_chunk_free)
207
208#define obstack_begin(h, size) \
209 _obstack_begin ((h), (size), 0, \
210 (void *(*) (long)) obstack_chunk_alloc, \
211 (void (*) (void *)) obstack_chunk_free)
212
213#define obstack_specify_allocation(h, size, alignment, chunkfun, freefun) \
214 _obstack_begin ((h), (size), (alignment), \
215 (void *(*) (long)) (chunkfun), \
216 (void (*) (void *)) (freefun))
217
218#define obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) \
219 _obstack_begin_1 ((h), (size), (alignment), \
220 (void *(*) (void *, long)) (chunkfun), \
221 (void (*) (void *, void *)) (freefun), (arg))
222
223#define obstack_chunkfun(h, newchunkfun) \
224 ((h) -> chunkfun = (struct _obstack_chunk *(*)(void *, long)) (newchunkfun))
225
226#define obstack_freefun(h, newfreefun) \
227 ((h) -> freefun = (void (*)(void *, struct _obstack_chunk *)) (newfreefun))
228
229#define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = (achar))
230
231#define obstack_blank_fast(h,n) ((h)->next_free += (n))
232
233#define obstack_memory_used(h) _obstack_memory_used (h)
234\f
235#if defined __GNUC__
236/* NextStep 2.0 cc is really gcc 1.93 but it defines __GNUC__ = 2 and
237 does not implement __extension__. But that compiler doesn't define
238 __GNUC_MINOR__. */
239# if __GNUC__ < 2 || (__NeXT__ && !__GNUC_MINOR__)
240# define __extension__
241# endif
242
243/* For GNU C, if not -traditional,
244 we can define these macros to compute all args only once
245 without using a global variable.
246 Also, we can avoid using the `temp' slot, to make faster code. */
247
248# define obstack_object_size(OBSTACK) \
249 __extension__ \
250 ({ struct obstack const *__o = (OBSTACK); \
251 (unsigned) (__o->next_free - __o->object_base); })
252
253# define obstack_room(OBSTACK) \
254 __extension__ \
255 ({ struct obstack const *__o = (OBSTACK); \
256 (unsigned) (__o->chunk_limit - __o->next_free); })
257
258# define obstack_make_room(OBSTACK,length) \
259__extension__ \
260({ struct obstack *__o = (OBSTACK); \
261 int __len = (length); \
262 if (__o->chunk_limit - __o->next_free < __len) \
263 _obstack_newchunk (__o, __len); \
264 (void) 0; })
265
266# define obstack_empty_p(OBSTACK) \
267 __extension__ \
268 ({ struct obstack const *__o = (OBSTACK); \
269 (__o->chunk->prev == 0 \
270 && __o->next_free == __PTR_ALIGN ((char *) __o->chunk, \
271 __o->chunk->contents, \
272 __o->alignment_mask)); })
273
274# define obstack_grow(OBSTACK,where,length) \
275__extension__ \
276({ struct obstack *__o = (OBSTACK); \
277 int __len = (length); \
278 if (__o->next_free + __len > __o->chunk_limit) \
279 _obstack_newchunk (__o, __len); \
280 memcpy (__o->next_free, where, __len); \
281 __o->next_free += __len; \
282 (void) 0; })
283
284# define obstack_grow0(OBSTACK,where,length) \
285__extension__ \
286({ struct obstack *__o = (OBSTACK); \
287 int __len = (length); \
288 if (__o->next_free + __len + 1 > __o->chunk_limit) \
289 _obstack_newchunk (__o, __len + 1); \
290 memcpy (__o->next_free, where, __len); \
291 __o->next_free += __len; \
292 *(__o->next_free)++ = 0; \
293 (void) 0; })
294
295# define obstack_1grow(OBSTACK,datum) \
296__extension__ \
297({ struct obstack *__o = (OBSTACK); \
298 if (__o->next_free + 1 > __o->chunk_limit) \
299 _obstack_newchunk (__o, 1); \
300 obstack_1grow_fast (__o, datum); \
301 (void) 0; })
302
303/* These assume that the obstack alignment is good enough for pointers
304 or ints, and that the data added so far to the current object
305 shares that much alignment. */
306
307# define obstack_ptr_grow(OBSTACK,datum) \
308__extension__ \
309({ struct obstack *__o = (OBSTACK); \
310 if (__o->next_free + sizeof (void *) > __o->chunk_limit) \
311 _obstack_newchunk (__o, sizeof (void *)); \
312 obstack_ptr_grow_fast (__o, datum); }) \
313
314# define obstack_int_grow(OBSTACK,datum) \
315__extension__ \
316({ struct obstack *__o = (OBSTACK); \
317 if (__o->next_free + sizeof (int) > __o->chunk_limit) \
318 _obstack_newchunk (__o, sizeof (int)); \
319 obstack_int_grow_fast (__o, datum); })
320
321# define obstack_ptr_grow_fast(OBSTACK,aptr) \
322__extension__ \
323({ struct obstack *__o1 = (OBSTACK); \
324 *(const void **) __o1->next_free = (aptr); \
325 __o1->next_free += sizeof (const void *); \
326 (void) 0; })
327
328# define obstack_int_grow_fast(OBSTACK,aint) \
329__extension__ \
330({ struct obstack *__o1 = (OBSTACK); \
331 *(int *) __o1->next_free = (aint); \
332 __o1->next_free += sizeof (int); \
333 (void) 0; })
334
335# define obstack_blank(OBSTACK,length) \
336__extension__ \
337({ struct obstack *__o = (OBSTACK); \
338 int __len = (length); \
339 if (__o->chunk_limit - __o->next_free < __len) \
340 _obstack_newchunk (__o, __len); \
341 obstack_blank_fast (__o, __len); \
342 (void) 0; })
343
344# define obstack_alloc(OBSTACK,length) \
345__extension__ \
346({ struct obstack *__h = (OBSTACK); \
347 obstack_blank (__h, (length)); \
348 obstack_finish (__h); })
349
350# define obstack_copy(OBSTACK,where,length) \
351__extension__ \
352({ struct obstack *__h = (OBSTACK); \
353 obstack_grow (__h, (where), (length)); \
354 obstack_finish (__h); })
355
356# define obstack_copy0(OBSTACK,where,length) \
357__extension__ \
358({ struct obstack *__h = (OBSTACK); \
359 obstack_grow0 (__h, (where), (length)); \
360 obstack_finish (__h); })
361
362/* The local variable is named __o1 to avoid a name conflict
363 when obstack_blank is called. */
364# define obstack_finish(OBSTACK) \
365__extension__ \
366({ struct obstack *__o1 = (OBSTACK); \
367 void *__value = (void *) __o1->object_base; \
368 if (__o1->next_free == __value) \
369 __o1->maybe_empty_object = 1; \
370 __o1->next_free \
371 = __PTR_ALIGN (__o1->object_base, __o1->next_free, \
372 __o1->alignment_mask); \
373 if (__o1->next_free - (char *)__o1->chunk \
374 > __o1->chunk_limit - (char *)__o1->chunk) \
375 __o1->next_free = __o1->chunk_limit; \
376 __o1->object_base = __o1->next_free; \
377 __value; })
378
379# define obstack_free(OBSTACK, OBJ) \
380__extension__ \
381({ struct obstack *__o = (OBSTACK); \
382 void *__obj = (OBJ); \
383 if (__obj > (void *)__o->chunk && __obj < (void *)__o->chunk_limit) \
384 __o->next_free = __o->object_base = (char *)__obj; \
385 else (obstack_free_func) (__o, __obj); })
386\f
387#else /* not __GNUC__ */
388
389# define obstack_object_size(h) \
390 (unsigned) ((h)->next_free - (h)->object_base)
391
392# define obstack_room(h) \
393 (unsigned) ((h)->chunk_limit - (h)->next_free)
394
395# define obstack_empty_p(h) \
396 ((h)->chunk->prev == 0 \
397 && (h)->next_free == __PTR_ALIGN ((char *) (h)->chunk, \
398 (h)->chunk->contents, \
399 (h)->alignment_mask))
400
401/* Note that the call to _obstack_newchunk is enclosed in (..., 0)
402 so that we can avoid having void expressions
403 in the arms of the conditional expression.
404 Casting the third operand to void was tried before,
405 but some compilers won't accept it. */
406
407# define obstack_make_room(h,length) \
408( (h)->temp.tempint = (length), \
409 (((h)->next_free + (h)->temp.tempint > (h)->chunk_limit) \
410 ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0))
411
412# define obstack_grow(h,where,length) \
413( (h)->temp.tempint = (length), \
414 (((h)->next_free + (h)->temp.tempint > (h)->chunk_limit) \
415 ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0), \
416 memcpy ((h)->next_free, where, (h)->temp.tempint), \
417 (h)->next_free += (h)->temp.tempint)
418
419# define obstack_grow0(h,where,length) \
420( (h)->temp.tempint = (length), \
421 (((h)->next_free + (h)->temp.tempint + 1 > (h)->chunk_limit) \
422 ? (_obstack_newchunk ((h), (h)->temp.tempint + 1), 0) : 0), \
423 memcpy ((h)->next_free, where, (h)->temp.tempint), \
424 (h)->next_free += (h)->temp.tempint, \
425 *((h)->next_free)++ = 0)
426
427# define obstack_1grow(h,datum) \
428( (((h)->next_free + 1 > (h)->chunk_limit) \
429 ? (_obstack_newchunk ((h), 1), 0) : 0), \
430 obstack_1grow_fast (h, datum))
431
432# define obstack_ptr_grow(h,datum) \
433( (((h)->next_free + sizeof (char *) > (h)->chunk_limit) \
434 ? (_obstack_newchunk ((h), sizeof (char *)), 0) : 0), \
435 obstack_ptr_grow_fast (h, datum))
436
437# define obstack_int_grow(h,datum) \
438( (((h)->next_free + sizeof (int) > (h)->chunk_limit) \
439 ? (_obstack_newchunk ((h), sizeof (int)), 0) : 0), \
440 obstack_int_grow_fast (h, datum))
441
442# define obstack_ptr_grow_fast(h,aptr) \
443 (((const void **) ((h)->next_free += sizeof (void *)))[-1] = (aptr))
444
445# define obstack_int_grow_fast(h,aint) \
446 (((int *) ((h)->next_free += sizeof (int)))[-1] = (aint))
447
448# define obstack_blank(h,length) \
449( (h)->temp.tempint = (length), \
450 (((h)->chunk_limit - (h)->next_free < (h)->temp.tempint) \
451 ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0), \
452 obstack_blank_fast (h, (h)->temp.tempint))
453
454# define obstack_alloc(h,length) \
455 (obstack_blank ((h), (length)), obstack_finish ((h)))
456
457# define obstack_copy(h,where,length) \
458 (obstack_grow ((h), (where), (length)), obstack_finish ((h)))
459
460# define obstack_copy0(h,where,length) \
461 (obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
462
463# define obstack_finish(h) \
464( ((h)->next_free == (h)->object_base \
465 ? (((h)->maybe_empty_object = 1), 0) \
466 : 0), \
467 (h)->temp.tempptr = (h)->object_base, \
468 (h)->next_free \
469 = __PTR_ALIGN ((h)->object_base, (h)->next_free, \
470 (h)->alignment_mask), \
471 (((h)->next_free - (char *) (h)->chunk \
472 > (h)->chunk_limit - (char *) (h)->chunk) \
473 ? ((h)->next_free = (h)->chunk_limit) : 0), \
474 (h)->object_base = (h)->next_free, \
475 (h)->temp.tempptr)
476
477# define obstack_free(h,obj) \
478( (h)->temp.tempint = (char *) (obj) - (char *) (h)->chunk, \
479 ((((h)->temp.tempint > 0 \
480 && (h)->temp.tempint < (h)->chunk_limit - (char *) (h)->chunk)) \
481 ? (((h)->next_free = (h)->object_base \
482 = (h)->temp.tempint + (char *) (h)->chunk), 0) \
483 : ((obstack_free_func) ((h), (h)->temp.tempint + (char *) (h)->chunk), 0)))
484
485#endif /* not __GNUC__ */
486
487/* START LOCAL ADDITION */
488static inline int obstack_printf(struct obstack *obst, const char *fmt, ...)
489{
490 char buf[1024];
491 va_list ap;
492 int len;
493
494 va_start(ap, fmt);
495 len = vsnprintf(buf, sizeof(buf), fmt, ap);
496 obstack_grow(obst, buf, len);
497 va_end(ap);
498
499 return len;
500}
501/* Determine default alignment. */
502union fooround
503{
504 uintmax_t i;
505 long double d;
506 void *p;
507};
508struct fooalign
509{
510 char c;
511 union fooround u;
512};
513/* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
514 But in fact it might be less smart and round addresses to as much as
515 DEFAULT_ROUNDING. So we prepare for it to do that. */
516enum
517 {
518 DEFAULT_ALIGNMENT = offsetof (struct fooalign, u),
519 DEFAULT_ROUNDING = sizeof (union fooround)
520 };
521
522/* When we copy a long block of data, this is the unit to do it with.
523 On some machines, copying successive ints does not work;
524 in such a case, redefine COPYING_UNIT to `long' (if that works)
525 or `char' as a last resort. */
526# ifndef COPYING_UNIT
527# define COPYING_UNIT int
528# endif
529
530
531/* The functions allocating more room by calling `obstack_chunk_alloc'
532 jump to the handler pointed to by `obstack_alloc_failed_handler'.
533 This can be set to a user defined function which should either
534 abort gracefully or use longjump - but shouldn't return. This
535 variable by default points to the internal function
536 `print_and_abort'. */
537static void print_and_abort (void);
538
539# ifdef _LIBC
540# if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3_4)
541/* A looong time ago (before 1994, anyway; we're not sure) this global variable
542 was used by non-GNU-C macros to avoid multiple evaluation. The GNU C
543 library still exports it because somebody might use it. */
544struct obstack *_obstack_compat;
545compat_symbol (libc, _obstack_compat, _obstack, GLIBC_2_0);
546# endif
547# endif
548
549/* Define a macro that either calls functions with the traditional malloc/free
550 calling interface, or calls functions with the mmalloc/mfree interface
551 (that adds an extra first argument), based on the state of use_extra_arg.
552 For free, do not use ?:, since some compilers, like the MIPS compilers,
553 do not allow (expr) ? void : void. */
554
555# define CALL_CHUNKFUN(h, size) \
556 (((h) -> use_extra_arg) \
557 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
558 : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
559
560# define CALL_FREEFUN(h, old_chunk) \
561 do { \
562 if ((h) -> use_extra_arg) \
563 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
564 else \
565 (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
566 } while (0)
567
568\f
569/* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
570 Objects start on multiples of ALIGNMENT (0 means use default).
571 CHUNKFUN is the function to use to allocate chunks,
572 and FREEFUN the function to free them.
573 Return nonzero if successful, calls obstack_alloc_failed_handler if
574 allocation fails. */
575
576static int _obstack_begin (struct obstack *h,
577 int size, int alignment,
578 void *(*chunkfun) (long),
579 void (*freefun) (void *))
580{
581 register struct _obstack_chunk *chunk; /* points to new chunk */
582
583 if (alignment == 0)
584 alignment = DEFAULT_ALIGNMENT;
585 if (size == 0)
586 /* Default size is what GNU malloc can fit in a 4096-byte block. */
587 {
588 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
589 Use the values for range checking, because if range checking is off,
590 the extra bytes won't be missed terribly, but if range checking is on
591 and we used a larger request, a whole extra 4096 bytes would be
592 allocated.
593 These number are irrelevant to the new GNU malloc. I suspect it is
594 less sensitive to the size of the request. */
595 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
596 + 4 + DEFAULT_ROUNDING - 1)
597 & ~(DEFAULT_ROUNDING - 1));
598 size = 4096 - extra;
599 }
600
601 h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun;
602 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
603 h->chunk_size = size;
604 h->alignment_mask = alignment - 1;
605 h->use_extra_arg = 0;
606
607 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
608 if (!chunk) print_and_abort();
609 h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
610 alignment - 1);
611 h->chunk_limit = chunk->limit
612 = (char *) chunk + h->chunk_size;
613 chunk->prev = 0;
614 /* The initial chunk now contains no empty object. */
615 h->maybe_empty_object = 0;
616 h->alloc_failed = 0;
617 return 1;
618}
619
620static int _obstack_begin_1 (struct obstack *h, int size, int alignment,
621 void *(*chunkfun) (void *, long),
622 void (*freefun) (void *, void *),
623 void *arg)
624{
625 register struct _obstack_chunk *chunk; /* points to new chunk */
626
627 if (alignment == 0)
628 alignment = DEFAULT_ALIGNMENT;
629 if (size == 0)
630 /* Default size is what GNU malloc can fit in a 4096-byte block. */
631 {
632 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
633 Use the values for range checking, because if range checking is off,
634 the extra bytes won't be missed terribly, but if range checking is on
635 and we used a larger request, a whole extra 4096 bytes would be
636 allocated.
637 These number are irrelevant to the new GNU malloc. I suspect it is
638 less sensitive to the size of the request. */
639 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
640 + 4 + DEFAULT_ROUNDING - 1)
641 & ~(DEFAULT_ROUNDING - 1));
642 size = 4096 - extra;
643 }
644
645 h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun;
646 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
647 h->chunk_size = size;
648 h->alignment_mask = alignment - 1;
649 h->extra_arg = arg;
650 h->use_extra_arg = 1;
651
652 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
653 if (!chunk) print_and_abort();
654 h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
655 alignment - 1);
656 h->chunk_limit = chunk->limit
657 = (char *) chunk + h->chunk_size;
658 chunk->prev = 0;
659 /* The initial chunk now contains no empty object. */
660 h->maybe_empty_object = 0;
661 h->alloc_failed = 0;
662 return 1;
663}
664
665/* Allocate a new current chunk for the obstack *H
666 on the assumption that LENGTH bytes need to be added
667 to the current object, or a new object of length LENGTH allocated.
668 Copies any partial object from the end of the old chunk
669 to the beginning of the new one. */
670
671static void _obstack_newchunk (struct obstack *h, int length)
672{
673 register struct _obstack_chunk *old_chunk = h->chunk;
674 register struct _obstack_chunk *new_chunk;
675 register long new_size;
676 register long obj_size = h->next_free - h->object_base;
677 register long i;
678 long already;
679 char *object_base;
680
681 /* Compute size for new chunk. */
682 new_size = (obj_size + length) + (obj_size >> 3) + h->alignment_mask + 100;
683 if (new_size < h->chunk_size)
684 new_size = h->chunk_size;
685
686 /* Allocate and initialize the new chunk. */
687 new_chunk = CALL_CHUNKFUN (h, new_size);
688 if (!new_chunk) print_and_abort();
689 h->chunk = new_chunk;
690 new_chunk->prev = old_chunk;
691 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
692
693 /* Compute an aligned object_base in the new chunk */
694 object_base =
695 __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask);
696
697 /* Move the existing object to the new chunk.
698 Word at a time is fast and is safe if the object
699 is sufficiently aligned. */
700 if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
701 {
702 for (i = obj_size / sizeof (COPYING_UNIT) - 1;
703 i >= 0; i--)
704 ((COPYING_UNIT *)object_base)[i]
705 = ((COPYING_UNIT *)h->object_base)[i];
706 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
707 but that can cross a page boundary on a machine
708 which does not do strict alignment for COPYING_UNITS. */
709 already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
710 }
711 else
712 already = 0;
713 /* Copy remaining bytes one by one. */
714 for (i = already; i < obj_size; i++)
715 object_base[i] = h->object_base[i];
716
717 /* If the object just copied was the only data in OLD_CHUNK,
718 free that chunk and remove it from the chain.
719 But not if that chunk might contain an empty object. */
720 if (! h->maybe_empty_object
721 && (h->object_base
722 == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents,
723 h->alignment_mask)))
724 {
725 new_chunk->prev = old_chunk->prev;
726 CALL_FREEFUN (h, old_chunk);
727 }
728
729 h->object_base = object_base;
730 h->next_free = h->object_base + obj_size;
731 /* The new chunk certainly contains no empty object yet. */
732 h->maybe_empty_object = 0;
733}
734
735/* Return nonzero if object OBJ has been allocated from obstack H.
736 This is here for debugging.
737 If you use it in a program, you are probably losing. */
738
739/* Free objects in obstack H, including OBJ and everything allocate
740 more recently than OBJ. If OBJ is zero, free everything in H. */
741static void obstack_free_func (struct obstack *h, void *obj)
742{
743 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
744 register struct _obstack_chunk *plp; /* point to previous chunk if any */
745
746 lp = h->chunk;
747 /* We use >= because there cannot be an object at the beginning of a chunk.
748 But there can be an empty object at that address
749 at the end of another chunk. */
750 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
751 {
752 plp = lp->prev;
753 CALL_FREEFUN (h, lp);
754 lp = plp;
755 /* If we switch chunks, we can't tell whether the new current
756 chunk contains an empty object, so assume that it may. */
757 h->maybe_empty_object = 1;
758 }
759 if (lp)
760 {
761 h->object_base = h->next_free = (char *) (obj);
762 h->chunk_limit = lp->limit;
763 h->chunk = lp;
764 }
765 else if (obj != 0)
766 /* obj is not in any of the chunks! */
767 abort ();
768}
769
770static int _obstack_memory_used (struct obstack *h)
771{
772 register struct _obstack_chunk* lp;
773 register int nbytes = 0;
774
775 for (lp = h->chunk; lp != 0; lp = lp->prev)
776 {
777 nbytes += lp->limit - (char *) lp;
778 }
779 return nbytes;
780}
781
782static void __attribute__ ((noreturn)) print_and_abort (void)
783{
784 fprintf(stderr, "%s\n", "memory exhausted");
785 exit(1);
786}
787
788/* END LOCAL ADDITION */
789
790#ifdef __cplusplus
791} /* C++ */
792#endif
793
794#endif /* obstack.h */