obstack.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /* obstack.h - object stack macros
  2. Copyright (C) 1986 Free Software Foundation, Inc.
  3. NO WARRANTY
  4. BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  5. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
  6. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  7. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  8. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  9. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  10. FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
  11. AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
  12. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  13. CORRECTION.
  14. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  15. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  16. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  17. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  18. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  19. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  20. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  21. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  22. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  23. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  24. GENERAL PUBLIC LICENSE TO COPY
  25. 1. You may copy and distribute verbatim copies of this source file
  26. as you receive it, in any medium, provided that you conspicuously and
  27. appropriately publish on each copy a valid copyright notice "Copyright
  28. (C) 1986 Free Software Foundation, Inc."; and include following the
  29. copyright notice a verbatim copy of the above disclaimer of warranty
  30. and of this License. You may charge a distribution fee for the
  31. physical act of transferring a copy.
  32. 2. You may modify your copy or copies of this source file or
  33. any portion of it, and copy and distribute such modifications under
  34. the terms of Paragraph 1 above, provided that you also do the following:
  35. a) cause the modified files to carry prominent notices stating
  36. that you changed the files and the date of any change; and
  37. b) cause the whole of any work that you distribute or publish,
  38. that in whole or in part contains or is a derivative of this
  39. program or any part thereof, to be licensed at no charge to all
  40. third parties on terms identical to those contained in this
  41. License Agreement (except that you may choose to grant more
  42. extensive warranty protection to third parties, at your option).
  43. c) You may charge a distribution fee for the physical act of
  44. transferring a copy, and you may at your option offer warranty
  45. protection in exchange for a fee.
  46. 3. You may copy and distribute this program or any portion of it in
  47. compiled, executable or object code form under the terms of Paragraphs
  48. 1 and 2 above provided that you do the following:
  49. a) cause each such copy to be accompanied by the
  50. corresponding machine-readable source code, which must
  51. be distributed under the terms of Paragraphs 1 and 2 above; or,
  52. b) cause each such copy to be accompanied by a
  53. written offer, with no time limit, to give any third party
  54. free (except for a nominal shipping charge) a machine readable
  55. copy of the corresponding source code, to be distributed
  56. under the terms of Paragraphs 1 and 2 above; or,
  57. c) in the case of a recipient of this program in compiled, executable
  58. or object code form (without the corresponding source code) you
  59. shall cause copies you distribute to be accompanied by a copy
  60. of the written offer of source code which you received along
  61. with the copy you received.
  62. 4. You may not copy, sublicense, distribute or transfer this program
  63. except as expressly provided under this License Agreement. Any attempt
  64. otherwise to copy, sublicense, distribute or transfer this program is void and
  65. your rights to use the program under this License agreement shall be
  66. automatically terminated. However, parties who have received computer
  67. software programs from you with this License Agreement will not have
  68. their licenses terminated so long as such parties remain in full compliance.
  69. 5. If you wish to incorporate parts of this program into other free
  70. programs whose distribution conditions are different, write to the Free
  71. Software Foundation at 1000 Mass Ave, Cambridge, MA 02138. We have not yet
  72. worked out a simple rule that can be stated here, but we will often permit
  73. this. We will be guided by the two goals of preserving the free status of
  74. all derivatives our free software and of promoting the sharing and reuse of
  75. software.
  76. In other words, you are welcome to use, share and improve this program.
  77. You are forbidden to forbid anyone else to use, share and improve
  78. what you give them. Help stamp out software-hoarding! */
  79. /* Summary:
  80. All the apparent functions defined here are macros. The idea
  81. is that you would use these pre-tested macros to solve a
  82. very specific set of problems, and they would run fast.
  83. Caution: no side-effects in arguments please!! They may be
  84. evaluated MANY times!!
  85. These macros operate a stack of objects. Each object starts life
  86. small, and may grow to maturity. (Consider building a word syllable
  87. by syllable.) An object can move while it is growing. Once it has
  88. been "finished" it never changes address again. So the "top of the
  89. stack" is typically an immature growing object, while the rest of the
  90. stack is of mature, fixed size and fixed address objects.
  91. These routines grab large chunks of memory, using a function you
  92. supply, called `obstack_chunk_alloc'. On occasion, they free chunks,
  93. by calling `obstack_chunk_free'. You must define them and declare
  94. them before using any obstack macros.
  95. Each independent stack is represented by a `struct obstack'.
  96. Each of the obstack macros expects a pointer to such a structure
  97. as the first argument.
  98. One motivation for this package is the problem of growing char strings
  99. in symbol tables. Unless you are "facist pig with a read-only mind"
  100. [Gosper's immortal quote from HAKMEM item 154, out of context] you
  101. would not like to put any arbitrary upper limit on the length of your
  102. symbols.
  103. In practice this often means you will build many short symbols and a
  104. few long symbols. At the time you are reading a symbol you don't know
  105. how long it is. One traditional method is to read a symbol into a
  106. buffer, realloc()ating the buffer every time you try to read a symbol
  107. that is longer than the buffer. This is beaut, but you still will
  108. want to copy the symbol from the buffer to a more permanent
  109. symbol-table entry say about half the time.
  110. With obstacks, you can work differently. Use one obstack for all symbol
  111. names. As you read a symbol, grow the name in the obstack gradually.
  112. When the name is complete, finalize it. Then, if the symbol exists already,
  113. free the newly read name.
  114. The way we do this is to take a large chunk, allocating memory from
  115. low addresses. When you want to build a aymbol in the chunk you just
  116. add chars above the current "high water mark" in the chunk. When you
  117. have finished adding chars, because you got to the end of the symbol,
  118. you know how long the chars are, and you can create a new object.
  119. Mostly the chars will not burst over the highest address of the chunk,
  120. because you would typically expect a chunk to be (say) 100 times as
  121. long as an average object.
  122. In case that isn't clear, when we have enough chars to make up
  123. the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed)
  124. so we just point to it where it lies. No moving of chars is
  125. needed and this is the second win: potentially long strings need
  126. never be explicitly shuffled. Once an object is formed, it does not
  127. change its address during its lifetime.
  128. When the chars burst over a chunk boundary, we allocate a larger
  129. chunk, and then copy the partly formed object from the end of the old
  130. chunk to the beggining of the new larger chunk. We then carry on
  131. accreting characters to the end of the object as we normaly would.
  132. A special macro is provided to add a single char at a time to a
  133. growing object. This allows the use of register variables, which
  134. break the ordinary 'growth' macro.
  135. Summary:
  136. We allocate large chunks.
  137. We carve out one object at a time from the current chunk.
  138. Once carved, an object never moves.
  139. We are free to append data of any size to the currently
  140. growing object.
  141. Exactly one object is growing in an obstack at any one time.
  142. You can run one obstack per control block.
  143. You may have as many control blocks as you dare.
  144. Because of the way we do it, you can `unwind' a obstack
  145. back to a previous state. (You may remove objects much
  146. as you would with a stack.)
  147. */
  148. #ifndef obstackH
  149. #define obstackH
  150. /* these #defines keep it brief */
  151. #define _Ll struct obstack_chunk
  152. #define _LL (8) /* _L length in chars */
  153. struct obstack_chunk /* Lives at front of each chunk. */
  154. {
  155. char *obstack_l_limit; /* 1 past end of this chunk */
  156. _Ll *obstack_l_prev; /* address of prior chunk or NULL */
  157. char obstack_l_0[4]; /* objects begin here */
  158. };
  159. #if 0
  160. This function, called like malloc but not returning on failure,
  161. must return a chunk of the size given to it as argument,
  162. aligned on a boundary of 2**OBSTACK_LOG_DEFAULT_ALIGNMENT bytes.
  163. struct obstack_chunk * obstack_chunk_alloc();
  164. #endif /* 0 */
  165. struct obstack /* control current object in current chunk */
  166. {
  167. long chunk_size; /* preferred size to allocate chunks in */
  168. _Ll* chunk; /* address of current struct obstack_chunk */
  169. char *object_base; /* address of object we are building */
  170. char *next_free; /* where to add next char to current object */
  171. char *chunk_limit; /* address of char after current chunk */
  172. int temp; /* Temporary for some macros. */
  173. int alignment_mask; /* Mask of alignment for each object. */
  174. };
  175. /* Pointer to beginning of object being allocated or to be allocated next.
  176. Note that this might not be the final address of the object
  177. because a new chunk might be needed to hold the final size. */
  178. #define obstack_base(h) ((h)->object_base)
  179. /* Pointer to next byte not yet allocated in current chunk. */
  180. #define obstack_next_free(h) ((h)->next_free)
  181. /* Size of object currently growing */
  182. #define obstack_object_size(h) ((h)->next_free - (h)->object_base)
  183. /* Mask specifying low bits that should be clear in address of an object. */
  184. #define obstack_alignment_mask(h) ((h)->alignment_mask)
  185. #define obstack_init(h) obstack_begin (h, 4096 - 4 - _LL)
  186. #define obstack_begin(h,try_length) \
  187. ((h)->chunk_size = (try_length) + (_LL), \
  188. (h)->alignment_mask = ((1 << 2) - 1), \
  189. _obstack_begin ((h), obstack_chunk_alloc))
  190. #define obstack_grow(h,where,length) \
  191. ( (h)->temp = (length), \
  192. (((h)->next_free + (h)->temp > (h)->chunk_limit) \
  193. ? _obstack_newchunk ((h), obstack_chunk_alloc, (h)->temp) : 0), \
  194. bcopy (where, (h)->next_free, (h)->temp), \
  195. (h)->next_free += (h)->temp)
  196. #define obstack_grow0(h,where,length) \
  197. ( (h)->temp = (length), \
  198. (((h)->next_free + (h)->temp + 1 > (h)->chunk_limit) \
  199. ? _obstack_newchunk ((h), obstack_chunk_alloc, (h)->temp + 1) : 0), \
  200. bcopy (where, (h)->next_free, (h)->temp), \
  201. (h)->next_free += (h)->temp, \
  202. *((h)->next_free)++ = 0)
  203. #define obstack_1grow(h,datum) \
  204. ( (((h)->next_free + 1 > (h)->chunk_limit) \
  205. ? _obstack_newchunk ((h), obstack_chunk_alloc, 1) : 0), \
  206. *((h)->next_free)++ = (datum))
  207. #define obstack_blank(h,length) \
  208. ( (h)->temp = (length), \
  209. (((h)->next_free + (h)->temp > (h)->chunk_limit) \
  210. ? _obstack_newchunk ((h), obstack_chunk_alloc, (h)->temp) : 0), \
  211. (h)->next_free += (h)->temp)
  212. #define obstack_alloc(h,length) \
  213. (obstack_blank ((h), (length)), obstack_finish (h))
  214. #define obstack_copy(h,where,length) \
  215. (obstack_grow ((h), (where), (length)), obstack_finish (h))
  216. #define obstack_copy0(h,where,length) \
  217. (obstack_grow0 ((h), (where), (length)), obstack_finish (h))
  218. #define obstack_room(h) ((long unsigned int) \
  219. ((h)->chunk_limit - (h)->next_free))
  220. #define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = achar)
  221. #define obstack_blank_fast(h,n) ((h)->next_free += (n))
  222. #define obstack_finish(h) \
  223. ((h)->temp = (int) (h)->object_base, \
  224. (h)->next_free \
  225. = (char*)((int)((h)->next_free+(h)->alignment_mask) \
  226. & ~ ((h)->alignment_mask)), \
  227. (((h)->next_free - (char *)(h)->chunk \
  228. > (h)->chunk_limit - (char *)(h)->chunk) \
  229. ? (h)->next_free = (h)->chunk_limit : 0), \
  230. (h)->object_base = (h)->next_free, \
  231. (char *) (h)->temp)
  232. #define obstack_free(h,obj) \
  233. (((h)->temp = (char *)(obj) - (char *) (h)->chunk), \
  234. (((h)->temp >= 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\
  235. ? (int) ((h)->next_free = (h)->object_base \
  236. = (h)->temp + (char *) (h)->chunk) \
  237. : (int) _obstack_free ((h), obstack_chunk_free, \
  238. (h)->temp + (char *) (h)->chunk)))
  239. #endif /* #ifndef obstackH */