home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-base.tgz / libg++-2.7.1-src.tar / fsf / libg++ / libiberty / obstack.c < prev    next >
C/C++ Source or Header  |  1995-07-07  |  15KB  |  494 lines

  1. /* obstack.c - subroutines used implicitly by object stack macros
  2.    Copyright (C) 1988, 89, 90, 91, 92, 93, 94 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 Library General Public License as published by the
  6. Free Software Foundation; either version 2, 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 Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  17.  
  18. #include "obstack.h"
  19.  
  20. /* This is just to get __GNU_LIBRARY__ defined.  */
  21. #include <stdio.h>
  22.  
  23. /* Comment out all this code if we are using the GNU C Library, and are not
  24.    actually compiling the library itself.  This code is part of the GNU C
  25.    Library, but also included in many other GNU distributions.  Compiling
  26.    and linking in this code is a waste when using the GNU C library
  27.    (especially if it is a shared library).  Rather than having every GNU
  28.    program understand `configure --with-gnu-libc' and omit the object files,
  29.    it is simpler to just do this in the source for each such file.  */
  30.  
  31. /* CYGNUS LOCAL.  No, don't comment the code out.  We will be using
  32.    ../include/obstack.h, which was changed relatively recently in a
  33.    way that is not binary compatible.  Until we feel confident that
  34.    nobody is using the old obstack.c code, force the use of this code.
  35.    This issue will arise anytime a change is made which is not binary
  36.    compatible.
  37. #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
  38. */
  39. #if 1
  40.  
  41.  
  42. #ifdef __STDC__
  43. #define POINTER void *
  44. #else
  45. #define POINTER char *
  46. #endif
  47.  
  48. /* Determine default alignment.  */
  49. struct fooalign {char x; double d;};
  50. #define DEFAULT_ALIGNMENT  \
  51.   ((PTR_INT_TYPE) ((char *)&((struct fooalign *) 0)->d - (char *)0))
  52. /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
  53.    But in fact it might be less smart and round addresses to as much as
  54.    DEFAULT_ROUNDING.  So we prepare for it to do that.  */
  55. union fooround {long x; double d;};
  56. #define DEFAULT_ROUNDING (sizeof (union fooround))
  57.  
  58. /* When we copy a long block of data, this is the unit to do it with.
  59.    On some machines, copying successive ints does not work;
  60.    in such a case, redefine COPYING_UNIT to `long' (if that works)
  61.    or `char' as a last resort.  */
  62. #ifndef COPYING_UNIT
  63. #define COPYING_UNIT int
  64. #endif
  65.  
  66. /* The non-GNU-C macros copy the obstack into this global variable
  67.    to avoid multiple evaluation.  */
  68.  
  69. struct obstack *_obstack;
  70.  
  71. /* Define a macro that either calls functions with the traditional malloc/free
  72.    calling interface, or calls functions with the mmalloc/mfree interface
  73.    (that adds an extra first argument), based on the state of use_extra_arg.
  74.    For free, do not use ?:, since some compilers, like the MIPS compilers,
  75.    do not allow (expr) ? void : void.  */
  76.  
  77. #define CALL_CHUNKFUN(h, size) \
  78.   (((h) -> use_extra_arg) \
  79.    ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
  80.    : (*(h)->chunkfun) ((size)))
  81.  
  82. #define CALL_FREEFUN(h, old_chunk) \
  83.   do { \
  84.     if ((h) -> use_extra_arg) \
  85.       (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
  86.     else \
  87.       (*(h)->freefun) ((old_chunk)); \
  88.   } while (0)
  89.  
  90.  
  91. /* Initialize an obstack H for use.  Specify chunk size SIZE (0 means default).
  92.    Objects start on multiples of ALIGNMENT (0 means use default).
  93.    CHUNKFUN is the function to use to allocate chunks,
  94.    and FREEFUN the function to free them.
  95.  
  96.    Return nonzero if successful, zero if out of memory.
  97.    To recover from an out of memory error,
  98.    free up some memory, then call this again.  */
  99.  
  100. int
  101. _obstack_begin (h, size, alignment, chunkfun, freefun)
  102.      struct obstack *h;
  103.      int size;
  104.      int alignment;
  105.      POINTER (*chunkfun) ();
  106.      void (*freefun) ();
  107. {
  108.   register struct _obstack_chunk* chunk; /* points to new chunk */
  109.  
  110.   if (alignment == 0)
  111.     alignment = DEFAULT_ALIGNMENT;
  112.   if (size == 0)
  113.     /* Default size is what GNU malloc can fit in a 4096-byte block.  */
  114.     {
  115.       /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
  116.      Use the values for range checking, because if range checking is off,
  117.      the extra bytes won't be missed terribly, but if range checking is on
  118.      and we used a larger request, a whole extra 4096 bytes would be
  119.      allocated.
  120.  
  121.      These number are irrelevant to the new GNU malloc.  I suspect it is
  122.      less sensitive to the size of the request.  */
  123.       int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
  124.             + 4 + DEFAULT_ROUNDING - 1)
  125.            & ~(DEFAULT_ROUNDING - 1));
  126.       size = 4096 - extra;
  127.     }
  128.  
  129.   h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
  130.   h->freefun = freefun;
  131.   h->chunk_size = size;
  132.   h->alignment_mask = alignment - 1;
  133.   h->use_extra_arg = 0;
  134.  
  135.   chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
  136.   if (!chunk)
  137.     {
  138.       h->alloc_failed = 1;
  139.       return 0;
  140.     }
  141.   h->alloc_failed = 0;
  142.   h->next_free = h->object_base = chunk->contents;
  143.   h->chunk_limit = chunk->limit
  144.     = (char *) chunk + h->chunk_size;
  145.   chunk->prev = 0;
  146.   /* The initial chunk now contains no empty object.  */
  147.   h->maybe_empty_object = 0;
  148.   return 1;
  149. }
  150.  
  151. int
  152. _obstack_begin_1 (h, size, alignment, chunkfun, freefun, arg)
  153.      struct obstack *h;
  154.      int size;
  155.      int alignment;
  156.      POINTER (*chunkfun) ();
  157.      void (*freefun) ();
  158.      POINTER arg;
  159. {
  160.   register struct _obstack_chunk* chunk; /* points to new chunk */
  161.  
  162.   if (alignment == 0)
  163.     alignment = DEFAULT_ALIGNMENT;
  164.   if (size == 0)
  165.     /* Default size is what GNU malloc can fit in a 4096-byte block.  */
  166.     {
  167.       /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
  168.      Use the values for range checking, because if range checking is off,
  169.      the extra bytes won't be missed terribly, but if range checking is on
  170.      and we used a larger request, a whole extra 4096 bytes would be
  171.      allocated.
  172.  
  173.      These number are irrelevant to the new GNU malloc.  I suspect it is
  174.      less sensitive to the size of the request.  */
  175.       int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
  176.             + 4 + DEFAULT_ROUNDING - 1)
  177.            & ~(DEFAULT_ROUNDING - 1));
  178.       size = 4096 - extra;
  179.     }
  180.  
  181.   h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
  182.   h->freefun = freefun;
  183.   h->chunk_size = size;
  184.   h->alignment_mask = alignment - 1;
  185.   h->extra_arg = arg;
  186.   h->use_extra_arg = 1;
  187.  
  188.   chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
  189.   if (!chunk)
  190.     {
  191.       h->alloc_failed = 1;
  192.       return 0;
  193.     }
  194.   h->alloc_failed = 0;
  195.   h->next_free = h->object_base = chunk->contents;
  196.   h->chunk_limit = chunk->limit
  197.     = (char *) chunk + h->chunk_size;
  198.   chunk->prev = 0;
  199.   /* The initial chunk now contains no empty object.  */
  200.   h->maybe_empty_object = 0;
  201.   return 1;
  202. }
  203.  
  204. /* Allocate a new current chunk for the obstack *H
  205.    on the assumption that LENGTH bytes need to be added
  206.    to the current object, or a new object of length LENGTH allocated.
  207.    Copies any partial object from the end of the old chunk
  208.    to the beginning of the new one.  */
  209.  
  210. void
  211. _obstack_newchunk (h, length)
  212.      struct obstack *h;
  213.      int length;
  214. {
  215.   register struct _obstack_chunk*    old_chunk = h->chunk;
  216.   register struct _obstack_chunk*    new_chunk;
  217.   register long    new_size;
  218.   register int obj_size = h->next_free - h->object_base;
  219.   register int i;
  220.   int already;
  221.  
  222.   /* Compute size for new chunk.  */
  223.   new_size = (obj_size + length) + (obj_size >> 3) + 100;
  224.   if (new_size < h->chunk_size)
  225.     new_size = h->chunk_size;
  226.  
  227.   /* Allocate and initialize the new chunk.  */
  228.   new_chunk = CALL_CHUNKFUN (h, new_size);
  229.   if (!new_chunk)
  230.     {
  231.       h->alloc_failed = 1;
  232.       return;
  233.     }
  234.   h->alloc_failed = 0;
  235.   h->chunk = new_chunk;
  236.   new_chunk->prev = old_chunk;
  237.   new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
  238.  
  239.   /* Move the existing object to the new chunk.
  240.      Word at a time is fast and is safe if the object
  241.      is sufficiently aligned.  */
  242.   if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
  243.     {
  244.       for (i = obj_size / sizeof (COPYING_UNIT) - 1;
  245.        i >= 0; i--)
  246.     ((COPYING_UNIT *)new_chunk->contents)[i]
  247.       = ((COPYING_UNIT *)h->object_base)[i];
  248.       /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
  249.      but that can cross a page boundary on a machine
  250.      which does not do strict alignment for COPYING_UNITS.  */
  251.       already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
  252.     }
  253.   else
  254.     already = 0;
  255.   /* Copy remaining bytes one by one.  */
  256.   for (i = already; i < obj_size; i++)
  257.     new_chunk->contents[i] = h->object_base[i];
  258.  
  259.   /* If the object just copied was the only data in OLD_CHUNK,
  260.      free that chunk and remove it from the chain.
  261.      But not if that chunk might contain an empty object.  */
  262.   if (h->object_base == old_chunk->contents && ! h->maybe_empty_object)
  263.     {
  264.       new_chunk->prev = old_chunk->prev;
  265.       CALL_FREEFUN (h, old_chunk);
  266.     }
  267.  
  268.   h->object_base = new_chunk->contents;
  269.   h->next_free = h->object_base + obj_size;
  270.   /* The new chunk certainly contains no empty object yet.  */
  271.   h->maybe_empty_object = 0;
  272. }
  273.  
  274. /* Return nonzero if object OBJ has been allocated from obstack H.
  275.    This is here for debugging.
  276.    If you use it in a program, you are probably losing.  */
  277.  
  278. #ifdef __STDC__
  279. /* Suppress -Wmissing-prototypes warning.  We don't want to declare this in
  280.    obstack.h because it is just for debugging.  */
  281. int _obstack_allocated_p (struct obstack *h, POINTER obj);
  282. #endif
  283.  
  284. int
  285. _obstack_allocated_p (h, obj)
  286.      struct obstack *h;
  287.      POINTER obj;
  288. {
  289.   register struct _obstack_chunk*  lp;    /* below addr of any objects in this chunk */
  290.   register struct _obstack_chunk*  plp;    /* point to previous chunk if any */
  291.  
  292.   lp = (h)->chunk;
  293.   /* We use >= rather than > since the object cannot be exactly at
  294.      the beginning of the chunk but might be an empty object exactly
  295.      at the end of an adjacent chunk. */
  296.   while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
  297.     {
  298.       plp = lp->prev;
  299.       lp = plp;
  300.     }
  301.   return lp != 0;
  302. }
  303.  
  304. /* Free objects in obstack H, including OBJ and everything allocate
  305.    more recently than OBJ.  If OBJ is zero, free everything in H.  */
  306.  
  307. #undef obstack_free
  308.  
  309. /* This function has two names with identical definitions.
  310.    This is the first one, called from non-ANSI code.  */
  311.  
  312. void
  313. _obstack_free (h, obj)
  314.      struct obstack *h;
  315.      POINTER obj;
  316. {
  317.   register struct _obstack_chunk*  lp;    /* below addr of any objects in this chunk */
  318.   register struct _obstack_chunk*  plp;    /* point to previous chunk if any */
  319.  
  320.   lp = h->chunk;
  321.   /* We use >= because there cannot be an object at the beginning of a chunk.
  322.      But there can be an empty object at that address
  323.      at the end of another chunk.  */
  324.   while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
  325.     {
  326.       plp = lp->prev;
  327.       CALL_FREEFUN (h, lp);
  328.       lp = plp;
  329.       /* If we switch chunks, we can't tell whether the new current
  330.      chunk contains an empty object, so assume that it may.  */
  331.       h->maybe_empty_object = 1;
  332.     }
  333.   if (lp)
  334.     {
  335.       h->object_base = h->next_free = (char *)(obj);
  336.       h->chunk_limit = lp->limit;
  337.       h->chunk = lp;
  338.     }
  339.   else if (obj != 0)
  340.     /* obj is not in any of the chunks! */
  341.     abort ();
  342. }
  343.  
  344. /* This function is used from ANSI code.  */
  345.  
  346. void
  347. obstack_free (h, obj)
  348.      struct obstack *h;
  349.      POINTER obj;
  350. {
  351.   register struct _obstack_chunk*  lp;    /* below addr of any objects in this chunk */
  352.   register struct _obstack_chunk*  plp;    /* point to previous chunk if any */
  353.  
  354.   lp = h->chunk;
  355.   /* We use >= because there cannot be an object at the beginning of a chunk.
  356.      But there can be an empty object at that address
  357.      at the end of another chunk.  */
  358.   while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
  359.     {
  360.       plp = lp->prev;
  361.       CALL_FREEFUN (h, lp);
  362.       lp = plp;
  363.       /* If we switch chunks, we can't tell whether the new current
  364.      chunk contains an empty object, so assume that it may.  */
  365.       h->maybe_empty_object = 1;
  366.     }
  367.   if (lp)
  368.     {
  369.       h->object_base = h->next_free = (char *)(obj);
  370.       h->chunk_limit = lp->limit;
  371.       h->chunk = lp;
  372.     }
  373.   else if (obj != 0)
  374.     /* obj is not in any of the chunks! */
  375.     abort ();
  376. }
  377.  
  378. #if 0
  379. /* These are now turned off because the applications do not use it
  380.    and it uses bcopy via obstack_grow, which causes trouble on sysV.  */
  381.  
  382. /* Now define the functional versions of the obstack macros.
  383.    Define them to simply use the corresponding macros to do the job.  */
  384.  
  385. #ifdef __STDC__
  386. /* These function definitions do not work with non-ANSI preprocessors;
  387.    they won't pass through the macro names in parentheses.  */
  388.  
  389. /* The function names appear in parentheses in order to prevent
  390.    the macro-definitions of the names from being expanded there.  */
  391.  
  392. POINTER (obstack_base) (obstack)
  393.      struct obstack *obstack;
  394. {
  395.   return obstack_base (obstack);
  396. }
  397.  
  398. POINTER (obstack_next_free) (obstack)
  399.      struct obstack *obstack;
  400. {
  401.   return obstack_next_free (obstack);
  402. }
  403.  
  404. int (obstack_object_size) (obstack)
  405.      struct obstack *obstack;
  406. {
  407.   return obstack_object_size (obstack);
  408. }
  409.  
  410. int (obstack_room) (obstack)
  411.      struct obstack *obstack;
  412. {
  413.   return obstack_room (obstack);
  414. }
  415.  
  416. void (obstack_grow) (obstack, pointer, length)
  417.      struct obstack *obstack;
  418.      POINTER pointer;
  419.      int length;
  420. {
  421.   obstack_grow (obstack, pointer, length);
  422. }
  423.  
  424. void (obstack_grow0) (obstack, pointer, length)
  425.      struct obstack *obstack;
  426.      POINTER pointer;
  427.      int length;
  428. {
  429.   obstack_grow0 (obstack, pointer, length);
  430. }
  431.  
  432. void (obstack_1grow) (obstack, character)
  433.      struct obstack *obstack;
  434.      int character;
  435. {
  436.   obstack_1grow (obstack, character);
  437. }
  438.  
  439. void (obstack_blank) (obstack, length)
  440.      struct obstack *obstack;
  441.      int length;
  442. {
  443.   obstack_blank (obstack, length);
  444. }
  445.  
  446. void (obstack_1grow_fast) (obstack, character)
  447.      struct obstack *obstack;
  448.      int character;
  449. {
  450.   obstack_1grow_fast (obstack, character);
  451. }
  452.  
  453. void (obstack_blank_fast) (obstack, length)
  454.      struct obstack *obstack;
  455.      int length;
  456. {
  457.   obstack_blank_fast (obstack, length);
  458. }
  459.  
  460. POINTER (obstack_finish) (obstack)
  461.      struct obstack *obstack;
  462. {
  463.   return obstack_finish (obstack);
  464. }
  465.  
  466. POINTER (obstack_alloc) (obstack, length)
  467.      struct obstack *obstack;
  468.      int length;
  469. {
  470.   return obstack_alloc (obstack, length);
  471. }
  472.  
  473. POINTER (obstack_copy) (obstack, pointer, length)
  474.      struct obstack *obstack;
  475.      POINTER pointer;
  476.      int length;
  477. {
  478.   return obstack_copy (obstack, pointer, length);
  479. }
  480.  
  481. POINTER (obstack_copy0) (obstack, pointer, length)
  482.      struct obstack *obstack;
  483.      POINTER pointer;
  484.      int length;
  485. {
  486.   return obstack_copy0 (obstack, pointer, length);
  487. }
  488.  
  489. #endif /* __STDC__ */
  490.  
  491. #endif /* 0 */
  492.  
  493. #endif    /* _LIBC or not __GNU_LIBRARY__.  */
  494.