obstack.c 14 KB

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