obstack.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /* obstack.h - object stack macros
  2. Copyright (C) 1988-1994,1996-1999,2003,2004,2005
  3. Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. /* Summary:
  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. These macros operate a stack of objects. Each object starts life
  23. small, and may grow to maturity. (Consider building a word syllable
  24. by syllable.) An object can move while it is growing. Once it has
  25. been "finished" it never changes address again. So the "top of the
  26. stack" is typically an immature growing object, while the rest of the
  27. stack is of mature, fixed size and fixed address objects.
  28. These routines grab large chunks of memory, using a function you
  29. supply, called `obstack_chunk_alloc'. On occasion, they free chunks,
  30. by calling `obstack_chunk_free'. You must define them and declare
  31. them before using any obstack macros.
  32. Each independent stack is represented by a `struct obstack'.
  33. Each of the obstack macros expects a pointer to such a structure
  34. as the first argument.
  35. One motivation for this package is the problem of growing char strings
  36. in symbol tables. Unless you are "fascist pig with a read-only mind"
  37. --Gosper's immortal quote from HAKMEM item 154, out of context--you
  38. would not like to put any arbitrary upper limit on the length of your
  39. symbols.
  40. In practice this often means you will build many short symbols and a
  41. few long symbols. At the time you are reading a symbol you don't know
  42. how long it is. One traditional method is to read a symbol into a
  43. buffer, realloc()ating the buffer every time you try to read a symbol
  44. that is longer than the buffer. This is beaut, but you still will
  45. want to copy the symbol from the buffer to a more permanent
  46. symbol-table entry say about half the time.
  47. With obstacks, you can work differently. Use one obstack for all symbol
  48. names. As you read a symbol, grow the name in the obstack gradually.
  49. When the name is complete, finalize it. Then, if the symbol exists already,
  50. free the newly read name.
  51. The way we do this is to take a large chunk, allocating memory from
  52. low addresses. When you want to build a symbol in the chunk you just
  53. add chars above the current "high water mark" in the chunk. When you
  54. have finished adding chars, because you got to the end of the symbol,
  55. you know how long the chars are, and you can create a new object.
  56. Mostly the chars will not burst over the highest address of the chunk,
  57. because you would typically expect a chunk to be (say) 100 times as
  58. long as an average object.
  59. In case that isn't clear, when we have enough chars to make up
  60. the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed)
  61. so we just point to it where it lies. No moving of chars is
  62. needed and this is the second win: potentially long strings need
  63. never be explicitly shuffled. Once an object is formed, it does not
  64. change its address during its lifetime.
  65. When the chars burst over a chunk boundary, we allocate a larger
  66. chunk, and then copy the partly formed object from the end of the old
  67. chunk to the beginning of the new larger chunk. We then carry on
  68. accreting characters to the end of the object as we normally would.
  69. A special macro is provided to add a single char at a time to a
  70. growing object. This allows the use of register variables, which
  71. break the ordinary 'growth' macro.
  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' an obstack
  82. back to a previous state. (You may remove objects much
  83. as you would with a stack.)
  84. */
  85. /* Don't do the contents of this file more than once. */
  86. #ifndef _OBSTACK_H
  87. #define _OBSTACK_H 1
  88. #ifdef __cplusplus
  89. extern "C" {
  90. #endif
  91. /* We need the type of a pointer subtraction. If __PTRDIFF_TYPE__ is
  92. defined, as with GNU C, use that; that way we don't pollute the
  93. namespace with <stddef.h>'s symbols. Otherwise, include <stddef.h>
  94. and use ptrdiff_t. */
  95. #ifdef __PTRDIFF_TYPE__
  96. # define PTR_INT_TYPE __PTRDIFF_TYPE__
  97. #else
  98. # include <stddef.h>
  99. # define PTR_INT_TYPE ptrdiff_t
  100. #endif
  101. /* If B is the base of an object addressed by P, return the result of
  102. aligning P to the next multiple of A + 1. B and P must be of type
  103. char *. A + 1 must be a power of 2. */
  104. #define __BPTR_ALIGN(B, P, A) ((B) + (((P) - (B) + (A)) & ~(A)))
  105. /* Similiar to _BPTR_ALIGN (B, P, A), except optimize the common case
  106. where pointers can be converted to integers, aligned as integers,
  107. and converted back again. If PTR_INT_TYPE is narrower than a
  108. pointer (e.g., the AS/400), play it safe and compute the alignment
  109. relative to B. Otherwise, use the faster strategy of computing the
  110. alignment relative to 0. */
  111. #define __PTR_ALIGN(B, P, A) \
  112. __BPTR_ALIGN (sizeof (PTR_INT_TYPE) < sizeof (void *) ? (B) : (char *) 0, \
  113. P, A)
  114. #include <string.h>
  115. struct _obstack_chunk /* Lives at front of each chunk. */
  116. {
  117. char *limit; /* 1 past end of this chunk */
  118. struct _obstack_chunk *prev; /* address of prior chunk or NULL */
  119. char contents[4]; /* objects begin here */
  120. };
  121. struct obstack /* control current object in current chunk */
  122. {
  123. long chunk_size; /* preferred size to allocate chunks in */
  124. struct _obstack_chunk *chunk; /* address of current struct obstack_chunk */
  125. char *object_base; /* address of object we are building */
  126. char *next_free; /* where to add next char to current object */
  127. char *chunk_limit; /* address of char after current chunk */
  128. union
  129. {
  130. PTR_INT_TYPE tempint;
  131. void *tempptr;
  132. } temp; /* Temporary for some macros. */
  133. int alignment_mask; /* Mask of alignment for each object. */
  134. /* These prototypes vary based on `use_extra_arg', and we use
  135. casts to the prototypeless function type in all assignments,
  136. but having prototypes here quiets -Wstrict-prototypes. */
  137. struct _obstack_chunk *(*chunkfun) (void *, long);
  138. void (*freefun) (void *, struct _obstack_chunk *);
  139. void *extra_arg; /* first arg for chunk alloc/dealloc funcs */
  140. unsigned use_extra_arg:1; /* chunk alloc/dealloc funcs take extra arg */
  141. unsigned maybe_empty_object:1;/* There is a possibility that the current
  142. chunk contains a zero-length object. This
  143. prevents freeing the chunk if we allocate
  144. a bigger chunk to replace it. */
  145. unsigned alloc_failed:1; /* No longer used, as we now call the failed
  146. handler on error, but retained for binary
  147. compatibility. */
  148. };
  149. /* Declare the external functions we use; they are in obstack.c. */
  150. extern void _obstack_newchunk (struct obstack *, int);
  151. extern int _obstack_begin (struct obstack *, int, int,
  152. void *(*) (long), void (*) (void *));
  153. extern int _obstack_begin_1 (struct obstack *, int, int,
  154. void *(*) (void *, long),
  155. void (*) (void *, void *), void *);
  156. extern int _obstack_memory_used (struct obstack *);
  157. void obstack_free (struct obstack *obstack, void *block);
  158. /* Error handler called when `obstack_chunk_alloc' failed to allocate
  159. more memory. This can be set to a user defined function which
  160. should either abort gracefully or use longjump - but shouldn't
  161. return. The default action is to print a message and abort. */
  162. extern void (*obstack_alloc_failed_handler) (void);
  163. /* Exit value used when `print_and_abort' is used. */
  164. extern int obstack_exit_failure;
  165. /* Pointer to beginning of object being allocated or to be allocated next.
  166. Note that this might not be the final address of the object
  167. because a new chunk might be needed to hold the final size. */
  168. #define obstack_base(h) ((void *) (h)->object_base)
  169. /* Size for allocating ordinary chunks. */
  170. #define obstack_chunk_size(h) ((h)->chunk_size)
  171. /* Pointer to next byte not yet allocated in current chunk. */
  172. #define obstack_next_free(h) ((h)->next_free)
  173. /* Mask specifying low bits that should be clear in address of an object. */
  174. #define obstack_alignment_mask(h) ((h)->alignment_mask)
  175. /* To prevent prototype warnings provide complete argument list. */
  176. #define obstack_init(h) \
  177. _obstack_begin ((h), 0, 0, \
  178. (void *(*) (long)) obstack_chunk_alloc, \
  179. (void (*) (void *)) obstack_chunk_free)
  180. #define obstack_begin(h, size) \
  181. _obstack_begin ((h), (size), 0, \
  182. (void *(*) (long)) obstack_chunk_alloc, \
  183. (void (*) (void *)) obstack_chunk_free)
  184. #define obstack_specify_allocation(h, size, alignment, chunkfun, freefun) \
  185. _obstack_begin ((h), (size), (alignment), \
  186. (void *(*) (long)) (chunkfun), \
  187. (void (*) (void *)) (freefun))
  188. #define obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) \
  189. _obstack_begin_1 ((h), (size), (alignment), \
  190. (void *(*) (void *, long)) (chunkfun), \
  191. (void (*) (void *, void *)) (freefun), (arg))
  192. #define obstack_chunkfun(h, newchunkfun) \
  193. ((h) -> chunkfun = (struct _obstack_chunk *(*)(void *, long)) (newchunkfun))
  194. #define obstack_freefun(h, newfreefun) \
  195. ((h) -> freefun = (void (*)(void *, struct _obstack_chunk *)) (newfreefun))
  196. #define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = (achar))
  197. #define obstack_blank_fast(h,n) ((h)->next_free += (n))
  198. #define obstack_memory_used(h) _obstack_memory_used (h)
  199. #if defined __GNUC__ && defined __STDC__ && __STDC__
  200. /* NextStep 2.0 cc is really gcc 1.93 but it defines __GNUC__ = 2 and
  201. does not implement __extension__. But that compiler doesn't define
  202. __GNUC_MINOR__. */
  203. # if __GNUC__ < 2 || (defined __NeXT__ && __NeXT__ && !__GNUC_MINOR__)
  204. # define __extension__
  205. # endif
  206. /* For GNU C, if not -traditional,
  207. we can define these macros to compute all args only once
  208. without using a global variable.
  209. Also, we can avoid using the `temp' slot, to make faster code. */
  210. # define obstack_object_size(OBSTACK) \
  211. __extension__ \
  212. ({ struct obstack const *__o = (OBSTACK); \
  213. (unsigned) (__o->next_free - __o->object_base); })
  214. # define obstack_room(OBSTACK) \
  215. __extension__ \
  216. ({ struct obstack const *__o = (OBSTACK); \
  217. (unsigned) (__o->chunk_limit - __o->next_free); })
  218. # define obstack_make_room(OBSTACK,length) \
  219. __extension__ \
  220. ({ struct obstack *__o = (OBSTACK); \
  221. int __len = (length); \
  222. if (__o->chunk_limit - __o->next_free < __len) \
  223. _obstack_newchunk (__o, __len); \
  224. (void) 0; })
  225. # define obstack_empty_p(OBSTACK) \
  226. __extension__ \
  227. ({ struct obstack const *__o = (OBSTACK); \
  228. (__o->chunk->prev == 0 \
  229. && __o->next_free == __PTR_ALIGN ((char *) __o->chunk, \
  230. __o->chunk->contents, \
  231. __o->alignment_mask)); })
  232. # define obstack_grow(OBSTACK,where,length) \
  233. __extension__ \
  234. ({ struct obstack *__o = (OBSTACK); \
  235. int __len = (length); \
  236. if (__o->next_free + __len > __o->chunk_limit) \
  237. _obstack_newchunk (__o, __len); \
  238. memcpy (__o->next_free, where, __len); \
  239. __o->next_free += __len; \
  240. (void) 0; })
  241. # define obstack_grow0(OBSTACK,where,length) \
  242. __extension__ \
  243. ({ struct obstack *__o = (OBSTACK); \
  244. int __len = (length); \
  245. if (__o->next_free + __len + 1 > __o->chunk_limit) \
  246. _obstack_newchunk (__o, __len + 1); \
  247. memcpy (__o->next_free, where, __len); \
  248. __o->next_free += __len; \
  249. *(__o->next_free)++ = 0; \
  250. (void) 0; })
  251. # define obstack_1grow(OBSTACK,datum) \
  252. __extension__ \
  253. ({ struct obstack *__o = (OBSTACK); \
  254. if (__o->next_free + 1 > __o->chunk_limit) \
  255. _obstack_newchunk (__o, 1); \
  256. obstack_1grow_fast (__o, datum); \
  257. (void) 0; })
  258. /* These assume that the obstack alignment is good enough for pointers
  259. or ints, and that the data added so far to the current object
  260. shares that much alignment. */
  261. # define obstack_ptr_grow(OBSTACK,datum) \
  262. __extension__ \
  263. ({ struct obstack *__o = (OBSTACK); \
  264. if (__o->next_free + sizeof (void *) > __o->chunk_limit) \
  265. _obstack_newchunk (__o, sizeof (void *)); \
  266. obstack_ptr_grow_fast (__o, datum); }) \
  267. # define obstack_int_grow(OBSTACK,datum) \
  268. __extension__ \
  269. ({ struct obstack *__o = (OBSTACK); \
  270. if (__o->next_free + sizeof (int) > __o->chunk_limit) \
  271. _obstack_newchunk (__o, sizeof (int)); \
  272. obstack_int_grow_fast (__o, datum); })
  273. # define obstack_ptr_grow_fast(OBSTACK,aptr) \
  274. __extension__ \
  275. ({ struct obstack *__o1 = (OBSTACK); \
  276. *(const void **) __o1->next_free = (aptr); \
  277. __o1->next_free += sizeof (const void *); \
  278. (void) 0; })
  279. # define obstack_int_grow_fast(OBSTACK,aint) \
  280. __extension__ \
  281. ({ struct obstack *__o1 = (OBSTACK); \
  282. *(int *) __o1->next_free = (aint); \
  283. __o1->next_free += sizeof (int); \
  284. (void) 0; })
  285. # define obstack_blank(OBSTACK,length) \
  286. __extension__ \
  287. ({ struct obstack *__o = (OBSTACK); \
  288. int __len = (length); \
  289. if (__o->chunk_limit - __o->next_free < __len) \
  290. _obstack_newchunk (__o, __len); \
  291. obstack_blank_fast (__o, __len); \
  292. (void) 0; })
  293. # define obstack_alloc(OBSTACK,length) \
  294. __extension__ \
  295. ({ struct obstack *__h = (OBSTACK); \
  296. obstack_blank (__h, (length)); \
  297. obstack_finish (__h); })
  298. # define obstack_copy(OBSTACK,where,length) \
  299. __extension__ \
  300. ({ struct obstack *__h = (OBSTACK); \
  301. obstack_grow (__h, (where), (length)); \
  302. obstack_finish (__h); })
  303. # define obstack_copy0(OBSTACK,where,length) \
  304. __extension__ \
  305. ({ struct obstack *__h = (OBSTACK); \
  306. obstack_grow0 (__h, (where), (length)); \
  307. obstack_finish (__h); })
  308. /* The local variable is named __o1 to avoid a name conflict
  309. when obstack_blank is called. */
  310. # define obstack_finish(OBSTACK) \
  311. __extension__ \
  312. ({ struct obstack *__o1 = (OBSTACK); \
  313. void *__value = (void *) __o1->object_base; \
  314. if (__o1->next_free == __value) \
  315. __o1->maybe_empty_object = 1; \
  316. __o1->next_free \
  317. = __PTR_ALIGN (__o1->object_base, __o1->next_free, \
  318. __o1->alignment_mask); \
  319. if (__o1->next_free - (char *)__o1->chunk \
  320. > __o1->chunk_limit - (char *)__o1->chunk) \
  321. __o1->next_free = __o1->chunk_limit; \
  322. __o1->object_base = __o1->next_free; \
  323. __value; })
  324. # define obstack_free(OBSTACK, OBJ) \
  325. __extension__ \
  326. ({ struct obstack *__o = (OBSTACK); \
  327. void *__obj = (OBJ); \
  328. if (__obj > (void *)__o->chunk && __obj < (void *)__o->chunk_limit) \
  329. __o->next_free = __o->object_base = (char *)__obj; \
  330. else (obstack_free) (__o, __obj); })
  331. #else /* not __GNUC__ or not __STDC__ */
  332. # define obstack_object_size(h) \
  333. (unsigned) ((h)->next_free - (h)->object_base)
  334. # define obstack_room(h) \
  335. (unsigned) ((h)->chunk_limit - (h)->next_free)
  336. # define obstack_empty_p(h) \
  337. ((h)->chunk->prev == 0 \
  338. && (h)->next_free == __PTR_ALIGN ((char *) (h)->chunk, \
  339. (h)->chunk->contents, \
  340. (h)->alignment_mask))
  341. /* Note that the call to _obstack_newchunk is enclosed in (..., 0)
  342. so that we can avoid having void expressions
  343. in the arms of the conditional expression.
  344. Casting the third operand to void was tried before,
  345. but some compilers won't accept it. */
  346. # define obstack_make_room(h,length) \
  347. ( (h)->temp.tempint = (length), \
  348. (((h)->next_free + (h)->temp.tempint > (h)->chunk_limit) \
  349. ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0))
  350. # define obstack_grow(h,where,length) \
  351. ( (h)->temp.tempint = (length), \
  352. (((h)->next_free + (h)->temp.tempint > (h)->chunk_limit) \
  353. ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0), \
  354. memcpy ((h)->next_free, where, (h)->temp.tempint), \
  355. (h)->next_free += (h)->temp.tempint)
  356. # define obstack_grow0(h,where,length) \
  357. ( (h)->temp.tempint = (length), \
  358. (((h)->next_free + (h)->temp.tempint + 1 > (h)->chunk_limit) \
  359. ? (_obstack_newchunk ((h), (h)->temp.tempint + 1), 0) : 0), \
  360. memcpy ((h)->next_free, where, (h)->temp.tempint), \
  361. (h)->next_free += (h)->temp.tempint, \
  362. *((h)->next_free)++ = 0)
  363. # define obstack_1grow(h,datum) \
  364. ( (((h)->next_free + 1 > (h)->chunk_limit) \
  365. ? (_obstack_newchunk ((h), 1), 0) : 0), \
  366. obstack_1grow_fast (h, datum))
  367. # define obstack_ptr_grow(h,datum) \
  368. ( (((h)->next_free + sizeof (char *) > (h)->chunk_limit) \
  369. ? (_obstack_newchunk ((h), sizeof (char *)), 0) : 0), \
  370. obstack_ptr_grow_fast (h, datum))
  371. # define obstack_int_grow(h,datum) \
  372. ( (((h)->next_free + sizeof (int) > (h)->chunk_limit) \
  373. ? (_obstack_newchunk ((h), sizeof (int)), 0) : 0), \
  374. obstack_int_grow_fast (h, datum))
  375. # define obstack_ptr_grow_fast(h,aptr) \
  376. (((const void **) ((h)->next_free += sizeof (void *)))[-1] = (aptr))
  377. # define obstack_int_grow_fast(h,aint) \
  378. (((int *) ((h)->next_free += sizeof (int)))[-1] = (aint))
  379. # define obstack_blank(h,length) \
  380. ( (h)->temp.tempint = (length), \
  381. (((h)->chunk_limit - (h)->next_free < (h)->temp.tempint) \
  382. ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0), \
  383. obstack_blank_fast (h, (h)->temp.tempint))
  384. # define obstack_alloc(h,length) \
  385. (obstack_blank ((h), (length)), obstack_finish ((h)))
  386. # define obstack_copy(h,where,length) \
  387. (obstack_grow ((h), (where), (length)), obstack_finish ((h)))
  388. # define obstack_copy0(h,where,length) \
  389. (obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
  390. # define obstack_finish(h) \
  391. ( ((h)->next_free == (h)->object_base \
  392. ? (((h)->maybe_empty_object = 1), 0) \
  393. : 0), \
  394. (h)->temp.tempptr = (h)->object_base, \
  395. (h)->next_free \
  396. = __PTR_ALIGN ((h)->object_base, (h)->next_free, \
  397. (h)->alignment_mask), \
  398. (((h)->next_free - (char *) (h)->chunk \
  399. > (h)->chunk_limit - (char *) (h)->chunk) \
  400. ? ((h)->next_free = (h)->chunk_limit) : 0), \
  401. (h)->object_base = (h)->next_free, \
  402. (h)->temp.tempptr)
  403. # define obstack_free(h,obj) \
  404. ( (h)->temp.tempint = (char *) (obj) - (char *) (h)->chunk, \
  405. ((((h)->temp.tempint > 0 \
  406. && (h)->temp.tempint < (h)->chunk_limit - (char *) (h)->chunk)) \
  407. ? (int) ((h)->next_free = (h)->object_base \
  408. = (h)->temp.tempint + (char *) (h)->chunk) \
  409. : (((obstack_free) ((h), (h)->temp.tempint + (char *) (h)->chunk), 0), 0)))
  410. #endif /* not __GNUC__ or not __STDC__ */
  411. #ifdef __cplusplus
  412. } /* C++ */
  413. #endif
  414. #endif /* obstack.h */