home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / AWK.ZIP / OBSTACK.C < prev    next >
Encoding:
Text File  |  1988-09-09  |  6.9 KB  |  160 lines

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