home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / glibc-1.06 / malloc / obstack.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-20  |  18.3 KB  |  488 lines

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