obstack.c 15 KB

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