obstack.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. libc_hidden_proto(_obstack_newchunk)
  152. extern int _obstack_begin (struct obstack *, int, int,
  153. void *(*) (long), void (*) (void *));
  154. extern int _obstack_begin_1 (struct obstack *, int, int,
  155. void *(*) (void *, long),
  156. void (*) (void *, void *), void *);
  157. extern int _obstack_memory_used (struct obstack *);
  158. void obstack_free (struct obstack *obstack, void *block);
  159. /* Error handler called when `obstack_chunk_alloc' failed to allocate
  160. more memory. This can be set to a user defined function which
  161. should either abort gracefully or use longjump - but shouldn't
  162. return. The default action is to print a message and abort. */
  163. extern void (*obstack_alloc_failed_handler) (void);
  164. /* Exit value used when `print_and_abort' is used. */
  165. extern int obstack_exit_failure;
  166. /* Pointer to beginning of object being allocated or to be allocated next.
  167. Note that this might not be the final address of the object
  168. because a new chunk might be needed to hold the final size. */
  169. #define obstack_base(h) ((void *) (h)->object_base)
  170. /* Size for allocating ordinary chunks. */
  171. #define obstack_chunk_size(h) ((h)->chunk_size)
  172. /* Pointer to next byte not yet allocated in current chunk. */
  173. #define obstack_next_free(h) ((h)->next_free)
  174. /* Mask specifying low bits that should be clear in address of an object. */
  175. #define obstack_alignment_mask(h) ((h)->alignment_mask)
  176. /* To prevent prototype warnings provide complete argument list. */
  177. #define obstack_init(h) \
  178. _obstack_begin ((h), 0, 0, \
  179. (void *(*) (long)) obstack_chunk_alloc, \
  180. (void (*) (void *)) obstack_chunk_free)
  181. #define obstack_begin(h, size) \
  182. _obstack_begin ((h), (size), 0, \
  183. (void *(*) (long)) obstack_chunk_alloc, \
  184. (void (*) (void *)) obstack_chunk_free)
  185. #define obstack_specify_allocation(h, size, alignment, chunkfun, freefun) \
  186. _obstack_begin ((h), (size), (alignment), \
  187. (void *(*) (long)) (chunkfun), \
  188. (void (*) (void *)) (freefun))
  189. #define obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) \
  190. _obstack_begin_1 ((h), (size), (alignment), \
  191. (void *(*) (void *, long)) (chunkfun), \
  192. (void (*) (void *, void *)) (freefun), (arg))
  193. #define obstack_chunkfun(h, newchunkfun) \
  194. ((h) -> chunkfun = (struct _obstack_chunk *(*)(void *, long)) (newchunkfun))
  195. #define obstack_freefun(h, newfreefun) \
  196. ((h) -> freefun = (void (*)(void *, struct _obstack_chunk *)) (newfreefun))
  197. #define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = (achar))
  198. #define obstack_blank_fast(h,n) ((h)->next_free += (n))
  199. #define obstack_memory_used(h) _obstack_memory_used (h)
  200. #if defined __GNUC__ && defined __STDC__ && __STDC__
  201. /* NextStep 2.0 cc is really gcc 1.93 but it defines __GNUC__ = 2 and
  202. does not implement __extension__. But that compiler doesn't define
  203. __GNUC_MINOR__. */
  204. # if __GNUC__ < 2 || (defined __NeXT__ && __NeXT__ && !__GNUC_MINOR__)
  205. # define __extension__
  206. # endif
  207. /* For GNU C, if not -traditional,
  208. we can define these macros to compute all args only once
  209. without using a global variable.
  210. Also, we can avoid using the `temp' slot, to make faster code. */
  211. # define obstack_object_size(OBSTACK) \
  212. __extension__ \
  213. ({ struct obstack const *__o = (OBSTACK); \
  214. (unsigned) (__o->next_free - __o->object_base); })
  215. # define obstack_room(OBSTACK) \
  216. __extension__ \
  217. ({ struct obstack const *__o = (OBSTACK); \
  218. (unsigned) (__o->chunk_limit - __o->next_free); })
  219. # define obstack_make_room(OBSTACK,length) \
  220. __extension__ \
  221. ({ struct obstack *__o = (OBSTACK); \
  222. int __len = (length); \
  223. if (__o->chunk_limit - __o->next_free < __len) \
  224. _obstack_newchunk (__o, __len); \
  225. (void) 0; })
  226. # define obstack_empty_p(OBSTACK) \
  227. __extension__ \
  228. ({ struct obstack const *__o = (OBSTACK); \
  229. (__o->chunk->prev == 0 \
  230. && __o->next_free == __PTR_ALIGN ((char *) __o->chunk, \
  231. __o->chunk->contents, \
  232. __o->alignment_mask)); })
  233. # define obstack_grow(OBSTACK,where,length) \
  234. __extension__ \
  235. ({ struct obstack *__o = (OBSTACK); \
  236. int __len = (length); \
  237. if (__o->next_free + __len > __o->chunk_limit) \
  238. _obstack_newchunk (__o, __len); \
  239. memcpy (__o->next_free, where, __len); \
  240. __o->next_free += __len; \
  241. (void) 0; })
  242. # define obstack_grow0(OBSTACK,where,length) \
  243. __extension__ \
  244. ({ struct obstack *__o = (OBSTACK); \
  245. int __len = (length); \
  246. if (__o->next_free + __len + 1 > __o->chunk_limit) \
  247. _obstack_newchunk (__o, __len + 1); \
  248. memcpy (__o->next_free, where, __len); \
  249. __o->next_free += __len; \
  250. *(__o->next_free)++ = 0; \
  251. (void) 0; })
  252. # define obstack_1grow(OBSTACK,datum) \
  253. __extension__ \
  254. ({ struct obstack *__o = (OBSTACK); \
  255. if (__o->next_free + 1 > __o->chunk_limit) \
  256. _obstack_newchunk (__o, 1); \
  257. obstack_1grow_fast (__o, datum); \
  258. (void) 0; })
  259. /* These assume that the obstack alignment is good enough for pointers
  260. or ints, and that the data added so far to the current object
  261. shares that much alignment. */
  262. # define obstack_ptr_grow(OBSTACK,datum) \
  263. __extension__ \
  264. ({ struct obstack *__o = (OBSTACK); \
  265. if (__o->next_free + sizeof (void *) > __o->chunk_limit) \
  266. _obstack_newchunk (__o, sizeof (void *)); \
  267. obstack_ptr_grow_fast (__o, datum); }) \
  268. # define obstack_int_grow(OBSTACK,datum) \
  269. __extension__ \
  270. ({ struct obstack *__o = (OBSTACK); \
  271. if (__o->next_free + sizeof (int) > __o->chunk_limit) \
  272. _obstack_newchunk (__o, sizeof (int)); \
  273. obstack_int_grow_fast (__o, datum); })
  274. # define obstack_ptr_grow_fast(OBSTACK,aptr) \
  275. __extension__ \
  276. ({ struct obstack *__o1 = (OBSTACK); \
  277. *(const void **) __o1->next_free = (aptr); \
  278. __o1->next_free += sizeof (const void *); \
  279. (void) 0; })
  280. # define obstack_int_grow_fast(OBSTACK,aint) \
  281. __extension__ \
  282. ({ struct obstack *__o1 = (OBSTACK); \
  283. *(int *) __o1->next_free = (aint); \
  284. __o1->next_free += sizeof (int); \
  285. (void) 0; })
  286. # define obstack_blank(OBSTACK,length) \
  287. __extension__ \
  288. ({ struct obstack *__o = (OBSTACK); \
  289. int __len = (length); \
  290. if (__o->chunk_limit - __o->next_free < __len) \
  291. _obstack_newchunk (__o, __len); \
  292. obstack_blank_fast (__o, __len); \
  293. (void) 0; })
  294. # define obstack_alloc(OBSTACK,length) \
  295. __extension__ \
  296. ({ struct obstack *__h = (OBSTACK); \
  297. obstack_blank (__h, (length)); \
  298. obstack_finish (__h); })
  299. # define obstack_copy(OBSTACK,where,length) \
  300. __extension__ \
  301. ({ struct obstack *__h = (OBSTACK); \
  302. obstack_grow (__h, (where), (length)); \
  303. obstack_finish (__h); })
  304. # define obstack_copy0(OBSTACK,where,length) \
  305. __extension__ \
  306. ({ struct obstack *__h = (OBSTACK); \
  307. obstack_grow0 (__h, (where), (length)); \
  308. obstack_finish (__h); })
  309. /* The local variable is named __o1 to avoid a name conflict
  310. when obstack_blank is called. */
  311. # define obstack_finish(OBSTACK) \
  312. __extension__ \
  313. ({ struct obstack *__o1 = (OBSTACK); \
  314. void *__value = (void *) __o1->object_base; \
  315. if (__o1->next_free == __value) \
  316. __o1->maybe_empty_object = 1; \
  317. __o1->next_free \
  318. = __PTR_ALIGN (__o1->object_base, __o1->next_free, \
  319. __o1->alignment_mask); \
  320. if (__o1->next_free - (char *)__o1->chunk \
  321. > __o1->chunk_limit - (char *)__o1->chunk) \
  322. __o1->next_free = __o1->chunk_limit; \
  323. __o1->object_base = __o1->next_free; \
  324. __value; })
  325. # define obstack_free(OBSTACK, OBJ) \
  326. __extension__ \
  327. ({ struct obstack *__o = (OBSTACK); \
  328. void *__obj = (OBJ); \
  329. if (__obj > (void *)__o->chunk && __obj < (void *)__o->chunk_limit) \
  330. __o->next_free = __o->object_base = (char *)__obj; \
  331. else (obstack_free) (__o, __obj); })
  332. #else /* not __GNUC__ or not __STDC__ */
  333. # define obstack_object_size(h) \
  334. (unsigned) ((h)->next_free - (h)->object_base)
  335. # define obstack_room(h) \
  336. (unsigned) ((h)->chunk_limit - (h)->next_free)
  337. # define obstack_empty_p(h) \
  338. ((h)->chunk->prev == 0 \
  339. && (h)->next_free == __PTR_ALIGN ((char *) (h)->chunk, \
  340. (h)->chunk->contents, \
  341. (h)->alignment_mask))
  342. /* Note that the call to _obstack_newchunk is enclosed in (..., 0)
  343. so that we can avoid having void expressions
  344. in the arms of the conditional expression.
  345. Casting the third operand to void was tried before,
  346. but some compilers won't accept it. */
  347. # define obstack_make_room(h,length) \
  348. ( (h)->temp.tempint = (length), \
  349. (((h)->next_free + (h)->temp.tempint > (h)->chunk_limit) \
  350. ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0))
  351. # define obstack_grow(h,where,length) \
  352. ( (h)->temp.tempint = (length), \
  353. (((h)->next_free + (h)->temp.tempint > (h)->chunk_limit) \
  354. ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0), \
  355. memcpy ((h)->next_free, where, (h)->temp.tempint), \
  356. (h)->next_free += (h)->temp.tempint)
  357. # define obstack_grow0(h,where,length) \
  358. ( (h)->temp.tempint = (length), \
  359. (((h)->next_free + (h)->temp.tempint + 1 > (h)->chunk_limit) \
  360. ? (_obstack_newchunk ((h), (h)->temp.tempint + 1), 0) : 0), \
  361. memcpy ((h)->next_free, where, (h)->temp.tempint), \
  362. (h)->next_free += (h)->temp.tempint, \
  363. *((h)->next_free)++ = 0)
  364. # define obstack_1grow(h,datum) \
  365. ( (((h)->next_free + 1 > (h)->chunk_limit) \
  366. ? (_obstack_newchunk ((h), 1), 0) : 0), \
  367. obstack_1grow_fast (h, datum))
  368. # define obstack_ptr_grow(h,datum) \
  369. ( (((h)->next_free + sizeof (char *) > (h)->chunk_limit) \
  370. ? (_obstack_newchunk ((h), sizeof (char *)), 0) : 0), \
  371. obstack_ptr_grow_fast (h, datum))
  372. # define obstack_int_grow(h,datum) \
  373. ( (((h)->next_free + sizeof (int) > (h)->chunk_limit) \
  374. ? (_obstack_newchunk ((h), sizeof (int)), 0) : 0), \
  375. obstack_int_grow_fast (h, datum))
  376. # define obstack_ptr_grow_fast(h,aptr) \
  377. (((const void **) ((h)->next_free += sizeof (void *)))[-1] = (aptr))
  378. # define obstack_int_grow_fast(h,aint) \
  379. (((int *) ((h)->next_free += sizeof (int)))[-1] = (aint))
  380. # define obstack_blank(h,length) \
  381. ( (h)->temp.tempint = (length), \
  382. (((h)->chunk_limit - (h)->next_free < (h)->temp.tempint) \
  383. ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0), \
  384. obstack_blank_fast (h, (h)->temp.tempint))
  385. # define obstack_alloc(h,length) \
  386. (obstack_blank ((h), (length)), obstack_finish ((h)))
  387. # define obstack_copy(h,where,length) \
  388. (obstack_grow ((h), (where), (length)), obstack_finish ((h)))
  389. # define obstack_copy0(h,where,length) \
  390. (obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
  391. # define obstack_finish(h) \
  392. ( ((h)->next_free == (h)->object_base \
  393. ? (((h)->maybe_empty_object = 1), 0) \
  394. : 0), \
  395. (h)->temp.tempptr = (h)->object_base, \
  396. (h)->next_free \
  397. = __PTR_ALIGN ((h)->object_base, (h)->next_free, \
  398. (h)->alignment_mask), \
  399. (((h)->next_free - (char *) (h)->chunk \
  400. > (h)->chunk_limit - (char *) (h)->chunk) \
  401. ? ((h)->next_free = (h)->chunk_limit) : 0), \
  402. (h)->object_base = (h)->next_free, \
  403. (h)->temp.tempptr)
  404. # define obstack_free(h,obj) \
  405. ( (h)->temp.tempint = (char *) (obj) - (char *) (h)->chunk, \
  406. ((((h)->temp.tempint > 0 \
  407. && (h)->temp.tempint < (h)->chunk_limit - (char *) (h)->chunk)) \
  408. ? (int) ((h)->next_free = (h)->object_base \
  409. = (h)->temp.tempint + (char *) (h)->chunk) \
  410. : (((obstack_free) ((h), (h)->temp.tempint + (char *) (h)->chunk), 0), 0)))
  411. #endif /* not __GNUC__ or not __STDC__ */
  412. #ifdef __cplusplus
  413. } /* C++ */
  414. #endif
  415. #endif /* obstack.h */