obstack.c 14 KB

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