123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- #ifdef HAVE_CONFIG_H
- # include <config.h>
- #endif
- #include "obstack.h"
- #define OBSTACK_INTERFACE_VERSION 1
- #include <stdio.h>
- #include <stddef.h>
- #include <stdint.h>
- union fooround
- {
- uintmax_t i;
- long double d;
- void *p;
- };
- struct fooalign
- {
- char c;
- union fooround u;
- };
- enum
- {
- DEFAULT_ALIGNMENT = offsetof (struct fooalign, u),
- DEFAULT_ROUNDING = sizeof (union fooround)
- };
- # ifndef COPYING_UNIT
- # define COPYING_UNIT int
- # endif
- static void print_and_abort (void);
- void (*obstack_alloc_failed_handler) (void) = print_and_abort;
- # include <stdlib.h>
- int obstack_exit_failure = EXIT_FAILURE;
- # define CALL_CHUNKFUN(h, size) \
- (((h) -> use_extra_arg) \
- ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
- : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
- # define CALL_FREEFUN(h, old_chunk) \
- do { \
- if ((h) -> use_extra_arg) \
- (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
- else \
- (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
- } while (0)
- int
- _obstack_begin (struct obstack *h,
- int size, int alignment,
- void *(*chunkfun) (long),
- void (*freefun) (void *))
- {
- register struct _obstack_chunk *chunk;
- if (alignment == 0)
- alignment = DEFAULT_ALIGNMENT;
- if (size == 0)
-
- {
-
- int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
- + 4 + DEFAULT_ROUNDING - 1)
- & ~(DEFAULT_ROUNDING - 1));
- size = 4096 - extra;
- }
- h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun;
- h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
- h->chunk_size = size;
- h->alignment_mask = alignment - 1;
- h->use_extra_arg = 0;
- chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
- if (!chunk) {
- (*obstack_alloc_failed_handler) ();
- return 0;
- }
- h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
- alignment - 1);
- h->chunk_limit = chunk->limit
- = (char *) chunk + h->chunk_size;
- chunk->prev = 0;
-
- h->maybe_empty_object = 0;
- h->alloc_failed = 0;
- return 1;
- }
- int
- _obstack_begin_1 (struct obstack *h, int size, int alignment,
- void *(*chunkfun) (void *, long),
- void (*freefun) (void *, void *),
- void *arg)
- {
- register struct _obstack_chunk *chunk;
- if (alignment == 0)
- alignment = DEFAULT_ALIGNMENT;
- if (size == 0)
-
- {
-
- int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
- + 4 + DEFAULT_ROUNDING - 1)
- & ~(DEFAULT_ROUNDING - 1));
- size = 4096 - extra;
- }
- h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun;
- h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
- h->chunk_size = size;
- h->alignment_mask = alignment - 1;
- h->extra_arg = arg;
- h->use_extra_arg = 1;
- chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
- if (!chunk)
- (*obstack_alloc_failed_handler) ();
- h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
- alignment - 1);
- h->chunk_limit = chunk->limit
- = (char *) chunk + h->chunk_size;
- chunk->prev = 0;
-
- h->maybe_empty_object = 0;
- h->alloc_failed = 0;
- return 1;
- }
- void
- _obstack_newchunk (struct obstack *h, int length)
- {
- register struct _obstack_chunk *old_chunk = h->chunk;
- register struct _obstack_chunk *new_chunk;
- register long new_size;
- register long obj_size = h->next_free - h->object_base;
- register long i;
- long already;
- char *object_base;
-
- new_size = (obj_size + length) + (obj_size >> 3) + h->alignment_mask + 100;
- if (new_size < h->chunk_size)
- new_size = h->chunk_size;
-
- new_chunk = CALL_CHUNKFUN (h, new_size);
- if (!new_chunk) {
- (*obstack_alloc_failed_handler) ();
- return;
- }
- h->chunk = new_chunk;
- new_chunk->prev = old_chunk;
- new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
-
- object_base =
- __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask);
-
- if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
- {
- for (i = obj_size / sizeof (COPYING_UNIT) - 1;
- i >= 0; i--)
- ((COPYING_UNIT *)object_base)[i]
- = ((COPYING_UNIT *)h->object_base)[i];
-
- already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
- }
- else
- already = 0;
-
- for (i = already; i < obj_size; i++)
- object_base[i] = h->object_base[i];
-
- if (! h->maybe_empty_object
- && (h->object_base
- == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents,
- h->alignment_mask)))
- {
- new_chunk->prev = old_chunk->prev;
- CALL_FREEFUN (h, old_chunk);
- }
- h->object_base = object_base;
- h->next_free = h->object_base + obj_size;
-
- h->maybe_empty_object = 0;
- }
- int _obstack_allocated_p (struct obstack *h, void *obj);
- int
- _obstack_allocated_p (struct obstack *h, void *obj)
- {
- register struct _obstack_chunk *lp;
- register struct _obstack_chunk *plp;
- lp = (h)->chunk;
-
- while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
- {
- plp = lp->prev;
- lp = plp;
- }
- return lp != 0;
- }
- # undef obstack_free
- void
- obstack_free (struct obstack *h, void *obj)
- {
- register struct _obstack_chunk *lp;
- register struct _obstack_chunk *plp;
- lp = h->chunk;
-
- while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
- {
- plp = lp->prev;
- CALL_FREEFUN (h, lp);
- lp = plp;
-
- h->maybe_empty_object = 1;
- }
- if (lp)
- {
- h->object_base = h->next_free = (char *) (obj);
- h->chunk_limit = lp->limit;
- h->chunk = lp;
- }
- else if (obj != 0)
-
- abort ();
- }
- int
- _obstack_memory_used (struct obstack *h)
- {
- register struct _obstack_chunk* lp;
- register int nbytes = 0;
- for (lp = h->chunk; lp != 0; lp = lp->prev)
- {
- nbytes += lp->limit - (char *) lp;
- }
- return nbytes;
- }
- static void
- #ifndef WIN32
- __attribute__ ((noreturn))
- #endif
- print_and_abort (void)
- {
-
- fprintf (stderr, "%s\n", "memory exhausted");
- exit (obstack_exit_failure);
- }
|