home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / NGAWK1.ZIP / OBSTACK.C < prev    next >
C/C++ Source or Header  |  1988-07-17  |  7KB  |  162 lines

  1. /* obstack.c - subroutines used implicitly by object stack macros
  2.    Copyright (c) 1986 Free Software Foundation, Inc.
  3.  
  4.                NO WARRANTY
  5.  
  6.   BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  7. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  8. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  9. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  10. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  11. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  13. AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  14. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  15. CORRECTION.
  16.  
  17.  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  18. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  19. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  20. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  21. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  22. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  23. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  24. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  25. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  26. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  27.  
  28.         GENERAL PUBLIC LICENSE TO COPY
  29.  
  30.   1. You may copy and distribute verbatim copies of this source file
  31. as you receive it, in any medium, provided that you conspicuously and
  32. appropriately publish on each copy a valid copyright notice "Copyright
  33. (C) 1986 Free Software Foundation, Inc."; and include following the
  34. copyright notice a verbatim copy of the above disclaimer of warranty
  35. and of this License.
  36.  
  37.   2. You may modify your copy or copies of this source file or
  38. any portion of it, and copy and distribute such modifications under
  39. the terms of Paragraph 1 above, provided that you also do the following:
  40.  
  41.     a) cause the modified files to carry prominent notices stating
  42.     that you changed the files and the date of any change; and
  43.  
  44.     b) cause the whole of any work that you distribute or publish,
  45.     that in whole or in part contains or is a derivative of this
  46.     program or any part thereof, to be freely distributed
  47.     and licensed to all third parties on terms identical to those
  48.     contained in this License Agreement (except that you may choose
  49.     to grant more extensive warranty protection to third parties,
  50.     at your option).
  51.  
  52.   3. You may copy and distribute this program or any portion of it in
  53. compiled, executable or object code form under the terms of Paragraphs
  54. 1 and 2 above provided that you do the following:
  55.  
  56.     a) cause each such copy to be accompanied by the
  57.     corresponding machine-readable source code, which must
  58.     be distributed under the terms of Paragraphs 1 and 2 above; or,
  59.  
  60.     b) cause each such copy to be accompanied by a
  61.     written offer, with no time limit, to give any third party
  62.     free (except for a nominal shipping charge) a machine readable
  63.     copy of the corresponding source code, to be distributed
  64.     under the terms of Paragraphs 1 and 2 above; or,
  65.  
  66.     c) in the case of a recipient of this program in compiled, executable
  67.     or object code form (without the corresponding source code) you
  68.     shall cause copies you distribute to be accompanied by a copy
  69.     of the written offer of source code which you received along
  70.     with the copy you received.
  71.  
  72.   4. You may not copy, sublicense, distribute or transfer this program
  73. except as expressly provided under this License Agreement.  Any attempt
  74. otherwise to copy, sublicense, distribute or transfer this program is void and
  75. your rights to use the program under this License agreement shall be
  76. automatically terminated.  However, parties who have received computer
  77. software programs from you with this License Agreement will not have
  78. their licenses terminated so long as such parties remain in full compliance.
  79.  
  80.   Modifications by Andrew D. Estes, July 1988
  81. */
  82.  
  83. #include <stdio.h>
  84. #include "awk.h"
  85. #include "obstack.h"
  86.  
  87. void
  88. _obstack_begin (h, chunkfun)
  89.      struct obstack *h;
  90.      void * (*chunkfun) ();
  91. {
  92.   register _Ll* chunk;        /* points to new chunk */
  93.   chunk    = h->chunk =
  94.     (_Ll*) (*chunkfun) ((size_t)h->chunk_size);
  95.   h->next_free = h->object_base = chunk->obstack_l_0;
  96.   h->chunk_limit = chunk->obstack_l_limit
  97.    = (char *) chunk + h->chunk_size;
  98.   chunk->obstack_l_prev = 0;
  99. }
  100.  
  101. /* Allocate a new current chunk for the obstack *H
  102.    on the assumption that LENGTH bytes need to be added
  103.    to the current object, or a new object of length LENGTH allocated.
  104.    Copies any partial object from the end of the old chunk
  105.    to the beginning of the new one.  */
  106.  
  107. void
  108. _obstack_newchunk (h, chunkfun, length)
  109.      struct obstack *h;
  110.      void * (*chunkfun) ();
  111.      INT length;        /* was int -ADE- */
  112. {
  113.   register _Ll*    old_chunk = h->chunk;
  114.   register _Ll*    new_chunk;
  115.   register long    new_size;
  116.   register INT obj_size = h->next_free - h->object_base;
  117.  
  118.   /* Compute size for new chunk.  */
  119.   new_size = (obj_size + length) << 1;
  120.   if (new_size < h->chunk_size)
  121.     new_size = h->chunk_size;
  122.  
  123.   /* Allocate and initialize the new chunk.  */
  124.   new_chunk = h->chunk = (_Ll*) (*chunkfun) ((size_t)new_size);
  125.   new_chunk->obstack_l_prev = old_chunk;
  126.   new_chunk->obstack_l_limit = h->chunk_limit = (char *) new_chunk + new_size;
  127.  
  128.   /* Move the existing object to the new chunk.  */
  129.   bcopy (h->object_base, new_chunk->obstack_l_0, obj_size);
  130.   h->object_base = new_chunk->obstack_l_0;
  131.   h->next_free = h->object_base + obj_size;
  132.  };
  133.  
  134. void
  135. _obstack_free (h, freechunkfun, obj)
  136.      struct obstack *h;
  137.      void (*freechunkfun) ();
  138.      char *obj;
  139. {
  140.   register _Ll*  lp;    /* below addr of any objects in this chunk */
  141.   register _Ll*  plp;    /* point to previous chunk if any */
  142.  
  143.   lp = (h)->chunk;
  144.   while (lp != 0 && ((char *)lp > obj || (h)->chunk_limit < obj))
  145.     {
  146.       plp = lp -> obstack_l_prev;
  147.       (*freechunkfun) (lp);
  148.       if(lp==plp)
  149.           plp=0;
  150.       lp = plp;
  151.     }
  152.   if (lp)
  153.     {
  154.       (h)->object_base = (h)->next_free = (char *)(obj);
  155.       (h)->chunk_limit = lp->obstack_l_limit;
  156.       (h)->chunk = lp;
  157.     }
  158.   else if (obj != 0)
  159.     /* obj is not in any of the chunks! */
  160.     abort ();
  161. }
  162.