obstack.h
上传用户:szlgq88
上传日期:2009-04-28
资源大小:48287k
文件大小:19k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  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.    The GNU C Library is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Lesser General Public
  7.    License as published by the Free Software Foundation; either
  8.    version 2.1 of the 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.    Lesser General Public License for more details.
  13.    You should have received a copy of the GNU Lesser General Public
  14.    License along with the GNU C Library; if not, write to the Free
  15.    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16.    02111-1307 USA.  */
  17. /* Summary:
  18. All the apparent functions defined here are macros. The idea
  19. is that you would use these pre-tested macros to solve a
  20. very specific set of problems, and they would run fast.
  21. Caution: no side-effects in arguments please!! They may be
  22. evaluated MANY times!!
  23. These macros operate a stack of objects.  Each object starts life
  24. small, and may grow to maturity.  (Consider building a word syllable
  25. by syllable.)  An object can move while it is growing.  Once it has
  26. been "finished" it never changes address again.  So the "top of the
  27. stack" is typically an immature growing object, while the rest of the
  28. stack is of mature, fixed size and fixed address objects.
  29. These routines grab large chunks of memory, using a function you
  30. supply, called `obstack_chunk_alloc'.  On occasion, they free chunks,
  31. by calling `obstack_chunk_free'.  You must define them and declare
  32. them before using any obstack macros.
  33. Each independent stack is represented by a `struct obstack'.
  34. Each of the obstack macros expects a pointer to such a structure
  35. as the first argument.
  36. One motivation for this package is the problem of growing char strings
  37. in symbol tables.  Unless you are "fascist pig with a read-only mind"
  38. --Gosper's immortal quote from HAKMEM item 154, out of context--you
  39. would not like to put any arbitrary upper limit on the length of your
  40. symbols.
  41. In practice this often means you will build many short symbols and a
  42. few long symbols.  At the time you are reading a symbol you don't know
  43. how long it is.  One traditional method is to read a symbol into a
  44. buffer, realloc()ating the buffer every time you try to read a symbol
  45. that is longer than the buffer.  This is beaut, but you still will
  46. want to copy the symbol from the buffer to a more permanent
  47. symbol-table entry say about half the time.
  48. With obstacks, you can work differently.  Use one obstack for all symbol
  49. names.  As you read a symbol, grow the name in the obstack gradually.
  50. When the name is complete, finalize it.  Then, if the symbol exists already,
  51. free the newly read name.
  52. The way we do this is to take a large chunk, allocating memory from
  53. low addresses.  When you want to build a symbol in the chunk you just
  54. add chars above the current "high water mark" in the chunk.  When you
  55. have finished adding chars, because you got to the end of the symbol,
  56. you know how long the chars are, and you can create a new object.
  57. Mostly the chars will not burst over the highest address of the chunk,
  58. because you would typically expect a chunk to be (say) 100 times as
  59. long as an average object.
  60. In case that isn't clear, when we have enough chars to make up
  61. the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed)
  62. so we just point to it where it lies.  No moving of chars is
  63. needed and this is the second win: potentially long strings need
  64. never be explicitly shuffled. Once an object is formed, it does not
  65. change its address during its lifetime.
  66. When the chars burst over a chunk boundary, we allocate a larger
  67. chunk, and then copy the partly formed object from the end of the old
  68. chunk to the beginning of the new larger chunk.  We then carry on
  69. accreting characters to the end of the object as we normally would.
  70. A special macro is provided to add a single char at a time to a
  71. growing object.  This allows the use of register variables, which
  72. break the ordinary 'growth' macro.
  73. Summary:
  74. We allocate large chunks.
  75. We carve out one object at a time from the current chunk.
  76. Once carved, an object never moves.
  77. We are free to append data of any size to the currently
  78.   growing object.
  79. Exactly one object is growing in an obstack at any one time.
  80. You can run one obstack per control block.
  81. You may have as many control blocks as you dare.
  82. Because of the way we do it, you can `unwind' an obstack
  83.   back to a previous state. (You may remove objects much
  84.   as you would with a stack.)
  85. */
  86. /* Don't do the contents of this file more than once.  */
  87. #ifndef _OBSTACK_H
  88. #define _OBSTACK_H 1
  89. #ifdef __cplusplus
  90. extern "C" {
  91. #endif
  92. /* We use subtraction of (char *) 0 instead of casting to int
  93.    because on word-addressable machines a simple cast to int
  94.    may ignore the byte-within-word field of the pointer.  */
  95. #ifndef __PTR_TO_INT
  96. # define __PTR_TO_INT(P) ((P) - (char *) 0)
  97. #endif
  98. #ifndef __INT_TO_PTR
  99. # define __INT_TO_PTR(P) ((P) + (char *) 0)
  100. #endif
  101. /* We need the type of the resulting object.  If __PTRDIFF_TYPE__ is
  102.    defined, as with GNU C, use that; that way we don't pollute the
  103.    namespace with <stddef.h>'s symbols.  Otherwise, include <stddef.h>
  104.    and use ptrdiff_t.  */
  105. #ifdef __PTRDIFF_TYPE__
  106. # define PTR_INT_TYPE __PTRDIFF_TYPE__
  107. #else
  108. # include <stddef.h>
  109. # define PTR_INT_TYPE ptrdiff_t
  110. #endif
  111. #include <string.h>
  112. struct _obstack_chunk /* Lives at front of each chunk. */
  113. {
  114.   char  *limit; /* 1 past end of this chunk */
  115.   struct _obstack_chunk *prev; /* address of prior chunk or NULL */
  116.   char contents[4]; /* objects begin here */
  117. };
  118. struct obstack /* control current object in current chunk */
  119. {
  120.   long chunk_size; /* preferred size to allocate chunks in */
  121.   struct _obstack_chunk *chunk; /* address of current struct obstack_chunk */
  122.   char *object_base; /* address of object we are building */
  123.   char *next_free; /* where to add next char to current object */
  124.   char *chunk_limit; /* address of char after current chunk */
  125.   PTR_INT_TYPE temp; /* Temporary for some macros.  */
  126.   int   alignment_mask; /* Mask of alignment for each object. */
  127.   /* These prototypes vary based on `use_extra_arg', and we use
  128.      casts to the prototypeless function type in all assignments,
  129.      but having prototypes here quiets -Wstrict-prototypes.  */
  130.   struct _obstack_chunk *(*chunkfun) (void *, long);
  131.   void (*freefun) (void *, struct _obstack_chunk *);
  132.   void *extra_arg; /* first arg for chunk alloc/dealloc funcs */
  133.   unsigned use_extra_arg:1; /* chunk alloc/dealloc funcs take extra arg */
  134.   unsigned maybe_empty_object:1;/* There is a possibility that the current
  135.    chunk contains a zero-length object.  This
  136.    prevents freeing the chunk if we allocate
  137.    a bigger chunk to replace it. */
  138.   unsigned alloc_failed:1; /* No longer used, as we now call the failed
  139.    handler on error, but retained for binary
  140.    compatibility.  */
  141. };
  142. /* Declare the external functions we use; they are in obstack.c.  */
  143. extern void _obstack_newchunk (struct obstack *, int);
  144. extern int _obstack_begin (struct obstack *, int, int,
  145.     void *(*) (long), void (*) (void *));
  146. extern int _obstack_begin_1 (struct obstack *, int, int,
  147.      void *(*) (void *, long),
  148.      void (*) (void *, void *), void *);
  149. extern int _obstack_memory_used (struct obstack *);
  150. void obstack_free (struct obstack *obstack, void *block);
  151. /* Error handler called when `obstack_chunk_alloc' failed to allocate
  152.    more memory.  This can be set to a user defined function which
  153.    should either abort gracefully or use longjump - but shouldn't
  154.    return.  The default action is to print a message and abort.  */
  155. extern void (*obstack_alloc_failed_handler) (void);
  156. /* Exit value used when `print_and_abort' is used.  */
  157. extern int obstack_exit_failure;
  158. /* Pointer to beginning of object being allocated or to be allocated next.
  159.    Note that this might not be the final address of the object
  160.    because a new chunk might be needed to hold the final size.  */
  161. #define obstack_base(h) ((void *) (h)->object_base)
  162. /* Size for allocating ordinary chunks.  */
  163. #define obstack_chunk_size(h) ((h)->chunk_size)
  164. /* Pointer to next byte not yet allocated in current chunk.  */
  165. #define obstack_next_free(h) ((h)->next_free)
  166. /* Mask specifying low bits that should be clear in address of an object.  */
  167. #define obstack_alignment_mask(h) ((h)->alignment_mask)
  168. /* To prevent prototype warnings provide complete argument list.  */
  169. #define obstack_init(h)
  170.   _obstack_begin ((h), 0, 0,
  171.   (void *(*) (long)) obstack_chunk_alloc,
  172.   (void (*) (void *)) obstack_chunk_free)
  173. #define obstack_begin(h, size)
  174.   _obstack_begin ((h), (size), 0,
  175.   (void *(*) (long)) obstack_chunk_alloc,
  176.   (void (*) (void *)) obstack_chunk_free)
  177. #define obstack_specify_allocation(h, size, alignment, chunkfun, freefun)  
  178.   _obstack_begin ((h), (size), (alignment),    
  179.   (void *(*) (long)) (chunkfun),    
  180.   (void (*) (void *)) (freefun))
  181. #define obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) 
  182.   _obstack_begin_1 ((h), (size), (alignment),
  183.     (void *(*) (void *, long)) (chunkfun),
  184.     (void (*) (void *, void *)) (freefun), (arg))
  185. #define obstack_chunkfun(h, newchunkfun) 
  186.   ((h) -> chunkfun = (struct _obstack_chunk *(*)(void *, long)) (newchunkfun))
  187. #define obstack_freefun(h, newfreefun) 
  188.   ((h) -> freefun = (void (*)(void *, struct _obstack_chunk *)) (newfreefun))
  189. #define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = (achar))
  190. #define obstack_blank_fast(h,n) ((h)->next_free += (n))
  191. #define obstack_memory_used(h) _obstack_memory_used (h)
  192. #if defined __GNUC__ && defined __STDC__ && __STDC__
  193. /* NextStep 2.0 cc is really gcc 1.93 but it defines __GNUC__ = 2 and
  194.    does not implement __extension__.  But that compiler doesn't define
  195.    __GNUC_MINOR__.  */
  196. # if __GNUC__ < 2 || (__NeXT__ && !__GNUC_MINOR__)
  197. #  define __extension__
  198. # endif
  199. /* For GNU C, if not -traditional,
  200.    we can define these macros to compute all args only once
  201.    without using a global variable.
  202.    Also, we can avoid using the `temp' slot, to make faster code.  */
  203. # define obstack_object_size(OBSTACK)
  204.   __extension__
  205.   ({ struct obstack const *__o = (OBSTACK);
  206.      (unsigned) (__o->next_free - __o->object_base); })
  207. # define obstack_room(OBSTACK)
  208.   __extension__
  209.   ({ struct obstack const *__o = (OBSTACK);
  210.      (unsigned) (__o->chunk_limit - __o->next_free); })
  211. # define obstack_make_room(OBSTACK,length)
  212. __extension__
  213. ({ struct obstack *__o = (OBSTACK);
  214.    int __len = (length);
  215.    if (__o->chunk_limit - __o->next_free < __len)
  216.      _obstack_newchunk (__o, __len);
  217.    (void) 0; })
  218. # define obstack_empty_p(OBSTACK)
  219.   __extension__
  220.   ({ struct obstack const *__o = (OBSTACK);
  221.      (__o->chunk->prev == 0 && __o->next_free - __o->chunk->contents == 0); })
  222. # define obstack_grow(OBSTACK,where,length)
  223. __extension__
  224. ({ struct obstack *__o = (OBSTACK);
  225.    int __len = (length);
  226.    if (__o->next_free + __len > __o->chunk_limit)
  227.      _obstack_newchunk (__o, __len);
  228.    memcpy (__o->next_free, where, __len);
  229.    __o->next_free += __len;
  230.    (void) 0; })
  231. # define obstack_grow0(OBSTACK,where,length)
  232. __extension__
  233. ({ struct obstack *__o = (OBSTACK);
  234.    int __len = (length);
  235.    if (__o->next_free + __len + 1 > __o->chunk_limit)
  236.      _obstack_newchunk (__o, __len + 1);
  237.    memcpy (__o->next_free, where, __len);
  238.    __o->next_free += __len;
  239.    *(__o->next_free)++ = 0;
  240.    (void) 0; })
  241. # define obstack_1grow(OBSTACK,datum)
  242. __extension__
  243. ({ struct obstack *__o = (OBSTACK);
  244.    if (__o->next_free + 1 > __o->chunk_limit)
  245.      _obstack_newchunk (__o, 1);
  246.    obstack_1grow_fast (__o, datum);
  247.    (void) 0; })
  248. /* These assume that the obstack alignment is good enough for pointers
  249.    or ints, and that the data added so far to the current object
  250.    shares that much alignment.  */
  251. # define obstack_ptr_grow(OBSTACK,datum)
  252. __extension__
  253. ({ struct obstack *__o = (OBSTACK);
  254.    if (__o->next_free + sizeof (void *) > __o->chunk_limit)
  255.      _obstack_newchunk (__o, sizeof (void *));
  256.    obstack_ptr_grow_fast (__o, datum); })
  257. # define obstack_int_grow(OBSTACK,datum)
  258. __extension__
  259. ({ struct obstack *__o = (OBSTACK);
  260.    if (__o->next_free + sizeof (int) > __o->chunk_limit)
  261.      _obstack_newchunk (__o, sizeof (int));
  262.    obstack_int_grow_fast (__o, datum); })
  263. # define obstack_ptr_grow_fast(OBSTACK,aptr)
  264. __extension__
  265. ({ struct obstack *__o1 = (OBSTACK);
  266.    *(const void **) __o1->next_free = (aptr);
  267.    __o1->next_free += sizeof (const void *);
  268.    (void) 0; })
  269. # define obstack_int_grow_fast(OBSTACK,aint)
  270. __extension__
  271. ({ struct obstack *__o1 = (OBSTACK);
  272.    *(int *) __o1->next_free = (aint);
  273.    __o1->next_free += sizeof (int);
  274.    (void) 0; })
  275. # define obstack_blank(OBSTACK,length)
  276. __extension__
  277. ({ struct obstack *__o = (OBSTACK);
  278.    int __len = (length);
  279.    if (__o->chunk_limit - __o->next_free < __len)
  280.      _obstack_newchunk (__o, __len);
  281.    obstack_blank_fast (__o, __len);
  282.    (void) 0; })
  283. # define obstack_alloc(OBSTACK,length)
  284. __extension__
  285. ({ struct obstack *__h = (OBSTACK);
  286.    obstack_blank (__h, (length));
  287.    obstack_finish (__h); })
  288. # define obstack_copy(OBSTACK,where,length)
  289. __extension__
  290. ({ struct obstack *__h = (OBSTACK);
  291.    obstack_grow (__h, (where), (length));
  292.    obstack_finish (__h); })
  293. # define obstack_copy0(OBSTACK,where,length)
  294. __extension__
  295. ({ struct obstack *__h = (OBSTACK);
  296.    obstack_grow0 (__h, (where), (length));
  297.    obstack_finish (__h); })
  298. /* The local variable is named __o1 to avoid a name conflict
  299.    when obstack_blank is called.  */
  300. # define obstack_finish(OBSTACK)
  301. __extension__
  302. ({ struct obstack *__o1 = (OBSTACK);
  303.    void *__value = (void *) __o1->object_base;
  304.    if (__o1->next_free == __value)
  305.      __o1->maybe_empty_object = 1;
  306.    __o1->next_free
  307.      = __INT_TO_PTR ((__PTR_TO_INT (__o1->next_free)+__o1->alignment_mask)
  308.      & ~ (__o1->alignment_mask));
  309.    if (__o1->next_free - (char *)__o1->chunk
  310.        > __o1->chunk_limit - (char *)__o1->chunk)
  311.      __o1->next_free = __o1->chunk_limit;
  312.    __o1->object_base = __o1->next_free;
  313.    __value; })
  314. # define obstack_free(OBSTACK, OBJ)
  315. __extension__
  316. ({ struct obstack *__o = (OBSTACK);
  317.    void *__obj = (OBJ);
  318.    if (__obj > (void *)__o->chunk && __obj < (void *)__o->chunk_limit)  
  319.      __o->next_free = __o->object_base = (char *)__obj;
  320.    else (obstack_free) (__o, __obj); })
  321. #else /* not __GNUC__ or not __STDC__ */
  322. # define obstack_object_size(h) 
  323.  (unsigned) ((h)->next_free - (h)->object_base)
  324. # define obstack_room(h)
  325.  (unsigned) ((h)->chunk_limit - (h)->next_free)
  326. # define obstack_empty_p(h) 
  327.  ((h)->chunk->prev == 0 && (h)->next_free - (h)->chunk->contents == 0)
  328. /* Note that the call to _obstack_newchunk is enclosed in (..., 0)
  329.    so that we can avoid having void expressions
  330.    in the arms of the conditional expression.
  331.    Casting the third operand to void was tried before,
  332.    but some compilers won't accept it.  */
  333. # define obstack_make_room(h,length)
  334. ( (h)->temp = (length),
  335.   (((h)->next_free + (h)->temp > (h)->chunk_limit)
  336.    ? (_obstack_newchunk ((h), (h)->temp), 0) : 0))
  337. # define obstack_grow(h,where,length)
  338. ( (h)->temp = (length),
  339.   (((h)->next_free + (h)->temp > (h)->chunk_limit)
  340.    ? (_obstack_newchunk ((h), (h)->temp), 0) : 0),
  341.   memcpy ((h)->next_free, where, (h)->temp),
  342.   (h)->next_free += (h)->temp)
  343. # define obstack_grow0(h,where,length)
  344. ( (h)->temp = (length),
  345.   (((h)->next_free + (h)->temp + 1 > (h)->chunk_limit)
  346.    ? (_obstack_newchunk ((h), (h)->temp + 1), 0) : 0),
  347.   memcpy ((h)->next_free, where, (h)->temp),
  348.   (h)->next_free += (h)->temp,
  349.   *((h)->next_free)++ = 0)
  350. # define obstack_1grow(h,datum)
  351. ( (((h)->next_free + 1 > (h)->chunk_limit)
  352.    ? (_obstack_newchunk ((h), 1), 0) : 0),
  353.   obstack_1grow_fast (h, datum))
  354. # define obstack_ptr_grow(h,datum)
  355. ( (((h)->next_free + sizeof (char *) > (h)->chunk_limit)
  356.    ? (_obstack_newchunk ((h), sizeof (char *)), 0) : 0),
  357.   obstack_ptr_grow_fast (h, datum))
  358. # define obstack_int_grow(h,datum)
  359. ( (((h)->next_free + sizeof (int) > (h)->chunk_limit)
  360.    ? (_obstack_newchunk ((h), sizeof (int)), 0) : 0),
  361.   obstack_int_grow_fast (h, datum))
  362. # define obstack_ptr_grow_fast(h,aptr)
  363.   (((const void **) ((h)->next_free += sizeof (void *)))[-1] = (aptr))
  364. # define obstack_int_grow_fast(h,aint)
  365.   (((int *) ((h)->next_free += sizeof (int)))[-1] = (aptr))
  366. # define obstack_blank(h,length)
  367. ( (h)->temp = (length),
  368.   (((h)->chunk_limit - (h)->next_free < (h)->temp)
  369.    ? (_obstack_newchunk ((h), (h)->temp), 0) : 0),
  370.   obstack_blank_fast (h, (h)->temp))
  371. # define obstack_alloc(h,length)
  372.  (obstack_blank ((h), (length)), obstack_finish ((h)))
  373. # define obstack_copy(h,where,length)
  374.  (obstack_grow ((h), (where), (length)), obstack_finish ((h)))
  375. # define obstack_copy0(h,where,length)
  376.  (obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
  377. # define obstack_finish(h)
  378. ( ((h)->next_free == (h)->object_base
  379.    ? (((h)->maybe_empty_object = 1), 0)
  380.    : 0),
  381.   (h)->temp = __PTR_TO_INT ((h)->object_base),
  382.   (h)->next_free
  383.     = __INT_TO_PTR ((__PTR_TO_INT ((h)->next_free)+(h)->alignment_mask)
  384.     & ~ ((h)->alignment_mask)),
  385.   (((h)->next_free - (char *) (h)->chunk
  386.     > (h)->chunk_limit - (char *) (h)->chunk)
  387.    ? ((h)->next_free = (h)->chunk_limit) : 0),
  388.   (h)->object_base = (h)->next_free,
  389.   (void *) __INT_TO_PTR ((h)->temp))
  390. # define obstack_free(h,obj)
  391. ( (h)->temp = (char *) (obj) - (char *) (h)->chunk,
  392.   (((h)->temp > 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)
  393.    ? (int) ((h)->next_free = (h)->object_base
  394.     = (h)->temp + (char *) (h)->chunk)
  395.    : (((obstack_free) ((h), (h)->temp + (char *) (h)->chunk), 0), 0)))
  396. #endif /* not __GNUC__ or not __STDC__ */
  397. #ifdef __cplusplus
  398. } /* C++ */
  399. #endif
  400. #endif /* obstack.h */