]>
Commit | Line | Data |
---|---|---|
f65fd747 | 1 | /* obstack.h - object stack macros |
688903eb | 2 | Copyright (C) 1988-2018 Free Software Foundation, Inc. |
2fd4de4b | 3 | This file is part of the GNU C Library. |
f65fd747 UD |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
f65fd747 UD |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
f65fd747 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
f65fd747 UD |
18 | |
19 | /* Summary: | |
20 | ||
6c8dbf00 OB |
21 | All the apparent functions defined here are macros. The idea |
22 | is that you would use these pre-tested macros to solve a | |
23 | very specific set of problems, and they would run fast. | |
24 | Caution: no side-effects in arguments please!! They may be | |
25 | evaluated MANY times!! | |
26 | ||
27 | These macros operate a stack of objects. Each object starts life | |
28 | small, and may grow to maturity. (Consider building a word syllable | |
29 | by syllable.) An object can move while it is growing. Once it has | |
30 | been "finished" it never changes address again. So the "top of the | |
31 | stack" is typically an immature growing object, while the rest of the | |
32 | stack is of mature, fixed size and fixed address objects. | |
33 | ||
34 | These routines grab large chunks of memory, using a function you | |
b100f9aa WN |
35 | supply, called 'obstack_chunk_alloc'. On occasion, they free chunks, |
36 | by calling 'obstack_chunk_free'. You must define them and declare | |
6c8dbf00 OB |
37 | them before using any obstack macros. |
38 | ||
b100f9aa | 39 | Each independent stack is represented by a 'struct obstack'. |
6c8dbf00 OB |
40 | Each of the obstack macros expects a pointer to such a structure |
41 | as the first argument. | |
42 | ||
43 | One motivation for this package is the problem of growing char strings | |
44 | in 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 | |
46 | would not like to put any arbitrary upper limit on the length of your | |
47 | symbols. | |
48 | ||
49 | In practice this often means you will build many short symbols and a | |
50 | few long symbols. At the time you are reading a symbol you don't know | |
51 | how long it is. One traditional method is to read a symbol into a | |
52 | buffer, realloc()ating the buffer every time you try to read a symbol | |
53 | that is longer than the buffer. This is beaut, but you still will | |
54 | want to copy the symbol from the buffer to a more permanent | |
55 | symbol-table entry say about half the time. | |
56 | ||
57 | With obstacks, you can work differently. Use one obstack for all symbol | |
58 | names. As you read a symbol, grow the name in the obstack gradually. | |
59 | When the name is complete, finalize it. Then, if the symbol exists already, | |
60 | free the newly read name. | |
61 | ||
62 | The way we do this is to take a large chunk, allocating memory from | |
63 | low addresses. When you want to build a symbol in the chunk you just | |
64 | add chars above the current "high water mark" in the chunk. When you | |
65 | have finished adding chars, because you got to the end of the symbol, | |
66 | you know how long the chars are, and you can create a new object. | |
67 | Mostly the chars will not burst over the highest address of the chunk, | |
68 | because you would typically expect a chunk to be (say) 100 times as | |
69 | long as an average object. | |
70 | ||
71 | In case that isn't clear, when we have enough chars to make up | |
72 | the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed) | |
73 | so we just point to it where it lies. No moving of chars is | |
74 | needed and this is the second win: potentially long strings need | |
75 | never be explicitly shuffled. Once an object is formed, it does not | |
76 | change its address during its lifetime. | |
77 | ||
78 | When the chars burst over a chunk boundary, we allocate a larger | |
79 | chunk, and then copy the partly formed object from the end of the old | |
80 | chunk to the beginning of the new larger chunk. We then carry on | |
81 | accreting characters to the end of the object as we normally would. | |
82 | ||
83 | A special macro is provided to add a single char at a time to a | |
84 | growing object. This allows the use of register variables, which | |
85 | break the ordinary 'growth' macro. | |
86 | ||
87 | Summary: | |
b100f9aa WN |
88 | 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.) | |
6c8dbf00 | 99 | */ |
f65fd747 UD |
100 | |
101 | ||
102 | /* Don't do the contents of this file more than once. */ | |
103 | ||
5107cf1d UD |
104 | #ifndef _OBSTACK_H |
105 | #define _OBSTACK_H 1 | |
1fb05e3d | 106 | |
2fd4de4b | 107 | /* We need the type of a pointer subtraction. If __PTRDIFF_TYPE__ is |
14ea22e9 | 108 | defined, as with GNU C, use that; that way we don't pollute the |
9d1e2b7d UD |
109 | namespace with <stddef.h>'s symbols. Otherwise, include <stddef.h> |
110 | and use ptrdiff_t. */ | |
f65fd747 | 111 | |
14ea22e9 UD |
112 | #ifdef __PTRDIFF_TYPE__ |
113 | # define PTR_INT_TYPE __PTRDIFF_TYPE__ | |
f65fd747 | 114 | #else |
9d1e2b7d UD |
115 | # include <stddef.h> |
116 | # define PTR_INT_TYPE ptrdiff_t | |
f65fd747 UD |
117 | #endif |
118 | ||
2fd4de4b RM |
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 | ||
b100f9aa | 123 | #define __BPTR_ALIGN(B, P, A) ((B) + (((P) - (B) + (A)) & ~(A))) |
2fd4de4b | 124 | |
b100f9aa | 125 | /* Similar to _BPTR_ALIGN (B, P, A), except optimize the common case |
2fd4de4b RM |
126 | 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 | ||
6c8dbf00 | 132 | #define __PTR_ALIGN(B, P, A) \ |
2fd4de4b | 133 | __BPTR_ALIGN (sizeof (PTR_INT_TYPE) < sizeof (void *) ? (B) : (char *) 0, \ |
b100f9aa | 134 | P, A) |
2fd4de4b | 135 | |
9d1e2b7d | 136 | #include <string.h> |
f65fd747 | 137 | |
b100f9aa WN |
138 | #ifndef __attribute_pure__ |
139 | # define __attribute_pure__ _GL_ATTRIBUTE_PURE | |
140 | #endif | |
141 | ||
142 | #ifdef __cplusplus | |
143 | extern "C" { | |
144 | #endif | |
145 | ||
6c8dbf00 | 146 | struct _obstack_chunk /* Lives at front of each chunk. */ |
f65fd747 | 147 | { |
6c8dbf00 OB |
148 | char *limit; /* 1 past end of this chunk */ |
149 | struct _obstack_chunk *prev; /* address of prior chunk or NULL */ | |
150 | char contents[4]; /* objects begin here */ | |
f65fd747 UD |
151 | }; |
152 | ||
6c8dbf00 | 153 | struct obstack /* control current object in current chunk */ |
f65fd747 | 154 | { |
6c8dbf00 OB |
155 | long chunk_size; /* preferred size to allocate chunks in */ |
156 | struct _obstack_chunk *chunk; /* address of current struct obstack_chunk */ | |
157 | char *object_base; /* address of object we are building */ | |
158 | char *next_free; /* where to add next char to current object */ | |
159 | char *chunk_limit; /* address of char after current chunk */ | |
2fd4de4b RM |
160 | union |
161 | { | |
162 | PTR_INT_TYPE tempint; | |
163 | void *tempptr; | |
6c8dbf00 OB |
164 | } temp; /* Temporary for some macros. */ |
165 | int alignment_mask; /* Mask of alignment for each object. */ | |
b100f9aa | 166 | /* These prototypes vary based on 'use_extra_arg', and we use |
f65fd747 UD |
167 | casts to the prototypeless function type in all assignments, |
168 | but having prototypes here quiets -Wstrict-prototypes. */ | |
169 | struct _obstack_chunk *(*chunkfun) (void *, long); | |
170 | void (*freefun) (void *, struct _obstack_chunk *); | |
6c8dbf00 OB |
171 | void *extra_arg; /* first arg for chunk alloc/dealloc funcs */ |
172 | unsigned use_extra_arg : 1; /* chunk alloc/dealloc funcs take extra arg */ | |
173 | unsigned maybe_empty_object : 1; /* There is a possibility that the current | |
b100f9aa WN |
174 | chunk contains a zero-length object. This |
175 | prevents freeing the chunk if we allocate | |
176 | a bigger chunk to replace it. */ | |
6c8dbf00 | 177 | unsigned alloc_failed : 1; /* No longer used, as we now call the failed |
b100f9aa WN |
178 | handler on error, but retained for binary |
179 | compatibility. */ | |
f65fd747 UD |
180 | }; |
181 | ||
182 | /* Declare the external functions we use; they are in obstack.c. */ | |
183 | ||
f65fd747 | 184 | extern void _obstack_newchunk (struct obstack *, int); |
f65fd747 | 185 | extern int _obstack_begin (struct obstack *, int, int, |
b100f9aa | 186 | void *(*)(long), void (*)(void *)); |
f65fd747 | 187 | extern int _obstack_begin_1 (struct obstack *, int, int, |
b100f9aa WN |
188 | void *(*)(void *, long), |
189 | void (*)(void *, void *), void *); | |
190 | extern int _obstack_memory_used (struct obstack *) __attribute_pure__; | |
191 | ||
192 | /* The default name of the function for freeing a chunk is 'obstack_free', | |
193 | but gnulib users can override this by defining '__obstack_free'. */ | |
194 | #ifndef __obstack_free | |
195 | # define __obstack_free obstack_free | |
196 | #endif | |
197 | extern void __obstack_free (struct obstack *, void *); | |
f65fd747 | 198 | |
6c8dbf00 | 199 | |
b100f9aa | 200 | /* Error handler called when 'obstack_chunk_alloc' failed to allocate |
8325d82c UD |
201 | more memory. This can be set to a user defined function which |
202 | should either abort gracefully or use longjump - but shouldn't | |
203 | return. The default action is to print a message and abort. */ | |
f65fd747 | 204 | extern void (*obstack_alloc_failed_handler) (void); |
f65fd747 | 205 | |
b100f9aa | 206 | /* Exit value used when 'print_and_abort' is used. */ |
f65fd747 | 207 | extern int obstack_exit_failure; |
6c8dbf00 | 208 | |
f65fd747 UD |
209 | /* Pointer to beginning of object being allocated or to be allocated next. |
210 | Note that this might not be the final address of the object | |
211 | because a new chunk might be needed to hold the final size. */ | |
212 | ||
0b8d1390 | 213 | #define obstack_base(h) ((void *) (h)->object_base) |
f65fd747 UD |
214 | |
215 | /* Size for allocating ordinary chunks. */ | |
216 | ||
217 | #define obstack_chunk_size(h) ((h)->chunk_size) | |
218 | ||
219 | /* Pointer to next byte not yet allocated in current chunk. */ | |
220 | ||
6c8dbf00 | 221 | #define obstack_next_free(h) ((h)->next_free) |
f65fd747 UD |
222 | |
223 | /* Mask specifying low bits that should be clear in address of an object. */ | |
224 | ||
225 | #define obstack_alignment_mask(h) ((h)->alignment_mask) | |
226 | ||
9d1e2b7d | 227 | /* To prevent prototype warnings provide complete argument list. */ |
6c8dbf00 OB |
228 | #define obstack_init(h) \ |
229 | _obstack_begin ((h), 0, 0, \ | |
b100f9aa WN |
230 | (void *(*)(long))obstack_chunk_alloc, \ |
231 | (void (*)(void *))obstack_chunk_free) | |
f65fd747 | 232 | |
6c8dbf00 OB |
233 | #define obstack_begin(h, size) \ |
234 | _obstack_begin ((h), (size), 0, \ | |
b100f9aa WN |
235 | (void *(*)(long))obstack_chunk_alloc, \ |
236 | (void (*)(void *))obstack_chunk_free) | |
f65fd747 | 237 | |
9d1e2b7d | 238 | #define obstack_specify_allocation(h, size, alignment, chunkfun, freefun) \ |
6c8dbf00 | 239 | _obstack_begin ((h), (size), (alignment), \ |
b100f9aa WN |
240 | (void *(*)(long))(chunkfun), \ |
241 | (void (*)(void *))(freefun)) | |
f65fd747 | 242 | |
9d1e2b7d | 243 | #define obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) \ |
6c8dbf00 | 244 | _obstack_begin_1 ((h), (size), (alignment), \ |
b100f9aa WN |
245 | (void *(*)(void *, long))(chunkfun), \ |
246 | (void (*)(void *, void *))(freefun), (arg)) | |
f65fd747 | 247 | |
9d1e2b7d | 248 | #define obstack_chunkfun(h, newchunkfun) \ |
6c8dbf00 | 249 | ((h)->chunkfun = (struct _obstack_chunk *(*)(void *, long))(newchunkfun)) |
f65fd747 | 250 | |
9d1e2b7d | 251 | #define obstack_freefun(h, newfreefun) \ |
6c8dbf00 | 252 | ((h)->freefun = (void (*)(void *, struct _obstack_chunk *))(newfreefun)) |
f65fd747 | 253 | |
6c8dbf00 | 254 | #define obstack_1grow_fast(h, achar) (*((h)->next_free)++ = (achar)) |
f65fd747 | 255 | |
6c8dbf00 | 256 | #define obstack_blank_fast(h, n) ((h)->next_free += (n)) |
f65fd747 UD |
257 | |
258 | #define obstack_memory_used(h) _obstack_memory_used (h) | |
6c8dbf00 | 259 | |
7071ad79 | 260 | #if defined __GNUC__ |
b100f9aa | 261 | # if ! (2 < __GNUC__ + (8 <= __GNUC_MINOR__)) |
14ea22e9 UD |
262 | # define __extension__ |
263 | # endif | |
f65fd747 UD |
264 | |
265 | /* For GNU C, if not -traditional, | |
266 | we can define these macros to compute all args only once | |
267 | without using a global variable. | |
b100f9aa | 268 | Also, we can avoid using the 'temp' slot, to make faster code. */ |
f65fd747 | 269 | |
6c8dbf00 OB |
270 | # define obstack_object_size(OBSTACK) \ |
271 | __extension__ \ | |
272 | ({ struct obstack const *__o = (OBSTACK); \ | |
273 | (unsigned) (__o->next_free - __o->object_base); }) | |
274 | ||
275 | # define obstack_room(OBSTACK) \ | |
276 | __extension__ \ | |
277 | ({ struct obstack const *__o = (OBSTACK); \ | |
278 | (unsigned) (__o->chunk_limit - __o->next_free); }) | |
279 | ||
280 | # define obstack_make_room(OBSTACK, length) \ | |
281 | __extension__ \ | |
282 | ({ struct obstack *__o = (OBSTACK); \ | |
283 | int __len = (length); \ | |
284 | if (__o->chunk_limit - __o->next_free < __len) \ | |
b100f9aa | 285 | _obstack_newchunk (__o, __len); \ |
6c8dbf00 OB |
286 | (void) 0; }) |
287 | ||
288 | # define obstack_empty_p(OBSTACK) \ | |
289 | __extension__ \ | |
290 | ({ struct obstack const *__o = (OBSTACK); \ | |
291 | (__o->chunk->prev == 0 \ | |
b100f9aa WN |
292 | && __o->next_free == __PTR_ALIGN ((char *) __o->chunk, \ |
293 | __o->chunk->contents, \ | |
294 | __o->alignment_mask)); }) | |
6c8dbf00 OB |
295 | |
296 | # define obstack_grow(OBSTACK, where, length) \ | |
297 | __extension__ \ | |
298 | ({ struct obstack *__o = (OBSTACK); \ | |
299 | int __len = (length); \ | |
300 | if (__o->next_free + __len > __o->chunk_limit) \ | |
b100f9aa | 301 | _obstack_newchunk (__o, __len); \ |
6c8dbf00 OB |
302 | memcpy (__o->next_free, where, __len); \ |
303 | __o->next_free += __len; \ | |
304 | (void) 0; }) | |
305 | ||
306 | # define obstack_grow0(OBSTACK, where, length) \ | |
307 | __extension__ \ | |
308 | ({ struct obstack *__o = (OBSTACK); \ | |
309 | int __len = (length); \ | |
310 | if (__o->next_free + __len + 1 > __o->chunk_limit) \ | |
b100f9aa | 311 | _obstack_newchunk (__o, __len + 1); \ |
6c8dbf00 OB |
312 | memcpy (__o->next_free, where, __len); \ |
313 | __o->next_free += __len; \ | |
314 | *(__o->next_free)++ = 0; \ | |
315 | (void) 0; }) | |
316 | ||
317 | # define obstack_1grow(OBSTACK, datum) \ | |
318 | __extension__ \ | |
319 | ({ struct obstack *__o = (OBSTACK); \ | |
320 | if (__o->next_free + 1 > __o->chunk_limit) \ | |
b100f9aa | 321 | _obstack_newchunk (__o, 1); \ |
6c8dbf00 OB |
322 | obstack_1grow_fast (__o, datum); \ |
323 | (void) 0; }) | |
f65fd747 | 324 | |
565bc88a UD |
325 | /* These assume that the obstack alignment is good enough for pointers |
326 | or ints, and that the data added so far to the current object | |
f65fd747 UD |
327 | shares that much alignment. */ |
328 | ||
6c8dbf00 OB |
329 | # define obstack_ptr_grow(OBSTACK, datum) \ |
330 | __extension__ \ | |
331 | ({ struct obstack *__o = (OBSTACK); \ | |
332 | if (__o->next_free + sizeof (void *) > __o->chunk_limit) \ | |
b100f9aa | 333 | _obstack_newchunk (__o, sizeof (void *)); \ |
6c8dbf00 OB |
334 | obstack_ptr_grow_fast (__o, datum); }) \ |
335 | ||
336 | # define obstack_int_grow(OBSTACK, datum) \ | |
337 | __extension__ \ | |
338 | ({ struct obstack *__o = (OBSTACK); \ | |
339 | if (__o->next_free + sizeof (int) > __o->chunk_limit) \ | |
b100f9aa | 340 | _obstack_newchunk (__o, sizeof (int)); \ |
6c8dbf00 OB |
341 | obstack_int_grow_fast (__o, datum); }) |
342 | ||
343 | # define obstack_ptr_grow_fast(OBSTACK, aptr) \ | |
344 | __extension__ \ | |
345 | ({ struct obstack *__o1 = (OBSTACK); \ | |
b100f9aa WN |
346 | void *__p1 = __o1->next_free; \ |
347 | *(const void **) __p1 = (aptr); \ | |
6c8dbf00 OB |
348 | __o1->next_free += sizeof (const void *); \ |
349 | (void) 0; }) | |
350 | ||
351 | # define obstack_int_grow_fast(OBSTACK, aint) \ | |
352 | __extension__ \ | |
353 | ({ struct obstack *__o1 = (OBSTACK); \ | |
b100f9aa WN |
354 | void *__p1 = __o1->next_free; \ |
355 | *(int *) __p1 = (aint); \ | |
6c8dbf00 OB |
356 | __o1->next_free += sizeof (int); \ |
357 | (void) 0; }) | |
358 | ||
359 | # define obstack_blank(OBSTACK, length) \ | |
360 | __extension__ \ | |
361 | ({ struct obstack *__o = (OBSTACK); \ | |
362 | int __len = (length); \ | |
363 | if (__o->chunk_limit - __o->next_free < __len) \ | |
b100f9aa | 364 | _obstack_newchunk (__o, __len); \ |
6c8dbf00 OB |
365 | obstack_blank_fast (__o, __len); \ |
366 | (void) 0; }) | |
367 | ||
368 | # define obstack_alloc(OBSTACK, length) \ | |
369 | __extension__ \ | |
370 | ({ struct obstack *__h = (OBSTACK); \ | |
371 | obstack_blank (__h, (length)); \ | |
372 | obstack_finish (__h); }) | |
373 | ||
374 | # define obstack_copy(OBSTACK, where, length) \ | |
375 | __extension__ \ | |
376 | ({ struct obstack *__h = (OBSTACK); \ | |
377 | obstack_grow (__h, (where), (length)); \ | |
378 | obstack_finish (__h); }) | |
379 | ||
380 | # define obstack_copy0(OBSTACK, where, length) \ | |
381 | __extension__ \ | |
382 | ({ struct obstack *__h = (OBSTACK); \ | |
383 | obstack_grow0 (__h, (where), (length)); \ | |
384 | obstack_finish (__h); }) | |
f65fd747 UD |
385 | |
386 | /* The local variable is named __o1 to avoid a name conflict | |
387 | when obstack_blank is called. */ | |
6c8dbf00 OB |
388 | # define obstack_finish(OBSTACK) \ |
389 | __extension__ \ | |
390 | ({ struct obstack *__o1 = (OBSTACK); \ | |
391 | void *__value = (void *) __o1->object_base; \ | |
392 | if (__o1->next_free == __value) \ | |
b100f9aa | 393 | __o1->maybe_empty_object = 1; \ |
6c8dbf00 | 394 | __o1->next_free \ |
b100f9aa WN |
395 | = __PTR_ALIGN (__o1->object_base, __o1->next_free, \ |
396 | __o1->alignment_mask); \ | |
6c8dbf00 | 397 | if (__o1->next_free - (char *) __o1->chunk \ |
b100f9aa WN |
398 | > __o1->chunk_limit - (char *) __o1->chunk) \ |
399 | __o1->next_free = __o1->chunk_limit; \ | |
6c8dbf00 OB |
400 | __o1->object_base = __o1->next_free; \ |
401 | __value; }) | |
402 | ||
403 | # define obstack_free(OBSTACK, OBJ) \ | |
404 | __extension__ \ | |
405 | ({ struct obstack *__o = (OBSTACK); \ | |
406 | void *__obj = (OBJ); \ | |
407 | if (__obj > (void *) __o->chunk && __obj < (void *) __o->chunk_limit) \ | |
b100f9aa WN |
408 | __o->next_free = __o->object_base = (char *) __obj; \ |
409 | else (__obstack_free) (__o, __obj); }) | |
6c8dbf00 | 410 | |
7071ad79 | 411 | #else /* not __GNUC__ */ |
f65fd747 | 412 | |
14ea22e9 | 413 | # define obstack_object_size(h) \ |
6c8dbf00 | 414 | (unsigned) ((h)->next_free - (h)->object_base) |
f65fd747 | 415 | |
6c8dbf00 OB |
416 | # define obstack_room(h) \ |
417 | (unsigned) ((h)->chunk_limit - (h)->next_free) | |
f65fd747 | 418 | |
14ea22e9 | 419 | # define obstack_empty_p(h) \ |
6c8dbf00 OB |
420 | ((h)->chunk->prev == 0 \ |
421 | && (h)->next_free == __PTR_ALIGN ((char *) (h)->chunk, \ | |
b100f9aa WN |
422 | (h)->chunk->contents, \ |
423 | (h)->alignment_mask)) | |
dfd2257a | 424 | |
f65fd747 UD |
425 | /* Note that the call to _obstack_newchunk is enclosed in (..., 0) |
426 | so that we can avoid having void expressions | |
427 | in the arms of the conditional expression. | |
428 | Casting the third operand to void was tried before, | |
429 | but some compilers won't accept it. */ | |
430 | ||
6c8dbf00 OB |
431 | # define obstack_make_room(h, length) \ |
432 | ((h)->temp.tempint = (length), \ | |
433 | (((h)->next_free + (h)->temp.tempint > (h)->chunk_limit) \ | |
2fd4de4b | 434 | ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0)) |
f65fd747 | 435 | |
6c8dbf00 OB |
436 | # define obstack_grow(h, where, length) \ |
437 | ((h)->temp.tempint = (length), \ | |
438 | (((h)->next_free + (h)->temp.tempint > (h)->chunk_limit) \ | |
439 | ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0), \ | |
440 | memcpy ((h)->next_free, where, (h)->temp.tempint), \ | |
441 | (h)->next_free += (h)->temp.tempint) | |
442 | ||
443 | # define obstack_grow0(h, where, length) \ | |
444 | ((h)->temp.tempint = (length), \ | |
445 | (((h)->next_free + (h)->temp.tempint + 1 > (h)->chunk_limit) \ | |
446 | ? (_obstack_newchunk ((h), (h)->temp.tempint + 1), 0) : 0), \ | |
447 | memcpy ((h)->next_free, where, (h)->temp.tempint), \ | |
448 | (h)->next_free += (h)->temp.tempint, \ | |
449 | *((h)->next_free)++ = 0) | |
450 | ||
451 | # define obstack_1grow(h, datum) \ | |
452 | ((((h)->next_free + 1 > (h)->chunk_limit) \ | |
453 | ? (_obstack_newchunk ((h), 1), 0) : 0), \ | |
454 | obstack_1grow_fast (h, datum)) | |
455 | ||
456 | # define obstack_ptr_grow(h, datum) \ | |
457 | ((((h)->next_free + sizeof (char *) > (h)->chunk_limit) \ | |
458 | ? (_obstack_newchunk ((h), sizeof (char *)), 0) : 0), \ | |
459 | obstack_ptr_grow_fast (h, datum)) | |
460 | ||
461 | # define obstack_int_grow(h, datum) \ | |
462 | ((((h)->next_free + sizeof (int) > (h)->chunk_limit) \ | |
463 | ? (_obstack_newchunk ((h), sizeof (int)), 0) : 0), \ | |
464 | obstack_int_grow_fast (h, datum)) | |
465 | ||
466 | # define obstack_ptr_grow_fast(h, aptr) \ | |
9d1e2b7d | 467 | (((const void **) ((h)->next_free += sizeof (void *)))[-1] = (aptr)) |
f65fd747 | 468 | |
6c8dbf00 | 469 | # define obstack_int_grow_fast(h, aint) \ |
58b52364 | 470 | (((int *) ((h)->next_free += sizeof (int)))[-1] = (aint)) |
f65fd747 | 471 | |
6c8dbf00 OB |
472 | # define obstack_blank(h, length) \ |
473 | ((h)->temp.tempint = (length), \ | |
474 | (((h)->chunk_limit - (h)->next_free < (h)->temp.tempint) \ | |
475 | ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0), \ | |
476 | obstack_blank_fast (h, (h)->temp.tempint)) | |
477 | ||
478 | # define obstack_alloc(h, length) \ | |
479 | (obstack_blank ((h), (length)), obstack_finish ((h))) | |
480 | ||
481 | # define obstack_copy(h, where, length) \ | |
482 | (obstack_grow ((h), (where), (length)), obstack_finish ((h))) | |
483 | ||
484 | # define obstack_copy0(h, where, length) \ | |
485 | (obstack_grow0 ((h), (where), (length)), obstack_finish ((h))) | |
486 | ||
487 | # define obstack_finish(h) \ | |
488 | (((h)->next_free == (h)->object_base \ | |
489 | ? (((h)->maybe_empty_object = 1), 0) \ | |
490 | : 0), \ | |
491 | (h)->temp.tempptr = (h)->object_base, \ | |
492 | (h)->next_free \ | |
493 | = __PTR_ALIGN ((h)->object_base, (h)->next_free, \ | |
b100f9aa | 494 | (h)->alignment_mask), \ |
6c8dbf00 OB |
495 | (((h)->next_free - (char *) (h)->chunk \ |
496 | > (h)->chunk_limit - (char *) (h)->chunk) \ | |
497 | ? ((h)->next_free = (h)->chunk_limit) : 0), \ | |
498 | (h)->object_base = (h)->next_free, \ | |
499 | (h)->temp.tempptr) | |
500 | ||
501 | # define obstack_free(h, obj) \ | |
502 | ((h)->temp.tempint = (char *) (obj) - (char *) (h)->chunk, \ | |
503 | ((((h)->temp.tempint > 0 \ | |
504 | && (h)->temp.tempint < (h)->chunk_limit - (char *) (h)->chunk)) \ | |
b100f9aa WN |
505 | ? (void) ((h)->next_free = (h)->object_base \ |
506 | = (h)->temp.tempint + (char *) (h)->chunk) \ | |
507 | : (__obstack_free) (h, (h)->temp.tempint + (char *) (h)->chunk))) | |
508 | ||
7071ad79 | 509 | #endif /* not __GNUC__ */ |
f65fd747 | 510 | |
1fb05e3d | 511 | #ifdef __cplusplus |
6c8dbf00 | 512 | } /* C++ */ |
1fb05e3d | 513 | #endif |
b100f9aa | 514 | |
5107cf1d | 515 | #endif /* obstack.h */ |