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