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