obstack.c 14 KB

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