obstack.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /* obstack.c - subroutines used implicitly by 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 General Public
  6. License as published by the Free Software Foundation; either
  7. version 3 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public
  13. License along with the GNU C Library; if not, see
  14. <https://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. # include <stdlib.h>
  47. # include <stdint.h>
  48. # ifndef MAX
  49. # define MAX(a,b) ((a) > (b) ? (a) : (b))
  50. # endif
  51. /* Determine default alignment. */
  52. /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
  53. But in fact it might be less smart and round addresses to as much as
  54. DEFAULT_ROUNDING. So we prepare for it to do that.
  55. DEFAULT_ALIGNMENT cannot be an enum constant; see gnulib's alignof.h. */
  56. #define DEFAULT_ALIGNMENT MAX (__alignof__ (long double), \
  57. MAX (__alignof__ (uintmax_t), \
  58. __alignof__ (void *)))
  59. #define DEFAULT_ROUNDING MAX (sizeof (long double), \
  60. MAX (sizeof (uintmax_t), \
  61. sizeof (void *)))
  62. /* Call functions with either the traditional malloc/free calling
  63. interface, or the mmalloc/mfree interface (that adds an extra first
  64. argument), based on the value of use_extra_arg. */
  65. static void *
  66. call_chunkfun (struct obstack *h, size_t size)
  67. {
  68. if (h->use_extra_arg)
  69. return h->chunkfun.extra (h->extra_arg, size);
  70. else
  71. return h->chunkfun.plain (size);
  72. }
  73. static void
  74. call_freefun (struct obstack *h, void *old_chunk)
  75. {
  76. if (h->use_extra_arg)
  77. h->freefun.extra (h->extra_arg, old_chunk);
  78. else
  79. h->freefun.plain (old_chunk);
  80. }
  81. /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
  82. Objects start on multiples of ALIGNMENT (0 means use default).
  83. Return nonzero if successful, calls obstack_alloc_failed_handler if
  84. allocation fails. */
  85. static int
  86. _obstack_begin_worker (struct obstack *h,
  87. _OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment)
  88. {
  89. struct _obstack_chunk *chunk; /* points to new chunk */
  90. if (alignment == 0)
  91. alignment = DEFAULT_ALIGNMENT;
  92. if (size == 0)
  93. /* Default size is what GNU malloc can fit in a 4096-byte block. */
  94. {
  95. /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
  96. Use the values for range checking, because if range checking is off,
  97. the extra bytes won't be missed terribly, but if range checking is on
  98. and we used a larger request, a whole extra 4096 bytes would be
  99. allocated.
  100. These number are irrelevant to the new GNU malloc. I suspect it is
  101. less sensitive to the size of the request. */
  102. int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
  103. + 4 + DEFAULT_ROUNDING - 1)
  104. & ~(DEFAULT_ROUNDING - 1));
  105. size = 4096 - extra;
  106. }
  107. h->chunk_size = size;
  108. h->alignment_mask = alignment - 1;
  109. chunk = h->chunk = call_chunkfun (h, h->chunk_size);
  110. if (!chunk)
  111. (*obstack_alloc_failed_handler) ();
  112. h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
  113. alignment - 1);
  114. h->chunk_limit = chunk->limit = (char *) chunk + h->chunk_size;
  115. chunk->prev = 0;
  116. /* The initial chunk now contains no empty object. */
  117. h->maybe_empty_object = 0;
  118. h->alloc_failed = 0;
  119. return 1;
  120. }
  121. int
  122. _obstack_begin (struct obstack *h,
  123. _OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment,
  124. void *(*chunkfun) (size_t),
  125. void (*freefun) (void *))
  126. {
  127. h->chunkfun.plain = chunkfun;
  128. h->freefun.plain = freefun;
  129. h->use_extra_arg = 0;
  130. return _obstack_begin_worker (h, size, alignment);
  131. }
  132. int
  133. _obstack_begin_1 (struct obstack *h,
  134. _OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment,
  135. void *(*chunkfun) (void *, size_t),
  136. void (*freefun) (void *, void *),
  137. void *arg)
  138. {
  139. h->chunkfun.extra = chunkfun;
  140. h->freefun.extra = freefun;
  141. h->extra_arg = arg;
  142. h->use_extra_arg = 1;
  143. return _obstack_begin_worker (h, size, alignment);
  144. }
  145. /* Allocate a new current chunk for the obstack *H
  146. on the assumption that LENGTH bytes need to be added
  147. to the current object, or a new object of length LENGTH allocated.
  148. Copies any partial object from the end of the old chunk
  149. to the beginning of the new one. */
  150. void
  151. _obstack_newchunk (struct obstack *h, _OBSTACK_SIZE_T length)
  152. {
  153. struct _obstack_chunk *old_chunk = h->chunk;
  154. struct _obstack_chunk *new_chunk = 0;
  155. size_t obj_size = h->next_free - h->object_base;
  156. char *object_base;
  157. /* Compute size for new chunk. */
  158. size_t sum1 = obj_size + length;
  159. size_t sum2 = sum1 + h->alignment_mask;
  160. size_t new_size = sum2 + (obj_size >> 3) + 100;
  161. if (new_size < sum2)
  162. new_size = sum2;
  163. if (new_size < h->chunk_size)
  164. new_size = h->chunk_size;
  165. /* Allocate and initialize the new chunk. */
  166. if (obj_size <= sum1 && sum1 <= sum2)
  167. new_chunk = call_chunkfun (h, new_size);
  168. if (!new_chunk)
  169. (*obstack_alloc_failed_handler)();
  170. h->chunk = new_chunk;
  171. new_chunk->prev = old_chunk;
  172. new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
  173. /* Compute an aligned object_base in the new chunk */
  174. object_base =
  175. __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask);
  176. /* Move the existing object to the new chunk. */
  177. memcpy (object_base, h->object_base, obj_size);
  178. /* If the object just copied was the only data in OLD_CHUNK,
  179. free that chunk and remove it from the chain.
  180. But not if that chunk might contain an empty object. */
  181. if (!h->maybe_empty_object
  182. && (h->object_base
  183. == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents,
  184. h->alignment_mask)))
  185. {
  186. new_chunk->prev = old_chunk->prev;
  187. call_freefun (h, old_chunk);
  188. }
  189. h->object_base = object_base;
  190. h->next_free = h->object_base + obj_size;
  191. /* The new chunk certainly contains no empty object yet. */
  192. h->maybe_empty_object = 0;
  193. }
  194. /* Return nonzero if object OBJ has been allocated from obstack H.
  195. This is here for debugging.
  196. If you use it in a program, you are probably losing. */
  197. /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
  198. obstack.h because it is just for debugging. */
  199. int _obstack_allocated_p (struct obstack *h, void *obj) __attribute_pure__;
  200. int
  201. _obstack_allocated_p (struct obstack *h, void *obj)
  202. {
  203. struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
  204. struct _obstack_chunk *plp; /* point to previous chunk if any */
  205. lp = (h)->chunk;
  206. /* We use >= rather than > since the object cannot be exactly at
  207. the beginning of the chunk but might be an empty object exactly
  208. at the end of an adjacent chunk. */
  209. while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
  210. {
  211. plp = lp->prev;
  212. lp = plp;
  213. }
  214. return lp != 0;
  215. }
  216. /* Free objects in obstack H, including OBJ and everything allocate
  217. more recently than OBJ. If OBJ is zero, free everything in H. */
  218. void
  219. _obstack_free (struct obstack *h, void *obj)
  220. {
  221. struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
  222. struct _obstack_chunk *plp; /* point to previous chunk if any */
  223. lp = h->chunk;
  224. /* We use >= because there cannot be an object at the beginning of a chunk.
  225. But there can be an empty object at that address
  226. at the end of another chunk. */
  227. while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
  228. {
  229. plp = lp->prev;
  230. call_freefun (h, lp);
  231. lp = plp;
  232. /* If we switch chunks, we can't tell whether the new current
  233. chunk contains an empty object, so assume that it may. */
  234. h->maybe_empty_object = 1;
  235. }
  236. if (lp)
  237. {
  238. h->object_base = h->next_free = (char *) (obj);
  239. h->chunk_limit = lp->limit;
  240. h->chunk = lp;
  241. }
  242. else if (obj != 0)
  243. /* obj is not in any of the chunks! */
  244. abort ();
  245. }
  246. _OBSTACK_SIZE_T
  247. _obstack_memory_used (struct obstack *h)
  248. {
  249. struct _obstack_chunk *lp;
  250. _OBSTACK_SIZE_T nbytes = 0;
  251. for (lp = h->chunk; lp != 0; lp = lp->prev)
  252. {
  253. nbytes += lp->limit - (char *) lp;
  254. }
  255. return nbytes;
  256. }
  257. # ifndef _OBSTACK_NO_ERROR_HANDLER
  258. /* Define the error handler. */
  259. # include <stdio.h>
  260. /* Exit value used when 'print_and_abort' is used. */
  261. # ifdef _LIBC
  262. int obstack_exit_failure = EXIT_FAILURE;
  263. # else
  264. # include "exitfail.h"
  265. # define obstack_exit_failure exit_failure
  266. # endif
  267. # ifdef _LIBC
  268. # include <libintl.h>
  269. # else
  270. # include "gettext.h"
  271. # endif
  272. # ifndef _
  273. # define _(msgid) gettext (msgid)
  274. # endif
  275. # ifdef _LIBC
  276. # include <libio/iolibio.h>
  277. # endif
  278. static __attribute_noreturn__ void
  279. print_and_abort (void)
  280. {
  281. /* Don't change any of these strings. Yes, it would be possible to add
  282. the newline to the string and use fputs or so. But this must not
  283. happen because the "memory exhausted" message appears in other places
  284. like this and the translation should be reused instead of creating
  285. a very similar string which requires a separate translation. */
  286. # ifdef _LIBC
  287. (void) __fxprintf (NULL, "%s\n", _("memory exhausted"));
  288. # else
  289. fprintf (stderr, "%s\n", _("memory exhausted"));
  290. # endif
  291. exit (obstack_exit_failure);
  292. }
  293. /* The functions allocating more room by calling 'obstack_chunk_alloc'
  294. jump to the handler pointed to by 'obstack_alloc_failed_handler'.
  295. This can be set to a user defined function which should either
  296. abort gracefully or use longjump - but shouldn't return. This
  297. variable by default points to the internal function
  298. 'print_and_abort'. */
  299. __attribute_noreturn__ void (*obstack_alloc_failed_handler) (void)
  300. = print_and_abort;
  301. # endif /* !_OBSTACK_NO_ERROR_HANDLER */
  302. #endif /* !_OBSTACK_ELIDE_CODE */