home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / TOP / USR / SRC / gawk2.0.t.Z / gawk2.0.t / obstack.c < prev    next >
Text File  |  1988-12-31  |  7KB  |  178 lines

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