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