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