home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume31 / tin / part14 / memory.c < prev    next >
C/C++ Source or Header  |  1992-07-08  |  8KB  |  332 lines

  1. /*
  2.  *  Project   : tin - a threaded Netnews reader
  3.  *  Module    : memory.c
  4.  *  Author    : I.Lea & R.Skrenta
  5.  *  Created   : 01-04-91
  6.  *  Updated   : 03-06-92
  7.  *  Notes     :
  8.  *  Copyright : (c) Copyright 1991-92 by Iain Lea & Rich Skrenta
  9.  *              You may  freely  copy or  redistribute  this software,
  10.  *              so  long as there is no profit made from its use, sale
  11.  *              trade or  reproduction.  You may not change this copy-
  12.  *              right notice, and it must be included in any copy made
  13.  */
  14.  
  15. #include    "tin.h"
  16.  
  17. int *my_group;                    /* .newsrc --> active[] */
  18. int *unread;                    /* highest art read in group */
  19. long *base;
  20. struct group_t *active;            /* active file */
  21. struct article_t *arts;
  22.  
  23. /*
  24.  *  Dynamic table management
  25.  *  These settings are memory conservative:  small initial allocations
  26.  *  and a 50% expansion on table overflow.  A fast vm system with
  27.  *  much memory might want to start with higher initial allocations
  28.  *  and a 100% expansion on overflow, especially for the arts[] array.
  29.  */
  30.  
  31. void init_alloc ()
  32. {
  33.     max_active = DEFAULT_ACTIVE_NUM;
  34.     max_art = DEFAULT_ARTICLE_NUM;
  35.  
  36.     active = (struct group_t *) my_malloc ((unsigned) sizeof(*active) * max_active);
  37.     my_group = (int *) my_malloc ((unsigned) sizeof(int) * max_active);
  38.     unread = (int *) my_malloc ((unsigned) sizeof(int) * max_active);
  39.  
  40.     arts = (struct article_t *) my_malloc ((unsigned) sizeof(*arts) * max_art);
  41.     base = (long *) my_malloc ((unsigned) sizeof(long) * max_art);
  42.  
  43.     max_kill = DEFAULT_KILL_NUM;
  44.     
  45.     killf = (struct kill_t *) my_malloc ((unsigned) sizeof(*killf) * max_kill);
  46.  
  47.     max_save = DEFAULT_SAVE_NUM;
  48.     
  49.     save = (struct save_t *) my_malloc ((unsigned) sizeof(*save) * max_save);
  50.  
  51.     screen = (struct screen_t *) 0;
  52. }
  53.  
  54.  
  55. void expand_art()
  56. {
  57.     max_art += max_art / 2;        /* increase by 50% */
  58.  
  59.     arts = (struct article_t *) my_realloc ((char *) arts, (unsigned) sizeof(*arts) * max_art);
  60.     base = (long *) my_realloc ((char *) base, (unsigned) sizeof(long) * max_art);
  61. }
  62.  
  63.  
  64. void expand_active()
  65. {
  66.     max_active += max_active / 2;        /* increase by 50% */
  67.  
  68.     if (active == (struct group_t *) 0) {
  69.         active = (struct group_t *) my_malloc ((unsigned) sizeof(*active) * max_active);
  70.         my_group = (int *) my_malloc ((unsigned) sizeof(int) * max_active);
  71.         unread = (int *) my_malloc ((unsigned) sizeof(int) * max_active);
  72.     } else {
  73.         active = (struct group_t *) my_realloc((char *) active,
  74.                  (unsigned) sizeof(*active) * max_active);
  75.         my_group = (int *) my_realloc((char *) my_group, (unsigned) sizeof(int) * max_active);
  76.         unread = (int *) my_realloc((char *) unread, (unsigned) sizeof(int) * max_active);
  77.     }
  78. }
  79.  
  80.  
  81. void expand_kill()
  82. {
  83.     max_kill += max_kill / 2;        /* increase by 50% */
  84.  
  85.     killf = (struct kill_t *) my_realloc((char *) killf, (unsigned) sizeof(struct kill_t) * max_kill);
  86. }
  87.  
  88.  
  89. void expand_save()
  90. {
  91.     max_save += max_save / 2;        /* increase by 50% */
  92.  
  93.     save = (struct save_t *) my_realloc((char *) save, (unsigned) sizeof(struct save_t) * max_save);
  94. }
  95.  
  96.  
  97. void init_screen_array (allocate)
  98.     int allocate;
  99. {
  100.     int i;
  101.  
  102.     if (allocate) {
  103.         screen = (struct screen_t *) my_malloc(
  104.             (unsigned) sizeof(struct screen_t) * LINES+1);
  105.  
  106.         for (i=0 ; i < LINES ; i++) {
  107.             screen[i].col = (char *) my_malloc ((unsigned) COLS+2);
  108.         }
  109.     } else {
  110.         if (screen != (struct screen_t *) 0) {
  111.             for (i=0 ; i < LINES ; i++) {
  112.                 if (screen[i].col != (char *) 0) {
  113.                     free ((char *) screen[i].col);
  114.                     screen[i].col = (char *) 0;
  115.                 }
  116.             }    
  117.  
  118.             free ((char *) screen);
  119.             screen = (struct screen_t *) 0;
  120.         }
  121.     }
  122. }
  123.  
  124.  
  125. void free_all_arrays ()
  126. {
  127.     hash_reclaim ();
  128.     
  129.     init_screen_array (FALSE);
  130.  
  131.     free_art_array ();
  132.  
  133.     if (arts != (struct article_t *) 0) {
  134.         free ((char *) arts);
  135.         arts = (struct article_t *) 0;
  136.     }
  137.  
  138.     free_active_arrays ();
  139.  
  140.     if (base != (long *) 0) {
  141.         free ((char *) base);
  142.         base = (long *) 0;
  143.     }
  144.  
  145.     if (killf != (struct kill_t *) 0) {
  146.         free_kill_array ();
  147.         if (killf != (struct kill_t *) 0) {
  148.             free ((char *) killf);
  149.             killf = (struct kill_t *) 0;
  150.         }
  151.     }
  152.  
  153.     if (save != (struct save_t *) 0) {
  154.         free_save_array ();
  155.         if (save != (struct save_t *) 0) {
  156.             free ((char *) save);
  157.             save = (struct save_t *) 0;
  158.         }
  159.     }
  160. }
  161.  
  162.  
  163. void free_art_array ()
  164. {
  165.     register int i;
  166.  
  167.     for (i=0 ; i < top ; i++) {
  168.         arts[i].artnum = 0L;
  169.         arts[i].thread = ART_EXPIRED;
  170.         arts[i].inthread = FALSE;
  171.         arts[i].unread = ART_UNREAD;
  172.         arts[i].killed = FALSE;
  173.         arts[i].tagged = FALSE;
  174.         arts[i].hot = FALSE;
  175.         arts[i].date[0] = '\0';
  176.         if (arts[i].part != (char *) 0) {
  177.             free ((char *) arts[i].part);
  178.             arts[i].part = (char *) 0;
  179.         }
  180.         if (arts[i].patch != (char *) 0) {
  181.             free ((char *) arts[i].patch);
  182.             arts[i].patch = (char *) 0;
  183.         }
  184.         if (arts[i].xref != NULL) {
  185.             free ((char *) arts[i].xref);
  186.             arts[i].xref = NULL;
  187.         }
  188.     }
  189. }
  190.  
  191.  
  192. void free_active_arrays ()
  193. {
  194.     register int i;
  195.     
  196.     if (my_group != (int *) 0) {            /* my_group[] */
  197.         free ((char *) my_group);
  198.         my_group = (int *) 0;
  199.     }
  200.  
  201.     if (unread != (int *) 0) {                /* unread[] */
  202.         free ((char *) unread);
  203.         unread = (int *) 0;
  204.     }
  205.  
  206.     if (active != (struct group_t *) 0) {    /* active[] */
  207.         for (i=0 ; i < num_active ; i++) {
  208.             if (active[i].name != (char *) 0) {
  209.                 free ((char *) active[i].name);
  210.                 active[i].name = (char *) 0;
  211.             }
  212.             if (active[i].description != (char *) 0) {
  213.                 free ((char *) active[i].description);
  214.                 active[i].description = (char *) 0;
  215.             }
  216.             if (active[i].attribute.server != (char *) 0) {
  217.                 free ((char *) active[i].attribute.server);
  218.                 active[i].attribute.server = (char *) 0;
  219.             }
  220.             if (active[i].attribute.maildir != (char *) 0 &&
  221.                 active[i].attribute.maildir != default_maildir) {
  222.                 free ((char *) active[i].attribute.maildir);
  223.                 active[i].attribute.maildir = (char *) 0;
  224.             }
  225.             if (active[i].attribute.savedir != (char *) 0 &&
  226.                 active[i].attribute.savedir != default_savedir) {
  227.                 free ((char *) active[i].attribute.savedir);
  228.                 active[i].attribute.savedir = (char *) 0;
  229.             }
  230.             if (active[i].attribute.sigfile != (char *) 0 &&
  231.                 active[i].attribute.sigfile != default_sigfile) {
  232.                 free ((char *) active[i].attribute.sigfile);
  233.                 active[i].attribute.sigfile = (char *) 0;
  234.             }
  235.         }
  236.         if (active != (struct group_t *) 0) {
  237.             free ((char *) active);
  238.             active = (struct group_t *) 0;
  239.         }
  240.     }
  241. }
  242.  
  243.  
  244. void free_kill_array ()
  245. {
  246.     int i;
  247.     
  248.     for (i=0 ; i < kill_num ; i++) {
  249.         if (killf[i].kill_subj != (char *) 0) {
  250.             free ((char *) killf[i].kill_subj);
  251.             killf[i].kill_subj = (char *) 0;
  252.         }
  253.         if (killf[i].kill_from != (char *) 0) {
  254.             free ((char *) killf[i].kill_from);
  255.             killf[i].kill_from = (char *) 0;
  256.         }
  257.     }
  258. }
  259.  
  260.  
  261. /*
  262.  *  reset save list array to 0 and free's all its allocated memory
  263.  */
  264.  
  265. void free_save_array ()
  266. {
  267.     int i;
  268.     
  269.     for (i=0 ; i < save_num ; i++) {
  270.         if (save[i].subject != (char *) 0) {
  271.             free ((char *) save[i].subject);
  272.             save[i].subject = (char *) 0;
  273.         }
  274.         if (save[i].archive != (char *) 0) {
  275.             free ((char *) save[i].archive);
  276.             save[i].archive = (char *) 0;
  277.         }
  278.         if (save[i].dir != (char *) 0) {
  279.             free ((char *) save[i].dir);
  280.             save[i].dir = (char *) 0;
  281.         }
  282.         if (save[i].file != (char *) 0) {
  283.             free ((char *) save[i].file);
  284.             save[i].file = (char *) 0;
  285.         }
  286.         if (save[i].part != (char *) 0) {
  287.             free ((char *) save[i].part);
  288.             save[i].part = (char *) 0;
  289.         }
  290.         if (save[i].patch != (char *) 0) {
  291.             free ((char *) save[i].patch);
  292.             save[i].patch = (char *) 0;
  293.         }
  294.         save[i].index   = -1;
  295.         save[i].saved   = FALSE;
  296.         save[i].is_mailbox = FALSE;
  297.     }
  298.     
  299.     save_num = 0;
  300. }
  301.  
  302.  
  303. char *my_malloc (size)
  304.     unsigned size;
  305. {
  306.     char *p;
  307.  
  308.     if ((p = (char *) calloc (1, (int) size)) == NULL) {
  309.         error_message (txt_out_of_memory, progname);
  310.         tin_done (1);
  311.     }
  312.     return p;
  313. }
  314.  
  315.  
  316. char *my_realloc (p, size)
  317.     char *p;
  318.     unsigned size;
  319. {
  320.     if (! p) {
  321.         p = (char *) calloc (1, (int) size);
  322.     } else {
  323.         p = (char *) realloc (p, (int) size);
  324.     }
  325.  
  326.     if (! p) {
  327.         error_message (txt_out_of_memory, progname);
  328.         tin_done (1);
  329.     }
  330.     return p;
  331. }
  332.