obstack.c 14 KB

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