home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / NETWORK / TEL23SRC.ZIP / TEK / TEKSTOR.C next >
Encoding:
C/C++ Source or Header  |  1991-06-24  |  4.3 KB  |  203 lines

  1. /*
  2. *
  3. *  tekstor.c by Aaron Contorer, NCSA
  4. *
  5. *  Character storage routines for use by Telnet VG 
  6. *    (virtual graphics screen) routines.
  7. *  Allocates storage incrementally as more data comes in.
  8. *  Uses larger and larger increments to avoid inefficiency.
  9. *  Full data abstraction is provided.
  10. *
  11. *  Data structure:  
  12. *    A unique-type header node begins the data structure.  This points
  13. *  to the head of a linked list of "handles".  Each handle contains:
  14. *  > a pointer to a "pool" of storage memory created by malloc()
  15. *  > an int stating the number of bytes in the pool
  16. *  > a pointer to the next handle
  17. *
  18. *  IDEAS FOR IMPROVEMENT:
  19. *  Store pool as part of handle, rather than pointed to by handle
  20. *
  21. */
  22.  
  23. #define MINPOOL 0x0200    /* smallest allowable pool */
  24. #define MAXPOOL 0x2000    /* largest allowable pool */
  25. #define STORMASTER
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #ifdef MSC
  30. #include <malloc.h>
  31. #endif
  32. #ifdef MEMORY_DEBUG
  33. #include "memdebug.h"
  34. #endif
  35. #include "tekstor.h"
  36. #include "externs.h"
  37.  
  38. #define TRUE 1
  39. #define FALSE 0
  40.  
  41. STOREP newstore()
  42. /* 
  43. *    Create a new, empty store and return a pointer to it.
  44. *    Returns NULL if not enough memory to create a new store.
  45. */
  46. {
  47.     STOREP s;
  48.     
  49.     s=(STOREP) malloc(sizeof(STORE));
  50.     if(s==NULL) {
  51.         return(NULL);
  52.       }
  53.     else {
  54.         s->lasth=s->thish=s->firsth=(HANDLEP) malloc(sizeof(HANDLE));
  55.         if(s->firsth==NULL) {
  56.             free(s);
  57.             return(NULL);
  58.           }    /* end if */
  59.         else {
  60.             s->firsth->pool=malloc(MINPOOL);
  61.             if(s->firsth->pool==NULL) {
  62.                 free(s->firsth);
  63.                 free(s);
  64.                 return(NULL);
  65.               }
  66.             else {
  67.                 s->lastelnum=s->thiselnum=-1;
  68.                 s->firsth->poolsize=MINPOOL;
  69.                 s->firsth->next=NULL;
  70.               }
  71.           }
  72.       }
  73.     return(s);
  74. }
  75.  
  76. void freestore(s)
  77. STOREP s;
  78. /*
  79.     Frees all pools and other memory space associated with store s.
  80. */
  81. {
  82.     HANDLEP h,h2;
  83.     h=s->firsth;
  84.     while(h!=NULL) {
  85.         h2=h;
  86.         free(h->pool);
  87.         h=h->next;
  88.         free(h2);
  89.       }
  90.     free(s);
  91. }
  92.  
  93. int addstore(s,d)
  94. STOREP s;
  95. char d;
  96. /*
  97.     Adds character d to the end of store s.
  98.     Returns 0 if successful, -1 if unable to add character (no memory).
  99. */
  100. {
  101.     unsigned int n; /* temp storage */
  102.     int size;
  103.     HANDLEP h;
  104.  
  105.     n=++(s->lastelnum);
  106.     size=s->lasth->poolsize;
  107.     if(n < (s->lasth->poolsize)) 
  108.         s->lasth->pool[n]=d;
  109.     else {            /* Pool full; allocate a new one. */
  110.         if(size<MAXPOOL) 
  111.             size <<= 1;
  112.         h=(HANDLEP)malloc(sizeof(HANDLE));
  113.         if(h==NULL) {
  114.             (s->lastelnum)--;
  115.             return(-1);
  116.           }
  117.         else {
  118.             h->pool=malloc(size);
  119.             if(h->pool==NULL) {
  120.                 free(h);
  121.                 (s->lastelnum)--;
  122.                 return(-1);
  123.               }
  124.             else {
  125.                 h->poolsize=size;
  126.                 h->next=NULL;
  127.                 s->lasth->next=h;
  128.                 s->lasth=h;
  129.                 s->lastelnum=0;
  130.                 h->pool[0]=d;
  131.               }
  132.           }
  133.       }             /* end of new pool allocation */
  134.     return(0);
  135. }                 /* end addstore() */
  136.  
  137. void topstore(s)
  138. STOREP s;
  139. /*
  140.     Reset stats so that a call to nextitem(s) will be retrieving the
  141.     first item in store s.
  142. */
  143. {
  144.     s->thish=s->firsth;
  145.     s->thiselnum=-1;
  146. }
  147.  
  148. int nextitem(s)
  149. STOREP s;
  150. /*
  151.     Increment the current location in store s.  Then return the
  152.     character at that location.  Returns -1 if no more characters.
  153. */
  154. {
  155.     HANDLEP h;
  156.  
  157.     if(s->thish==s->lasth&&s->thiselnum==s->lastelnum) 
  158.         return(-1);
  159.     else {
  160.         h=s->thish;
  161.         if((++(s->thiselnum)) < (int)(s->thish->poolsize)) 
  162.             return((int)(s->thish->pool[s->thiselnum]));
  163.         else {            /* move to next pool */
  164.             h=h->next;
  165.             s->thish=h;
  166.             s->thiselnum=0;
  167.             return((int)(h->pool[0]));
  168.           }
  169.       }
  170. }             /* end nextitem() */
  171.  
  172. int unstore(s)
  173. STOREP s;
  174. /*
  175.     Removes ("pops") the last item from the specified store.
  176.     Returns that item (in range 0-255), or returns -1 if there
  177.     are no items in the store.
  178. */
  179. {
  180.     HANDLEP nextolast;
  181.  
  182.     if(s->lastelnum>-1)              /* last pool not empty */
  183.         return((int)(s->lasth->pool[(s->lastelnum)--]));
  184.     else {                         /* last pool empty */
  185.         if(s->lasth==s->firsth) 
  186.             return(-1);
  187.         else {                     /* move back one pool */
  188.             nextolast=s->firsth;
  189.             while(nextolast->next!=s->lasth)
  190.                 nextolast=nextolast->next;
  191.             free(nextolast->next);
  192.             s->lasth=nextolast;
  193.             s->lastelnum=nextolast->poolsize-2;
  194.             if(s->thish==nextolast->next) {
  195.                 s->thish=nextolast;
  196.                 s->thiselnum=s->lastelnum;
  197.               }
  198.             nextolast->next=NULL;
  199.             return((int)(nextolast->pool[s->lastelnum+1]));
  200.           }
  201.       }
  202. }
  203.