home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff256.lzh / Stevie / alloc.c < prev    next >
C/C++ Source or Header  |  1989-10-19  |  5KB  |  217 lines

  1. /*
  2.  * STEVIE - Simply Try this Editor for VI Enthusiasts
  3.  *
  4.  * Code Contributions By : Tim Thompson           twitch!tjt
  5.  *                         Tony Andrews           onecom!wldrdg!tony 
  6.  *                         G. R. (Fred) Walter    watmath!grwalter 
  7.  */
  8.  
  9. #ifdef AMIGA
  10. # include <proto/exec.h>
  11. # include <exec/memory.h>
  12. # define PANIC_FACTOR_CHIP 40000
  13. #endif
  14. #include "stevie.h"
  15.  
  16. /*
  17.  * This file contains various routines dealing with allocation and
  18.  * deallocation of data structures. 
  19.  */
  20.  
  21. char           *
  22. alloc(size)
  23.     unsigned        size;
  24. {
  25.     char           *p;        /* pointer to new storage space */
  26.  
  27.     p = malloc(size);
  28.     if (p == (char *) NULL) {    /* if there is no more room... */
  29.     emsg("alloc() is unable to find memory!");
  30.     sleep(5);
  31.     }
  32. #ifdef AMIGA
  33.     if (AvailMem(MEMF_CHIP) < PANIC_FACTOR_CHIP) {
  34.     free(p);
  35.     p = (char *) NULL;
  36.     emsg("alloc() - not enough CHIP memory!");
  37.     sleep(5);
  38.     }
  39. #endif
  40.  
  41.     return (p);
  42. }
  43.  
  44. char           *
  45. strsave(string)
  46.     char           *string;
  47. {
  48.     char           *s;
  49.  
  50.     s = alloc((unsigned) (strlen(string) + 1));
  51.     if (s != (char *) NULL)
  52.     strcpy(s, string);
  53.     return (s);
  54. }
  55.  
  56. void
  57. screenalloc()
  58. {
  59.     int             i;
  60.  
  61.     /*
  62.      * If we're changing the size of the screen, free the old arrays 
  63.      */
  64.     if (LinePointers != (LINE **) NULL)
  65.     free((char *) LinePointers);
  66.     if (LineSizes != (char *) NULL)
  67.     free(LineSizes);
  68.  
  69.     LinePointers = (LINE **) malloc((unsigned) (Rows * sizeof(LINE *)));
  70.     LineSizes = malloc((unsigned) Rows);
  71.     if (LinePointers == (LINE **) NULL || LineSizes == (char *) NULL) {
  72.     fprintf(stderr, "Unable to allocate screen memory!\n");
  73.     getout(1);
  74.     }
  75.     for (i = 0; i < Rows; i++) {
  76.     LinePointers[i] = (LINE *) NULL;
  77.     LineSizes[i] = (char) 0;
  78.     }
  79.     NumLineSizes = -1;
  80. }
  81.  
  82. /*
  83.  * Allocate and initialize a new line structure with room for 'nchars'
  84.  * characters. 
  85.  */
  86. LINE           *
  87. newline(nchars)
  88.     int             nchars;
  89. {
  90.     register LINE  *l;
  91.  
  92.     if (nchars == 0)
  93.     nchars = 1;
  94.  
  95.     l = (LINE *) alloc((unsigned) sizeof(LINE));
  96.     if (l != (LINE *) NULL) {
  97.     l->s = alloc((unsigned) nchars);    /* the line is empty */
  98.     if (l->s != (char *) NULL) {
  99.         l->s[0] = NUL;
  100.         l->size = nchars;
  101.  
  102.         l->prev = (LINE *) NULL;    /* should be initialized by caller */
  103.         l->next = (LINE *) NULL;
  104.     } else {
  105.         free((char *) l);
  106.         l = (LINE *) NULL;
  107.     }
  108.     }
  109.     return l;
  110. }
  111.  
  112. /*
  113.  * filealloc() - construct an initial empty file buffer 
  114.  */
  115. void
  116. filealloc()
  117. {
  118.     Filemem->linep = newline(1);
  119.     Filetop->linep = newline(1);
  120.     Fileend->linep = newline(1);
  121.     if (Filemem->linep == (LINE *) NULL ||
  122.     Filetop->linep == (LINE *) NULL ||
  123.     Fileend->linep == (LINE *) NULL) {
  124.     fprintf(stderr, "Unable to allocate file memory!\n");
  125.     getout(1);
  126.     }
  127.     Filemem->index = 0;
  128.     Filetop->index = 0;
  129.     Fileend->index = 0;
  130.  
  131.     Filetop->linep->prev = (LINE *) NULL;
  132.     Filetop->linep->next = Filemem->linep;    /* connect Filetop to Filemem */
  133.     Filemem->linep->prev = Filetop->linep;
  134.  
  135.     Filemem->linep->next = Fileend->linep;    /* connect Filemem to Fileend */
  136.     Fileend->linep->prev = Filemem->linep;
  137.     Fileend->linep->next = (LINE *) NULL;
  138.  
  139.     *Curschar = *Filemem;
  140.     *Topchar = *Filemem;
  141.  
  142.     Filemem->linep->num = 0;
  143.     Fileend->linep->num = 0xffffffffL;
  144.  
  145.     clrall();            /* clear all marks */
  146. }
  147.  
  148. /*
  149.  * freeall() - free the current buffer 
  150.  *
  151.  * Free all lines in the current buffer. 
  152.  */
  153. void
  154. freeall()
  155. {
  156.     LINE           *lp;
  157.     LINE           *xlp;
  158.     int             i;
  159.  
  160.     for (lp = Filetop->linep; lp != (LINE *) NULL; lp = xlp) {
  161.     if (lp->s != (char *) NULL)
  162.         free(lp->s);
  163.     xlp = lp->next;
  164.     free((char *) lp);
  165.     }
  166.  
  167.     Curschar->linep = (LINE *) NULL;    /* clear pointers */
  168.     Filemem->linep = (LINE *) NULL;
  169.     Filetop->linep = (LINE *) NULL;
  170.     Fileend->linep = (LINE *) NULL;
  171.  
  172.     for (i = 0; i < Rows; i++) {/* clear screen information */
  173.     LinePointers[i] = (LINE *) NULL;
  174.     LineSizes[i] = (char) 0;
  175.     }
  176.     NumLineSizes = -1;
  177. }
  178.  
  179. /*
  180.  * canincrease(n) - returns TRUE if the current line can be increased 'n'
  181.  * bytes 
  182.  *
  183.  * This routine returns immediately if the requested space is available. If not,
  184.  * it attempts to allocate the space and adjust the data structures
  185.  * accordingly. If everything fails it returns FALSE. 
  186.  */
  187. bool_t
  188. canincrease(n)
  189.     register int    n;
  190. {
  191.     register int    nsize;
  192.     register char  *s;        /* pointer to new space */
  193.  
  194.     nsize = strlen(Curschar->linep->s) + 1 + n;    /* size required */
  195.  
  196.     if (nsize <= Curschar->linep->size)
  197.     return TRUE;
  198.  
  199.     /*
  200.      * Need to allocate more space for the string. Allow some extra space on
  201.      * the assumption that we may need it soon. This avoids excessive numbers
  202.      * of calls to malloc while entering new text. 
  203.      */
  204.     s = alloc((unsigned) (nsize + SLOP));
  205.     if (s == (char *) NULL) {
  206.     emsg("Can't add anything, file is too big!");
  207.     State = NORMAL;
  208.     return FALSE;
  209.     }
  210.     Curschar->linep->size = nsize + SLOP;
  211.     strcpy(s, Curschar->linep->s);
  212.     free(Curschar->linep->s);
  213.     Curschar->linep->s = s;
  214.  
  215.     return TRUE;
  216. }
  217.