Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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