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