Ignore:
Timestamp:
Jul 2, 2005, 11:38:03 PM (20 years ago)
Author:
bird
Message:

Added obstack.h and it's implementation from GLIBC.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/emx/include/obstack.h

    • Property cvs2svn:cvs-rev changed from 1.2 to 1.3
    r2154 r2155  
    1 /* obstack.h,v 1.2 2004/09/14 22:27:34 bird Exp */
     1/* obstack.h - object stack macros
     2   Copyright (C) 1988-1994,1996-1999,2003,2004 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
     6   The GNU C Library is free software; you can redistribute it and/or
     7   modify it under the terms of the GNU Lesser General Public
     8   License as published by the Free Software Foundation; either
     9   version 2.1 of the License, or (at your option) any later version.
     10
     11   The GNU C Library is distributed in the hope that it will be useful,
     12   but WITHOUT ANY WARRANTY; without even the implied warranty of
     13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14   Lesser General Public License for more details.
     15
     16   You should have received a copy of the GNU Lesser General Public
     17   License along with the GNU C Library; if not, write to the Free
     18   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
     19   02111-1307 USA.  */
    220/** @file
    3  * GNU, -liberty.
     21 * GLIBC 2.3.x CVS
    422 */
    5 
    6 /* obstack.h - object stack macros
    7    Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998,
    8    1999, 2000
    9    Free Software Foundation, Inc.
    10 
    11 
    12    NOTE: The canonical source of this file is maintained with the GNU C Library.
    13    Bugs can be reported to bug-glibc@gnu.org.
    14 
    15    This program is free software; you can redistribute it and/or modify it
    16    under the terms of the GNU General Public License as published by the
    17    Free Software Foundation; either version 2, or (at your option) any
    18    later version.
    19 
    20    This program is distributed in the hope that it will be useful,
    21    but WITHOUT ANY WARRANTY; without even the implied warranty of
    22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    23    GNU General Public License for more details.
    24 
    25    You should have received a copy of the GNU General Public License
    26    along with this program; if not, write to the Free Software
    27    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    28    USA.  */
    2923
    3024/* Summary:
     
    135129/* We need the type of the resulting object.  If __PTRDIFF_TYPE__ is
    136130   defined, as with GNU C, use that; that way we don't pollute the
    137    namespace with <stddef.h>'s symbols.  Otherwise, if <stddef.h> is
    138    available, include it and use ptrdiff_t.  In traditional C, long is
    139    the best that we can do.  */
     131   namespace with <stddef.h>'s symbols.  Otherwise, include <stddef.h>
     132   and use ptrdiff_t.  */
    140133
    141134#ifdef __PTRDIFF_TYPE__
    142135# define PTR_INT_TYPE __PTRDIFF_TYPE__
    143136#else
    144 # ifdef HAVE_STDDEF_H
    145 #  include <stddef.h>
    146 #  define PTR_INT_TYPE ptrdiff_t
    147 # else
    148 #  define PTR_INT_TYPE long
    149 # endif
     137# include <stddef.h>
     138# define PTR_INT_TYPE ptrdiff_t
    150139#endif
    151140
    152 #if defined _LIBC || defined HAVE_STRING_H
    153 # include <string.h>
    154 # if defined __STDC__ && __STDC__
    155 #  define _obstack_memcpy(To, From, N) memcpy ((To), (From), (N))
    156 # else
    157 #  define _obstack_memcpy(To, From, N) memcpy ((To), (char *)(From), (N))
    158 # endif
    159 #else
    160 # ifdef memcpy
    161 #  define _obstack_memcpy(To, From, N) memcpy ((To), (char *)(From), (N))
    162 # else
    163 #  define _obstack_memcpy(To, From, N) bcopy ((char *)(From), (To), (N))
    164 # endif
    165 #endif
     141#include <string.h>
    166142
    167143struct _obstack_chunk           /* Lives at front of each chunk. */
     
    181157  PTR_INT_TYPE temp;            /* Temporary for some macros.  */
    182158  int   alignment_mask;         /* Mask of alignment for each object. */
    183 #if defined __STDC__ && __STDC__
    184159  /* These prototypes vary based on `use_extra_arg', and we use
    185160     casts to the prototypeless function type in all assignments,
     
    188163  void (*freefun) (void *, struct _obstack_chunk *);
    189164  void *extra_arg;              /* first arg for chunk alloc/dealloc funcs */
    190 #else
    191   struct _obstack_chunk *(*chunkfun) (); /* User's fcn to allocate a chunk.  */
    192   void (*freefun) ();           /* User's function to free a chunk.  */
    193   char *extra_arg;              /* first arg for chunk alloc/dealloc funcs */
    194 #endif
    195165  unsigned use_extra_arg:1;     /* chunk alloc/dealloc funcs take extra arg */
    196166  unsigned maybe_empty_object:1;/* There is a possibility that the current
     
    205175/* Declare the external functions we use; they are in obstack.c.  */
    206176
    207 #if defined __STDC__ && __STDC__
    208177extern void _obstack_newchunk (struct obstack *, int);
    209 extern void _obstack_free (struct obstack *, void *);
    210178extern int _obstack_begin (struct obstack *, int, int,
    211179                            void *(*) (long), void (*) (void *));
     
    214182                             void (*) (void *, void *), void *);
    215183extern int _obstack_memory_used (struct obstack *);
    216 #else
    217 extern void _obstack_newchunk ();
    218 extern void _obstack_free ();
    219 extern int _obstack_begin ();
    220 extern int _obstack_begin_1 ();
    221 extern int _obstack_memory_used ();
    222 #endif
    223 
    224 
    225 #if defined __STDC__ && __STDC__
    226 
    227 /* Do the function-declarations after the structs
    228    but before defining the macros.  */
    229 
    230 void obstack_init (struct obstack *obstack);
    231 
    232 void * obstack_alloc (struct obstack *obstack, int size);
    233 
    234 void * obstack_copy (struct obstack *obstack, void *address, int size);
    235 void * obstack_copy0 (struct obstack *obstack, void *address, int size);
    236184
    237185void obstack_free (struct obstack *obstack, void *block);
    238186
    239 void obstack_blank (struct obstack *obstack, int size);
    240 
    241 void obstack_grow (struct obstack *obstack, void *data, int size);
    242 void obstack_grow0 (struct obstack *obstack, void *data, int size);
    243 
    244 void obstack_1grow (struct obstack *obstack, int data_char);
    245 void obstack_ptr_grow (struct obstack *obstack, void *data);
    246 void obstack_int_grow (struct obstack *obstack, int data);
    247 
    248 void * obstack_finish (struct obstack *obstack);
    249 
    250 int obstack_object_size (struct obstack *obstack);
    251 
    252 int obstack_room (struct obstack *obstack);
    253 void obstack_make_room (struct obstack *obstack, int size);
    254 void obstack_1grow_fast (struct obstack *obstack, int data_char);
    255 void obstack_ptr_grow_fast (struct obstack *obstack, void *data);
    256 void obstack_int_grow_fast (struct obstack *obstack, int data);
    257 void obstack_blank_fast (struct obstack *obstack, int size);
    258 
    259 void * obstack_base (struct obstack *obstack);
    260 void * obstack_next_free (struct obstack *obstack);
    261 int obstack_alignment_mask (struct obstack *obstack);
    262 int obstack_chunk_size (struct obstack *obstack);
    263 int obstack_memory_used (struct obstack *obstack);
    264 
    265 #endif /* __STDC__ */
    266 
    267 /* Non-ANSI C cannot really support alternative functions for these macros,
    268    so we do not declare them.  */
     187
    269188
    270189/* Error handler called when `obstack_chunk_alloc' failed to allocate
    271    more memory.  This can be set to a user defined function.  The
    272    default action is to print a message and abort.  */
    273 #if defined __STDC__ && __STDC__
     190   more memory.  This can be set to a user defined function which
     191   should either abort gracefully or use longjump - but shouldn't
     192   return.  The default action is to print a message and abort.  */
    274193extern void (*obstack_alloc_failed_handler) (void);
    275 #else
    276 extern void (*obstack_alloc_failed_handler) ();
    277 #endif
    278194
    279195/* Exit value used when `print_and_abort' is used.  */
     
    285201   because a new chunk might be needed to hold the final size.  */
    286202
    287 #define obstack_base(h) ((h)->object_base)
     203#define obstack_base(h) ((void *) (h)->object_base)
    288204
    289205/* Size for allocating ordinary chunks.  */
     
    299215#define obstack_alignment_mask(h) ((h)->alignment_mask)
    300216
    301 /* To prevent prototype warnings provide complete argument list in
    302    standard C version.  */
    303 #if defined __STDC__ && __STDC__
    304 
    305 # define obstack_init(h) \
    306   _obstack_begin ((h), 0, 0, \
    307                   (void *(*) (long)) obstack_chunk_alloc, (void (*) (void *)) obstack_chunk_free)
    308 
    309 # define obstack_begin(h, size) \
    310   _obstack_begin ((h), (size), 0, \
    311                   (void *(*) (long)) obstack_chunk_alloc, (void (*) (void *)) obstack_chunk_free)
    312 
    313 # define obstack_specify_allocation(h, size, alignment, chunkfun, freefun) \
    314   _obstack_begin ((h), (size), (alignment), \
    315                     (void *(*) (long)) (chunkfun), (void (*) (void *)) (freefun))
    316 
    317 # define obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) \
    318   _obstack_begin_1 ((h), (size), (alignment), \
    319                     (void *(*) (void *, long)) (chunkfun), \
     217/* To prevent prototype warnings provide complete argument list.  */
     218#define obstack_init(h)                                         \
     219  _obstack_begin ((h), 0, 0,                                    \
     220                  (void *(*) (long)) obstack_chunk_alloc,       \
     221                  (void (*) (void *)) obstack_chunk_free)
     222
     223#define obstack_begin(h, size)                                  \
     224  _obstack_begin ((h), (size), 0,                               \
     225                  (void *(*) (long)) obstack_chunk_alloc,       \
     226                  (void (*) (void *)) obstack_chunk_free)
     227
     228#define obstack_specify_allocation(h, size, alignment, chunkfun, freefun)  \
     229  _obstack_begin ((h), (size), (alignment),                                \
     230                  (void *(*) (long)) (chunkfun),                          \
     231                  (void (*) (void *)) (freefun))
     232
     233#define obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) \
     234  _obstack_begin_1 ((h), (size), (alignment),                           \
     235                    (void *(*) (void *, long)) (chunkfun),              \
    320236                    (void (*) (void *, void *)) (freefun), (arg))
    321237
    322 # define obstack_chunkfun(h, newchunkfun) \
     238#define obstack_chunkfun(h, newchunkfun) \
    323239  ((h) -> chunkfun = (struct _obstack_chunk *(*)(void *, long)) (newchunkfun))
    324240
    325 # define obstack_freefun(h, newfreefun) \
     241#define obstack_freefun(h, newfreefun) \
    326242  ((h) -> freefun = (void (*)(void *, struct _obstack_chunk *)) (newfreefun))
    327243
    328 #else
    329 
    330 # define obstack_init(h) \
    331   _obstack_begin ((h), 0, 0, \
    332                   (void *(*) ()) obstack_chunk_alloc, (void (*) ()) obstack_chunk_free)
    333 
    334 # define obstack_begin(h, size) \
    335   _obstack_begin ((h), (size), 0, \
    336                   (void *(*) ()) obstack_chunk_alloc, (void (*) ()) obstack_chunk_free)
    337 
    338 # define obstack_specify_allocation(h, size, alignment, chunkfun, freefun) \
    339   _obstack_begin ((h), (size), (alignment), \
    340                     (void *(*) ()) (chunkfun), (void (*) ()) (freefun))
    341 
    342 # define obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) \
    343   _obstack_begin_1 ((h), (size), (alignment), \
    344                     (void *(*) ()) (chunkfun), (void (*) ()) (freefun), (arg))
    345 
    346 # define obstack_chunkfun(h, newchunkfun) \
    347   ((h) -> chunkfun = (struct _obstack_chunk *(*)()) (newchunkfun))
    348 
    349 # define obstack_freefun(h, newfreefun) \
    350   ((h) -> freefun = (void (*)()) (newfreefun))
    351 
    352 #endif
    353 
    354 #define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = achar)
     244#define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = (achar))
    355245
    356246#define obstack_blank_fast(h,n) ((h)->next_free += (n))
     
    363253   does not implement __extension__.  But that compiler doesn't define
    364254   __GNUC_MINOR__.  */
    365 # if __GNUC__ < 2 || (__NeXT__ && !__GNUC_MINOR__)
     255# if __GNUC__ < 2 || (defined __NeXT__ && __NeXT__ && !__GNUC_MINOR__)
    366256#  define __extension__
    367257# endif
     
    374264# define obstack_object_size(OBSTACK)                                   \
    375265  __extension__                                                         \
    376   ({ struct obstack *__o = (OBSTACK);                                   \
     266  ({ struct obstack const *__o = (OBSTACK);                             \
    377267     (unsigned) (__o->next_free - __o->object_base); })
    378268
    379269# define obstack_room(OBSTACK)                                          \
    380270  __extension__                                                         \
    381   ({ struct obstack *__o = (OBSTACK);                                   \
     271  ({ struct obstack const *__o = (OBSTACK);                             \
    382272     (unsigned) (__o->chunk_limit - __o->next_free); })
    383273
     
    392282# define obstack_empty_p(OBSTACK)                                       \
    393283  __extension__                                                         \
    394   ({ struct obstack *__o = (OBSTACK);                                   \
     284  ({ struct obstack const *__o = (OBSTACK);                             \
    395285     (__o->chunk->prev == 0 && __o->next_free - __o->chunk->contents == 0); })
    396286
     
    401291   if (__o->next_free + __len > __o->chunk_limit)                       \
    402292     _obstack_newchunk (__o, __len);                                    \
    403    _obstack_memcpy (__o->next_free, (where), __len);                    \
     293   memcpy (__o->next_free, where, __len);                               \
    404294   __o->next_free += __len;                                             \
    405295   (void) 0; })
     
    411301   if (__o->next_free + __len + 1 > __o->chunk_limit)                   \
    412302     _obstack_newchunk (__o, __len + 1);                                \
    413    _obstack_memcpy (__o->next_free, (where), __len);                    \
     303   memcpy (__o->next_free, where, __len);                               \
    414304   __o->next_free += __len;                                             \
    415305   *(__o->next_free)++ = 0;                                             \
     
    421311   if (__o->next_free + 1 > __o->chunk_limit)                           \
    422312     _obstack_newchunk (__o, 1);                                        \
    423    *(__o->next_free)++ = (datum);                                       \
    424    (void) 0; })
    425 
    426 /* These assume that the obstack alignment is good enough for pointers or ints,
    427    and that the data added so far to the current object
     313   obstack_1grow_fast (__o, datum);                                     \
     314   (void) 0; })
     315
     316/* These assume that the obstack alignment is good enough for pointers
     317   or ints, and that the data added so far to the current object
    428318   shares that much alignment.  */
    429319
     
    433323   if (__o->next_free + sizeof (void *) > __o->chunk_limit)             \
    434324     _obstack_newchunk (__o, sizeof (void *));                          \
    435    *((void **)__o->next_free)++ = ((void *)datum);                      \
    436    (void) 0; })
     325   obstack_ptr_grow_fast (__o, datum); })                               \
    437326
    438327# define obstack_int_grow(OBSTACK,datum)                                \
     
    441330   if (__o->next_free + sizeof (int) > __o->chunk_limit)                \
    442331     _obstack_newchunk (__o, sizeof (int));                             \
    443    *((int *)__o->next_free)++ = ((int)datum);                           \
    444    (void) 0; })
    445 
    446 # define obstack_ptr_grow_fast(h,aptr) (*((void **) (h)->next_free)++ = (void *)aptr)
    447 # define obstack_int_grow_fast(h,aint) (*((int *) (h)->next_free)++ = (int) aint)
     332   obstack_int_grow_fast (__o, datum); })
     333
     334# define obstack_ptr_grow_fast(OBSTACK,aptr)                            \
     335__extension__                                                           \
     336({ struct obstack *__o1 = (OBSTACK);                                    \
     337   *(const void **) __o1->next_free = (aptr);                           \
     338   __o1->next_free += sizeof (const void *);                            \
     339   (void) 0; })
     340
     341# define obstack_int_grow_fast(OBSTACK,aint)                            \
     342__extension__                                                           \
     343({ struct obstack *__o1 = (OBSTACK);                                    \
     344   *(int *) __o1->next_free = (aint);                                   \
     345   __o1->next_free += sizeof (int);                                     \
     346   (void) 0; })
    448347
    449348# define obstack_blank(OBSTACK,length)                                  \
     
    453352   if (__o->chunk_limit - __o->next_free < __len)                       \
    454353     _obstack_newchunk (__o, __len);                                    \
    455    __o->next_free += __len;                                             \
     354   obstack_blank_fast (__o, __len);                                     \
    456355   (void) 0; })
    457356
     
    476375/* The local variable is named __o1 to avoid a name conflict
    477376   when obstack_blank is called.  */
    478 # define obstack_finish(OBSTACK)                                        \
     377# define obstack_finish(OBSTACK)                                        \
    479378__extension__                                                           \
    480379({ struct obstack *__o1 = (OBSTACK);                                    \
    481    void *value;                                                         \
    482    value = (void *) __o1->object_base;                                  \
    483    if (__o1->next_free == value)                                        \
     380   void *__value = (void *) __o1->object_base;                          \
     381   if (__o1->next_free == __value)                                      \
    484382     __o1->maybe_empty_object = 1;                                      \
    485383   __o1->next_free                                                      \
     
    490388     __o1->next_free = __o1->chunk_limit;                               \
    491389   __o1->object_base = __o1->next_free;                                 \
    492    value; })
     390   __value; })
    493391
    494392# define obstack_free(OBSTACK, OBJ)                                     \
     
    497395   void *__obj = (OBJ);                                                 \
    498396   if (__obj > (void *)__o->chunk && __obj < (void *)__o->chunk_limit)  \
    499      __o->next_free = __o->object_base = __obj;                         \
     397     __o->next_free = __o->object_base = (char *)__obj;                 \
    500398   else (obstack_free) (__o, __obj); })
    501399
     
    527425  (((h)->next_free + (h)->temp > (h)->chunk_limit)                      \
    528426   ? (_obstack_newchunk ((h), (h)->temp), 0) : 0),                      \
    529   _obstack_memcpy ((h)->next_free, (where), (h)->temp),                 \
     427  memcpy ((h)->next_free, where, (h)->temp),                            \
    530428  (h)->next_free += (h)->temp)
    531429
     
    534432  (((h)->next_free + (h)->temp + 1 > (h)->chunk_limit)                  \
    535433   ? (_obstack_newchunk ((h), (h)->temp + 1), 0) : 0),                  \
    536   _obstack_memcpy ((h)->next_free, (where), (h)->temp),                 \
     434  memcpy ((h)->next_free, where, (h)->temp),                            \
    537435  (h)->next_free += (h)->temp,                                          \
    538436  *((h)->next_free)++ = 0)
     
    541439( (((h)->next_free + 1 > (h)->chunk_limit)                              \
    542440   ? (_obstack_newchunk ((h), 1), 0) : 0),                              \
    543   (*((h)->next_free)++ = (datum)))
     441  obstack_1grow_fast (h, datum))
    544442
    545443# define obstack_ptr_grow(h,datum)                                      \
    546444( (((h)->next_free + sizeof (char *) > (h)->chunk_limit)                \
    547445   ? (_obstack_newchunk ((h), sizeof (char *)), 0) : 0),                \
    548   (*((char **) (((h)->next_free+=sizeof(char *))-sizeof(char *))) = ((char *) datum)))
     446  obstack_ptr_grow_fast (h, datum))
    549447
    550448# define obstack_int_grow(h,datum)                                      \
    551449( (((h)->next_free + sizeof (int) > (h)->chunk_limit)                   \
    552450   ? (_obstack_newchunk ((h), sizeof (int)), 0) : 0),                   \
    553   (*((int *) (((h)->next_free+=sizeof(int))-sizeof(int))) = ((int) datum)))
    554 
    555 # define obstack_ptr_grow_fast(h,aptr) (*((char **) (h)->next_free)++ = (char *) aptr)
    556 # define obstack_int_grow_fast(h,aint) (*((int *) (h)->next_free)++ = (int) aint)
     451  obstack_int_grow_fast (h, datum))
     452
     453# define obstack_ptr_grow_fast(h,aptr)                                  \
     454  (((const void **) ((h)->next_free += sizeof (void *)))[-1] = (aptr))
     455
     456# define obstack_int_grow_fast(h,aint)                                  \
     457  (((int *) ((h)->next_free += sizeof (int)))[-1] = (aptr))
    557458
    558459# define obstack_blank(h,length)                                        \
     
    560461  (((h)->chunk_limit - (h)->next_free < (h)->temp)                      \
    561462   ? (_obstack_newchunk ((h), (h)->temp), 0) : 0),                      \
    562   ((h)->next_free += (h)->temp))
     463  obstack_blank_fast (h, (h)->temp))
    563464
    564465# define obstack_alloc(h,length)                                        \
     
    571472 (obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
    572473
    573 # define obstack_finish(h)                                              \
     474# define obstack_finish(h)                                              \
    574475( ((h)->next_free == (h)->object_base                                   \
    575476   ? (((h)->maybe_empty_object = 1), 0)                                 \
     
    583484   ? ((h)->next_free = (h)->chunk_limit) : 0),                          \
    584485  (h)->object_base = (h)->next_free,                                    \
    585   __INT_TO_PTR ((h)->temp))
    586 
    587 # if defined __STDC__ && __STDC__
    588 #  define obstack_free(h,obj)                                           \
     486  (void *) __INT_TO_PTR ((h)->temp))
     487
     488# define obstack_free(h,obj)                                            \
    589489( (h)->temp = (char *) (obj) - (char *) (h)->chunk,                     \
    590490  (((h)->temp > 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\
     
    592492            = (h)->temp + (char *) (h)->chunk)                          \
    593493   : (((obstack_free) ((h), (h)->temp + (char *) (h)->chunk), 0), 0)))
    594 # else
    595 #  define obstack_free(h,obj)                                           \
    596 ( (h)->temp = (char *) (obj) - (char *) (h)->chunk,                     \
    597   (((h)->temp > 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\
    598    ? (int) ((h)->next_free = (h)->object_base                           \
    599             = (h)->temp + (char *) (h)->chunk)                          \
    600    : (_obstack_free ((h), (h)->temp + (char *) (h)->chunk), 0)))
    601 # endif
    602494
    603495#endif /* not __GNUC__ or not __STDC__ */
Note: See TracChangeset for help on using the changeset viewer.