blob: 6a44b44b97f3619a8b66477ed09b3270e7ab4ebb [file] [log] [blame]
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001/* obstack.h - object stack macros
Ying Wang05436632013-04-05 16:01:00 -07002 Copyright (C) 1988-1994, 1996-1999, 2003-2006, 2009-2012 Free Software
3 Foundation, Inc.
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08004 This file is part of the GNU C Library.
5
Ying Wang05436632013-04-05 16:01:00 -07006 This program is free software: you can redistribute it and/or modify
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08007 it under the terms of the GNU General Public License as published by
Ying Wang05436632013-04-05 16:01:00 -07008 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080010
11 This program 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
14 GNU General Public License for more details.
15
Ying Wang05436632013-04-05 16:01:00 -070016 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080018
19/* Summary:
20
21All the apparent functions defined here are macros. The idea
22is that you would use these pre-tested macros to solve a
23very specific set of problems, and they would run fast.
24Caution: no side-effects in arguments please!! They may be
25evaluated MANY times!!
26
27These macros operate a stack of objects. Each object starts life
28small, and may grow to maturity. (Consider building a word syllable
29by syllable.) An object can move while it is growing. Once it has
30been "finished" it never changes address again. So the "top of the
31stack" is typically an immature growing object, while the rest of the
32stack is of mature, fixed size and fixed address objects.
33
34These routines grab large chunks of memory, using a function you
Ying Wang05436632013-04-05 16:01:00 -070035supply, called 'obstack_chunk_alloc'. On occasion, they free chunks,
36by calling 'obstack_chunk_free'. You must define them and declare
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080037them before using any obstack macros.
38
Ying Wang05436632013-04-05 16:01:00 -070039Each independent stack is represented by a 'struct obstack'.
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080040Each of the obstack macros expects a pointer to such a structure
41as the first argument.
42
43One motivation for this package is the problem of growing char strings
44in symbol tables. Unless you are "fascist pig with a read-only mind"
45--Gosper's immortal quote from HAKMEM item 154, out of context--you
46would not like to put any arbitrary upper limit on the length of your
47symbols.
48
49In practice this often means you will build many short symbols and a
50few long symbols. At the time you are reading a symbol you don't know
51how long it is. One traditional method is to read a symbol into a
52buffer, realloc()ating the buffer every time you try to read a symbol
53that is longer than the buffer. This is beaut, but you still will
54want to copy the symbol from the buffer to a more permanent
55symbol-table entry say about half the time.
56
57With obstacks, you can work differently. Use one obstack for all symbol
58names. As you read a symbol, grow the name in the obstack gradually.
59When the name is complete, finalize it. Then, if the symbol exists already,
60free the newly read name.
61
62The way we do this is to take a large chunk, allocating memory from
63low addresses. When you want to build a symbol in the chunk you just
64add chars above the current "high water mark" in the chunk. When you
65have finished adding chars, because you got to the end of the symbol,
66you know how long the chars are, and you can create a new object.
67Mostly the chars will not burst over the highest address of the chunk,
68because you would typically expect a chunk to be (say) 100 times as
69long as an average object.
70
71In case that isn't clear, when we have enough chars to make up
72the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed)
73so we just point to it where it lies. No moving of chars is
74needed and this is the second win: potentially long strings need
75never be explicitly shuffled. Once an object is formed, it does not
76change its address during its lifetime.
77
78When the chars burst over a chunk boundary, we allocate a larger
79chunk, and then copy the partly formed object from the end of the old
80chunk to the beginning of the new larger chunk. We then carry on
81accreting characters to the end of the object as we normally would.
82
83A special macro is provided to add a single char at a time to a
84growing object. This allows the use of register variables, which
85break the ordinary 'growth' macro.
86
87Summary:
Ying Wang05436632013-04-05 16:01:00 -070088 We allocate large chunks.
89 We carve out one object at a time from the current chunk.
90 Once carved, an object never moves.
91 We are free to append data of any size to the currently
92 growing object.
93 Exactly one object is growing in an obstack at any one time.
94 You can run one obstack per control block.
95 You may have as many control blocks as you dare.
96 Because of the way we do it, you can "unwind" an obstack
97 back to a previous state. (You may remove objects much
98 as you would with a stack.)
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080099*/
100
101
102/* Don't do the contents of this file more than once. */
103
104#ifndef _OBSTACK_H
105#define _OBSTACK_H 1
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800106
107/* We need the type of a pointer subtraction. If __PTRDIFF_TYPE__ is
108 defined, as with GNU C, use that; that way we don't pollute the
109 namespace with <stddef.h>'s symbols. Otherwise, include <stddef.h>
110 and use ptrdiff_t. */
111
112#ifdef __PTRDIFF_TYPE__
113# define PTR_INT_TYPE __PTRDIFF_TYPE__
114#else
115# include <stddef.h>
116# define PTR_INT_TYPE ptrdiff_t
117#endif
118
119/* If B is the base of an object addressed by P, return the result of
120 aligning P to the next multiple of A + 1. B and P must be of type
121 char *. A + 1 must be a power of 2. */
122
123#define __BPTR_ALIGN(B, P, A) ((B) + (((P) - (B) + (A)) & ~(A)))
124
Ying Wang05436632013-04-05 16:01:00 -0700125/* Similar to _BPTR_ALIGN (B, P, A), except optimize the common case
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800126 where pointers can be converted to integers, aligned as integers,
127 and converted back again. If PTR_INT_TYPE is narrower than a
128 pointer (e.g., the AS/400), play it safe and compute the alignment
129 relative to B. Otherwise, use the faster strategy of computing the
130 alignment relative to 0. */
131
Ying Wang05436632013-04-05 16:01:00 -0700132#define __PTR_ALIGN(B, P, A) \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800133 __BPTR_ALIGN (sizeof (PTR_INT_TYPE) < sizeof (void *) ? (B) : (char *) 0, \
Ying Wang05436632013-04-05 16:01:00 -0700134 P, A)
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800135
136#include <string.h>
137
Ying Wang05436632013-04-05 16:01:00 -0700138#ifdef __cplusplus
139extern "C" {
140#endif
141
142struct _obstack_chunk /* Lives at front of each chunk. */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800143{
Ying Wang05436632013-04-05 16:01:00 -0700144 char *limit; /* 1 past end of this chunk */
145 struct _obstack_chunk *prev; /* address of prior chunk or NULL */
146 char contents[4]; /* objects begin here */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800147};
148
Ying Wang05436632013-04-05 16:01:00 -0700149struct obstack /* control current object in current chunk */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800150{
Ying Wang05436632013-04-05 16:01:00 -0700151 long chunk_size; /* preferred size to allocate chunks in */
152 struct _obstack_chunk *chunk; /* address of current struct obstack_chunk */
153 char *object_base; /* address of object we are building */
154 char *next_free; /* where to add next char to current object */
155 char *chunk_limit; /* address of char after current chunk */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800156 union
157 {
158 PTR_INT_TYPE tempint;
159 void *tempptr;
Ying Wang05436632013-04-05 16:01:00 -0700160 } temp; /* Temporary for some macros. */
161 int alignment_mask; /* Mask of alignment for each object. */
162 /* These prototypes vary based on 'use_extra_arg', and we use
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800163 casts to the prototypeless function type in all assignments,
164 but having prototypes here quiets -Wstrict-prototypes. */
165 struct _obstack_chunk *(*chunkfun) (void *, long);
166 void (*freefun) (void *, struct _obstack_chunk *);
Ying Wang05436632013-04-05 16:01:00 -0700167 void *extra_arg; /* first arg for chunk alloc/dealloc funcs */
168 unsigned use_extra_arg:1; /* chunk alloc/dealloc funcs take extra arg */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800169 unsigned maybe_empty_object:1;/* There is a possibility that the current
Ying Wang05436632013-04-05 16:01:00 -0700170 chunk contains a zero-length object. This
171 prevents freeing the chunk if we allocate
172 a bigger chunk to replace it. */
173 unsigned alloc_failed:1; /* No longer used, as we now call the failed
174 handler on error, but retained for binary
175 compatibility. */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800176};
177
178/* Declare the external functions we use; they are in obstack.c. */
179
180extern void _obstack_newchunk (struct obstack *, int);
181extern int _obstack_begin (struct obstack *, int, int,
Ying Wang05436632013-04-05 16:01:00 -0700182 void *(*) (long), void (*) (void *));
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800183extern int _obstack_begin_1 (struct obstack *, int, int,
Ying Wang05436632013-04-05 16:01:00 -0700184 void *(*) (void *, long),
185 void (*) (void *, void *), void *);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800186extern int _obstack_memory_used (struct obstack *);
187
Ying Wang05436632013-04-05 16:01:00 -0700188/* The default name of the function for freeing a chunk is 'obstack_free',
189 but gnulib users can override this by defining '__obstack_free'. */
190#ifndef __obstack_free
191# define __obstack_free obstack_free
192#endif
193extern void __obstack_free (struct obstack *obstack, void *block);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800194
195
Ying Wang05436632013-04-05 16:01:00 -0700196/* Error handler called when 'obstack_chunk_alloc' failed to allocate
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800197 more memory. This can be set to a user defined function which
198 should either abort gracefully or use longjump - but shouldn't
199 return. The default action is to print a message and abort. */
200extern void (*obstack_alloc_failed_handler) (void);
201
Ying Wang05436632013-04-05 16:01:00 -0700202/* Exit value used when 'print_and_abort' is used. */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800203extern int obstack_exit_failure;
204
205/* Pointer to beginning of object being allocated or to be allocated next.
206 Note that this might not be the final address of the object
207 because a new chunk might be needed to hold the final size. */
208
209#define obstack_base(h) ((void *) (h)->object_base)
210
211/* Size for allocating ordinary chunks. */
212
213#define obstack_chunk_size(h) ((h)->chunk_size)
214
215/* Pointer to next byte not yet allocated in current chunk. */
216
Ying Wang05436632013-04-05 16:01:00 -0700217#define obstack_next_free(h) ((h)->next_free)
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800218
219/* Mask specifying low bits that should be clear in address of an object. */
220
221#define obstack_alignment_mask(h) ((h)->alignment_mask)
222
223/* To prevent prototype warnings provide complete argument list. */
Ying Wang05436632013-04-05 16:01:00 -0700224#define obstack_init(h) \
225 _obstack_begin ((h), 0, 0, \
226 (void *(*) (long)) obstack_chunk_alloc, \
227 (void (*) (void *)) obstack_chunk_free)
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800228
Ying Wang05436632013-04-05 16:01:00 -0700229#define obstack_begin(h, size) \
230 _obstack_begin ((h), (size), 0, \
231 (void *(*) (long)) obstack_chunk_alloc, \
232 (void (*) (void *)) obstack_chunk_free)
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800233
234#define obstack_specify_allocation(h, size, alignment, chunkfun, freefun) \
Ying Wang05436632013-04-05 16:01:00 -0700235 _obstack_begin ((h), (size), (alignment), \
236 (void *(*) (long)) (chunkfun), \
237 (void (*) (void *)) (freefun))
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800238
239#define obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) \
Ying Wang05436632013-04-05 16:01:00 -0700240 _obstack_begin_1 ((h), (size), (alignment), \
241 (void *(*) (void *, long)) (chunkfun), \
242 (void (*) (void *, void *)) (freefun), (arg))
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800243
244#define obstack_chunkfun(h, newchunkfun) \
245 ((h) -> chunkfun = (struct _obstack_chunk *(*)(void *, long)) (newchunkfun))
246
247#define obstack_freefun(h, newfreefun) \
248 ((h) -> freefun = (void (*)(void *, struct _obstack_chunk *)) (newfreefun))
249
250#define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = (achar))
251
252#define obstack_blank_fast(h,n) ((h)->next_free += (n))
253
254#define obstack_memory_used(h) _obstack_memory_used (h)
255
Ying Wang05436632013-04-05 16:01:00 -0700256#if defined __GNUC__
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800257/* NextStep 2.0 cc is really gcc 1.93 but it defines __GNUC__ = 2 and
258 does not implement __extension__. But that compiler doesn't define
259 __GNUC_MINOR__. */
260# if __GNUC__ < 2 || (__NeXT__ && !__GNUC_MINOR__)
261# define __extension__
262# endif
263
264/* For GNU C, if not -traditional,
265 we can define these macros to compute all args only once
266 without using a global variable.
Ying Wang05436632013-04-05 16:01:00 -0700267 Also, we can avoid using the 'temp' slot, to make faster code. */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800268
Ying Wang05436632013-04-05 16:01:00 -0700269# define obstack_object_size(OBSTACK) \
270 __extension__ \
271 ({ struct obstack const *__o = (OBSTACK); \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800272 (unsigned) (__o->next_free - __o->object_base); })
273
Ying Wang05436632013-04-05 16:01:00 -0700274# define obstack_room(OBSTACK) \
275 __extension__ \
276 ({ struct obstack const *__o = (OBSTACK); \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800277 (unsigned) (__o->chunk_limit - __o->next_free); })
278
Ying Wang05436632013-04-05 16:01:00 -0700279# define obstack_make_room(OBSTACK,length) \
280__extension__ \
281({ struct obstack *__o = (OBSTACK); \
282 int __len = (length); \
283 if (__o->chunk_limit - __o->next_free < __len) \
284 _obstack_newchunk (__o, __len); \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800285 (void) 0; })
286
Ying Wang05436632013-04-05 16:01:00 -0700287# define obstack_empty_p(OBSTACK) \
288 __extension__ \
289 ({ struct obstack const *__o = (OBSTACK); \
290 (__o->chunk->prev == 0 \
291 && __o->next_free == __PTR_ALIGN ((char *) __o->chunk, \
292 __o->chunk->contents, \
293 __o->alignment_mask)); })
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800294
Ying Wang05436632013-04-05 16:01:00 -0700295# define obstack_grow(OBSTACK,where,length) \
296__extension__ \
297({ struct obstack *__o = (OBSTACK); \
298 int __len = (length); \
299 if (__o->next_free + __len > __o->chunk_limit) \
300 _obstack_newchunk (__o, __len); \
301 memcpy (__o->next_free, where, __len); \
302 __o->next_free += __len; \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800303 (void) 0; })
304
Ying Wang05436632013-04-05 16:01:00 -0700305# define obstack_grow0(OBSTACK,where,length) \
306__extension__ \
307({ struct obstack *__o = (OBSTACK); \
308 int __len = (length); \
309 if (__o->next_free + __len + 1 > __o->chunk_limit) \
310 _obstack_newchunk (__o, __len + 1); \
311 memcpy (__o->next_free, where, __len); \
312 __o->next_free += __len; \
313 *(__o->next_free)++ = 0; \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800314 (void) 0; })
315
Ying Wang05436632013-04-05 16:01:00 -0700316# define obstack_1grow(OBSTACK,datum) \
317__extension__ \
318({ struct obstack *__o = (OBSTACK); \
319 if (__o->next_free + 1 > __o->chunk_limit) \
320 _obstack_newchunk (__o, 1); \
321 obstack_1grow_fast (__o, datum); \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800322 (void) 0; })
323
324/* These assume that the obstack alignment is good enough for pointers
325 or ints, and that the data added so far to the current object
326 shares that much alignment. */
327
Ying Wang05436632013-04-05 16:01:00 -0700328# define obstack_ptr_grow(OBSTACK,datum) \
329__extension__ \
330({ struct obstack *__o = (OBSTACK); \
331 if (__o->next_free + sizeof (void *) > __o->chunk_limit) \
332 _obstack_newchunk (__o, sizeof (void *)); \
333 obstack_ptr_grow_fast (__o, datum); }) \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800334
Ying Wang05436632013-04-05 16:01:00 -0700335# define obstack_int_grow(OBSTACK,datum) \
336__extension__ \
337({ struct obstack *__o = (OBSTACK); \
338 if (__o->next_free + sizeof (int) > __o->chunk_limit) \
339 _obstack_newchunk (__o, sizeof (int)); \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800340 obstack_int_grow_fast (__o, datum); })
341
Ying Wang05436632013-04-05 16:01:00 -0700342# define obstack_ptr_grow_fast(OBSTACK,aptr) \
343__extension__ \
344({ struct obstack *__o1 = (OBSTACK); \
345 *(const void **) __o1->next_free = (aptr); \
346 __o1->next_free += sizeof (const void *); \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800347 (void) 0; })
348
Ying Wang05436632013-04-05 16:01:00 -0700349# define obstack_int_grow_fast(OBSTACK,aint) \
350__extension__ \
351({ struct obstack *__o1 = (OBSTACK); \
352 *(int *) __o1->next_free = (aint); \
353 __o1->next_free += sizeof (int); \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800354 (void) 0; })
355
Ying Wang05436632013-04-05 16:01:00 -0700356# define obstack_blank(OBSTACK,length) \
357__extension__ \
358({ struct obstack *__o = (OBSTACK); \
359 int __len = (length); \
360 if (__o->chunk_limit - __o->next_free < __len) \
361 _obstack_newchunk (__o, __len); \
362 obstack_blank_fast (__o, __len); \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800363 (void) 0; })
364
Ying Wang05436632013-04-05 16:01:00 -0700365# define obstack_alloc(OBSTACK,length) \
366__extension__ \
367({ struct obstack *__h = (OBSTACK); \
368 obstack_blank (__h, (length)); \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800369 obstack_finish (__h); })
370
Ying Wang05436632013-04-05 16:01:00 -0700371# define obstack_copy(OBSTACK,where,length) \
372__extension__ \
373({ struct obstack *__h = (OBSTACK); \
374 obstack_grow (__h, (where), (length)); \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800375 obstack_finish (__h); })
376
Ying Wang05436632013-04-05 16:01:00 -0700377# define obstack_copy0(OBSTACK,where,length) \
378__extension__ \
379({ struct obstack *__h = (OBSTACK); \
380 obstack_grow0 (__h, (where), (length)); \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800381 obstack_finish (__h); })
382
383/* The local variable is named __o1 to avoid a name conflict
384 when obstack_blank is called. */
Ying Wang05436632013-04-05 16:01:00 -0700385# define obstack_finish(OBSTACK) \
386__extension__ \
387({ struct obstack *__o1 = (OBSTACK); \
388 void *__value = (void *) __o1->object_base; \
389 if (__o1->next_free == __value) \
390 __o1->maybe_empty_object = 1; \
391 __o1->next_free \
392 = __PTR_ALIGN (__o1->object_base, __o1->next_free, \
393 __o1->alignment_mask); \
394 if (__o1->next_free - (char *)__o1->chunk \
395 > __o1->chunk_limit - (char *)__o1->chunk) \
396 __o1->next_free = __o1->chunk_limit; \
397 __o1->object_base = __o1->next_free; \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800398 __value; })
399
Ying Wang05436632013-04-05 16:01:00 -0700400# define obstack_free(OBSTACK, OBJ) \
401__extension__ \
402({ struct obstack *__o = (OBSTACK); \
403 void *__obj = (OBJ); \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800404 if (__obj > (void *)__o->chunk && __obj < (void *)__o->chunk_limit) \
Ying Wang05436632013-04-05 16:01:00 -0700405 __o->next_free = __o->object_base = (char *)__obj; \
406 else (__obstack_free) (__o, __obj); })
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800407
Ying Wang05436632013-04-05 16:01:00 -0700408#else /* not __GNUC__ */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800409
410# define obstack_object_size(h) \
411 (unsigned) ((h)->next_free - (h)->object_base)
412
Ying Wang05436632013-04-05 16:01:00 -0700413# define obstack_room(h) \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800414 (unsigned) ((h)->chunk_limit - (h)->next_free)
415
416# define obstack_empty_p(h) \
Ying Wang05436632013-04-05 16:01:00 -0700417 ((h)->chunk->prev == 0 \
418 && (h)->next_free == __PTR_ALIGN ((char *) (h)->chunk, \
419 (h)->chunk->contents, \
420 (h)->alignment_mask))
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800421
422/* Note that the call to _obstack_newchunk is enclosed in (..., 0)
423 so that we can avoid having void expressions
424 in the arms of the conditional expression.
425 Casting the third operand to void was tried before,
426 but some compilers won't accept it. */
427
Ying Wang05436632013-04-05 16:01:00 -0700428# define obstack_make_room(h,length) \
429( (h)->temp.tempint = (length), \
430 (((h)->next_free + (h)->temp.tempint > (h)->chunk_limit) \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800431 ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0))
432
Ying Wang05436632013-04-05 16:01:00 -0700433# define obstack_grow(h,where,length) \
434( (h)->temp.tempint = (length), \
435 (((h)->next_free + (h)->temp.tempint > (h)->chunk_limit) \
436 ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0), \
437 memcpy ((h)->next_free, where, (h)->temp.tempint), \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800438 (h)->next_free += (h)->temp.tempint)
439
Ying Wang05436632013-04-05 16:01:00 -0700440# define obstack_grow0(h,where,length) \
441( (h)->temp.tempint = (length), \
442 (((h)->next_free + (h)->temp.tempint + 1 > (h)->chunk_limit) \
443 ? (_obstack_newchunk ((h), (h)->temp.tempint + 1), 0) : 0), \
444 memcpy ((h)->next_free, where, (h)->temp.tempint), \
445 (h)->next_free += (h)->temp.tempint, \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800446 *((h)->next_free)++ = 0)
447
Ying Wang05436632013-04-05 16:01:00 -0700448# define obstack_1grow(h,datum) \
449( (((h)->next_free + 1 > (h)->chunk_limit) \
450 ? (_obstack_newchunk ((h), 1), 0) : 0), \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800451 obstack_1grow_fast (h, datum))
452
Ying Wang05436632013-04-05 16:01:00 -0700453# define obstack_ptr_grow(h,datum) \
454( (((h)->next_free + sizeof (char *) > (h)->chunk_limit) \
455 ? (_obstack_newchunk ((h), sizeof (char *)), 0) : 0), \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800456 obstack_ptr_grow_fast (h, datum))
457
Ying Wang05436632013-04-05 16:01:00 -0700458# define obstack_int_grow(h,datum) \
459( (((h)->next_free + sizeof (int) > (h)->chunk_limit) \
460 ? (_obstack_newchunk ((h), sizeof (int)), 0) : 0), \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800461 obstack_int_grow_fast (h, datum))
462
Ying Wang05436632013-04-05 16:01:00 -0700463# define obstack_ptr_grow_fast(h,aptr) \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800464 (((const void **) ((h)->next_free += sizeof (void *)))[-1] = (aptr))
465
Ying Wang05436632013-04-05 16:01:00 -0700466# define obstack_int_grow_fast(h,aint) \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800467 (((int *) ((h)->next_free += sizeof (int)))[-1] = (aint))
468
Ying Wang05436632013-04-05 16:01:00 -0700469# define obstack_blank(h,length) \
470( (h)->temp.tempint = (length), \
471 (((h)->chunk_limit - (h)->next_free < (h)->temp.tempint) \
472 ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0), \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800473 obstack_blank_fast (h, (h)->temp.tempint))
474
Ying Wang05436632013-04-05 16:01:00 -0700475# define obstack_alloc(h,length) \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800476 (obstack_blank ((h), (length)), obstack_finish ((h)))
477
Ying Wang05436632013-04-05 16:01:00 -0700478# define obstack_copy(h,where,length) \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800479 (obstack_grow ((h), (where), (length)), obstack_finish ((h)))
480
Ying Wang05436632013-04-05 16:01:00 -0700481# define obstack_copy0(h,where,length) \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800482 (obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
483
Ying Wang05436632013-04-05 16:01:00 -0700484# define obstack_finish(h) \
485( ((h)->next_free == (h)->object_base \
486 ? (((h)->maybe_empty_object = 1), 0) \
487 : 0), \
488 (h)->temp.tempptr = (h)->object_base, \
489 (h)->next_free \
490 = __PTR_ALIGN ((h)->object_base, (h)->next_free, \
491 (h)->alignment_mask), \
492 (((h)->next_free - (char *) (h)->chunk \
493 > (h)->chunk_limit - (char *) (h)->chunk) \
494 ? ((h)->next_free = (h)->chunk_limit) : 0), \
495 (h)->object_base = (h)->next_free, \
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800496 (h)->temp.tempptr)
497
Ying Wang05436632013-04-05 16:01:00 -0700498# define obstack_free(h,obj) \
499( (h)->temp.tempint = (char *) (obj) - (char *) (h)->chunk, \
500 ((((h)->temp.tempint > 0 \
501 && (h)->temp.tempint < (h)->chunk_limit - (char *) (h)->chunk)) \
502 ? (int) ((h)->next_free = (h)->object_base \
503 = (h)->temp.tempint + (char *) (h)->chunk) \
504 : (((__obstack_free) ((h), (h)->temp.tempint + (char *) (h)->chunk), 0), 0)))
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800505
Ying Wang05436632013-04-05 16:01:00 -0700506#endif /* not __GNUC__ */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800507
508#ifdef __cplusplus
Ying Wang05436632013-04-05 16:01:00 -0700509} /* C++ */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -0800510#endif
511
512#endif /* obstack.h */