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