obstack.c 14 KB

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