obstack.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /* obstack.h - object stack macros
  2. Copyright (C) 1988-2020 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://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. #ifndef __attribute_pure__
  112. # define __attribute_pure__ _GL_ATTRIBUTE_PURE
  113. #endif
  114. #ifdef __cplusplus
  115. extern "C" {
  116. #endif
  117. struct _obstack_chunk /* Lives at front of each chunk. */
  118. {
  119. char *limit; /* 1 past end of this chunk */
  120. struct _obstack_chunk *prev; /* address of prior chunk or NULL */
  121. char contents[4]; /* objects begin here */
  122. };
  123. struct obstack /* control current object in current chunk */
  124. {
  125. long chunk_size; /* preferred size to allocate chunks in */
  126. struct _obstack_chunk *chunk; /* address of current struct obstack_chunk */
  127. char *object_base; /* address of object we are building */
  128. char *next_free; /* where to add next char to current object */
  129. char *chunk_limit; /* address of char after current chunk */
  130. union
  131. {
  132. PTR_INT_TYPE tempint;
  133. void *tempptr;
  134. } temp; /* Temporary for some macros. */
  135. int alignment_mask; /* Mask of alignment for each object. */
  136. /* These prototypes vary based on 'use_extra_arg', and we use
  137. casts to the prototypeless function type in all assignments,
  138. but having prototypes here quiets -Wstrict-prototypes. */
  139. struct _obstack_chunk *(*chunkfun) (void *, long);
  140. void (*freefun) (void *, struct _obstack_chunk *);
  141. void *extra_arg; /* first arg for chunk alloc/dealloc funcs */
  142. unsigned use_extra_arg : 1; /* chunk alloc/dealloc funcs take extra arg */
  143. unsigned maybe_empty_object : 1; /* There is a possibility that the current
  144. chunk contains a zero-length object. This
  145. prevents freeing the chunk if we allocate
  146. a bigger chunk to replace it. */
  147. unsigned alloc_failed : 1; /* No longer used, as we now call the failed
  148. handler on error, but retained for binary
  149. compatibility. */
  150. };
  151. /* Declare the external functions we use; they are in obstack.c. */
  152. extern void _obstack_newchunk (struct obstack *, int);
  153. extern int _obstack_begin (struct obstack *, int, int,
  154. void *(*)(long), void (*)(void *));
  155. extern int _obstack_begin_1 (struct obstack *, int, int,
  156. void *(*)(void *, long),
  157. void (*)(void *, void *), void *);
  158. extern int _obstack_memory_used (struct obstack *) __attribute_pure__;
  159. /* The default name of the function for freeing a chunk is 'obstack_free',
  160. but gnulib users can override this by defining '__obstack_free'. */
  161. #ifndef __obstack_free
  162. # define __obstack_free obstack_free
  163. #endif
  164. extern void __obstack_free (struct obstack *, void *);
  165. /* Error handler called when 'obstack_chunk_alloc' failed to allocate
  166. more memory. This can be set to a user defined function which
  167. should either abort gracefully or use longjump - but shouldn't
  168. return. The default action is to print a message and abort. */
  169. extern void (*obstack_alloc_failed_handler) (void);
  170. /* Exit value used when 'print_and_abort' is used. */
  171. extern int obstack_exit_failure;
  172. /* Pointer to beginning of object being allocated or to be allocated next.
  173. Note that this might not be the final address of the object
  174. because a new chunk might be needed to hold the final size. */
  175. #define obstack_base(h) ((void *) (h)->object_base)
  176. /* Size for allocating ordinary chunks. */
  177. #define obstack_chunk_size(h) ((h)->chunk_size)
  178. /* Pointer to next byte not yet allocated in current chunk. */
  179. #define obstack_next_free(h) ((h)->next_free)
  180. /* Mask specifying low bits that should be clear in address of an object. */
  181. #define obstack_alignment_mask(h) ((h)->alignment_mask)
  182. /* To prevent prototype warnings provide complete argument list. */
  183. #define obstack_init(h) \
  184. _obstack_begin ((h), 0, 0, \
  185. (void *(*)(long))obstack_chunk_alloc, \
  186. (void (*)(void *))obstack_chunk_free)
  187. #define obstack_begin(h, size) \
  188. _obstack_begin ((h), (size), 0, \
  189. (void *(*)(long))obstack_chunk_alloc, \
  190. (void (*)(void *))obstack_chunk_free)
  191. #define obstack_specify_allocation(h, size, alignment, chunkfun, freefun) \
  192. _obstack_begin ((h), (size), (alignment), \
  193. (void *(*)(long))(chunkfun), \
  194. (void (*)(void *))(freefun))
  195. #define obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) \
  196. _obstack_begin_1 ((h), (size), (alignment), \
  197. (void *(*)(void *, long))(chunkfun), \
  198. (void (*)(void *, void *))(freefun), (arg))
  199. #define obstack_chunkfun(h, newchunkfun) \
  200. ((h)->chunkfun = (struct _obstack_chunk *(*)(void *, long))(newchunkfun))
  201. #define obstack_freefun(h, newfreefun) \
  202. ((h)->freefun = (void (*)(void *, struct _obstack_chunk *))(newfreefun))
  203. #define obstack_1grow_fast(h, achar) (*((h)->next_free)++ = (achar))
  204. #define obstack_blank_fast(h, n) ((h)->next_free += (n))
  205. #define obstack_memory_used(h) _obstack_memory_used (h)
  206. #if defined __GNUC__
  207. # if ! (2 < __GNUC__ + (8 <= __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. ? (void) ((h)->next_free = (h)->object_base \
  414. = (h)->temp.tempint + (char *) (h)->chunk) \
  415. : (__obstack_free) (h, (h)->temp.tempint + (char *) (h)->chunk)))
  416. #endif /* not __GNUC__ */
  417. #ifdef __cplusplus
  418. } /* C++ */
  419. #endif
  420. #endif /* obstack.h */