home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Atari / Gnu / gdb36p4s.zoo / obstack.c < prev    next >
C/C++ Source or Header  |  1990-03-07  |  9KB  |  322 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.       /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
  77.      Use the values for range checking, because if range checking is off,
  78.      the extra bytes won't be missed terribly, but if range checking is on
  79.      and we used a larger request, a whole extra 4096 bytes would be
  80.      allocated.
  81.  
  82.      These number are irrelevant to the new GNU malloc.  I suspect it is
  83.      less sensitive to the size of the request.  */
  84.       int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
  85.             + 4 + DEFAULT_ROUNDING - 1)
  86.            & ~(DEFAULT_ROUNDING - 1));
  87.       size = 4096 - extra;
  88.     }
  89.  
  90.   h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
  91.   h->freefun = freefun;
  92.   h->chunk_size = size;
  93.   h->alignment_mask = alignment - 1;
  94.  
  95.   chunk    = h->chunk = (*h->chunkfun) (h->chunk_size);
  96.   h->next_free = h->object_base = chunk->contents;
  97.   h->chunk_limit = chunk->limit
  98.    = (char *) chunk + h->chunk_size;
  99.   chunk->prev = 0;
  100. }
  101.  
  102. /* Allocate a new current chunk for the obstack *H
  103.    on the assumption that LENGTH bytes need to be added
  104.    to the current object, or a new object of length LENGTH allocated.
  105.    Copies any partial object from the end of the old chunk
  106.    to the beginning of the new one.  */
  107.  
  108. void
  109. _obstack_newchunk (h, length)
  110.      struct obstack *h;
  111.      int length;
  112. {
  113.   register struct _obstack_chunk*    old_chunk = h->chunk;
  114.   register struct _obstack_chunk*    new_chunk;
  115.   register long    new_size;
  116.   register int obj_size = h->next_free - h->object_base;
  117.   register int i;
  118.  
  119.   /* Compute size for new chunk.  */
  120.   new_size = (obj_size + length) << 1;
  121.   if (new_size < h->chunk_size)
  122.     new_size = h->chunk_size;
  123.  
  124.   /* Allocate and initialize the new chunk.  */
  125.   new_chunk = h->chunk = (*h->chunkfun) (new_size);
  126.   new_chunk->prev = old_chunk;
  127.   new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
  128.  
  129.   /* Move the existing object to the new chunk.
  130.      Word at a time is fast and is safe because these
  131.      structures are aligned at least that much.  */
  132.   for (i = (obj_size + sizeof (COPYING_UNIT) - 1) / sizeof (COPYING_UNIT) - 1;
  133.        i >= 0; i--)
  134.     ((COPYING_UNIT *)new_chunk->contents)[i]
  135.       = ((COPYING_UNIT *)h->object_base)[i];
  136.  
  137.   h->object_base = new_chunk->contents;
  138.   h->next_free = h->object_base + obj_size;
  139. }
  140.  
  141. /* Return nonzero if object OBJ has been allocated from obstack H.
  142.    This is here for debugging.
  143.    If you use it in a program, you are probably losing.  */
  144.  
  145. int
  146. _obstack_allocated_p (h, obj)
  147.      struct obstack *h;
  148.      POINTER obj;
  149. {
  150.   register struct _obstack_chunk*  lp;    /* below addr of any objects in this chunk */
  151.   register struct _obstack_chunk*  plp;    /* point to previous chunk if any */
  152.  
  153.   lp = (h)->chunk;
  154.   while (lp != 0 && ((POINTER)lp > obj || (POINTER)(lp)->limit < obj))
  155.     {
  156.       plp = lp -> prev;
  157.       lp = plp;
  158.     }
  159.   return lp != 0;
  160. }
  161.  
  162. /* Free objects in obstack H, including OBJ and everything allocate
  163.    more recently than OBJ.  If OBJ is zero, free everything in H.  */
  164.  
  165. void
  166. #ifdef __STDC__
  167. #undef obstack_free
  168. obstack_free (struct obstack *h, POINTER obj)
  169. #else
  170. _obstack_free (h, obj)
  171.      struct obstack *h;
  172.      POINTER obj;
  173. #endif
  174. {
  175.   register struct _obstack_chunk*  lp;    /* below addr of any objects in this chunk */
  176.   register struct _obstack_chunk*  plp;    /* point to previous chunk if any */
  177.  
  178.   lp = (h)->chunk;
  179.   while (lp != 0 && ((POINTER)lp > obj || (POINTER)(lp)->limit < obj))
  180.     {
  181.       plp = lp -> prev;
  182.       (*h->freefun) (lp);
  183.       lp = plp;
  184.     }
  185.   if (lp)
  186.     {
  187.       (h)->object_base = (h)->next_free = (char *)(obj);
  188.       (h)->chunk_limit = lp->limit;
  189.       (h)->chunk = lp;
  190.     }
  191.   else if (obj != 0)
  192.     /* obj is not in any of the chunks! */
  193.     abort ();
  194. }
  195.  
  196. /* Let same .o link with output of gcc and other compilers.  */
  197.  
  198. #ifdef __STDC__
  199. void
  200. _obstack_free (h, obj)
  201.      struct obstack *h;
  202.      POINTER obj;
  203. {
  204.   obstack_free (h, obj);
  205. }
  206. #endif
  207.  
  208. #if 0
  209. /* These are now turned off because the applications do not use it
  210.    and it uses bcopy via obstack_grow, which causes trouble on sysV.  */
  211.  
  212. /* Now define the functional versions of the obstack macros.
  213.    Define them to simply use the corresponding macros to do the job.  */
  214.  
  215. #ifdef __STDC__
  216. /* These function definitions do not work with non-ANSI preprocessors;
  217.    they won't pass through the macro names in parentheses.  */
  218.  
  219. /* The function names appear in parentheses in order to prevent
  220.    the macro-definitions of the names from being expanded there.  */
  221.  
  222. POINTER (obstack_base) (obstack)
  223.      struct obstack *obstack;
  224. {
  225.   return obstack_base (obstack);
  226. }
  227.  
  228. POINTER (obstack_next_free) (obstack)
  229.      struct obstack *obstack;
  230. {
  231.   return obstack_next_free (obstack);
  232. }
  233.  
  234. int (obstack_object_size) (obstack)
  235.      struct obstack *obstack;
  236. {
  237.   return obstack_object_size (obstack);
  238. }
  239.  
  240. int (obstack_room) (obstack)
  241.      struct obstack *obstack;
  242. {
  243.   return obstack_room (obstack);
  244. }
  245.  
  246. void (obstack_grow) (obstack, pointer, length)
  247.      struct obstack *obstack;
  248.      POINTER pointer;
  249.      int length;
  250. {
  251.   obstack_grow (obstack, pointer, length);
  252. }
  253.  
  254. void (obstack_grow0) (obstack, pointer, length)
  255.      struct obstack *obstack;
  256.      POINTER pointer;
  257.      int length;
  258. {
  259.   obstack_grow0 (obstack, pointer, length);
  260. }
  261.  
  262. void (obstack_1grow) (obstack, character)
  263.      struct obstack *obstack;
  264.      int character;
  265. {
  266.   obstack_1grow (obstack, character);
  267. }
  268.  
  269. void (obstack_blank) (obstack, length)
  270.      struct obstack *obstack;
  271.      int length;
  272. {
  273.   obstack_blank (obstack, length);
  274. }
  275.  
  276. void (obstack_1grow_fast) (obstack, character)
  277.      struct obstack *obstack;
  278.      int character;
  279. {
  280.   obstack_1grow_fast (obstack, character);
  281. }
  282.  
  283. void (obstack_blank_fast) (obstack, length)
  284.      struct obstack *obstack;
  285.      int length;
  286. {
  287.   obstack_blank_fast (obstack, length);
  288. }
  289.  
  290. POINTER (obstack_finish) (obstack)
  291.      struct obstack *obstack;
  292. {
  293.   return obstack_finish (obstack);
  294. }
  295.  
  296. POINTER (obstack_alloc) (obstack, length)
  297.      struct obstack *obstack;
  298.      int length;
  299. {
  300.   return obstack_alloc (obstack, length);
  301. }
  302.  
  303. POINTER (obstack_copy) (obstack, pointer, length)
  304.      struct obstack *obstack;
  305.      POINTER pointer;
  306.      int length;
  307. {
  308.   return obstack_copy (obstack, pointer, length);
  309. }
  310.  
  311. POINTER (obstack_copy0) (obstack, pointer, length)
  312.      struct obstack *obstack;
  313.      POINTER pointer;
  314.      int length;
  315. {
  316.   return obstack_copy0 (obstack, pointer, length);
  317. }
  318.  
  319. #endif /* __STDC__ */
  320.  
  321. #endif /* 0 */
  322.