obstack.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. #include "obstack.h"
  21. /* NOTE BEFORE MODIFYING THIS FILE: This version number must be
  22. incremented whenever callers compiled using an old obstack.h can no
  23. longer properly call the functions in this obstack.c. */
  24. #define OBSTACK_INTERFACE_VERSION 1
  25. #include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */
  26. #include <stddef.h>
  27. #include <stdint.h>
  28. /* Determine default alignment. */
  29. union fooround
  30. {
  31. uintmax_t i;
  32. long double d;
  33. void *p;
  34. };
  35. struct fooalign
  36. {
  37. char c;
  38. union fooround u;
  39. };
  40. /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
  41. But in fact it might be less smart and round addresses to as much as
  42. DEFAULT_ROUNDING. So we prepare for it to do that. */
  43. enum
  44. {
  45. DEFAULT_ALIGNMENT = offsetof (struct fooalign, u),
  46. DEFAULT_ROUNDING = sizeof (union fooround)
  47. };
  48. /* When we copy a long block of data, this is the unit to do it with.
  49. On some machines, copying successive ints does not work;
  50. in such a case, redefine COPYING_UNIT to `long' (if that works)
  51. or `char' as a last resort. */
  52. # ifndef COPYING_UNIT
  53. # define COPYING_UNIT int
  54. # endif
  55. /* The functions allocating more room by calling `obstack_chunk_alloc'
  56. jump to the handler pointed to by `obstack_alloc_failed_handler'.
  57. This can be set to a user defined function which should either
  58. abort gracefully or use longjump - but shouldn't return. This
  59. variable by default points to the internal function
  60. `print_and_abort'. */
  61. static void print_and_abort (void);
  62. void (*obstack_alloc_failed_handler) (void) = print_and_abort;
  63. /* Exit value used when `print_and_abort' is used. */
  64. # include <stdlib.h>
  65. int obstack_exit_failure = EXIT_FAILURE;
  66. /* Define a macro that either calls functions with the traditional malloc/free
  67. calling interface, or calls functions with the mmalloc/mfree interface
  68. (that adds an extra first argument), based on the state of use_extra_arg.
  69. For free, do not use ?:, since some compilers, like the MIPS compilers,
  70. do not allow (expr) ? void : void. */
  71. # define CALL_CHUNKFUN(h, size) \
  72. (((h) -> use_extra_arg) \
  73. ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
  74. : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
  75. # define CALL_FREEFUN(h, old_chunk) \
  76. do { \
  77. if ((h) -> use_extra_arg) \
  78. (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
  79. else \
  80. (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
  81. } while (0)
  82. /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
  83. Objects start on multiples of ALIGNMENT (0 means use default).
  84. CHUNKFUN is the function to use to allocate chunks,
  85. and FREEFUN the function to free them.
  86. Return nonzero if successful, calls obstack_alloc_failed_handler if
  87. allocation fails. */
  88. int
  89. _obstack_begin (struct obstack *h,
  90. int size, int alignment,
  91. void *(*chunkfun) (long),
  92. void (*freefun) (void *))
  93. {
  94. register struct _obstack_chunk *chunk; /* points to new chunk */
  95. if (alignment == 0)
  96. alignment = DEFAULT_ALIGNMENT;
  97. if (size == 0)
  98. /* Default size is what GNU malloc can fit in a 4096-byte block. */
  99. {
  100. /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
  101. Use the values for range checking, because if range checking is off,
  102. the extra bytes won't be missed terribly, but if range checking is on
  103. and we used a larger request, a whole extra 4096 bytes would be
  104. allocated.
  105. These number are irrelevant to the new GNU malloc. I suspect it is
  106. less sensitive to the size of the request. */
  107. int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
  108. + 4 + DEFAULT_ROUNDING - 1)
  109. & ~(DEFAULT_ROUNDING - 1));
  110. size = 4096 - extra;
  111. }
  112. h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun;
  113. h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
  114. h->chunk_size = size;
  115. h->alignment_mask = alignment - 1;
  116. h->use_extra_arg = 0;
  117. chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
  118. if (!chunk) {
  119. (*obstack_alloc_failed_handler) ();
  120. return 0;
  121. }
  122. h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
  123. alignment - 1);
  124. h->chunk_limit = chunk->limit
  125. = (char *) chunk + h->chunk_size;
  126. chunk->prev = 0;
  127. /* The initial chunk now contains no empty object. */
  128. h->maybe_empty_object = 0;
  129. h->alloc_failed = 0;
  130. return 1;
  131. }
  132. int
  133. _obstack_begin_1 (struct obstack *h, int size, int alignment,
  134. void *(*chunkfun) (void *, long),
  135. void (*freefun) (void *, void *),
  136. void *arg)
  137. {
  138. register struct _obstack_chunk *chunk; /* points to new chunk */
  139. if (alignment == 0)
  140. alignment = DEFAULT_ALIGNMENT;
  141. if (size == 0)
  142. /* Default size is what GNU malloc can fit in a 4096-byte block. */
  143. {
  144. /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
  145. Use the values for range checking, because if range checking is off,
  146. the extra bytes won't be missed terribly, but if range checking is on
  147. and we used a larger request, a whole extra 4096 bytes would be
  148. allocated.
  149. These number are irrelevant to the new GNU malloc. I suspect it is
  150. less sensitive to the size of the request. */
  151. int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
  152. + 4 + DEFAULT_ROUNDING - 1)
  153. & ~(DEFAULT_ROUNDING - 1));
  154. size = 4096 - extra;
  155. }
  156. h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun;
  157. h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
  158. h->chunk_size = size;
  159. h->alignment_mask = alignment - 1;
  160. h->extra_arg = arg;
  161. h->use_extra_arg = 1;
  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. /* Allocate a new current chunk for the obstack *H
  176. on the assumption that LENGTH bytes need to be added
  177. to the current object, or a new object of length LENGTH allocated.
  178. Copies any partial object from the end of the old chunk
  179. to the beginning of the new one. */
  180. void
  181. _obstack_newchunk (struct obstack *h, int length)
  182. {
  183. register struct _obstack_chunk *old_chunk = h->chunk;
  184. register struct _obstack_chunk *new_chunk;
  185. register long new_size;
  186. register long obj_size = h->next_free - h->object_base;
  187. register long i;
  188. long already;
  189. char *object_base;
  190. /* Compute size for new chunk. */
  191. new_size = (obj_size + length) + (obj_size >> 3) + h->alignment_mask + 100;
  192. if (new_size < h->chunk_size)
  193. new_size = h->chunk_size;
  194. /* Allocate and initialize the new chunk. */
  195. new_chunk = CALL_CHUNKFUN (h, new_size);
  196. if (!new_chunk) {
  197. (*obstack_alloc_failed_handler) ();
  198. return;
  199. }
  200. h->chunk = new_chunk;
  201. new_chunk->prev = old_chunk;
  202. new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
  203. /* Compute an aligned object_base in the new chunk */
  204. object_base =
  205. __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask);
  206. /* Move the existing object to the new chunk.
  207. Word at a time is fast and is safe if the object
  208. is sufficiently aligned. */
  209. if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
  210. {
  211. for (i = obj_size / sizeof (COPYING_UNIT) - 1;
  212. i >= 0; i--)
  213. ((COPYING_UNIT *)object_base)[i]
  214. = ((COPYING_UNIT *)h->object_base)[i];
  215. /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
  216. but that can cross a page boundary on a machine
  217. which does not do strict alignment for COPYING_UNITS. */
  218. already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
  219. }
  220. else
  221. already = 0;
  222. /* Copy remaining bytes one by one. */
  223. for (i = already; i < obj_size; i++)
  224. object_base[i] = h->object_base[i];
  225. /* If the object just copied was the only data in OLD_CHUNK,
  226. free that chunk and remove it from the chain.
  227. But not if that chunk might contain an empty object. */
  228. if (! h->maybe_empty_object
  229. && (h->object_base
  230. == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents,
  231. h->alignment_mask)))
  232. {
  233. new_chunk->prev = old_chunk->prev;
  234. CALL_FREEFUN (h, old_chunk);
  235. }
  236. h->object_base = object_base;
  237. h->next_free = h->object_base + obj_size;
  238. /* The new chunk certainly contains no empty object yet. */
  239. h->maybe_empty_object = 0;
  240. }
  241. /* Return nonzero if object OBJ has been allocated from obstack H.
  242. This is here for debugging.
  243. If you use it in a program, you are probably losing. */
  244. /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
  245. obstack.h because it is just for debugging. */
  246. int _obstack_allocated_p (struct obstack *h, void *obj);
  247. int
  248. _obstack_allocated_p (struct obstack *h, void *obj)
  249. {
  250. register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
  251. register struct _obstack_chunk *plp; /* point to previous chunk if any */
  252. lp = (h)->chunk;
  253. /* We use >= rather than > since the object cannot be exactly at
  254. the beginning of the chunk but might be an empty object exactly
  255. at the end of an adjacent chunk. */
  256. while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
  257. {
  258. plp = lp->prev;
  259. lp = plp;
  260. }
  261. return lp != 0;
  262. }
  263. /* Free objects in obstack H, including OBJ and everything allocate
  264. more recently than OBJ. If OBJ is zero, free everything in H. */
  265. # undef obstack_free
  266. void
  267. obstack_free (struct obstack *h, void *obj)
  268. {
  269. register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
  270. register struct _obstack_chunk *plp; /* point to previous chunk if any */
  271. lp = h->chunk;
  272. /* We use >= because there cannot be an object at the beginning of a chunk.
  273. But there can be an empty object at that address
  274. at the end of another chunk. */
  275. while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
  276. {
  277. plp = lp->prev;
  278. CALL_FREEFUN (h, lp);
  279. lp = plp;
  280. /* If we switch chunks, we can't tell whether the new current
  281. chunk contains an empty object, so assume that it may. */
  282. h->maybe_empty_object = 1;
  283. }
  284. if (lp)
  285. {
  286. h->object_base = h->next_free = (char *) (obj);
  287. h->chunk_limit = lp->limit;
  288. h->chunk = lp;
  289. }
  290. else if (obj != 0)
  291. /* obj is not in any of the chunks! */
  292. abort ();
  293. }
  294. int
  295. _obstack_memory_used (struct obstack *h)
  296. {
  297. register struct _obstack_chunk* lp;
  298. register int nbytes = 0;
  299. for (lp = h->chunk; lp != 0; lp = lp->prev)
  300. {
  301. nbytes += lp->limit - (char *) lp;
  302. }
  303. return nbytes;
  304. }
  305. static void
  306. #ifndef WIN32
  307. __attribute__ ((noreturn))
  308. #endif
  309. print_and_abort (void)
  310. {
  311. /* Don't change any of these strings. Yes, it would be possible to add
  312. the newline to the string and use fputs or so. But this must not
  313. happen because the "memory exhausted" message appears in other places
  314. like this and the translation should be reused instead of creating
  315. a very similar string which requires a separate translation. */
  316. fprintf (stderr, "%s\n", "memory exhausted");
  317. exit (obstack_exit_failure);
  318. }