obstack.h 25 KB

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