obstack.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /* obstack.h - object stack macros
  2. Copyright (C) 1988 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) 1988 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 extensive
  42. warranty protection to some or all 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. Mere aggregation of another unrelated program with this program (or its
  47. derivative) on a volume of a storage or distribution medium does not bring
  48. the other program under the scope of these terms.
  49. 3. You may copy and distribute this program or any portion of it in
  50. compiled, executable or object code form under the terms of Paragraphs
  51. 1 and 2 above provided that you do the following:
  52. a) accompany it with the complete corresponding machine-readable
  53. source code, which must be distributed under the terms of
  54. Paragraphs 1 and 2 above; or,
  55. b) accompany it with a written offer, valid for at least three
  56. years, to give any third party free (except for a nominal
  57. shipping charge) a complete machine-readable copy of the
  58. corresponding source code, to be distributed under the terms of
  59. Paragraphs 1 and 2 above; or,
  60. c) accompany it with the information you received as to where the
  61. corresponding source code may be obtained. (This alternative is
  62. allowed only for noncommercial distribution and only if you
  63. received the program in object code or executable form alone.)
  64. For an executable file, complete source code means all the source code for
  65. all modules it contains; but, as a special exception, it need not include
  66. source code for modules which are standard libraries that accompany the
  67. operating system on which the executable file runs.
  68. 4. You may not copy, sublicense, distribute or transfer this program
  69. except as expressly provided under this License Agreement. Any attempt
  70. otherwise to copy, sublicense, distribute or transfer this program is void and
  71. your rights to use the program under this License agreement shall be
  72. automatically terminated. However, parties who have received computer
  73. software programs from you with this License Agreement will not have
  74. their licenses terminated so long as such parties remain in full compliance.
  75. 5. If you wish to incorporate parts of this program into other free
  76. programs whose distribution conditions are different, write to the Free
  77. Software Foundation at 675 Mass Ave, Cambridge, MA 02139. We have not yet
  78. worked out a simple rule that can be stated here, but we will often permit
  79. this. We will be guided by the two goals of preserving the free status of
  80. all derivatives our free software and of promoting the sharing and reuse of
  81. software.
  82. In other words, you are welcome to use, share and improve this program.
  83. You are forbidden to forbid anyone else to use, share and improve
  84. what you give them. Help stamp out software-hoarding! */
  85. /* Summary:
  86. All the apparent functions defined here are macros. The idea
  87. is that you would use these pre-tested macros to solve a
  88. very specific set of problems, and they would run fast.
  89. Caution: no side-effects in arguments please!! They may be
  90. evaluated MANY times!!
  91. These macros operate a stack of objects. Each object starts life
  92. small, and may grow to maturity. (Consider building a word syllable
  93. by syllable.) An object can move while it is growing. Once it has
  94. been "finished" it never changes address again. So the "top of the
  95. stack" is typically an immature growing object, while the rest of the
  96. stack is of mature, fixed size and fixed address objects.
  97. These routines grab large chunks of memory, using a function you
  98. supply, called `obstack_chunk_alloc'. On occasion, they free chunks,
  99. by calling `obstack_chunk_free'. You must define them and declare
  100. them before using any obstack macros.
  101. Each independent stack is represented by a `struct obstack'.
  102. Each of the obstack macros expects a pointer to such a structure
  103. as the first argument.
  104. One motivation for this package is the problem of growing char strings
  105. in symbol tables. Unless you are "facist pig with a read-only mind"
  106. [Gosper's immortal quote from HAKMEM item 154, out of context] you
  107. would not like to put any arbitrary upper limit on the length of your
  108. symbols.
  109. In practice this often means you will build many short symbols and a
  110. few long symbols. At the time you are reading a symbol you don't know
  111. how long it is. One traditional method is to read a symbol into a
  112. buffer, realloc()ating the buffer every time you try to read a symbol
  113. that is longer than the buffer. This is beaut, but you still will
  114. want to copy the symbol from the buffer to a more permanent
  115. symbol-table entry say about half the time.
  116. With obstacks, you can work differently. Use one obstack for all symbol
  117. names. As you read a symbol, grow the name in the obstack gradually.
  118. When the name is complete, finalize it. Then, if the symbol exists already,
  119. free the newly read name.
  120. The way we do this is to take a large chunk, allocating memory from
  121. low addresses. When you want to build a aymbol in the chunk you just
  122. add chars above the current "high water mark" in the chunk. When you
  123. have finished adding chars, because you got to the end of the symbol,
  124. you know how long the chars are, and you can create a new object.
  125. Mostly the chars will not burst over the highest address of the chunk,
  126. because you would typically expect a chunk to be (say) 100 times as
  127. long as an average object.
  128. In case that isn't clear, when we have enough chars to make up
  129. the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed)
  130. so we just point to it where it lies. No moving of chars is
  131. needed and this is the second win: potentially long strings need
  132. never be explicitly shuffled. Once an object is formed, it does not
  133. change its address during its lifetime.
  134. When the chars burst over a chunk boundary, we allocate a larger
  135. chunk, and then copy the partly formed object from the end of the old
  136. chunk to the beggining of the new larger chunk. We then carry on
  137. accreting characters to the end of the object as we normaly would.
  138. A special macro is provided to add a single char at a time to a
  139. growing object. This allows the use of register variables, which
  140. break the ordinary 'growth' macro.
  141. Summary:
  142. We allocate large chunks.
  143. We carve out one object at a time from the current chunk.
  144. Once carved, an object never moves.
  145. We are free to append data of any size to the currently
  146. growing object.
  147. Exactly one object is growing in an obstack at any one time.
  148. You can run one obstack per control block.
  149. You may have as many control blocks as you dare.
  150. Because of the way we do it, you can `unwind' a obstack
  151. back to a previous state. (You may remove objects much
  152. as you would with a stack.)
  153. */
  154. /* Don't do the contents of this file more than once. */
  155. #ifndef __OBSTACKS__
  156. #define __OBSTACKS__
  157. struct _obstack_chunk /* Lives at front of each chunk. */
  158. {
  159. char *limit; /* 1 past end of this chunk */
  160. struct _obstack_chunk *prev; /* address of prior chunk or NULL */
  161. char contents[4]; /* objects begin here */
  162. };
  163. struct obstack /* control current object in current chunk */
  164. {
  165. long chunk_size; /* preferred size to allocate chunks in */
  166. struct _obstack_chunk* chunk; /* address of current struct obstack_chunk */
  167. char *object_base; /* address of object we are building */
  168. char *next_free; /* where to add next char to current object */
  169. char *chunk_limit; /* address of char after current chunk */
  170. int temp; /* Temporary for some macros. */
  171. int alignment_mask; /* Mask of alignment for each object. */
  172. struct _obstack_chunk *(*chunkfun) (); /* User's fcn to allocate a chunk. */
  173. void (*freefun) (); /* User's function to free a chunk. */
  174. };
  175. #ifdef __STDC__
  176. /* Do the function-declarations after the structs
  177. but before defining the macros. */
  178. void obstack_init (struct obstack *obstack);
  179. void * obstack_alloc (struct obstack *obstack, int size);
  180. void * obstack_copy (struct obstack *obstack, void *address, int size);
  181. void * obstack_copy0 (struct obstack *obstack, void *address, int size);
  182. void obstack_free (struct obstack *obstack, void *block);
  183. void obstack_blank (struct obstack *obstack, int size);
  184. void obstack_grow (struct obstack *obstack, void *data, int size);
  185. void obstack_grow0 (struct obstack *obstack, void *data, int size);
  186. void obstack_1grow (struct obstack *obstack, int data_char);
  187. void * obstack_finish (struct obstack *obstack);
  188. int obstack_object_size (struct obstack *obstack);
  189. int obstack_room (struct obstack *obstack);
  190. void obstack_1grow_fast (struct obstack *obstack, int data_char);
  191. void obstack_blank_fast (struct obstack *obstack, int size);
  192. void * object_base (struct obstack *obstack);
  193. void * object_next_free (struct obstack *obstack);
  194. int obstack_alignment_mask (struct obstack *obstack);
  195. int obstack_chunk_size (struct obstack *obstack);
  196. #endif /* __STDC__ */
  197. /* Non-ANSI C cannot really support alternative functions for these macros,
  198. so we do not declare them. */
  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. #define obstack_base(h) ((h)->object_base)
  203. /* Size for allocating ordinary chunks. */
  204. #define obstack_chunk_size(h) ((h)->chunk_size)
  205. /* Pointer to next byte not yet allocated in current chunk. */
  206. #define obstack_next_free(h) ((h)->next_free)
  207. /* Mask specifying low bits that should be clear in address of an object. */
  208. #define obstack_alignment_mask(h) ((h)->alignment_mask)
  209. #define obstack_init(h) \
  210. _obstack_begin ((h), 4096 - 4, 0, obstack_chunk_alloc, obstack_chunk_free)
  211. #define obstack_begin(h, size) \
  212. _obstack_begin ((h), (size), 0, obstack_chunk_alloc, obstack_chunk_free)
  213. #define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = achar)
  214. #define obstack_blank_fast(h,n) ((h)->next_free += (n))
  215. #ifdef __GNUC__
  216. /* For GNU C we can define these macros to compute all args only once
  217. without using a global variable.
  218. Also, we can avoid using the `temp' slot, to make faster code. */
  219. #define obstack_object_size(OBSTACK) \
  220. ({ struct obstack *__o = (OBSTACK); \
  221. (unsigned) (__o->next_free - __o->object_base); })
  222. #define obstack_room(OBSTACK) \
  223. ({ struct obstack *__o = (OBSTACK); \
  224. (unsigned) (__o->chunk_limit - __o->next_free); })
  225. #define obstack_grow(OBSTACK,where,length) \
  226. ({ struct obstack *__o = (OBSTACK); \
  227. int __len = (length); \
  228. ((__o->next_free + __len > __o->chunk_limit) \
  229. ? _obstack_newchunk (__o, __len) : 0); \
  230. bcopy (where, __o->next_free, __len); \
  231. __o->next_free += __len; \
  232. (void) 0; })
  233. #define obstack_grow0(OBSTACK,where,length) \
  234. ({ struct obstack *__o = (OBSTACK); \
  235. int __len = (length); \
  236. ((__o->next_free + __len + 1 > __o->chunk_limit) \
  237. ? _obstack_newchunk (__o, __len + 1) : 0), \
  238. bcopy (where, __o->next_free, __len), \
  239. __o->next_free += __len, \
  240. *(__o->next_free)++ = 0; \
  241. (void) 0; })
  242. #define obstack_1grow(OBSTACK,datum) \
  243. ({ struct obstack *__o = (OBSTACK); \
  244. ((__o->next_free + 1 > __o->chunk_limit) \
  245. ? _obstack_newchunk (__o, 1) : 0), \
  246. *(__o->next_free)++ = (datum); \
  247. (void) 0; })
  248. #define obstack_blank(OBSTACK,length) \
  249. ({ struct obstack *__o = (OBSTACK); \
  250. int __len = (length); \
  251. ((__o->next_free + __len > __o->chunk_limit) \
  252. ? _obstack_newchunk (__o, __len) : 0); \
  253. __o->next_free += __len; \
  254. (void) 0; })
  255. #define obstack_alloc(OBSTACK,length) \
  256. ({ struct obstack *__h = (OBSTACK); \
  257. obstack_blank (__h, (length)); \
  258. obstack_finish (__h); })
  259. #define obstack_copy(OBSTACK,where,length) \
  260. ({ struct obstack *__h = (OBSTACK); \
  261. obstack_grow (__h, (where), (length)); \
  262. obstack_finish (__h); })
  263. #define obstack_copy0(OBSTACK,where,length) \
  264. ({ struct obstack *__h = (OBSTACK); \
  265. obstack_grow0 (__h, (where), (length)); \
  266. obstack_finish (__h); })
  267. #define obstack_finish(OBSTACK) \
  268. ({ struct obstack *__o = (OBSTACK); \
  269. void *value = (void *) __o->object_base; \
  270. __o->next_free \
  271. = (char*)((int)(__o->next_free+__o->alignment_mask) \
  272. & ~ (__o->alignment_mask)); \
  273. ((__o->next_free - (char *)__o->chunk \
  274. > __o->chunk_limit - (char *)__o->chunk) \
  275. ? __o->next_free = __o->chunk_limit : 0); \
  276. __o->object_base = __o->next_free; \
  277. value; })
  278. #define obstack_free(OBSTACK, OBJ) \
  279. ({ struct obstack *__o = (OBSTACK); \
  280. void *__obj = (OBJ); \
  281. if (__obj >= (void *)__o->chunk && __obj < (void *)__o->chunk_limit) \
  282. __o->next_free = __o->object_base = __obj; \
  283. else (obstack_free) (__o, __obj); })
  284. #else /* not __GNUC__ */
  285. /* The non-GNU macros copy the obstack-pointer into this global variable
  286. to avoid multiple evaluation. */
  287. extern struct obstack *_obstack;
  288. #define obstack_object_size(h) \
  289. (unsigned) (_obstack = (h), (h)->next_free - (h)->object_base)
  290. #define obstack_room(h) \
  291. (unsigned) (_obstack = (h), (h)->chunk_limit - (h)->next_free)
  292. #define obstack_grow(h,where,length) \
  293. ( (h)->temp = (length), \
  294. (((h)->next_free + (h)->temp > (h)->chunk_limit) \
  295. ? _obstack_newchunk ((h), (h)->temp) : 0), \
  296. bcopy (where, (h)->next_free, (h)->temp), \
  297. (h)->next_free += (h)->temp)
  298. #define obstack_grow0(h,where,length) \
  299. ( (h)->temp = (length), \
  300. (((h)->next_free + (h)->temp + 1 > (h)->chunk_limit) \
  301. ? _obstack_newchunk ((h), (h)->temp + 1) : 0), \
  302. bcopy (where, (h)->next_free, (h)->temp), \
  303. (h)->next_free += (h)->temp, \
  304. *((h)->next_free)++ = 0)
  305. #define obstack_1grow(h,datum) \
  306. ( (((h)->next_free + 1 > (h)->chunk_limit) \
  307. ? _obstack_newchunk ((h), 1) : 0), \
  308. *((h)->next_free)++ = (datum))
  309. #define obstack_blank(h,length) \
  310. ( (h)->temp = (length), \
  311. (((h)->next_free + (h)->temp > (h)->chunk_limit) \
  312. ? _obstack_newchunk ((h), (h)->temp) : 0), \
  313. (h)->next_free += (h)->temp)
  314. #define obstack_alloc(h,length) \
  315. (obstack_blank ((h), (length)), obstack_finish ((h)))
  316. #define obstack_copy(h,where,length) \
  317. (obstack_grow ((h), (where), (length)), obstack_finish ((h)))
  318. #define obstack_copy0(h,where,length) \
  319. (obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
  320. #define obstack_finish(h) \
  321. ( (h)->temp = (int) (h)->object_base, \
  322. (h)->next_free \
  323. = (char*)((int)((h)->next_free+(h)->alignment_mask) \
  324. & ~ ((h)->alignment_mask)), \
  325. (((h)->next_free - (char *)(h)->chunk \
  326. > (h)->chunk_limit - (char *)(h)->chunk) \
  327. ? (h)->next_free = (h)->chunk_limit : 0), \
  328. (h)->object_base = (h)->next_free, \
  329. (char *) (h)->temp)
  330. #ifdef __STDC__
  331. #define obstack_free(h,obj) \
  332. ( (h)->temp = (char *)(obj) - (char *) (h)->chunk, \
  333. (((h)->temp >= 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\
  334. ? (int) ((h)->next_free = (h)->object_base \
  335. = (h)->temp + (char *) (h)->chunk) \
  336. : ((obstack_free) ((h), (h)->temp + (char *) (h)->chunk), 0)))
  337. #else
  338. #define obstack_free(h,obj) \
  339. ( (h)->temp = (char *)(obj) - (char *) (h)->chunk, \
  340. (((h)->temp >= 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\
  341. ? (int) ((h)->next_free = (h)->object_base \
  342. = (h)->temp + (char *) (h)->chunk) \
  343. : (int) _obstack_free ((h), (h)->temp + (char *) (h)->chunk)))
  344. #endif
  345. #endif /* not __GNUC__ */
  346. #endif /* not __OBSTACKS__ */