home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / NGAWK1.ZIP / OBSTACK.H < prev    next >
C/C++ Source or Header  |  1988-07-17  |  9KB  |  216 lines

  1. /* obstack.h - object stack macros
  2.    Copyright (c) 1986 Free Software Foundation, Inc.
  3.  
  4. Summary:
  5.  
  6. All the apparent functions defined here are macros. The idea
  7. is that you would use these pre-tested macros to solve a
  8. very specific set of problems, and they would run fast.
  9. Caution: no side-effects in arguments please!! They may be
  10. evaluated MANY times!!
  11.  
  12. These macros operate a stack of objects.  Each object starts life
  13. small, and may grow to maturity.  (Consider building a word syllable
  14. by syllable.)  An object can move while it is growing.  Once it has
  15. been "finished" it never changes address again.  So the "top of the
  16. stack" is typically an immature growing object, while the rest of the
  17. stack is of mature, fixed size and fixed address objects.
  18.  
  19. These routines grab large chunks of memory, using a function you
  20. supply, called `obstack_chunk_alloc'.  On occasion, they free chunks,
  21. by calling `obstack_chunk_free'.  You must define them and declare
  22. them before using any obstack macros.
  23.  
  24. Each independent stack is represented by a `struct obstack'.
  25. Each of the obstack macros expects a pointer to such a structure
  26. as the first argument.
  27.  
  28. One motivation for this package is the problem of growing char strings
  29. in symbol tables.  Unless you are "facist pig with a read-only mind"
  30. [Gosper's immortal quote from HAKMEM item 154, out of context] you
  31. would not like to put any arbitrary upper limit on the length of your
  32. symbols.
  33.  
  34. In practice this often means you will build many short symbols and a
  35. few long symbols.  At the time you are reading a symbol you don't know
  36. how long it is.  One traditional method is to read a symbol into a
  37. buffer, realloc()ating the buffer every time you try to read a symbol
  38. that is longer than the buffer.  This is beaut, but you still will
  39. want to copy the symbol from the buffer to a more permanent
  40. symbol-table entry say about half the time.
  41.  
  42. With obstacks, you can work differently.  Use one obstack for all symbol
  43. names.  As you read a symbol, grow the name in the obstack gradually.
  44. When the name is complete, finalize it.  Then, if the symbol exists already,
  45. free the newly read name.
  46.  
  47. The way we do this is to take a large chunk, allocating memory from
  48. low addresses.  When you want to build a aymbol in the chunk you just
  49. add chars above the current "high water mark" in the chunk.  When you
  50. have finished adding chars, because you got to the end of the symbol,
  51. you know how long the chars are, and you can create a new object.
  52. Mostly the chars will not burst over the highest address of the chunk,
  53. because you would typically expect a chunk to be (say) 100 times as
  54. long as an average object.
  55.  
  56. In case that isn't clear, when we have enough chars to make up
  57. the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed)
  58. so we just point to it where it lies.  No moving of chars is
  59. needed and this is the second win: potentially long strings need
  60. never be explicitly shuffled. Once an object is formed, it does not
  61. change its address during its lifetime.
  62.  
  63. When the chars burst over a chunk boundary, we allocate a larger
  64. chunk, and then copy the partly formed object from the end of the old
  65. chunk to the beggining of the new larger chunk.  We then carry on
  66. accreting characters to the end of the object as we normaly would.
  67.  
  68. A special macro is provided to add a single char at a time to a
  69. growing object.  This allows the use of register variables, which
  70. break the ordinary 'growth' macro.
  71.  
  72. Summary:
  73.     We allocate large chunks.
  74.     We carve out one object at a time from the current chunk.
  75.     Once carved, an object never moves.
  76.     We are free to append data of any size to the currently
  77.       growing object.
  78.     Exactly one object is growing in an obstack at any one time.
  79.     You can run one obstack per control block.
  80.     You may have as many control blocks as you dare.
  81.     Because of the way we do it, you can `unwind' a obstack
  82.       back to a previous state. (You may remove objects much
  83.       as you would with a stack.)
  84.  
  85.    Modifications by Andrew D. Estes, July 1988
  86. */
  87.  
  88. #ifndef obstackH
  89.  
  90. /* Added to allow large model under MSC -ADE- */
  91.  
  92. #if (defined(M_I86CM) || defined(M_I86LM) || defined(M_I86HM))
  93. #define INT long        /* ADE */
  94. #else
  95. #define INT int
  96. #endif
  97.  
  98. #define obstackH
  99.                 /* these #defines keep it brief */
  100. #define _Ll struct obstack_chunk
  101. #define _LL (8)            /* _L length in chars */
  102.  
  103. struct obstack_chunk        /* Lives at front of each chunk. */
  104. {
  105.   char  *obstack_l_limit;    /* 1 past end of this chunk */
  106.   _Ll    *obstack_l_prev;    /* address of prior chunk or NULL */
  107.   char    obstack_l_0[4];        /* objects begin here */
  108. };
  109.  
  110. #if 0
  111. This function, called like malloc but not returning on failure,
  112. must return a chunk of the size given to it as argument,
  113. aligned on a boundary of 2**OBSTACK_LOG_DEFAULT_ALIGNMENT bytes.
  114.  
  115. struct obstack_chunk * obstack_chunk_alloc();
  116. #endif /* 0 */
  117.  
  118. struct obstack        /* control current object in current chunk */
  119. {
  120.   long    chunk_size;        /* preferred size to allocate chunks in */
  121.   _Ll*    chunk;            /* address of current struct obstack_chunk */
  122.   char    *object_base;        /* address of object we are building */
  123.   char    *next_free;        /* where to add next char to current object */
  124.   char    *chunk_limit;        /* address of char after current chunk */
  125.   INT    temp;            /* Temporary for some macros.  [was int -ADE-] */
  126.   int   alignment_mask;        /* Mask of alignment for each object. */
  127. };
  128.  
  129. /* Pointer to beginning of object being allocated or to be allocated next.
  130.    Note that this might not be the final address of the object
  131.    because a new chunk might be needed to hold the final size.  */
  132.  
  133. #define obstack_base(h) ((h)->object_base)
  134.  
  135. /* Pointer to next byte not yet allocated in current chunk.  */
  136.  
  137. #define obstack_next_free(h)    ((h)->next_free)
  138.  
  139. /* Size of object currently growing */
  140.  
  141. #define obstack_object_size(h)  ((h)->next_free - (h)->object_base)
  142.  
  143. /* Mask specifying low bits that should be clear in address of an object.  */
  144.  
  145. #define obstack_alignment_mask(h) ((h)->alignment_mask)
  146.  
  147. #define obstack_init(h) obstack_begin (h, 4096 - 4 - _LL)
  148.  
  149. #define obstack_begin(h,try_length)                    \
  150. ((h)->chunk_size = (try_length) + (_LL),                \
  151.  (h)->alignment_mask = ((1 << 2) - 1),                    \
  152.  _obstack_begin ((h), obstack_chunk_alloc))
  153.  
  154. #define obstack_grow(h,where,length)                    \
  155. ( (h)->temp = (length),                            \
  156.   (((h)->next_free + (h)->temp > (h)->chunk_limit)            \
  157.    ? _obstack_newchunk ((h), obstack_chunk_alloc, (h)->temp) : 0),    \
  158.   bcopy (where, (h)->next_free, (h)->temp),                \
  159.   (h)->next_free += (h)->temp)
  160.  
  161. #define obstack_grow0(h,where,length)                    \
  162. ( (h)->temp = (length),                            \
  163.   (((h)->next_free + (h)->temp + 1 > (h)->chunk_limit)            \
  164.    ? _obstack_newchunk ((h), obstack_chunk_alloc, (h)->temp + 1) : 0),    \
  165.   bcopy (where, (h)->next_free, (h)->temp),                \
  166.   (h)->next_free += (h)->temp,                        \
  167.   *((h)->next_free)++ = 0)
  168.  
  169. #define obstack_1grow(h,datum)                        \
  170. ( (((h)->next_free + 1 > (h)->chunk_limit)                \
  171.    ? _obstack_newchunk ((h), obstack_chunk_alloc, (INT)1) : 0),      \
  172.   *((h)->next_free)++ = (datum))
  173.  
  174. #define obstack_blank(h,length)                        \
  175. ( (h)->temp = (length),                            \
  176.   (((h)->next_free + (h)->temp > (h)->chunk_limit)            \
  177.    ? _obstack_newchunk ((h), obstack_chunk_alloc, (h)->temp) : 0),    \
  178.   (h)->next_free += (h)->temp)
  179.  
  180. #define obstack_alloc(h,length)                        \
  181.  (obstack_blank ((h), (length)), obstack_finish (h))
  182.  
  183. #define obstack_copy(h,where,length)                    \
  184.  (obstack_grow ((h), (where), (length)), obstack_finish (h))
  185.  
  186. #define obstack_copy0(h,where,length)                    \
  187.  (obstack_grow0 ((h), (where), (length)), obstack_finish (h))
  188.  
  189. #define obstack_room(h) ((long unsigned int)                \
  190.  ((h)->chunk_limit - (h)->next_free))
  191.  
  192. #define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = achar)
  193.  
  194. #define obstack_blank_fast(h,n) ((h)->next_free += (n))
  195.  
  196. #define obstack_finish(h)                          \
  197.  ((h)->temp = (long) (h)->object_base,                     \
  198.   (h)->next_free                            \
  199.     = (char*)((long)((h)->next_free+(h)->alignment_mask)         \
  200.           & ~ ((h)->alignment_mask)),                \
  201.   (((h)->next_free - (char *)(h)->chunk                    \
  202.     > (h)->chunk_limit - (char *)(h)->chunk)                \
  203.    ? (h)->next_free = (h)->chunk_limit : 0),                \
  204.   (h)->object_base = (h)->next_free,                    \
  205.   (char *) (h)->temp)
  206.  
  207. #define obstack_free(h,obj)                        \
  208. (((h)->temp = (char *)(obj) - (char *) (h)->chunk),            \
  209.  (((h)->temp >= 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\
  210.   ? (int) ((h)->next_free = (h)->object_base                \
  211.        = (h)->temp + (char *) (h)->chunk)                \
  212.   : (int) _obstack_free ((h), obstack_chunk_free,            \
  213.              (h)->temp + (char *) (h)->chunk)))
  214.  
  215. #endif                /* #ifndef obstackH */
  216.