home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / TOP / USR / SRC / gawk2.0.t.Z / gawk2.0.t / obstack.h < prev    next >
Text File  |  1988-12-31  |  9KB  |  220 lines

  1. /* obstack.h - object stack macros
  2.  * Copyright (c) 1986 Free Software Foundation, Inc.
  3.  *
  4.  * $Log:    obstack.h,v $
  5.  * Revision 1.2  88/04/08  14:48:42  david
  6.  * changes from Arnold Robbins
  7.  * 
  8.  * Revision 1.1  87/10/27  15:23:43  david
  9.  * Initial revision
  10.  * 
  11.  */
  12.  
  13. /*
  14.  
  15. Summary:
  16.  
  17. All the apparent functions defined here are macros. The idea
  18. is that you would use these pre-tested macros to solve a
  19. very specific set of problems, and they would run fast.
  20. Caution: no side-effects in arguments please!! They may be
  21. evaluated MANY times!!
  22.  
  23. These macros operate a stack of objects.  Each object starts life
  24. small, and may grow to maturity.  (Consider building a word syllable
  25. by syllable.)  An object can move while it is growing.  Once it has
  26. been "finished" it never changes address again.  So the "top of the
  27. stack" is typically an immature growing object, while the rest of the
  28. stack is of mature, fixed size and fixed address objects.
  29.  
  30. These routines grab large chunks of memory, using a function you
  31. supply, called `obstack_chunk_alloc'.  On occasion, they free chunks,
  32. by calling `obstack_chunk_free'.  You must define them and declare
  33. them before using any obstack macros.
  34.  
  35. Each independent stack is represented by a `struct obstack'.
  36. Each of the obstack macros expects a pointer to such a structure
  37. as the first argument.
  38.  
  39. One motivation for this package is the problem of growing char strings
  40. in symbol tables.  Unless you are "facist pig with a read-only mind"
  41. [Gosper's immortal quote from HAKMEM item 154, out of context] you
  42. would not like to put any arbitrary upper limit on the length of your
  43. symbols.
  44.  
  45. In practice this often means you will build many short symbols and a
  46. few long symbols.  At the time you are reading a symbol you don't know
  47. how long it is.  One traditional method is to read a symbol into a
  48. buffer, realloc()ating the buffer every time you try to read a symbol
  49. that is longer than the buffer.  This is beaut, but you still will
  50. want to copy the symbol from the buffer to a more permanent
  51. symbol-table entry say about half the time.
  52.  
  53. With obstacks, you can work differently.  Use one obstack for all symbol
  54. names.  As you read a symbol, grow the name in the obstack gradually.
  55. When the name is complete, finalize it.  Then, if the symbol exists already,
  56. free the newly read name.
  57.  
  58. The way we do this is to take a large chunk, allocating memory from
  59. low addresses.  When you want to build a aymbol in the chunk you just
  60. add chars above the current "high water mark" in the chunk.  When you
  61. have finished adding chars, because you got to the end of the symbol,
  62. you know how long the chars are, and you can create a new object.
  63. Mostly the chars will not burst over the highest address of the chunk,
  64. because you would typically expect a chunk to be (say) 100 times as
  65. long as an average object.
  66.  
  67. In case that isn't clear, when we have enough chars to make up
  68. the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed)
  69. so we just point to it where it lies.  No moving of chars is
  70. needed and this is the second win: potentially long strings need
  71. never be explicitly shuffled. Once an object is formed, it does not
  72. change its address during its lifetime.
  73.  
  74. When the chars burst over a chunk boundary, we allocate a larger
  75. chunk, and then copy the partly formed object from the end of the old
  76. chunk to the beggining of the new larger chunk.  We then carry on
  77. accreting characters to the end of the object as we normaly would.
  78.  
  79. A special macro is provided to add a single char at a time to a
  80. growing object.  This allows the use of register variables, which
  81. break the ordinary 'growth' macro.
  82.  
  83. Summary:
  84.     We allocate large chunks.
  85.     We carve out one object at a time from the current chunk.
  86.     Once carved, an object never moves.
  87.     We are free to append data of any size to the currently
  88.       growing object.
  89.     Exactly one object is growing in an obstack at any one time.
  90.     You can run one obstack per control block.
  91.     You may have as many control blocks as you dare.
  92.     Because of the way we do it, you can `unwind' a obstack
  93.       back to a previous state. (You may remove objects much
  94.       as you would with a stack.)
  95. */
  96.  
  97. #ifndef obstackH
  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.  */
  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, 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 = (int) (h)->object_base,                    \
  198.   (h)->next_free                            \
  199.     = (char*)((int)((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. /* nasty nasty SunOS-ism */
  216. #ifdef sparc
  217. #include <alloca.h>
  218. #endif
  219. #endif                /* #ifndef obstackH */
  220.