home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / glibc-1.06 / malloc / obstack.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-21  |  13.7 KB  |  458 lines

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