obstack.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /* obstack.c - subroutines used implicitly by object stack macros
  2. Copyright (C) 1988-2022 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. #else
  18. # include <config.h>
  19. # include "obstack.h"
  20. #endif
  21. /* NOTE BEFORE MODIFYING THIS FILE: _OBSTACK_INTERFACE_VERSION in
  22. obstack.h must be incremented whenever callers compiled using an old
  23. obstack.h can no longer properly call the functions in this file. */
  24. /* Comment out all this code if we are using the GNU C Library, and are not
  25. actually compiling the library itself, and the installed library
  26. supports the same library interface we do. This code is part of the GNU
  27. C Library, but also included in many other GNU distributions. Compiling
  28. and linking in this code is a waste when using the GNU C library
  29. (especially if it is a shared library). Rather than having every GNU
  30. program understand 'configure --with-gnu-libc' and omit the object
  31. files, it is simpler to just do this in the source for each such file. */
  32. #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
  33. # include <gnu-versions.h>
  34. # if (_GNU_OBSTACK_INTERFACE_VERSION == _OBSTACK_INTERFACE_VERSION \
  35. || (_GNU_OBSTACK_INTERFACE_VERSION == 1 \
  36. && _OBSTACK_INTERFACE_VERSION == 2 \
  37. && defined SIZEOF_INT && defined SIZEOF_SIZE_T \
  38. && SIZEOF_INT == SIZEOF_SIZE_T))
  39. # define _OBSTACK_ELIDE_CODE
  40. # endif
  41. #endif
  42. #ifndef _OBSTACK_ELIDE_CODE
  43. /* If GCC, or if an oddball (testing?) host that #defines __alignof__,
  44. use the already-supplied __alignof__. Otherwise, this must be Gnulib
  45. (as glibc assumes GCC); defer to Gnulib's alignof_type. */
  46. # if !defined __GNUC__ && !defined __IBM__ALIGNOF__ && !defined __alignof__
  47. # if defined __cplusplus
  48. template <class type> struct alignof_helper { char __slot1; type __slot2; };
  49. # define __alignof__(type) offsetof (alignof_helper<type>, __slot2)
  50. # else
  51. # define __alignof__(type) \
  52. offsetof (struct { char __slot1; type __slot2; }, __slot2)
  53. # endif
  54. # endif
  55. # include <stdlib.h>
  56. # include <stdint.h>
  57. # ifndef MAX
  58. # define MAX(a,b) ((a) > (b) ? (a) : (b))
  59. # endif
  60. /* Determine default alignment. */
  61. /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
  62. But in fact it might be less smart and round addresses to as much as
  63. DEFAULT_ROUNDING. So we prepare for it to do that.
  64. DEFAULT_ALIGNMENT cannot be an enum constant; see gnulib's alignof.h. */
  65. #define DEFAULT_ALIGNMENT MAX (__alignof__ (long double), \
  66. MAX (__alignof__ (uintmax_t), \
  67. __alignof__ (void *)))
  68. #define DEFAULT_ROUNDING MAX (sizeof (long double), \
  69. MAX (sizeof (uintmax_t), \
  70. sizeof (void *)))
  71. /* Call functions with either the traditional malloc/free calling
  72. interface, or the mmalloc/mfree interface (that adds an extra first
  73. argument), based on the value of use_extra_arg. */
  74. static void *
  75. call_chunkfun (struct obstack *h, size_t size)
  76. {
  77. if (h->use_extra_arg)
  78. return h->chunkfun.extra (h->extra_arg, size);
  79. else
  80. return h->chunkfun.plain (size);
  81. }
  82. static void
  83. call_freefun (struct obstack *h, void *old_chunk)
  84. {
  85. if (h->use_extra_arg)
  86. h->freefun.extra (h->extra_arg, old_chunk);
  87. else
  88. h->freefun.plain (old_chunk);
  89. }
  90. /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
  91. Objects start on multiples of ALIGNMENT (0 means use default).
  92. Return nonzero if successful, calls obstack_alloc_failed_handler if
  93. allocation fails. */
  94. static int
  95. _obstack_begin_worker (struct obstack *h,
  96. _OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment)
  97. {
  98. struct _obstack_chunk *chunk; /* points to new chunk */
  99. if (alignment == 0)
  100. alignment = DEFAULT_ALIGNMENT;
  101. if (size == 0)
  102. /* Default size is what GNU malloc can fit in a 4096-byte block. */
  103. {
  104. /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
  105. Use the values for range checking, because if range checking is off,
  106. the extra bytes won't be missed terribly, but if range checking is on
  107. and we used a larger request, a whole extra 4096 bytes would be
  108. allocated.
  109. These number are irrelevant to the new GNU malloc. I suspect it is
  110. less sensitive to the size of the request. */
  111. int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
  112. + 4 + DEFAULT_ROUNDING - 1)
  113. & ~(DEFAULT_ROUNDING - 1));
  114. size = 4096 - extra;
  115. }
  116. h->chunk_size = size;
  117. h->alignment_mask = alignment - 1;
  118. chunk = (struct _obstack_chunk *) call_chunkfun (h, h->chunk_size);
  119. if (!chunk)
  120. (*obstack_alloc_failed_handler) ();
  121. h->chunk = chunk;
  122. h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
  123. alignment - 1);
  124. h->chunk_limit = chunk->limit = (char *) chunk + h->chunk_size;
  125. chunk->prev = 0;
  126. /* The initial chunk now contains no empty object. */
  127. h->maybe_empty_object = 0;
  128. h->alloc_failed = 0;
  129. return 1;
  130. }
  131. int
  132. _obstack_begin (struct obstack *h,
  133. _OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment,
  134. void *(*chunkfun) (size_t),
  135. void (*freefun) (void *))
  136. {
  137. h->chunkfun.plain = chunkfun;
  138. h->freefun.plain = freefun;
  139. h->use_extra_arg = 0;
  140. return _obstack_begin_worker (h, size, alignment);
  141. }
  142. int
  143. _obstack_begin_1 (struct obstack *h,
  144. _OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment,
  145. void *(*chunkfun) (void *, size_t),
  146. void (*freefun) (void *, void *),
  147. void *arg)
  148. {
  149. h->chunkfun.extra = chunkfun;
  150. h->freefun.extra = freefun;
  151. h->extra_arg = arg;
  152. h->use_extra_arg = 1;
  153. return _obstack_begin_worker (h, size, alignment);
  154. }
  155. /* Allocate a new current chunk for the obstack *H
  156. on the assumption that LENGTH bytes need to be added
  157. to the current object, or a new object of length LENGTH allocated.
  158. Copies any partial object from the end of the old chunk
  159. to the beginning of the new one. */
  160. void
  161. _obstack_newchunk (struct obstack *h, _OBSTACK_SIZE_T length)
  162. {
  163. struct _obstack_chunk *old_chunk = h->chunk;
  164. struct _obstack_chunk *new_chunk = 0;
  165. size_t obj_size = h->next_free - h->object_base;
  166. char *object_base;
  167. /* Compute size for new chunk. */
  168. size_t sum1 = obj_size + length;
  169. size_t sum2 = sum1 + h->alignment_mask;
  170. size_t new_size = sum2 + (obj_size >> 3) + 100;
  171. if (new_size < sum2)
  172. new_size = sum2;
  173. if (new_size < h->chunk_size)
  174. new_size = h->chunk_size;
  175. /* Allocate and initialize the new chunk. */
  176. if (obj_size <= sum1 && sum1 <= sum2)
  177. new_chunk = (struct _obstack_chunk *) call_chunkfun (h, new_size);
  178. if (!new_chunk)
  179. (*obstack_alloc_failed_handler)();
  180. h->chunk = new_chunk;
  181. new_chunk->prev = old_chunk;
  182. new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
  183. /* Compute an aligned object_base in the new chunk */
  184. object_base =
  185. __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask);
  186. /* Move the existing object to the new chunk. */
  187. memcpy (object_base, h->object_base, obj_size);
  188. /* If the object just copied was the only data in OLD_CHUNK,
  189. free that chunk and remove it from the chain.
  190. But not if that chunk might contain an empty object. */
  191. if (!h->maybe_empty_object
  192. && (h->object_base
  193. == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents,
  194. h->alignment_mask)))
  195. {
  196. new_chunk->prev = old_chunk->prev;
  197. call_freefun (h, old_chunk);
  198. }
  199. h->object_base = object_base;
  200. h->next_free = h->object_base + obj_size;
  201. /* The new chunk certainly contains no empty object yet. */
  202. h->maybe_empty_object = 0;
  203. }
  204. /* Return nonzero if object OBJ has been allocated from obstack H.
  205. This is here for debugging.
  206. If you use it in a program, you are probably losing. */
  207. /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
  208. obstack.h because it is just for debugging. */
  209. int _obstack_allocated_p (struct obstack *h, void *obj) __attribute_pure__;
  210. int
  211. _obstack_allocated_p (struct obstack *h, void *obj)
  212. {
  213. struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
  214. struct _obstack_chunk *plp; /* point to previous chunk if any */
  215. lp = (h)->chunk;
  216. /* We use >= rather than > since the object cannot be exactly at
  217. the beginning of the chunk but might be an empty object exactly
  218. at the end of an adjacent chunk. */
  219. while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
  220. {
  221. plp = lp->prev;
  222. lp = plp;
  223. }
  224. return lp != 0;
  225. }
  226. /* Free objects in obstack H, including OBJ and everything allocate
  227. more recently than OBJ. If OBJ is zero, free everything in H. */
  228. void
  229. _obstack_free (struct obstack *h, void *obj)
  230. {
  231. struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
  232. struct _obstack_chunk *plp; /* point to previous chunk if any */
  233. lp = h->chunk;
  234. /* We use >= because there cannot be an object at the beginning of a chunk.
  235. But there can be an empty object at that address
  236. at the end of another chunk. */
  237. while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
  238. {
  239. plp = lp->prev;
  240. call_freefun (h, lp);
  241. lp = plp;
  242. /* If we switch chunks, we can't tell whether the new current
  243. chunk contains an empty object, so assume that it may. */
  244. h->maybe_empty_object = 1;
  245. }
  246. if (lp)
  247. {
  248. h->object_base = h->next_free = (char *) (obj);
  249. h->chunk_limit = lp->limit;
  250. h->chunk = lp;
  251. }
  252. else if (obj != 0)
  253. /* obj is not in any of the chunks! */
  254. abort ();
  255. }
  256. _OBSTACK_SIZE_T
  257. _obstack_memory_used (struct obstack *h)
  258. {
  259. struct _obstack_chunk *lp;
  260. _OBSTACK_SIZE_T nbytes = 0;
  261. for (lp = h->chunk; lp != 0; lp = lp->prev)
  262. {
  263. nbytes += lp->limit - (char *) lp;
  264. }
  265. return nbytes;
  266. }
  267. # ifndef _OBSTACK_NO_ERROR_HANDLER
  268. /* Define the error handler. */
  269. # include <stdio.h>
  270. /* Exit value used when 'print_and_abort' is used. */
  271. # ifdef _LIBC
  272. int obstack_exit_failure = EXIT_FAILURE;
  273. # else
  274. # ifndef EXIT_FAILURE
  275. # define EXIT_FAILURE 1
  276. # endif
  277. # define obstack_exit_failure EXIT_FAILURE
  278. # endif
  279. # if defined _LIBC || (HAVE_LIBINTL_H && ENABLE_NLS)
  280. # include <libintl.h>
  281. # ifndef _
  282. # define _(msgid) gettext (msgid)
  283. # endif
  284. # else
  285. # ifndef _
  286. # define _(msgid) (msgid)
  287. # endif
  288. # endif
  289. # if !(defined _Noreturn \
  290. || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112))
  291. # if ((defined __GNUC__ \
  292. && (__GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8))) \
  293. || (defined __SUNPRO_C && __SUNPRO_C >= 0x5110))
  294. # define _Noreturn __attribute__ ((__noreturn__))
  295. # elif defined _MSC_VER && _MSC_VER >= 1200
  296. # define _Noreturn __declspec (noreturn)
  297. # else
  298. # define _Noreturn
  299. # endif
  300. # endif
  301. # ifdef _LIBC
  302. # include <libio/iolibio.h>
  303. # endif
  304. static _Noreturn void
  305. print_and_abort (void)
  306. {
  307. /* Don't change any of these strings. Yes, it would be possible to add
  308. the newline to the string and use fputs or so. But this must not
  309. happen because the "memory exhausted" message appears in other places
  310. like this and the translation should be reused instead of creating
  311. a very similar string which requires a separate translation. */
  312. # ifdef _LIBC
  313. (void) __fxprintf (NULL, "%s\n", _("memory exhausted"));
  314. # else
  315. fprintf (stderr, "%s\n", _("memory exhausted"));
  316. # endif
  317. exit (obstack_exit_failure);
  318. }
  319. /* The functions allocating more room by calling 'obstack_chunk_alloc'
  320. jump to the handler pointed to by 'obstack_alloc_failed_handler'.
  321. This can be set to a user defined function which should either
  322. abort gracefully or use longjump - but shouldn't return. This
  323. variable by default points to the internal function
  324. 'print_and_abort'. */
  325. void (*obstack_alloc_failed_handler) (void) = print_and_abort;
  326. # endif /* !_OBSTACK_NO_ERROR_HANDLER */
  327. #endif /* !_OBSTACK_ELIDE_CODE */