obstack.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /* obstack.c - subroutines used implicitly by object stack macros
  2. Copyright (C) 1988-1994,96,97,98,99,2000 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library. Its master source is NOT part of
  4. the C library, however. The master source lives in /gd/gnu/lib.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. 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. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with the GNU C Library; see the file COPYING.LIB. If not,
  15. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. Boston, MA 02111-1307, 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. #if defined (__STDC__) && __STDC__
  42. #define POINTER void *
  43. #else
  44. #define POINTER char *
  45. #endif
  46. /* Determine default alignment. */
  47. struct fooalign {char x; double d;};
  48. #define DEFAULT_ALIGNMENT \
  49. ((PTR_INT_TYPE) ((char *) &((struct fooalign *) 0)->d - (char *) 0))
  50. /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
  51. But in fact it might be less smart and round addresses to as much as
  52. DEFAULT_ROUNDING. So we prepare for it to do that. */
  53. union fooround {long x; double d;};
  54. #define DEFAULT_ROUNDING (sizeof (union fooround))
  55. /* When we copy a long block of data, this is the unit to do it with.
  56. On some machines, copying successive ints does not work;
  57. in such a case, redefine COPYING_UNIT to `long' (if that works)
  58. or `char' as a last resort. */
  59. #ifndef COPYING_UNIT
  60. #define COPYING_UNIT int
  61. #endif
  62. /* The functions allocating more room by calling `obstack_chunk_alloc'
  63. jump to the handler pointed to by `obstack_alloc_failed_handler'.
  64. This can be set to a user defined function which should either
  65. abort gracefully or use longjump - but shouldn't return. This
  66. variable by default points to the internal function
  67. `print_and_abort'. */
  68. #if defined (__STDC__) && __STDC__
  69. static void print_and_abort (void);
  70. void (*obstack_alloc_failed_handler) (void) = print_and_abort;
  71. #else
  72. static void print_and_abort ();
  73. void (*obstack_alloc_failed_handler) () = print_and_abort;
  74. #endif
  75. /* Exit value used when `print_and_abort' is used. */
  76. #if defined __GNU_LIBRARY__ || defined HAVE_STDLIB_H
  77. #include <stdlib.h>
  78. #endif
  79. #ifndef EXIT_FAILURE
  80. #define EXIT_FAILURE 1
  81. #endif
  82. int obstack_exit_failure = EXIT_FAILURE;
  83. /* The non-GNU-C macros copy the obstack into this global variable
  84. to avoid multiple evaluation. */
  85. struct obstack *_obstack;
  86. /* Define a macro that either calls functions with the traditional malloc/free
  87. calling interface, or calls functions with the mmalloc/mfree interface
  88. (that adds an extra first argument), based on the state of use_extra_arg.
  89. For free, do not use ?:, since some compilers, like the MIPS compilers,
  90. do not allow (expr) ? void : void. */
  91. #if defined (__STDC__) && __STDC__
  92. #define CALL_CHUNKFUN(h, size) \
  93. (((h) -> use_extra_arg) \
  94. ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
  95. : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
  96. #define CALL_FREEFUN(h, old_chunk) \
  97. do { \
  98. if ((h) -> use_extra_arg) \
  99. (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
  100. else \
  101. (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
  102. } while (0)
  103. #else
  104. #define CALL_CHUNKFUN(h, size) \
  105. (((h) -> use_extra_arg) \
  106. ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
  107. : (*(struct _obstack_chunk *(*) ()) (h)->chunkfun) ((size)))
  108. #define CALL_FREEFUN(h, old_chunk) \
  109. do { \
  110. if ((h) -> use_extra_arg) \
  111. (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
  112. else \
  113. (*(void (*) ()) (h)->freefun) ((old_chunk)); \
  114. } while (0)
  115. #endif
  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 (h, size, alignment, chunkfun, freefun)
  124. struct obstack *h;
  125. int size;
  126. int alignment;
  127. #if defined (__STDC__) && __STDC__
  128. POINTER (*chunkfun) (long);
  129. void (*freefun) (void *);
  130. #else
  131. POINTER (*chunkfun) ();
  132. void (*freefun) ();
  133. #endif
  134. {
  135. register struct _obstack_chunk *chunk; /* points to new chunk */
  136. if (alignment == 0)
  137. alignment = (int) DEFAULT_ALIGNMENT;
  138. if (size == 0)
  139. /* Default size is what GNU malloc can fit in a 4096-byte block. */
  140. {
  141. /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
  142. Use the values for range checking, because if range checking is off,
  143. the extra bytes won't be missed terribly, but if range checking is on
  144. and we used a larger request, a whole extra 4096 bytes would be
  145. allocated.
  146. These number are irrelevant to the new GNU malloc. I suspect it is
  147. less sensitive to the size of the request. */
  148. int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
  149. + 4 + DEFAULT_ROUNDING - 1)
  150. & ~(DEFAULT_ROUNDING - 1));
  151. size = 4096 - extra;
  152. }
  153. #if defined (__STDC__) && __STDC__
  154. h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun;
  155. h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
  156. #else
  157. h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
  158. h->freefun = freefun;
  159. #endif
  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 = chunk->contents;
  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. int
  176. _obstack_begin_1 (h, size, alignment, chunkfun, freefun, arg)
  177. struct obstack *h;
  178. int size;
  179. int alignment;
  180. #if defined (__STDC__) && __STDC__
  181. POINTER (*chunkfun) (POINTER, long);
  182. void (*freefun) (POINTER, POINTER);
  183. #else
  184. POINTER (*chunkfun) ();
  185. void (*freefun) ();
  186. #endif
  187. POINTER arg;
  188. {
  189. register struct _obstack_chunk *chunk; /* points to new chunk */
  190. if (alignment == 0)
  191. alignment = (int) DEFAULT_ALIGNMENT;
  192. if (size == 0)
  193. /* Default size is what GNU malloc can fit in a 4096-byte block. */
  194. {
  195. /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
  196. Use the values for range checking, because if range checking is off,
  197. the extra bytes won't be missed terribly, but if range checking is on
  198. and we used a larger request, a whole extra 4096 bytes would be
  199. allocated.
  200. These number are irrelevant to the new GNU malloc. I suspect it is
  201. less sensitive to the size of the request. */
  202. int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
  203. + 4 + DEFAULT_ROUNDING - 1)
  204. & ~(DEFAULT_ROUNDING - 1));
  205. size = 4096 - extra;
  206. }
  207. #if defined(__STDC__) && __STDC__
  208. h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun;
  209. h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
  210. #else
  211. h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
  212. h->freefun = freefun;
  213. #endif
  214. h->chunk_size = size;
  215. h->alignment_mask = alignment - 1;
  216. h->extra_arg = arg;
  217. h->use_extra_arg = 1;
  218. chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
  219. if (!chunk)
  220. (*obstack_alloc_failed_handler) ();
  221. h->next_free = h->object_base = chunk->contents;
  222. h->chunk_limit = chunk->limit
  223. = (char *) chunk + h->chunk_size;
  224. chunk->prev = 0;
  225. /* The initial chunk now contains no empty object. */
  226. h->maybe_empty_object = 0;
  227. h->alloc_failed = 0;
  228. return 1;
  229. }
  230. /* Allocate a new current chunk for the obstack *H
  231. on the assumption that LENGTH bytes need to be added
  232. to the current object, or a new object of length LENGTH allocated.
  233. Copies any partial object from the end of the old chunk
  234. to the beginning of the new one. */
  235. void
  236. _obstack_newchunk (h, length)
  237. struct obstack *h;
  238. int length;
  239. {
  240. register struct _obstack_chunk *old_chunk = h->chunk;
  241. register struct _obstack_chunk *new_chunk;
  242. register long new_size;
  243. register long obj_size = h->next_free - h->object_base;
  244. register long i;
  245. long already;
  246. char *object_base;
  247. /* Compute size for new chunk. */
  248. new_size = (obj_size + length) + (obj_size >> 3) + h->alignment_mask + 100;
  249. if (new_size < h->chunk_size)
  250. new_size = h->chunk_size;
  251. /* Allocate and initialize the new chunk. */
  252. new_chunk = CALL_CHUNKFUN (h, new_size);
  253. if (!new_chunk)
  254. (*obstack_alloc_failed_handler) ();
  255. h->chunk = new_chunk;
  256. new_chunk->prev = old_chunk;
  257. new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
  258. /* Compute an aligned object_base in the new chunk */
  259. object_base =
  260. __INT_TO_PTR ((__PTR_TO_INT (new_chunk->contents) + h->alignment_mask)
  261. & ~ (h->alignment_mask));
  262. /* Move the existing object to the new chunk.
  263. Word at a time is fast and is safe if the object
  264. is sufficiently aligned. */
  265. if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
  266. {
  267. for (i = obj_size / sizeof (COPYING_UNIT) - 1;
  268. i >= 0; i--)
  269. ((COPYING_UNIT *)object_base)[i]
  270. = ((COPYING_UNIT *)h->object_base)[i];
  271. /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
  272. but that can cross a page boundary on a machine
  273. which does not do strict alignment for COPYING_UNITS. */
  274. already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
  275. }
  276. else
  277. already = 0;
  278. /* Copy remaining bytes one by one. */
  279. for (i = already; i < obj_size; i++)
  280. object_base[i] = h->object_base[i];
  281. /* If the object just copied was the only data in OLD_CHUNK,
  282. free that chunk and remove it from the chain.
  283. But not if that chunk might contain an empty object. */
  284. if (h->object_base == old_chunk->contents && ! h->maybe_empty_object)
  285. {
  286. new_chunk->prev = old_chunk->prev;
  287. CALL_FREEFUN (h, old_chunk);
  288. }
  289. h->object_base = object_base;
  290. h->next_free = h->object_base + obj_size;
  291. /* The new chunk certainly contains no empty object yet. */
  292. h->maybe_empty_object = 0;
  293. }
  294. /* Return nonzero if object OBJ has been allocated from obstack H.
  295. This is here for debugging.
  296. If you use it in a program, you are probably losing. */
  297. #if defined (__STDC__) && __STDC__
  298. /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
  299. obstack.h because it is just for debugging. */
  300. int _obstack_allocated_p (struct obstack *h, POINTER obj);
  301. #endif
  302. int
  303. _obstack_allocated_p (h, obj)
  304. struct obstack *h;
  305. POINTER obj;
  306. {
  307. register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
  308. register struct _obstack_chunk *plp; /* point to previous chunk if any */
  309. lp = (h)->chunk;
  310. /* We use >= rather than > since the object cannot be exactly at
  311. the beginning of the chunk but might be an empty object exactly
  312. at the end of an adjacent chunk. */
  313. while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
  314. {
  315. plp = lp->prev;
  316. lp = plp;
  317. }
  318. return lp != 0;
  319. }
  320. /* Free objects in obstack H, including OBJ and everything allocate
  321. more recently than OBJ. If OBJ is zero, free everything in H. */
  322. #undef obstack_free
  323. /* This function has two names with identical definitions.
  324. This is the first one, called from non-ANSI code. */
  325. void
  326. _obstack_free (h, obj)
  327. struct obstack *h;
  328. POINTER obj;
  329. {
  330. register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
  331. register struct _obstack_chunk *plp; /* point to previous chunk if any */
  332. lp = h->chunk;
  333. /* We use >= because there cannot be an object at the beginning of a chunk.
  334. But there can be an empty object at that address
  335. at the end of another chunk. */
  336. while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
  337. {
  338. plp = lp->prev;
  339. CALL_FREEFUN (h, lp);
  340. lp = plp;
  341. /* If we switch chunks, we can't tell whether the new current
  342. chunk contains an empty object, so assume that it may. */
  343. h->maybe_empty_object = 1;
  344. }
  345. if (lp)
  346. {
  347. h->object_base = h->next_free = (char *) (obj);
  348. h->chunk_limit = lp->limit;
  349. h->chunk = lp;
  350. }
  351. else if (obj != 0)
  352. /* obj is not in any of the chunks! */
  353. abort ();
  354. }
  355. /* This function is used from ANSI code. */
  356. void
  357. obstack_free (h, obj)
  358. struct obstack *h;
  359. POINTER obj;
  360. {
  361. register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
  362. register struct _obstack_chunk *plp; /* point to previous chunk if any */
  363. lp = h->chunk;
  364. /* We use >= because there cannot be an object at the beginning of a chunk.
  365. But there can be an empty object at that address
  366. at the end of another chunk. */
  367. while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
  368. {
  369. plp = lp->prev;
  370. CALL_FREEFUN (h, lp);
  371. lp = plp;
  372. /* If we switch chunks, we can't tell whether the new current
  373. chunk contains an empty object, so assume that it may. */
  374. h->maybe_empty_object = 1;
  375. }
  376. if (lp)
  377. {
  378. h->object_base = h->next_free = (char *) (obj);
  379. h->chunk_limit = lp->limit;
  380. h->chunk = lp;
  381. }
  382. else if (obj != 0)
  383. /* obj is not in any of the chunks! */
  384. abort ();
  385. }
  386. int
  387. _obstack_memory_used (h)
  388. struct obstack *h;
  389. {
  390. register struct _obstack_chunk* lp;
  391. register int nbytes = 0;
  392. for (lp = h->chunk; lp != 0; lp = lp->prev)
  393. {
  394. nbytes += lp->limit - (char *) lp;
  395. }
  396. return nbytes;
  397. }
  398. /* Define the error handler. */
  399. #ifndef _
  400. # if defined HAVE_LIBINTL_H || defined _LIBC
  401. # include <libintl.h>
  402. # ifndef _
  403. # define _(Str) gettext (Str)
  404. # endif
  405. # else
  406. # define _(Str) (Str)
  407. # endif
  408. #endif
  409. #if defined _LIBC && defined USE_IN_LIBIO
  410. # include <libio/iolibio.h>
  411. # define fputs(s, f) _IO_fputs (s, f)
  412. #endif
  413. static void
  414. print_and_abort ()
  415. {
  416. fputs (_("memory exhausted"), stderr);
  417. fputc ('\n', stderr);
  418. exit (obstack_exit_failure);
  419. }
  420. #if 0
  421. /* These are now turned off because the applications do not use it
  422. and it uses bcopy via obstack_grow, which causes trouble on sysV. */
  423. /* Now define the functional versions of the obstack macros.
  424. Define them to simply use the corresponding macros to do the job. */
  425. #if defined (__STDC__) && __STDC__
  426. /* These function definitions do not work with non-ANSI preprocessors;
  427. they won't pass through the macro names in parentheses. */
  428. /* The function names appear in parentheses in order to prevent
  429. the macro-definitions of the names from being expanded there. */
  430. POINTER (obstack_base) (obstack)
  431. struct obstack *obstack;
  432. {
  433. return obstack_base (obstack);
  434. }
  435. POINTER (obstack_next_free) (obstack)
  436. struct obstack *obstack;
  437. {
  438. return obstack_next_free (obstack);
  439. }
  440. int (obstack_object_size) (obstack)
  441. struct obstack *obstack;
  442. {
  443. return obstack_object_size (obstack);
  444. }
  445. int (obstack_room) (obstack)
  446. struct obstack *obstack;
  447. {
  448. return obstack_room (obstack);
  449. }
  450. int (obstack_make_room) (obstack, length)
  451. struct obstack *obstack;
  452. int length;
  453. {
  454. return obstack_make_room (obstack, length);
  455. }
  456. void (obstack_grow) (obstack, data, length)
  457. struct obstack *obstack;
  458. const POINTER data;
  459. int length;
  460. {
  461. obstack_grow (obstack, data, length);
  462. }
  463. void (obstack_grow0) (obstack, data, length)
  464. struct obstack *obstack;
  465. const POINTER data;
  466. int length;
  467. {
  468. obstack_grow0 (obstack, data, length);
  469. }
  470. void (obstack_1grow) (obstack, character)
  471. struct obstack *obstack;
  472. int character;
  473. {
  474. obstack_1grow (obstack, character);
  475. }
  476. void (obstack_blank) (obstack, length)
  477. struct obstack *obstack;
  478. int length;
  479. {
  480. obstack_blank (obstack, length);
  481. }
  482. void (obstack_1grow_fast) (obstack, character)
  483. struct obstack *obstack;
  484. int character;
  485. {
  486. obstack_1grow_fast (obstack, character);
  487. }
  488. void (obstack_blank_fast) (obstack, length)
  489. struct obstack *obstack;
  490. int length;
  491. {
  492. obstack_blank_fast (obstack, length);
  493. }
  494. POINTER (obstack_finish) (obstack)
  495. struct obstack *obstack;
  496. {
  497. return obstack_finish (obstack);
  498. }
  499. POINTER (obstack_alloc) (obstack, length)
  500. struct obstack *obstack;
  501. int length;
  502. {
  503. return obstack_alloc (obstack, length);
  504. }
  505. POINTER (obstack_copy) (obstack, address, length)
  506. struct obstack *obstack;
  507. const POINTER address;
  508. int length;
  509. {
  510. return obstack_copy (obstack, address, length);
  511. }
  512. POINTER (obstack_copy0) (obstack, address, length)
  513. struct obstack *obstack;
  514. const POINTER address;
  515. int length;
  516. {
  517. return obstack_copy0 (obstack, address, length);
  518. }
  519. #endif /* __STDC__ */
  520. #endif /* 0 */
  521. #endif /* !ELIDE_CODE */