home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / vn.jan.88 / part03 / storage.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-30  |  2.2 KB  |  155 lines

  1. /*
  2. ** vn news reader.
  3. **
  4. ** storage.c - storage allocation routines
  5. **
  6. ** see copyright disclaimer / history in vn.c source file
  7. */
  8.  
  9. #include <stdio.h>
  10. #include "tune.h"
  11. #include "node.h"
  12. #include "page.h"
  13.  
  14. extern char *malloc();
  15.  
  16. extern int L_allow;
  17.  
  18. extern PAGE Page;
  19. /*
  20.     Storage allocaters.
  21. */
  22.  
  23. char *str_store (s)
  24. char *s;
  25. {
  26.     static unsigned av_len = 0;    /* current storage available */
  27.     static char *avail;
  28.     int len;
  29.  
  30.     if (s == NULL)
  31.         s = "";
  32.  
  33.     if ((len = strlen(s)+1) > av_len)
  34.     {
  35.         if (len > STRBLKSIZE)
  36.             av_len = len;
  37.         else
  38.             av_len = STRBLKSIZE;
  39.         if ((avail = malloc(av_len)) == NULL)
  40.             printex ("can't allocate memory for string storage");
  41.     }
  42.     strcpy (avail,s);
  43.     s = avail;
  44.     avail += len;
  45.     av_len -= len;
  46.     return (s);
  47. }
  48.  
  49. /*
  50. ** called after number of terminal lines (L_allow) is known, to set
  51. ** up storage for Page.
  52. */
  53. page_alloc ()
  54. {
  55.     char *body;
  56.  
  57.     if ((body = malloc(L_allow*sizeof(BODY))) == NULL)
  58.         printex ("can't allocate memory for display storage");
  59.  
  60.     Page.b = (BODY *) body;
  61. }
  62.  
  63. NODE
  64. *node_store()
  65. {
  66.     static int nd_avail = 0;
  67.     static NODE *nd;
  68.     NODE *ret;
  69.  
  70.     if (nd_avail <= 0)
  71.     {
  72.         if ((nd = (NODE *) malloc(sizeof(NODE)*NDBLKSIZE)) == NULL)
  73.             printex ("can't allocate memory for newsgroup table");
  74.         nd_avail = NDBLKSIZE;
  75.     }
  76.     --nd_avail;
  77.     ret = nd;
  78.     ++nd;
  79.     return(ret);
  80. }
  81.  
  82. /*
  83. ** temp string storage
  84. */
  85.  
  86. typedef struct
  87. {
  88.     int len;
  89.     int idx;
  90.     char **ptr;
  91. } STRINGPOOL;
  92.  
  93. char *
  94. str_tpool(n)
  95. int n;
  96. {
  97.     int size;
  98.     STRINGPOOL *p;
  99.  
  100.     size = sizeof(STRINGPOOL) + n * sizeof(char **);
  101.  
  102.     if ((p = (STRINGPOOL *) malloc(size)) == NULL)
  103.         printex("Cannot allocate temporary string storage");
  104.  
  105.     p->ptr = (char **)(p+1);
  106.     p->len = n;
  107.     p->idx = 0;
  108.  
  109.     return((char *) p);
  110. }
  111.  
  112. char *
  113. str_tstore(cp,s)
  114. char *cp;
  115. char *s;
  116. {
  117.     STRINGPOOL *p;
  118.     int len;
  119.  
  120.     p = (STRINGPOOL *) cp;
  121.     if (p->idx >= p->len)
  122.         printex("Temporary string storage overflow");
  123.     len = strlen(s)+1;
  124.     if ((cp = malloc(len)) == NULL)
  125.         printex("Cannot allocate copy of string");
  126.     strcpy(cp,s);
  127.     (p->ptr)[p->idx] = cp;
  128.     ++(p->idx);
  129.  
  130.     return(cp);
  131. }
  132.  
  133. char **
  134. str_taptr(cp)
  135. char *cp;
  136. {
  137.     STRINGPOOL *p;
  138.  
  139.     p = (STRINGPOOL *) cp;
  140.  
  141.     return (p->ptr + p->idx);
  142. }
  143.  
  144. str_tfree(cp)
  145. char *cp;
  146. {
  147.     STRINGPOOL *p;
  148.     int i;
  149.  
  150.     p = (STRINGPOOL *) cp;
  151.     for (i=0; i < p->idx; ++i)
  152.         free((p->ptr)[i]);
  153.     free (cp);
  154. }
  155.