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