obstack.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /* obstack.c - subroutines used implicitly by object stack macros
  2. Copyright (C) 1988,89,90,91,92,93,94,96,97 Free Software Foundation, Inc.
  3. NOTE: This source is derived from an old version taken from the GNU C
  4. Library (glibc).
  5. This program is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option) any
  8. 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
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
  16. 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. /* Comment out all this code if we are using the GNU C Library, and are not
  26. actually compiling the library itself, and the installed library
  27. supports the same library interface we do. This code is part of the GNU
  28. C Library, but also included in many other GNU distributions. Compiling
  29. and linking in this code is a waste when using the GNU C library
  30. (especially if it is a shared library). Rather than having every GNU
  31. program understand `configure --with-gnu-libc' and omit the object
  32. files, it is simpler to just do this in the source for each such file. */
  33. #include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */
  34. #if !defined (_LIBC) && defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
  35. #include <gnu-versions.h>
  36. #if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION
  37. #define ELIDE_CODE
  38. #endif
  39. #endif
  40. #ifndef ELIDE_CODE
  41. #define POINTER void *
  42. /* Determine default alignment. */
  43. struct fooalign {char x; double d;};
  44. #define DEFAULT_ALIGNMENT \
  45. ((PTR_INT_TYPE) ((char *) &((struct fooalign *) 0)->d - (char *) 0))
  46. /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
  47. But in fact it might be less smart and round addresses to as much as
  48. DEFAULT_ROUNDING. So we prepare for it to do that. */
  49. union fooround {long x; double d;};
  50. #define DEFAULT_ROUNDING (sizeof (union fooround))
  51. /* When we copy a long block of data, this is the unit to do it with.
  52. On some machines, copying successive ints does not work;
  53. in such a case, redefine COPYING_UNIT to `long' (if that works)
  54. or `char' as a last resort. */
  55. #ifndef COPYING_UNIT
  56. #define COPYING_UNIT int
  57. #endif
  58. /* The functions allocating more room by calling `obstack_chunk_alloc'
  59. jump to the handler pointed to by `obstack_alloc_failed_handler'.
  60. This variable by default points to the internal function
  61. `print_and_abort'. */
  62. static void print_and_abort (void);
  63. void (*obstack_alloc_failed_handler) (void) = print_and_abort;
  64. /* Exit value used when `print_and_abort' is used. */
  65. #if defined __GNU_LIBRARY__ || defined HAVE_STDLIB_H
  66. #include <stdlib.h>
  67. #endif
  68. #ifndef EXIT_FAILURE
  69. #define EXIT_FAILURE 1
  70. #endif
  71. int obstack_exit_failure = EXIT_FAILURE;
  72. /* The non-GNU-C macros copy the obstack into this global variable
  73. to avoid multiple evaluation. */
  74. struct obstack *_obstack;
  75. /* Define a macro that either calls functions with the traditional malloc/free
  76. calling interface, or calls functions with the mmalloc/mfree interface
  77. (that adds an extra first argument), based on the state of use_extra_arg.
  78. For free, do not use ?:, since some compilers, like the MIPS compilers,
  79. do not allow (expr) ? void : void. */
  80. #if defined (__STDC__) && __STDC__
  81. #define CALL_CHUNKFUN(h, size) \
  82. (((h) -> use_extra_arg) \
  83. ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
  84. : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
  85. #define CALL_FREEFUN(h, old_chunk) \
  86. do { \
  87. if ((h) -> use_extra_arg) \
  88. (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
  89. else \
  90. (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
  91. } while (0)
  92. #else
  93. #define CALL_CHUNKFUN(h, size) \
  94. (((h) -> use_extra_arg) \
  95. ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
  96. : (*(struct _obstack_chunk *(*) ()) (h)->chunkfun) ((size)))
  97. #define CALL_FREEFUN(h, old_chunk) \
  98. do { \
  99. if ((h) -> use_extra_arg) \
  100. (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
  101. else \
  102. (*(void (*) ()) (h)->freefun) ((old_chunk)); \
  103. } while (0)
  104. #endif
  105. /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
  106. Objects start on multiples of ALIGNMENT (0 means use default).
  107. CHUNKFUN is the function to use to allocate chunks,
  108. and FREEFUN the function to free them.
  109. Return nonzero if successful, zero if out of memory.
  110. To recover from an out of memory error,
  111. free up some memory, then call this again. */
  112. int
  113. _obstack_begin (struct obstack *h, int size, int alignment,
  114. POINTER (*chunkfun) (long), void (*freefun) (void *))
  115. {
  116. register struct _obstack_chunk *chunk; /* points to new chunk */
  117. if (alignment == 0)
  118. alignment = (int) DEFAULT_ALIGNMENT;
  119. if (size == 0)
  120. /* Default size is what GNU malloc can fit in a 4096-byte block. */
  121. {
  122. /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
  123. Use the values for range checking, because if range checking is off,
  124. the extra bytes won't be missed terribly, but if range checking is on
  125. and we used a larger request, a whole extra 4096 bytes would be
  126. allocated.
  127. These number are irrelevant to the new GNU malloc. I suspect it is
  128. less sensitive to the size of the request. */
  129. int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
  130. + 4 + DEFAULT_ROUNDING - 1)
  131. & ~(DEFAULT_ROUNDING - 1));
  132. size = 4096 - extra;
  133. }
  134. h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun;
  135. h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
  136. h->chunk_size = size;
  137. h->alignment_mask = alignment - 1;
  138. h->use_extra_arg = 0;
  139. chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
  140. if (!chunk)
  141. (*obstack_alloc_failed_handler) ();
  142. h->next_free = h->object_base = chunk->contents;
  143. h->chunk_limit = chunk->limit
  144. = (char *) chunk + h->chunk_size;
  145. chunk->prev = 0;
  146. /* The initial chunk now contains no empty object. */
  147. h->maybe_empty_object = 0;
  148. h->alloc_failed = 0;
  149. return 1;
  150. }
  151. int
  152. _obstack_begin_1 (struct obstack *h, int size, int alignment,
  153. POINTER (*chunkfun) (POINTER, long),
  154. void (*freefun) (POINTER, POINTER), POINTER arg)
  155. {
  156. register struct _obstack_chunk *chunk; /* points to new chunk */
  157. if (alignment == 0)
  158. alignment = (int) DEFAULT_ALIGNMENT;
  159. if (size == 0)
  160. /* Default size is what GNU malloc can fit in a 4096-byte block. */
  161. {
  162. /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
  163. Use the values for range checking, because if range checking is off,
  164. the extra bytes won't be missed terribly, but if range checking is on
  165. and we used a larger request, a whole extra 4096 bytes would be
  166. allocated.
  167. These number are irrelevant to the new GNU malloc. I suspect it is
  168. less sensitive to the size of the request. */
  169. int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
  170. + 4 + DEFAULT_ROUNDING - 1)
  171. & ~(DEFAULT_ROUNDING - 1));
  172. size = 4096 - extra;
  173. }
  174. h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun;
  175. h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
  176. h->chunk_size = size;
  177. h->alignment_mask = alignment - 1;
  178. h->extra_arg = arg;
  179. h->use_extra_arg = 1;
  180. chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
  181. if (!chunk)
  182. (*obstack_alloc_failed_handler) ();
  183. h->next_free = h->object_base = chunk->contents;
  184. h->chunk_limit = chunk->limit
  185. = (char *) chunk + h->chunk_size;
  186. chunk->prev = 0;
  187. /* The initial chunk now contains no empty object. */
  188. h->maybe_empty_object = 0;
  189. h->alloc_failed = 0;
  190. return 1;
  191. }
  192. /* Allocate a new current chunk for the obstack *H
  193. on the assumption that LENGTH bytes need to be added
  194. to the current object, or a new object of length LENGTH allocated.
  195. Copies any partial object from the end of the old chunk
  196. to the beginning of the new one. */
  197. void
  198. _obstack_newchunk (struct obstack *h, int length)
  199. {
  200. register struct _obstack_chunk *old_chunk = h->chunk;
  201. register struct _obstack_chunk *new_chunk;
  202. register long new_size;
  203. register long obj_size = h->next_free - h->object_base;
  204. register long i;
  205. long already;
  206. /* Compute size for new chunk. */
  207. new_size = (obj_size + length) + (obj_size >> 3) + 100;
  208. if (new_size < h->chunk_size)
  209. new_size = h->chunk_size;
  210. /* Allocate and initialize the new chunk. */
  211. new_chunk = CALL_CHUNKFUN (h, new_size);
  212. if (!new_chunk)
  213. (*obstack_alloc_failed_handler) ();
  214. h->chunk = new_chunk;
  215. new_chunk->prev = old_chunk;
  216. new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
  217. /* Move the existing object to the new chunk.
  218. Word at a time is fast and is safe if the object
  219. is sufficiently aligned. */
  220. if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
  221. {
  222. for (i = obj_size / sizeof (COPYING_UNIT) - 1;
  223. i >= 0; i--)
  224. ((COPYING_UNIT *)new_chunk->contents)[i]
  225. = ((COPYING_UNIT *)h->object_base)[i];
  226. /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
  227. but that can cross a page boundary on a machine
  228. which does not do strict alignment for COPYING_UNITS. */
  229. already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
  230. }
  231. else
  232. already = 0;
  233. /* Copy remaining bytes one by one. */
  234. for (i = already; i < obj_size; i++)
  235. new_chunk->contents[i] = h->object_base[i];
  236. /* If the object just copied was the only data in OLD_CHUNK,
  237. free that chunk and remove it from the chain.
  238. But not if that chunk might contain an empty object. */
  239. if (h->object_base == old_chunk->contents && ! h->maybe_empty_object)
  240. {
  241. new_chunk->prev = old_chunk->prev;
  242. CALL_FREEFUN (h, old_chunk);
  243. }
  244. h->object_base = new_chunk->contents;
  245. h->next_free = h->object_base + obj_size;
  246. /* The new chunk certainly contains no empty object yet. */
  247. h->maybe_empty_object = 0;
  248. }
  249. /* Return nonzero if object OBJ has been allocated from obstack H.
  250. This is here for debugging.
  251. If you use it in a program, you are probably losing. */
  252. /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
  253. obstack.h because it is just for debugging. */
  254. int _obstack_allocated_p (struct obstack *h, POINTER obj);
  255. int
  256. _obstack_allocated_p (struct obstack *h, POINTER obj)
  257. {
  258. register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
  259. register struct _obstack_chunk *plp; /* point to previous chunk if any */
  260. lp = (h)->chunk;
  261. /* We use >= rather than > since the object cannot be exactly at
  262. the beginning of the chunk but might be an empty object exactly
  263. at the end of an adjacent chunk. */
  264. while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
  265. {
  266. plp = lp->prev;
  267. lp = plp;
  268. }
  269. return lp != 0;
  270. }
  271. /* Free objects in obstack H, including OBJ and everything allocate
  272. more recently than OBJ. If OBJ is zero, free everything in H. */
  273. #undef obstack_free
  274. /* This function has two names with identical definitions.
  275. This is the first one, called from non-ANSI code. */
  276. void
  277. _obstack_free (struct obstack *h, POINTER obj)
  278. {
  279. register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
  280. register struct _obstack_chunk *plp; /* point to previous chunk if any */
  281. lp = h->chunk;
  282. /* We use >= because there cannot be an object at the beginning of a chunk.
  283. But there can be an empty object at that address
  284. at the end of another chunk. */
  285. while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
  286. {
  287. plp = lp->prev;
  288. CALL_FREEFUN (h, lp);
  289. lp = plp;
  290. /* If we switch chunks, we can't tell whether the new current
  291. chunk contains an empty object, so assume that it may. */
  292. h->maybe_empty_object = 1;
  293. }
  294. if (lp)
  295. {
  296. h->object_base = h->next_free = (char *) (obj);
  297. h->chunk_limit = lp->limit;
  298. h->chunk = lp;
  299. }
  300. else if (obj != 0)
  301. /* obj is not in any of the chunks! */
  302. abort ();
  303. }
  304. /* This function is used from ANSI code. */
  305. void
  306. obstack_free (struct obstack *h, POINTER obj)
  307. {
  308. register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
  309. register struct _obstack_chunk *plp; /* point to previous chunk if any */
  310. lp = h->chunk;
  311. /* We use >= because there cannot be an object at the beginning of a chunk.
  312. But there can be an empty object at that address
  313. at the end of another chunk. */
  314. while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
  315. {
  316. plp = lp->prev;
  317. CALL_FREEFUN (h, lp);
  318. lp = plp;
  319. /* If we switch chunks, we can't tell whether the new current
  320. chunk contains an empty object, so assume that it may. */
  321. h->maybe_empty_object = 1;
  322. }
  323. if (lp)
  324. {
  325. h->object_base = h->next_free = (char *) (obj);
  326. h->chunk_limit = lp->limit;
  327. h->chunk = lp;
  328. }
  329. else if (obj != 0)
  330. /* obj is not in any of the chunks! */
  331. abort ();
  332. }
  333. int
  334. _obstack_memory_used (struct obstack *h)
  335. {
  336. register struct _obstack_chunk* lp;
  337. register int nbytes = 0;
  338. for (lp = h->chunk; lp != 0; lp = lp->prev)
  339. {
  340. nbytes += lp->limit - (char *) lp;
  341. }
  342. return nbytes;
  343. }
  344. /* Define the error handler. */
  345. #ifndef _
  346. # if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
  347. # include <libintl.h>
  348. # ifndef _
  349. # define _(Str) gettext (Str)
  350. # endif
  351. # else
  352. # define _(Str) (Str)
  353. # endif
  354. #endif
  355. static void
  356. print_and_abort (void)
  357. {
  358. fputs (_("memory exhausted\n"), stderr);
  359. exit (obstack_exit_failure);
  360. }
  361. #if 0
  362. /* These are now turned off because the applications do not use it
  363. and it uses bcopy via obstack_grow, which causes trouble on sysV. */
  364. /* Now define the functional versions of the obstack macros.
  365. Define them to simply use the corresponding macros to do the job. */
  366. /* The function names appear in parentheses in order to prevent
  367. the macro-definitions of the names from being expanded there. */
  368. POINTER (obstack_base) (struct obstack *obstack)
  369. {
  370. return obstack_base (obstack);
  371. }
  372. POINTER (obstack_next_free) (struct obstack *obstack)
  373. {
  374. return obstack_next_free (obstack);
  375. }
  376. int (obstack_object_size) (struct obstack *obstack)
  377. {
  378. return obstack_object_size (obstack);
  379. }
  380. int (obstack_room) (struct obstack *obstack)
  381. {
  382. return obstack_room (obstack);
  383. }
  384. int (obstack_make_room) (struct obstack *obstack, int length)
  385. {
  386. return obstack_make_room (obstack, length);
  387. }
  388. void (obstack_grow) (struct obstack *obstack, POINTER pointer, int length)
  389. {
  390. obstack_grow (obstack, pointer, length);
  391. }
  392. void (obstack_grow0) (struct obstack *obstack, POINTER pointer, int length)
  393. {
  394. obstack_grow0 (obstack, pointer, length);
  395. }
  396. void (obstack_1grow) (struct obstack *obstack, int character)
  397. {
  398. obstack_1grow (obstack, character);
  399. }
  400. void (obstack_blank) (struct obstack *obstack, int length)
  401. {
  402. obstack_blank (obstack, length);
  403. }
  404. void (obstack_1grow_fast) (struct obstack *obstack, int character)
  405. {
  406. obstack_1grow_fast (obstack, character);
  407. }
  408. void (obstack_blank_fast) (struct obstack *obstack, int length)
  409. {
  410. obstack_blank_fast (obstack, length);
  411. }
  412. POINTER (obstack_finish) (struct obstack *obstack)
  413. {
  414. return obstack_finish (obstack);
  415. }
  416. POINTER (obstack_alloc) (struct obstack *obstack, int length)
  417. {
  418. return obstack_alloc (obstack, length);
  419. }
  420. POINTER (obstack_copy) (struct obstack *obstack, POINTER pointer, int length)
  421. {
  422. return obstack_copy (obstack, pointer, length);
  423. }
  424. POINTER (obstack_copy0) (struct obstack *obstack, POINTER pointer, int length)
  425. {
  426. return obstack_copy0 (obstack, pointer, length);
  427. }
  428. #endif /* 0 */
  429. #endif /* !ELIDE_CODE */