X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fobstack.c;h=f830fdca3414234131f42b72b99dd41ad47d9bb5;hb=c2b08ae26cdc70a3acf7d1bc15bc77c92bbcc97c;hp=9bbc3d4ca16eff768c9d4046917b9ab4b59fe6d4;hpb=e868156834d09d1a0b2046c3defac1691fcfd953;p=gnulib.git diff --git a/lib/obstack.c b/lib/obstack.c index 9bbc3d4ca..f830fdca3 100644 --- a/lib/obstack.c +++ b/lib/obstack.c @@ -1,30 +1,25 @@ /* obstack.c - subroutines used implicitly by object stack macros - Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997, - 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, - Inc. + Copyright (C) 1988-1994, 1996-2006, 2009-2013 Free Software Foundation, Inc. - This program is free software; you can redistribute it and/or modify + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2, or (at your option) - any later version. + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, - Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ - -#ifdef HAVE_CONFIG_H -# include -#endif + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ #ifdef _LIBC # include +# include #else +# include # include "obstack.h" #endif @@ -39,10 +34,10 @@ C Library, but also included in many other GNU distributions. Compiling and linking in this code is a waste when using the GNU C library (especially if it is a shared library). Rather than having every GNU - program understand `configure --with-gnu-libc' and omit the object + program understand 'configure --with-gnu-libc' and omit the object files, it is simpler to just do this in the source for each such file. */ -#include /* Random thing to get __GNU_LIBRARY__. */ +#include /* Random thing to get __GNU_LIBRARY__. */ #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1 # include # if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION @@ -50,50 +45,52 @@ # endif #endif -#if defined _LIBC && defined USE_IN_LIBIO -# include -#endif - #include #ifndef ELIDE_CODE +# include /* Determine default alignment. */ union fooround { - long int i; + uintmax_t i; long double d; void *p; }; +struct fooalign +{ + char c; + union fooround u; +}; /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT. But in fact it might be less smart and round addresses to as much as DEFAULT_ROUNDING. So we prepare for it to do that. */ enum { - DEFAULT_ALIGNMENT = offsetof (struct { char c; union fooround u; }, u), + DEFAULT_ALIGNMENT = offsetof (struct fooalign, u), DEFAULT_ROUNDING = sizeof (union fooround) }; /* When we copy a long block of data, this is the unit to do it with. On some machines, copying successive ints does not work; - in such a case, redefine COPYING_UNIT to `long' (if that works) - or `char' as a last resort. */ + in such a case, redefine COPYING_UNIT to 'long' (if that works) + or 'char' as a last resort. */ # ifndef COPYING_UNIT # define COPYING_UNIT int # endif -/* The functions allocating more room by calling `obstack_chunk_alloc' - jump to the handler pointed to by `obstack_alloc_failed_handler'. +/* The functions allocating more room by calling 'obstack_chunk_alloc' + jump to the handler pointed to by 'obstack_alloc_failed_handler'. This can be set to a user defined function which should either abort gracefully or use longjump - but shouldn't return. This variable by default points to the internal function - `print_and_abort'. */ -static void print_and_abort (void); + 'print_and_abort'. */ +static _Noreturn void print_and_abort (void); void (*obstack_alloc_failed_handler) (void) = print_and_abort; -/* Exit value used when `print_and_abort' is used. */ +/* Exit value used when 'print_and_abort' is used. */ # include # ifdef _LIBC int obstack_exit_failure = EXIT_FAILURE; @@ -102,10 +99,15 @@ int obstack_exit_failure = EXIT_FAILURE; # define obstack_exit_failure exit_failure # endif -/* The non-GNU-C macros copy the obstack into this global variable - to avoid multiple evaluation. */ - -struct obstack *_obstack; +# ifdef _LIBC +# if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3_4) +/* A looong time ago (before 1994, anyway; we're not sure) this global variable + was used by non-GNU-C macros to avoid multiple evaluation. The GNU C + library still exports it because somebody might use it. */ +struct obstack *_obstack_compat; +compat_symbol (libc, _obstack_compat, _obstack, GLIBC_2_0); +# endif +# endif /* Define a macro that either calls functions with the traditional malloc/free calling interface, or calls functions with the mmalloc/mfree interface @@ -137,9 +139,9 @@ struct obstack *_obstack; int _obstack_begin (struct obstack *h, - int size, int alignment, - void *(*chunkfun) (long), - void (*freefun) (void *)) + int size, int alignment, + void *(*chunkfun) (long), + void (*freefun) (void *)) { register struct _obstack_chunk *chunk; /* points to new chunk */ @@ -149,16 +151,16 @@ _obstack_begin (struct obstack *h, /* Default size is what GNU malloc can fit in a 4096-byte block. */ { /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc. - Use the values for range checking, because if range checking is off, - the extra bytes won't be missed terribly, but if range checking is on - and we used a larger request, a whole extra 4096 bytes would be - allocated. + Use the values for range checking, because if range checking is off, + the extra bytes won't be missed terribly, but if range checking is on + and we used a larger request, a whole extra 4096 bytes would be + allocated. - These number are irrelevant to the new GNU malloc. I suspect it is - less sensitive to the size of the request. */ + These number are irrelevant to the new GNU malloc. I suspect it is + less sensitive to the size of the request. */ int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1)) - + 4 + DEFAULT_ROUNDING - 1) - & ~(DEFAULT_ROUNDING - 1)); + + 4 + DEFAULT_ROUNDING - 1) + & ~(DEFAULT_ROUNDING - 1)); size = 4096 - extra; } @@ -171,7 +173,8 @@ _obstack_begin (struct obstack *h, chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size); if (!chunk) (*obstack_alloc_failed_handler) (); - h->next_free = h->object_base = chunk->contents; + 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; @@ -183,9 +186,9 @@ _obstack_begin (struct obstack *h, int _obstack_begin_1 (struct obstack *h, int size, int alignment, - void *(*chunkfun) (void *, long), - void (*freefun) (void *, void *), - void *arg) + void *(*chunkfun) (void *, long), + void (*freefun) (void *, void *), + void *arg) { register struct _obstack_chunk *chunk; /* points to new chunk */ @@ -195,16 +198,16 @@ _obstack_begin_1 (struct obstack *h, int size, int alignment, /* Default size is what GNU malloc can fit in a 4096-byte block. */ { /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc. - Use the values for range checking, because if range checking is off, - the extra bytes won't be missed terribly, but if range checking is on - and we used a larger request, a whole extra 4096 bytes would be - allocated. + Use the values for range checking, because if range checking is off, + the extra bytes won't be missed terribly, but if range checking is on + and we used a larger request, a whole extra 4096 bytes would be + allocated. - These number are irrelevant to the new GNU malloc. I suspect it is - less sensitive to the size of the request. */ + These number are irrelevant to the new GNU malloc. I suspect it is + less sensitive to the size of the request. */ int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1)) - + 4 + DEFAULT_ROUNDING - 1) - & ~(DEFAULT_ROUNDING - 1)); + + 4 + DEFAULT_ROUNDING - 1) + & ~(DEFAULT_ROUNDING - 1)); size = 4096 - extra; } @@ -218,7 +221,8 @@ _obstack_begin_1 (struct obstack *h, int size, int alignment, chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size); if (!chunk) (*obstack_alloc_failed_handler) (); - h->next_free = h->object_base = chunk->contents; + 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; @@ -239,7 +243,7 @@ _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 new_size; register long obj_size = h->next_free - h->object_base; register long i; long already; @@ -268,12 +272,12 @@ _obstack_newchunk (struct obstack *h, int length) 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]; + i >= 0; i--) + ((COPYING_UNIT *)object_base)[i] + = ((COPYING_UNIT *)h->object_base)[i]; /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT, - but that can cross a page boundary on a machine - which does not do strict alignment for COPYING_UNITS. */ + but that can cross a page boundary on a machine + which does not do strict alignment for COPYING_UNITS. */ already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT); } else @@ -285,7 +289,10 @@ _obstack_newchunk (struct obstack *h, int length) /* If the object just copied was the only data in OLD_CHUNK, free that chunk and remove it from the chain. But not if that chunk might contain an empty object. */ - if (h->object_base == old_chunk->contents && ! h->maybe_empty_object) + 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); @@ -306,13 +313,13 @@ libc_hidden_def (_obstack_newchunk) /* Suppress -Wmissing-prototypes warning. We don't want to declare this in obstack.h because it is just for debugging. */ -int _obstack_allocated_p (struct obstack *h, void *obj); +int _obstack_allocated_p (struct obstack *h, void *obj) _GL_ATTRIBUTE_PURE; int _obstack_allocated_p (struct obstack *h, void *obj) { - register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */ - register struct _obstack_chunk *plp; /* point to previous chunk if any */ + register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */ + register struct _obstack_chunk *plp; /* point to previous chunk if any */ lp = (h)->chunk; /* We use >= rather than > since the object cannot be exactly at @@ -332,10 +339,10 @@ _obstack_allocated_p (struct obstack *h, void *obj) # undef obstack_free void -obstack_free (struct obstack *h, void *obj) +__obstack_free (struct obstack *h, void *obj) { - register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */ - register struct _obstack_chunk *plp; /* point to previous chunk if any */ + register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */ + register struct _obstack_chunk *plp; /* point to previous chunk if any */ lp = h->chunk; /* We use >= because there cannot be an object at the beginning of a chunk. @@ -347,7 +354,7 @@ obstack_free (struct obstack *h, void *obj) CALL_FREEFUN (h, lp); lp = plp; /* If we switch chunks, we can't tell whether the new current - chunk contains an empty object, so assume that it may. */ + chunk contains an empty object, so assume that it may. */ h->maybe_empty_object = 1; } if (lp) @@ -394,15 +401,7 @@ _obstack_memory_used (struct obstack *h) # include # endif -# ifndef __attribute__ -/* This feature is available in gcc versions 2.5 and later. */ -# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) -# define __attribute__(Spec) /* empty */ -# endif -# endif - -static void -__attribute__ ((noreturn)) +static _Noreturn void print_and_abort (void) { /* Don't change any of these strings. Yes, it would be possible to add @@ -410,13 +409,12 @@ print_and_abort (void) happen because the "memory exhausted" message appears in other places like this and the translation should be reused instead of creating a very similar string which requires a separate translation. */ -# if defined _LIBC && defined USE_IN_LIBIO - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s\n", _("memory exhausted")); - else +# ifdef _LIBC + (void) __fxprintf (NULL, "%s\n", _("memory exhausted")); +# else + fprintf (stderr, "%s\n", _("memory exhausted")); # endif - fprintf (stderr, "%s\n", _("memory exhausted")); exit (obstack_exit_failure); } -#endif /* !ELIDE_CODE */ +#endif /* !ELIDE_CODE */