home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / m / m4v05as.zip / OBSTACK.H < prev    next >
C/C++ Source or Header  |  1992-02-22  |  17KB  |  461 lines

  1. /* obstack.h - object stack macros
  2.    Copyright (C) 1988 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 1, or (at your option) any
  7. later version.
  8.  
  9. This program 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
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.  
  19. In other words, you are welcome to use, share and improve this program.
  20. You are forbidden to forbid anyone else to use, share and improve
  21. what you give them.   Help stamp out software-hoarding!  */
  22.  
  23.  
  24. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  25.  
  26. WARNING: This is still a beta version of the port, proper functioning
  27. with far pointers has not yet been verified completely.
  28.  
  29. This port is also distributed under the terms of the GNU General Public
  30. License as published by the Free Software Foundation.
  31.  
  32. Please note that this file is not identical to the original GNU release,
  33. you should have received this code as patch to the official release.
  34.  
  35. $Header: e:/gnu/m4/RCS/obstack.h 0.5.1.0 90/09/28 18:36:25 tho Exp $  */
  36.  
  37.  
  38. /* Summary:
  39.  
  40. All the apparent functions defined here are macros. The idea
  41. is that you would use these pre-tested macros to solve a
  42. very specific set of problems, and they would run fast.
  43. Caution: no side-effects in arguments please!! They may be
  44. evaluated MANY times!!
  45.  
  46. These macros operate a stack of objects.  Each object starts life
  47. small, and may grow to maturity.  (Consider building a word syllable
  48. by syllable.)  An object can move while it is growing.  Once it has
  49. been "finished" it never changes address again.  So the "top of the
  50. stack" is typically an immature growing object, while the rest of the
  51. stack is of mature, fixed size and fixed address objects.
  52.  
  53. These routines grab large chunks of memory, using a function you
  54. supply, called `obstack_chunk_alloc'.  On occasion, they free chunks,
  55. by calling `obstack_chunk_free'.  You must define them and declare
  56. them before using any obstack macros.
  57.  
  58. Each independent stack is represented by a `struct obstack'.
  59. Each of the obstack macros expects a pointer to such a structure
  60. as the first argument.
  61.  
  62. One motivation for this package is the problem of growing char strings
  63. in symbol tables.  Unless you are "facist pig with a read-only mind"
  64. [Gosper's immortal quote from HAKMEM item 154, out of context] you
  65. would not like to put any arbitrary upper limit on the length of your
  66. symbols.
  67.  
  68. In practice this often means you will build many short symbols and a
  69. few long symbols.  At the time you are reading a symbol you don't know
  70. how long it is.  One traditional method is to read a symbol into a
  71. buffer, realloc()ating the buffer every time you try to read a symbol
  72. that is longer than the buffer.  This is beaut, but you still will
  73. want to copy the symbol from the buffer to a more permanent
  74. symbol-table entry say about half the time.
  75.  
  76. With obstacks, you can work differently.  Use one obstack for all symbol
  77. names.  As you read a symbol, grow the name in the obstack gradually.
  78. When the name is complete, finalize it.  Then, if the symbol exists already,
  79. free the newly read name.
  80.  
  81. The way we do this is to take a large chunk, allocating memory from
  82. low addresses.  When you want to build a aymbol in the chunk you just
  83. add chars above the current "high water mark" in the chunk.  When you
  84. have finished adding chars, because you got to the end of the symbol,
  85. you know how long the chars are, and you can create a new object.
  86. Mostly the chars will not burst over the highest address of the chunk,
  87. because you would typically expect a chunk to be (say) 100 times as
  88. long as an average object.
  89.  
  90. In case that isn't clear, when we have enough chars to make up
  91. the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed)
  92. so we just point to it where it lies.  No moving of chars is
  93. needed and this is the second win: potentially long strings need
  94. never be explicitly shuffled. Once an object is formed, it does not
  95. change its address during its lifetime.
  96.  
  97. When the chars burst over a chunk boundary, we allocate a larger
  98. chunk, and then copy the partly formed object from the end of the old
  99. chunk to the beggining of the new larger chunk.  We then carry on
  100. accreting characters to the end of the object as we normaly would.
  101.  
  102. A special macro is provided to add a single char at a time to a
  103. growing object.  This allows the use of register variables, which
  104. break the ordinary 'growth' macro.
  105.  
  106. Summary:
  107.     We allocate large chunks.
  108.     We carve out one object at a time from the current chunk.
  109.     Once carved, an object never moves.
  110.     We are free to append data of any size to the currently
  111.       growing object.
  112.     Exactly one object is growing in an obstack at any one time.
  113.     You can run one obstack per control block.
  114.     You may have as many control blocks as you dare.
  115.     Because of the way we do it, you can `unwind' a obstack
  116.       back to a previous state. (You may remove objects much
  117.       as you would with a stack.)
  118. */
  119.  
  120.  
  121. /* Don't do the contents of this file more than once.  */
  122.  
  123. #ifndef __OBSTACKS__
  124. #define __OBSTACKS__
  125.  
  126. #ifdef MSDOS
  127. #ifndef FILE
  128. #include <stdio.h>
  129. #endif
  130. #define temp16    tmp._16
  131. #define temp32    tmp._32
  132. #if defined(M_I86CM) || defined(M_I86LM)    /* Intel 80x86, _far pointers.  */
  133. #define LONG    long
  134. #define SIZE_T    size_t
  135. #define HUGE    _huge
  136. #define PDIFF(p1, p2) \
  137.   ((long)((char HUGE *) (p1) - (char HUGE *) (p2)))
  138. #else                        /* Intel 80x86, _near pointers.  */
  139. #define LONG    int
  140. #define SIZE_T    size_t
  141. #define HUGE
  142. #define PDIFF(p1, p2) ((char *) (p1) - (char *) (p2))
  143. #endif
  144. #else /* not MSDOS */                /* `real' computers ...  */
  145. #define temp16    temp
  146. #define temp32    temp
  147. #define LONG    int
  148. #define SIZE_T    long
  149. #define HUGE
  150. #define PDIFF(p1, p2) ((char *) (p1) - (char *) (p2))
  151. #endif /* not MSDOS */
  152.  
  153.  
  154. /* We use subtraction of (char *)0 instead of casting to int
  155.    because on word-addressable machines a simple cast to int
  156.    may ignore the byte-within-word field of the pointer.  */
  157.  
  158. #ifndef __PTR_TO_INT
  159. #ifdef MSDOS
  160. #define __PTR_TO_INT(P) ((LONG)(P))
  161. #else
  162. #define __PTR_TO_INT(P) ((P) - (char *)0)
  163. #endif
  164. #endif
  165.  
  166. #ifndef __INT_TO_PTR
  167. #ifdef MSDOS
  168. #define __INT_TO_PTR(P) ((char HUGE *)(P))
  169. #else
  170. #define __INT_TO_PTR(P) ((P) + (char *)0)
  171. #endif
  172. #endif
  173.  
  174. struct _obstack_chunk        /* Lives at front of each chunk. */
  175. {
  176.   char  *limit;            /* 1 past end of this chunk */
  177.   struct _obstack_chunk *prev;    /* address of prior chunk or NULL */
  178.   char    contents[4];        /* objects begin here */
  179. };
  180.  
  181. struct obstack        /* control current object in current chunk */
  182. {
  183.   SIZE_T chunk_size;        /* preferred size to allocate chunks in */
  184.   struct _obstack_chunk* chunk;    /* address of current struct obstack_chunk */
  185.   char    *object_base;        /* address of object we are building */
  186.   char    *next_free;        /* where to add next char to current object */
  187.   char    *chunk_limit;        /* address of char after current chunk */
  188. #ifdef MSDOS
  189.   union                /* Temporary for some macros.  */
  190.     {
  191.       LONG _32;            /* 32 bit for _far pointer differences */
  192.       size_t _16;        /* 16 bit for malloc()  */
  193.     } tmp;
  194.   LONG  alignment_mask;        /* Mask of alignment for each object. */
  195.   struct _obstack_chunk *(*chunkfun) (unsigned int size);
  196.                 /* User's fcn to allocate a chunk.  */
  197.   void (*freefun) (char *p);    /* User's function to free a chunk.  */
  198. #else /* not MSDOS */
  199.   int    temp;            /* Temporary for some macros.  */
  200.   int   alignment_mask;        /* Mask of alignment for each object. */
  201.   struct _obstack_chunk *(*chunkfun) (); /* User's fcn to allocate a chunk.  */
  202.   void (*freefun) ();        /* User's function to free a chunk.  */
  203. #endif /* not MSDOS */
  204. };
  205.  
  206. #ifdef __STDC__
  207.  
  208. /* Do the function-declarations after the structs
  209.    but before defining the macros.  */
  210.  
  211. void obstack_init (struct obstack *obstack);
  212.  
  213. void * obstack_alloc (struct obstack *obstack, int size);
  214.  
  215. void * obstack_copy (struct obstack *obstack, void *address, int size);
  216. void * obstack_copy0 (struct obstack *obstack, void *address, int size);
  217.  
  218. void obstack_free (struct obstack *obstack, void *block);
  219.  
  220. void obstack_blank (struct obstack *obstack, int size);
  221.  
  222. void obstack_grow (struct obstack *obstack, void *data, int size);
  223. void obstack_grow0 (struct obstack *obstack, void *data, int size);
  224.  
  225. void obstack_1grow (struct obstack *obstack, int data_char);
  226.  
  227. void * obstack_finish (struct obstack *obstack);
  228.  
  229. int obstack_object_size (struct obstack *obstack);
  230.  
  231. int obstack_room (struct obstack *obstack);
  232. void obstack_1grow_fast (struct obstack *obstack, int data_char);
  233. void obstack_blank_fast (struct obstack *obstack, int size);
  234.  
  235. void * obstack_base (struct obstack *obstack);
  236. void * obstack_next_free (struct obstack *obstack);
  237. int obstack_alignment_mask (struct obstack *obstack);
  238. int obstack_chunk_size (struct obstack *obstack);
  239.  
  240. #endif /* __STDC__ */
  241.  
  242. /* Non-ANSI C cannot really support alternative functions for these macros,
  243.    so we do not declare them.  */
  244.  
  245. /* Pointer to beginning of object being allocated or to be allocated next.
  246.    Note that this might not be the final address of the object
  247.    because a new chunk might be needed to hold the final size.  */
  248.  
  249. #define obstack_base(h) ((h)->object_base)
  250.  
  251. /* Size for allocating ordinary chunks.  */
  252.  
  253. #define obstack_chunk_size(h) ((h)->chunk_size)
  254.  
  255. /* Pointer to next byte not yet allocated in current chunk.  */
  256.  
  257. #define obstack_next_free(h)    ((h)->next_free)
  258.  
  259. /* Mask specifying low bits that should be clear in address of an object.  */
  260.  
  261. #define obstack_alignment_mask(h) ((h)->alignment_mask)
  262.  
  263. #define obstack_init(h) \
  264.   _obstack_begin ((h), 0, 0, obstack_chunk_alloc, obstack_chunk_free)
  265.  
  266. #define obstack_begin(h, size) \
  267.   _obstack_begin ((h), (size), 0, obstack_chunk_alloc, obstack_chunk_free)
  268.  
  269. #define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = achar)
  270.  
  271. #define obstack_blank_fast(h,n) ((h)->next_free += (n))
  272.  
  273. #if defined (__GNUC__) && defined (__STDC__)
  274.  
  275. /* For GNU C, if not -traditional,
  276.    we can define these macros to compute all args only once
  277.    without using a global variable.
  278.    Also, we can avoid using the `temp' slot, to make faster code.  */
  279.  
  280. #define obstack_object_size(OBSTACK)                    \
  281.   ({ struct obstack *__o = (OBSTACK);                    \
  282.      (unsigned) (__o->next_free - __o->object_base); })
  283.  
  284. #define obstack_room(OBSTACK)                        \
  285.   ({ struct obstack *__o = (OBSTACK);                    \
  286.      (unsigned) (__o->chunk_limit - __o->next_free); })
  287.  
  288. #define obstack_grow(OBSTACK,where,length)                \
  289. ({ struct obstack *__o = (OBSTACK);                    \
  290.    int __len = (length);                        \
  291.    ((__o->next_free + __len > __o->chunk_limit)                \
  292.     ? _obstack_newchunk (__o, __len) : 0);                \
  293.    bcopy (where, __o->next_free, __len);                \
  294.    __o->next_free += __len;                        \
  295.    (void) 0; })
  296.  
  297. #define obstack_grow0(OBSTACK,where,length)                \
  298. ({ struct obstack *__o = (OBSTACK);                    \
  299.    int __len = (length);                        \
  300.    ((__o->next_free + __len + 1 > __o->chunk_limit)            \
  301.     ? _obstack_newchunk (__o, __len + 1) : 0),                \
  302.    bcopy (where, __o->next_free, __len),                \
  303.    __o->next_free += __len,                        \
  304.    *(__o->next_free)++ = 0;                        \
  305.    (void) 0; })
  306.  
  307. #define obstack_1grow(OBSTACK,datum)                    \
  308. ({ struct obstack *__o = (OBSTACK);                    \
  309.    ((__o->next_free + 1 > __o->chunk_limit)                \
  310.     ? _obstack_newchunk (__o, 1) : 0),                    \
  311.    *(__o->next_free)++ = (datum);                    \
  312.    (void) 0; })
  313.  
  314. #define obstack_blank(OBSTACK,length)                    \
  315. ({ struct obstack *__o = (OBSTACK);                    \
  316.    int __len = (length);                        \
  317.    ((__o->next_free + __len > __o->chunk_limit)                \
  318.     ? _obstack_newchunk (__o, __len) : 0);                \
  319.    __o->next_free += __len;                        \
  320.    (void) 0; })
  321.  
  322. #define obstack_alloc(OBSTACK,length)                    \
  323. ({ struct obstack *__h = (OBSTACK);                    \
  324.    obstack_blank (__h, (length));                    \
  325.    obstack_finish (__h); })
  326.  
  327. #define obstack_copy(OBSTACK,where,length)                \
  328. ({ struct obstack *__h = (OBSTACK);                    \
  329.    obstack_grow (__h, (where), (length));                \
  330.    obstack_finish (__h); })
  331.  
  332. #define obstack_copy0(OBSTACK,where,length)                \
  333. ({ struct obstack *__h = (OBSTACK);                    \
  334.    obstack_grow0 (__h, (where), (length));                \
  335.    obstack_finish (__h); })
  336.  
  337. #define obstack_finish(OBSTACK)                      \
  338. ({ struct obstack *__o = (OBSTACK);                    \
  339.    void *value = (void *) __o->object_base;                \
  340.    __o->next_free                            \
  341.      = __INT_TO_PTR ((__PTR_TO_INT (__o->next_free)+__o->alignment_mask)\
  342.              & ~ (__o->alignment_mask));            \
  343.    ((__o->next_free - (char *)__o->chunk                \
  344.      > __o->chunk_limit - (char *)__o->chunk)                \
  345.     ? (__o->next_free = __o->chunk_limit) : 0);                \
  346.    __o->object_base = __o->next_free;                    \
  347.    value; })
  348.  
  349. #define obstack_free(OBSTACK, OBJ)                    \
  350. ({ struct obstack *__o = (OBSTACK);                    \
  351.    void *__obj = (OBJ);                            \
  352.    if (__obj >= (void *)__o->chunk && __obj < (void *)__o->chunk_limit) \
  353.      __o->next_free = __o->object_base = __obj;                \
  354.    else (obstack_free) (__o, __obj); })
  355.  
  356. #else /* not __GNUC__ or not __STDC__ */
  357.  
  358. /* The non-GNU macros copy the obstack-pointer into this global variable
  359.    to avoid multiple evaluation.  */
  360.  
  361. extern struct obstack *_obstack;
  362.  
  363. #define obstack_object_size(h) \
  364.  (unsigned) (_obstack = (h), PDIFF ((h)->next_free, (h)->object_base))
  365.  
  366. #define obstack_room(h)        \
  367.  (unsigned) (_obstack = (h), PDIFF ((h)->chunk_limit, (h)->next_free))
  368.  
  369. #define obstack_grow(h,where,length)                    \
  370. ( (h)->temp16 = (length),                        \
  371.   (((h)->next_free + (h)->temp16 > (h)->chunk_limit)            \
  372.    ? _obstack_newchunk ((h), (h)->temp16) : 0),                \
  373.   bcopy (where, (h)->next_free, (h)->temp16),                \
  374.   (h)->next_free += (h)->temp16)
  375.  
  376. #define obstack_grow0(h,where,length)                    \
  377. ( (h)->temp16 = (length),                        \
  378.   (((h)->next_free + (h)->temp16 + 1 > (h)->chunk_limit)        \
  379.    ? _obstack_newchunk ((h), (h)->temp16 + 1) : 0),            \
  380.   bcopy (where, (h)->next_free, (h)->temp16),                \
  381.   (h)->next_free += (h)->temp16,                    \
  382.   *((h)->next_free)++ = 0)
  383.  
  384. #define obstack_1grow(h,datum)                        \
  385. ( (((h)->next_free + 1 > (h)->chunk_limit)                \
  386.    ? _obstack_newchunk ((h), 1) : 0),                    \
  387.   *((h)->next_free)++ = (char) (datum))
  388.  
  389. #define obstack_blank(h,length)                        \
  390. ( (h)->temp16 = (length),                        \
  391.   (((h)->next_free + (h)->temp16 > (h)->chunk_limit)            \
  392.    ? _obstack_newchunk ((h), (h)->temp16) : 0),                \
  393.   (h)->next_free += (h)->temp16)
  394.  
  395. #define obstack_alloc(h,length)                        \
  396.  (obstack_blank ((h), (length)), obstack_finish ((h)))
  397.  
  398. #define obstack_copy(h,where,length)                    \
  399.  (obstack_grow ((h), (where), (length)), obstack_finish ((h)))
  400.  
  401. #define obstack_copy0(h,where,length)                    \
  402.  (obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
  403.  
  404. #define obstack_finish(h)                          \
  405. ( (h)->temp32 = __PTR_TO_INT ((h)->object_base),            \
  406.   (h)->next_free                            \
  407.     = __INT_TO_PTR ((__PTR_TO_INT ((h)->next_free)+(h)->alignment_mask)    \
  408.             & ~ ((h)->alignment_mask)),                \
  409.   ((PDIFF ((h)->next_free, (h)->chunk)                    \
  410.     > PDIFF ((h)->chunk_limit, (h)->chunk))                \
  411.    ? ((h)->next_free = (h)->chunk_limit) : 0),                \
  412.   (h)->object_base = (h)->next_free,                    \
  413.   __INT_TO_PTR ((h)->temp32))
  414.  
  415.  
  416. #if defined(M_I86CM) || defined(M_I86LM)    /* Intel 80x86, far pointers.   */
  417.  
  418. /* We assume here that
  419.  
  420.      (char *)(obj) ==
  421.     ((char *)(obj) - (char *) (h)->chunk) + (char *) (h)->chunk)
  422.  
  423.    Which might be not true because of alignment (???), but this
  424.    avoids a pointer subtraction, which is always a problem in segmented
  425.    architecture .... */
  426.  
  427. #define obstack_free(h,obj)                        \
  428. ( (h)->temp32 = PDIFF (obj, (h)->chunk),                \
  429.   (((h)->temp32 >= 0 && (h)->temp32                    \
  430.     < PDIFF ((h)->chunk_limit, (h)->chunk))                \
  431.    ? (LONG) ((h)->next_free = (h)->object_base = (char *) (obj))    \
  432.    : ((obstack_free) ((h), (char *) (obj)), 0)))
  433.  
  434. #else /* not  (M_I86CM || M_I86LM) */
  435.  
  436. #ifdef __STDC__
  437. #define obstack_free(h,obj)                        \
  438. ( (h)->temp32 = PDIFF (obj, (h)->chunk),                \
  439.   (((h)->temp32 >= 0 && (h)->temp32                    \
  440.     < PDIFF ((h)->chunk_limit, (h)->chunk))                \
  441.    ? (int) ((h)->next_free = (h)->object_base                \
  442.         = (h)->temp32 + (char HUGE *) (h)->chunk)            \
  443.    : ((obstack_free) ((h), (h)->temp32 + (char HUGE *) (h)->chunk), 0)))
  444. #else
  445. #define obstack_free(h,obj)                        \
  446. ( (h)->temp32 = (char *)(obj) - (char *) (h)->chunk,            \
  447.   (((h)->temp32 >= 0 && (h)->temp32                    \
  448.     < (h)->chunk_limit - (char *) (h)->chunk)                \
  449.    ? (int) ((h)->next_free = (h)->object_base                \
  450.         = (h)->temp32 + (char *) (h)->chunk)            \
  451.    : (int) _obstack_free ((h), (h)->temp32 + (char *) (h)->chunk)))
  452. #endif
  453.  
  454. #endif /* not  (M_I86CM || M_I86LM) */
  455.  
  456. #endif /* not __GNUC__ or not __STDC__ */
  457.  
  458. #endif /* not __OBSTACKS__ */
  459.  
  460.  
  461.