home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / FGREP11.ZIP / OBSTACK.C < prev    next >
C/C++ Source or Header  |  1990-08-27  |  9KB  |  327 lines

  1. /* obstack.c - subroutines used implicitly by 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. #include "obstack.h"
  25.  
  26. #ifdef __STDC__
  27. #define POINTER void *
  28. #else
  29. #define POINTER char *
  30. #endif
  31.  
  32. /* Determine default alignment.  */
  33. struct fooalign {char x; double d;};
  34. #define DEFAULT_ALIGNMENT ((char *)&((struct fooalign *) 0)->d - (char *)0)
  35. /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
  36.    But in fact it might be less smart and round addresses to as much as
  37.    DEFAULT_ROUNDING.  So we prepare for it to do that.  */
  38. union fooround {long x; double d;};
  39. #define DEFAULT_ROUNDING (sizeof (union fooround))
  40.  
  41. /* When we copy a long block of data, this is the unit to do it with.
  42.    On some machines, copying successive ints does not work;
  43.    in such a case, redefine COPYING_UNIT to `long' (if that works)
  44.    or `char' as a last resort.  */
  45. #ifndef COPYING_UNIT
  46. #define COPYING_UNIT int
  47. #endif
  48.  
  49. /* The non-GNU-C macros copy the obstack into this global variable
  50.    to avoid multiple evaluation.  */
  51.  
  52. struct obstack *_obstack;
  53.  
  54. /* Initialize an obstack H for use.  Specify chunk size SIZE (0 means default).
  55.    Objects start on multiples of ALIGNMENT (0 means use default).
  56.    CHUNKFUN is the function to use to allocate chunks,
  57.    and FREEFUN the function to free them.  */
  58.  
  59. void
  60. _obstack_begin (h, size, alignment, chunkfun, freefun)
  61.      struct obstack *h;
  62.      int size;
  63.      int alignment;
  64.      POINTER (*chunkfun) ();
  65.      void (*freefun) ();
  66. {
  67.   register struct _obstack_chunk* chunk; /* points to new chunk */
  68.  
  69.   if (alignment == 0)
  70.     alignment = DEFAULT_ALIGNMENT;
  71.   if (size == 0)
  72.     /* Default size is what GNU malloc can fit in a 4096-byte block.
  73.        Pick a number small enough that when rounded up to DEFAULT_ROUNDING
  74.        it is still smaller than 4096 - 4.  */
  75.     {
  76.       int extra = 4;
  77.       if (extra < DEFAULT_ROUNDING)
  78.     extra = DEFAULT_ROUNDING;
  79.       size = 4096 - extra;
  80.     }
  81.  
  82.   h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
  83.   h->freefun = freefun;
  84.   h->chunk_size = size;
  85.   h->alignment_mask = alignment - 1;
  86.  
  87.   chunk    = h->chunk = (*h->chunkfun) (h->chunk_size);
  88.   h->next_free = h->object_base = chunk->contents;
  89.   h->chunk_limit = chunk->limit
  90.    = (char *) chunk + h->chunk_size;
  91.   chunk->prev = 0;
  92. }
  93.  
  94. /* Allocate a new current chunk for the obstack *H
  95.    on the assumption that LENGTH bytes need to be added
  96.    to the current object, or a new object of length LENGTH allocated.
  97.    Copies any partial object from the end of the old chunk
  98.    to the beginning of the new one.  */
  99.  
  100. void
  101. _obstack_newchunk (h, length)
  102.      struct obstack *h;
  103.      int length;
  104. {
  105.   register struct _obstack_chunk*    old_chunk = h->chunk;
  106.   register struct _obstack_chunk*    new_chunk;
  107.   register long    new_size;
  108.   register int obj_size = h->next_free - h->object_base;
  109.   register int i;
  110.   int already;
  111.  
  112.   /* Compute size for new chunk.  */
  113.   new_size = (obj_size + length) + (obj_size >> 3) + 100;
  114.   if (new_size < h->chunk_size)
  115.     new_size = h->chunk_size;
  116.  
  117.   /* Allocate and initialize the new chunk.  */
  118.   new_chunk = h->chunk = (*h->chunkfun) (new_size);
  119.   new_chunk->prev = old_chunk;
  120.   new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
  121.  
  122.   /* Move the existing object to the new chunk.
  123.      Word at a time is fast and is safe if the object
  124.      is sufficiently aligned.  */
  125.   if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
  126.     {
  127.       for (i = obj_size / sizeof (COPYING_UNIT) - 1;
  128.        i >= 0; i--)
  129.     ((COPYING_UNIT *)new_chunk->contents)[i]
  130.       = ((COPYING_UNIT *)h->object_base)[i];
  131.       /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
  132.      but that can cross a page boundary on a machine
  133.      which does not do strict alignment for COPYING_UNITS.  */
  134.       already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
  135.     }
  136.   else
  137.     already = 0;
  138.   /* Copy remaining bytes one by one.  */
  139.   for (i = already; i < obj_size; i++)
  140.     new_chunk->contents[i] = h->object_base[i];
  141.  
  142.   h->object_base = new_chunk->contents;
  143.   h->next_free = h->object_base + obj_size;
  144. }
  145.  
  146. /* Return nonzero if object OBJ has been allocated from obstack H.
  147.    This is here for debugging.
  148.    If you use it in a program, you are probably losing.  */
  149.  
  150. int
  151. _obstack_allocated_p (h, obj)
  152.      struct obstack *h;
  153.      POINTER obj;
  154. {
  155.   register struct _obstack_chunk*  lp;    /* below addr of any objects in this chunk */
  156.   register struct _obstack_chunk*  plp;    /* point to previous chunk if any */
  157.  
  158.   lp = (h)->chunk;
  159.   while (lp != 0 && ((POINTER)lp > obj || (POINTER)(lp)->limit < obj))
  160.     {
  161.       plp = lp -> prev;
  162.       lp = plp;
  163.     }
  164.   return lp != 0;
  165. }
  166.  
  167. /* Free objects in obstack H, including OBJ and everything allocate
  168.    more recently than OBJ.  If OBJ is zero, free everything in H.  */
  169.  
  170. void
  171. #ifdef __STDC__
  172. #undef obstack_free
  173. obstack_free (struct obstack *h, POINTER obj)
  174. #else
  175. _obstack_free (h, obj)
  176.      struct obstack *h;
  177.      POINTER obj;
  178. #endif
  179. {
  180.   register struct _obstack_chunk*  lp;    /* below addr of any objects in this chunk */
  181.   register struct _obstack_chunk*  plp;    /* point to previous chunk if any */
  182.  
  183.   lp = (h)->chunk;
  184.   while (lp != 0 && ((POINTER)lp > obj || (POINTER)(lp)->limit < obj))
  185.     {
  186.       plp = lp -> prev;
  187.       (*h->freefun) (lp);
  188.       lp = plp;
  189.     }
  190.   if (lp)
  191.     {
  192.       (h)->object_base = (h)->next_free = (char *)(obj);
  193.       (h)->chunk_limit = lp->limit;
  194.       (h)->chunk = lp;
  195.     }
  196.   else if (obj != 0)
  197.     /* obj is not in any of the chunks! */
  198.     abort ();
  199. }
  200.  
  201. /* Let same .o link with output of gcc and other compilers.  */
  202.  
  203. #ifdef __STDC__
  204. void
  205. _obstack_free (h, obj)
  206.      struct obstack *h;
  207.      POINTER obj;
  208. {
  209.   obstack_free (h, obj);
  210. }
  211. #endif
  212.  
  213. #if 0
  214. /* These are now turned off because the applications do not use it
  215.    and it uses bcopy via obstack_grow, which causes trouble on sysV.  */
  216.  
  217. /* Now define the functional versions of the obstack macros.
  218.    Define them to simply use the corresponding macros to do the job.  */
  219.  
  220. #ifdef __STDC__
  221. /* These function definitions do not work with non-ANSI preprocessors;
  222.    they won't pass through the macro names in parentheses.  */
  223.  
  224. /* The function names appear in parentheses in order to prevent
  225.    the macro-definitions of the names from being expanded there.  */
  226.  
  227. POINTER (obstack_base) (obstack)
  228.      struct obstack *obstack;
  229. {
  230.   return obstack_base (obstack);
  231. }
  232.  
  233. POINTER (obstack_next_free) (obstack)
  234.      struct obstack *obstack;
  235. {
  236.   return obstack_next_free (obstack);
  237. }
  238.  
  239. int (obstack_object_size) (obstack)
  240.      struct obstack *obstack;
  241. {
  242.   return obstack_object_size (obstack);
  243. }
  244.  
  245. int (obstack_room) (obstack)
  246.      struct obstack *obstack;
  247. {
  248.   return obstack_room (obstack);
  249. }
  250.  
  251. void (obstack_grow) (obstack, pointer, length)
  252.      struct obstack *obstack;
  253.      POINTER pointer;
  254.      int length;
  255. {
  256.   obstack_grow (obstack, pointer, length);
  257. }
  258.  
  259. void (obstack_grow0) (obstack, pointer, length)
  260.      struct obstack *obstack;
  261.      POINTER pointer;
  262.      int length;
  263. {
  264.   obstack_grow0 (obstack, pointer, length);
  265. }
  266.  
  267. void (obstack_1grow) (obstack, character)
  268.      struct obstack *obstack;
  269.      int character;
  270. {
  271.   obstack_1grow (obstack, character);
  272. }
  273.  
  274. void (obstack_blank) (obstack, length)
  275.      struct obstack *obstack;
  276.      int length;
  277. {
  278.   obstack_blank (obstack, length);
  279. }
  280.  
  281. void (obstack_1grow_fast) (obstack, character)
  282.      struct obstack *obstack;
  283.      int character;
  284. {
  285.   obstack_1grow_fast (obstack, character);
  286. }
  287.  
  288. void (obstack_blank_fast) (obstack, length)
  289.      struct obstack *obstack;
  290.      int length;
  291. {
  292.   obstack_blank_fast (obstack, length);
  293. }
  294.  
  295. POINTER (obstack_finish) (obstack)
  296.      struct obstack *obstack;
  297. {
  298.   return obstack_finish (obstack);
  299. }
  300.  
  301. POINTER (obstack_alloc) (obstack, length)
  302.      struct obstack *obstack;
  303.      int length;
  304. {
  305.   return obstack_alloc (obstack, length);
  306. }
  307.  
  308. POINTER (obstack_copy) (obstack, pointer, length)
  309.      struct obstack *obstack;
  310.      POINTER pointer;
  311.      int length;
  312. {
  313.   return obstack_copy (obstack, pointer, length);
  314. }
  315.  
  316. POINTER (obstack_copy0) (obstack, pointer, length)
  317.      struct obstack *obstack;
  318.      POINTER pointer;
  319.      int length;
  320. {
  321.   return obstack_copy0 (obstack, pointer, length);
  322. }
  323.  
  324. #endif /* __STDC__ */
  325.  
  326. #endif /* 0 */
  327.