obstack.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /* obstack.c - subroutines used implicitly by object stack macros
  2. Copyright (C) 1988-2019 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. <http://www.gnu.org/licenses/>. */
  15. #ifdef _LIBC
  16. # include <obstack.h>
  17. # include <shlib-compat.h>
  18. #else
  19. # include <config.h>
  20. # include "obstack.h"
  21. #endif
  22. /* NOTE BEFORE MODIFYING THIS FILE: This version number must be
  23. incremented whenever callers compiled using an old obstack.h can no
  24. longer properly call the functions in this obstack.c. */
  25. #define OBSTACK_INTERFACE_VERSION 1
  26. /* Comment out all this code if we are using the GNU C Library, and are not
  27. actually compiling the library itself, and the installed library
  28. supports the same library interface we do. This code is part of the GNU
  29. C Library, but also included in many other GNU distributions. Compiling
  30. and linking in this code is a waste when using the GNU C library
  31. (especially if it is a shared library). Rather than having every GNU
  32. program understand 'configure --with-gnu-libc' and omit the object
  33. files, it is simpler to just do this in the source for each such file. */
  34. #include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */
  35. #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
  36. # include <gnu-versions.h>
  37. # if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION
  38. # define ELIDE_CODE
  39. # endif
  40. #endif
  41. #include <stddef.h>
  42. #ifndef ELIDE_CODE
  43. # include <stdint.h>
  44. /* Determine default alignment. */
  45. union fooround
  46. {
  47. uintmax_t i;
  48. long double d;
  49. void *p;
  50. };
  51. struct fooalign
  52. {
  53. char c;
  54. union fooround u;
  55. };
  56. /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
  57. But in fact it might be less smart and round addresses to as much as
  58. DEFAULT_ROUNDING. So we prepare for it to do that. */
  59. enum
  60. {
  61. DEFAULT_ALIGNMENT = offsetof (struct fooalign, u),
  62. DEFAULT_ROUNDING = sizeof (union fooround)
  63. };
  64. /* When we copy a long block of data, this is the unit to do it with.
  65. On some machines, copying successive ints does not work;
  66. in such a case, redefine COPYING_UNIT to 'long' (if that works)
  67. or 'char' as a last resort. */
  68. # ifndef COPYING_UNIT
  69. # define COPYING_UNIT int
  70. # endif
  71. /* The functions allocating more room by calling 'obstack_chunk_alloc'
  72. jump to the handler pointed to by 'obstack_alloc_failed_handler'.
  73. This can be set to a user defined function which should either
  74. abort gracefully or use longjump - but shouldn't return. This
  75. variable by default points to the internal function
  76. 'print_and_abort'. */
  77. static _Noreturn void print_and_abort (void);
  78. void (*obstack_alloc_failed_handler) (void) = print_and_abort;
  79. /* Exit value used when 'print_and_abort' is used. */
  80. # include <stdlib.h>
  81. # ifdef _LIBC
  82. int obstack_exit_failure = EXIT_FAILURE;
  83. # else
  84. # include "exitfail.h"
  85. # define obstack_exit_failure exit_failure
  86. # endif
  87. # ifdef _LIBC
  88. # if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3_4)
  89. /* A looong time ago (before 1994, anyway; we're not sure) this global variable
  90. was used by non-GNU-C macros to avoid multiple evaluation. The GNU C
  91. library still exports it because somebody might use it. */
  92. struct obstack *_obstack_compat = 0;
  93. compat_symbol (libc, _obstack_compat, _obstack, GLIBC_2_0);
  94. # endif
  95. # endif
  96. /* Define a macro that either calls functions with the traditional malloc/free
  97. calling interface, or calls functions with the mmalloc/mfree interface
  98. (that adds an extra first argument), based on the state of use_extra_arg.
  99. For free, do not use ?:, since some compilers, like the MIPS compilers,
  100. do not allow (expr) ? void : void. */
  101. # define CALL_CHUNKFUN(h, size) \
  102. (((h)->use_extra_arg) \
  103. ? (*(h)->chunkfun)((h)->extra_arg, (size)) \
  104. : (*(struct _obstack_chunk *(*)(long))(h)->chunkfun)((size)))
  105. # define CALL_FREEFUN(h, old_chunk) \
  106. do { \
  107. if ((h)->use_extra_arg) \
  108. (*(h)->freefun)((h)->extra_arg, (old_chunk)); \
  109. else \
  110. (*(void (*)(void *))(h)->freefun)((old_chunk)); \
  111. } while (0)
  112. /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
  113. Objects start on multiples of ALIGNMENT (0 means use default).
  114. CHUNKFUN is the function to use to allocate chunks,
  115. and FREEFUN the function to free them.
  116. Return nonzero if successful, calls obstack_alloc_failed_handler if
  117. allocation fails. */
  118. int
  119. _obstack_begin (struct obstack *h,
  120. int size, int alignment,
  121. void *(*chunkfun) (long),
  122. void (*freefun) (void *))
  123. {
  124. struct _obstack_chunk *chunk; /* points to new chunk */
  125. if (alignment == 0)
  126. alignment = DEFAULT_ALIGNMENT;
  127. if (size == 0)
  128. /* Default size is what GNU malloc can fit in a 4096-byte block. */
  129. {
  130. /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
  131. Use the values for range checking, because if range checking is off,
  132. the extra bytes won't be missed terribly, but if range checking is on
  133. and we used a larger request, a whole extra 4096 bytes would be
  134. allocated.
  135. These number are irrelevant to the new GNU malloc. I suspect it is
  136. less sensitive to the size of the request. */
  137. int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
  138. + 4 + DEFAULT_ROUNDING - 1)
  139. & ~(DEFAULT_ROUNDING - 1));
  140. size = 4096 - extra;
  141. }
  142. h->chunkfun = (struct _obstack_chunk * (*) (void *, long)) chunkfun;
  143. h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
  144. h->chunk_size = size;
  145. h->alignment_mask = alignment - 1;
  146. h->use_extra_arg = 0;
  147. chunk = h->chunk = CALL_CHUNKFUN (h, h->chunk_size);
  148. if (!chunk)
  149. (*obstack_alloc_failed_handler) ();
  150. h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
  151. alignment - 1);
  152. h->chunk_limit = chunk->limit
  153. = (char *) chunk + h->chunk_size;
  154. chunk->prev = 0;
  155. /* The initial chunk now contains no empty object. */
  156. h->maybe_empty_object = 0;
  157. h->alloc_failed = 0;
  158. return 1;
  159. }
  160. int
  161. _obstack_begin_1 (struct obstack *h, int size, int alignment,
  162. void *(*chunkfun) (void *, long),
  163. void (*freefun) (void *, void *),
  164. void *arg)
  165. {
  166. struct _obstack_chunk *chunk; /* points to new chunk */
  167. if (alignment == 0)
  168. alignment = DEFAULT_ALIGNMENT;
  169. if (size == 0)
  170. /* Default size is what GNU malloc can fit in a 4096-byte block. */
  171. {
  172. /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
  173. Use the values for range checking, because if range checking is off,
  174. the extra bytes won't be missed terribly, but if range checking is on
  175. and we used a larger request, a whole extra 4096 bytes would be
  176. allocated.
  177. These number are irrelevant to the new GNU malloc. I suspect it is
  178. less sensitive to the size of the request. */
  179. int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
  180. + 4 + DEFAULT_ROUNDING - 1)
  181. & ~(DEFAULT_ROUNDING - 1));
  182. size = 4096 - extra;
  183. }
  184. h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun;
  185. h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
  186. h->chunk_size = size;
  187. h->alignment_mask = alignment - 1;
  188. h->extra_arg = arg;
  189. h->use_extra_arg = 1;
  190. chunk = h->chunk = CALL_CHUNKFUN (h, h->chunk_size);
  191. if (!chunk)
  192. (*obstack_alloc_failed_handler) ();
  193. h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
  194. alignment - 1);
  195. h->chunk_limit = chunk->limit
  196. = (char *) chunk + h->chunk_size;
  197. chunk->prev = 0;
  198. /* The initial chunk now contains no empty object. */
  199. h->maybe_empty_object = 0;
  200. h->alloc_failed = 0;
  201. return 1;
  202. }
  203. /* Allocate a new current chunk for the obstack *H
  204. on the assumption that LENGTH bytes need to be added
  205. to the current object, or a new object of length LENGTH allocated.
  206. Copies any partial object from the end of the old chunk
  207. to the beginning of the new one. */
  208. void
  209. _obstack_newchunk (struct obstack *h, int length)
  210. {
  211. struct _obstack_chunk *old_chunk = h->chunk;
  212. struct _obstack_chunk *new_chunk;
  213. long new_size;
  214. long obj_size = h->next_free - h->object_base;
  215. long i;
  216. long already;
  217. char *object_base;
  218. /* Compute size for new chunk. */
  219. new_size = (obj_size + length) + (obj_size >> 3) + h->alignment_mask + 100;
  220. if (new_size < h->chunk_size)
  221. new_size = h->chunk_size;
  222. /* Allocate and initialize the new chunk. */
  223. new_chunk = CALL_CHUNKFUN (h, new_size);
  224. if (!new_chunk)
  225. (*obstack_alloc_failed_handler)();
  226. h->chunk = new_chunk;
  227. new_chunk->prev = old_chunk;
  228. new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
  229. /* Compute an aligned object_base in the new chunk */
  230. object_base =
  231. __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask);
  232. /* Move the existing object to the new chunk.
  233. Word at a time is fast and is safe if the object
  234. is sufficiently aligned. */
  235. if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
  236. {
  237. for (i = obj_size / sizeof (COPYING_UNIT) - 1;
  238. i >= 0; i--)
  239. ((COPYING_UNIT *) object_base)[i]
  240. = ((COPYING_UNIT *) h->object_base)[i];
  241. /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
  242. but that can cross a page boundary on a machine
  243. which does not do strict alignment for COPYING_UNITS. */
  244. already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
  245. }
  246. else
  247. already = 0;
  248. /* Copy remaining bytes one by one. */
  249. for (i = already; i < obj_size; i++)
  250. object_base[i] = h->object_base[i];
  251. /* If the object just copied was the only data in OLD_CHUNK,
  252. free that chunk and remove it from the chain.
  253. But not if that chunk might contain an empty object. */
  254. if (!h->maybe_empty_object
  255. && (h->object_base
  256. == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents,
  257. h->alignment_mask)))
  258. {
  259. new_chunk->prev = old_chunk->prev;
  260. CALL_FREEFUN (h, old_chunk);
  261. }
  262. h->object_base = object_base;
  263. h->next_free = h->object_base + obj_size;
  264. /* The new chunk certainly contains no empty object yet. */
  265. h->maybe_empty_object = 0;
  266. }
  267. # ifdef _LIBC
  268. libc_hidden_def (_obstack_newchunk)
  269. # endif
  270. /* Return nonzero if object OBJ has been allocated from obstack H.
  271. This is here for debugging.
  272. If you use it in a program, you are probably losing. */
  273. /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
  274. obstack.h because it is just for debugging. */
  275. int _obstack_allocated_p (struct obstack *h, void *obj) __attribute_pure__;
  276. int
  277. _obstack_allocated_p (struct obstack *h, void *obj)
  278. {
  279. struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
  280. struct _obstack_chunk *plp; /* point to previous chunk if any */
  281. lp = (h)->chunk;
  282. /* We use >= rather than > since the object cannot be exactly at
  283. the beginning of the chunk but might be an empty object exactly
  284. at the end of an adjacent chunk. */
  285. while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
  286. {
  287. plp = lp->prev;
  288. lp = plp;
  289. }
  290. return lp != 0;
  291. }
  292. /* Free objects in obstack H, including OBJ and everything allocate
  293. more recently than OBJ. If OBJ is zero, free everything in H. */
  294. # undef obstack_free
  295. void
  296. __obstack_free (struct obstack *h, void *obj)
  297. {
  298. struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
  299. struct _obstack_chunk *plp; /* point to previous chunk if any */
  300. lp = h->chunk;
  301. /* We use >= because there cannot be an object at the beginning of a chunk.
  302. But there can be an empty object at that address
  303. at the end of another chunk. */
  304. while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
  305. {
  306. plp = lp->prev;
  307. CALL_FREEFUN (h, lp);
  308. lp = plp;
  309. /* If we switch chunks, we can't tell whether the new current
  310. chunk contains an empty object, so assume that it may. */
  311. h->maybe_empty_object = 1;
  312. }
  313. if (lp)
  314. {
  315. h->object_base = h->next_free = (char *) (obj);
  316. h->chunk_limit = lp->limit;
  317. h->chunk = lp;
  318. }
  319. else if (obj != 0)
  320. /* obj is not in any of the chunks! */
  321. abort ();
  322. }
  323. # ifdef _LIBC
  324. /* Older versions of libc used a function _obstack_free intended to be
  325. called by non-GCC compilers. */
  326. strong_alias (obstack_free, _obstack_free)
  327. # endif
  328. int
  329. _obstack_memory_used (struct obstack *h)
  330. {
  331. struct _obstack_chunk *lp;
  332. int nbytes = 0;
  333. for (lp = h->chunk; lp != 0; lp = lp->prev)
  334. {
  335. nbytes += lp->limit - (char *) lp;
  336. }
  337. return nbytes;
  338. }
  339. /* Define the error handler. */
  340. # ifdef _LIBC
  341. # include <libintl.h>
  342. # else
  343. # include "gettext.h"
  344. # endif
  345. # ifndef _
  346. # define _(msgid) gettext (msgid)
  347. # endif
  348. # ifdef _LIBC
  349. # include <libio/iolibio.h>
  350. # endif
  351. static _Noreturn void
  352. print_and_abort (void)
  353. {
  354. /* Don't change any of these strings. Yes, it would be possible to add
  355. the newline to the string and use fputs or so. But this must not
  356. happen because the "memory exhausted" message appears in other places
  357. like this and the translation should be reused instead of creating
  358. a very similar string which requires a separate translation. */
  359. # ifdef _LIBC
  360. (void) __fxprintf (NULL, "%s\n", _("memory exhausted"));
  361. # else
  362. fprintf (stderr, "%s\n", _("memory exhausted"));
  363. # endif
  364. exit (obstack_exit_failure);
  365. }
  366. #endif /* !ELIDE_CODE */