home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / AP / ASH / ASH-LINU.2 / ASH-LINU / ash-linux-0.2 / memalloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-24  |  6.9 KB  |  294 lines

  1. /*-
  2.  * Copyright (c) 1991 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Kenneth Almquist.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #ifndef lint
  38. /*static char sccsid[] = "from: @(#)memalloc.c    5.2 (Berkeley) 3/13/91";*/
  39. static char rcsid[] = "memalloc.c,v 1.4 1993/08/01 18:58:10 mycroft Exp";
  40. #endif /* not lint */
  41.  
  42. #include "shell.h"
  43. #include "output.h"
  44. #include "memalloc.h"
  45. #include "error.h"
  46. #include "machdep.h"
  47. #include "mystring.h"
  48.  
  49. /*
  50.  * Like malloc, but returns an error when out of space.
  51.  */
  52.  
  53. pointer
  54. ckmalloc(nbytes) {
  55.     register pointer p;
  56.     pointer malloc();
  57.  
  58.     if ((p = malloc(nbytes)) == NULL)
  59.         error("Out of space");
  60.     return p;
  61. }
  62.  
  63.  
  64. /*
  65.  * Same for realloc.
  66.  */
  67.  
  68. pointer
  69. ckrealloc(p, nbytes)
  70.     register pointer p;
  71.     {
  72.     pointer realloc();
  73.  
  74.     if ((p = realloc(p, nbytes)) == NULL)
  75.         error("Out of space");
  76.     return p;
  77. }
  78.  
  79.  
  80. /*
  81.  * Make a copy of a string in safe storage.
  82.  */
  83.  
  84. char *
  85. savestr(s)
  86.     char *s;
  87.     {
  88.     register char *p;
  89.  
  90.     p = ckmalloc(strlen(s) + 1);
  91.     scopy(s, p);
  92.     return p;
  93. }
  94.  
  95.  
  96. /*
  97.  * Parse trees for commands are allocated in lifo order, so we use a stack
  98.  * to make this more efficient, and also to avoid all sorts of exception
  99.  * handling code to handle interrupts in the middle of a parse.
  100.  *
  101.  * The size 504 was chosen because the Ultrix malloc handles that size
  102.  * well.
  103.  */
  104.  
  105. #define MINSIZE 504        /* minimum size of a block */
  106.  
  107.  
  108. struct stack_block {
  109.     struct stack_block *prev;
  110.     char space[MINSIZE];
  111. };
  112.  
  113. struct stack_block stackbase;
  114. struct stack_block *stackp = &stackbase;
  115. char *stacknxt = stackbase.space;
  116. int stacknleft = MINSIZE;
  117. int sstrnleft;
  118. int herefd = -1;
  119.  
  120.  
  121.  
  122. pointer
  123. stalloc(nbytes) {
  124.     register char *p;
  125.  
  126.     nbytes = ALIGN(nbytes);
  127.     if (nbytes > stacknleft) {
  128.         int blocksize;
  129.         struct stack_block *sp;
  130.  
  131.         blocksize = nbytes;
  132.         if (blocksize < MINSIZE)
  133.             blocksize = MINSIZE;
  134.         INTOFF;
  135.         sp = ckmalloc(sizeof(struct stack_block) - MINSIZE + blocksize);
  136.         sp->prev = stackp;
  137.         stacknxt = sp->space;
  138.         stacknleft = blocksize;
  139.         stackp = sp;
  140.         INTON;
  141.     }
  142.     p = stacknxt;
  143.     stacknxt += nbytes;
  144.     stacknleft -= nbytes;
  145.     return p;
  146. }
  147.  
  148.  
  149. void
  150. stunalloc(p)
  151.     pointer p;
  152.     {
  153.     if (p == NULL) {        /*DEBUG */
  154.         write(2, "stunalloc\n", 10);
  155.         abort();
  156.     }
  157.     stacknleft += stacknxt - (char *)p;
  158.     stacknxt = p;
  159. }
  160.  
  161.  
  162.  
  163. void
  164. setstackmark(mark)
  165.     struct stackmark *mark;
  166.     {
  167.     mark->stackp = stackp;
  168.     mark->stacknxt = stacknxt;
  169.     mark->stacknleft = stacknleft;
  170. }
  171.  
  172.  
  173. void
  174. popstackmark(mark)
  175.     struct stackmark *mark;
  176.     {
  177.     struct stack_block *sp;
  178.  
  179.     INTOFF;
  180.     while (stackp != mark->stackp) {
  181.         sp = stackp;
  182.         stackp = sp->prev;
  183.         ckfree(sp);
  184.     }
  185.     stacknxt = mark->stacknxt;
  186.     stacknleft = mark->stacknleft;
  187.     INTON;
  188. }
  189.  
  190.  
  191. /*
  192.  * When the parser reads in a string, it wants to stick the string on the
  193.  * stack and only adjust the stack pointer when it knows how big the
  194.  * string is.  Stackblock (defined in stack.h) returns a pointer to a block
  195.  * of space on top of the stack and stackblocklen returns the length of
  196.  * this block.  Growstackblock will grow this space by at least one byte,
  197.  * possibly moving it (like realloc).  Grabstackblock actually allocates the
  198.  * part of the block that has been used.
  199.  */
  200.  
  201. void
  202. growstackblock() {
  203.     char *p;
  204.     int newlen = stacknleft * 2 + 100;
  205.     char *oldspace = stacknxt;
  206.     int oldlen = stacknleft;
  207.     struct stack_block *sp;
  208.  
  209.     if (stacknxt == stackp->space && stackp != &stackbase) {
  210.         INTOFF;
  211.         sp = stackp;
  212.         stackp = sp->prev;
  213.         sp = ckrealloc((pointer)sp, sizeof(struct stack_block) - MINSIZE + newlen);
  214.         sp->prev = stackp;
  215.         stackp = sp;
  216.         stacknxt = sp->space;
  217.         stacknleft = newlen;
  218.         INTON;
  219.     } else {
  220.         p = stalloc(newlen);
  221.         bcopy(oldspace, p, oldlen);
  222.         stacknxt = p;            /* free the space */
  223.         stacknleft += newlen;        /* we just allocated */
  224.     }
  225. }
  226.  
  227.  
  228.  
  229. void
  230. grabstackblock(len) {
  231.     len = ALIGN(len);
  232.     stacknxt += len;
  233.     stacknleft -= len;
  234. }
  235.  
  236.  
  237.  
  238. /*
  239.  * The following routines are somewhat easier to use that the above.
  240.  * The user declares a variable of type STACKSTR, which may be declared
  241.  * to be a register.  The macro STARTSTACKSTR initializes things.  Then
  242.  * the user uses the macro STPUTC to add characters to the string.  In
  243.  * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
  244.  * grown as necessary.  When the user is done, she can just leave the
  245.  * string there and refer to it using stackblock().  Or she can allocate
  246.  * the space for it using grabstackstr().  If it is necessary to allow
  247.  * someone else to use the stack temporarily and then continue to grow
  248.  * the string, the user should use grabstack to allocate the space, and
  249.  * then call ungrabstr(p) to return to the previous mode of operation.
  250.  *
  251.  * USTPUTC is like STPUTC except that it doesn't check for overflow.
  252.  * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
  253.  * is space for at least one character.
  254.  */
  255.  
  256.  
  257. char *
  258. growstackstr() {
  259.     int len = stackblocksize();
  260.     if (herefd >= 0 && len >= 1024) {
  261.         xwrite(herefd, stackblock(), len);
  262.         sstrnleft = len - 1;
  263.         return stackblock();
  264.     }
  265.     growstackblock();
  266.     sstrnleft = stackblocksize() - len - 1;
  267.     return stackblock() + len;
  268. }
  269.  
  270.  
  271. /*
  272.  * Called from CHECKSTRSPACE.
  273.  */
  274.  
  275. char *
  276. makestrspace() {
  277.     int len = stackblocksize() - sstrnleft;
  278.     growstackblock();
  279.     sstrnleft = stackblocksize() - len;
  280.     return stackblock() + len;
  281. }
  282.  
  283.  
  284.  
  285. void
  286. ungrabstackstr(s, p)
  287.     char *s;
  288.     char *p;
  289.     {
  290.     stacknleft += stacknxt - s;
  291.     stacknxt = s;
  292.     sstrnleft = stacknleft - (p - s);
  293. }
  294.