home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / utilities / cli / gnuinfo / Source / c / gc < prev    next >
Encoding:
Text File  |  1994-10-01  |  3.0 KB  |  97 lines

  1. #include "defines.h"
  2. /* gc.c -- Functions to remember and garbage collect unused node contents. */
  3.  
  4. /* This file is part of GNU Info, a program for reading online documentation
  5.    stored in Info format.
  6.  
  7.    Copyright (C) 1993 Free Software Foundation, Inc.
  8.  
  9.    This program is free software; you can redistribute it and/or modify
  10.    it under the terms of the GNU General Public License as published by
  11.    the Free Software Foundation; either version 2, or (at your option)
  12.    any later version.
  13.  
  14.    This program is distributed in the hope that it will be useful,
  15.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.    GNU General Public License for more details.
  18.  
  19.    You should have received a copy of the GNU General Public License
  20.    along with this program; if not, write to the Free Software
  21.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23.    Written by Brian Fox (bfox@ai.mit.edu). */
  24.  
  25. #include "info.h"
  26.  
  27. /* Array of pointers to the contents of gc-able nodes.  A pointer on this
  28.    list can be garbage collected when no info window contains a node whose
  29.    contents member match the pointer. */
  30. static char **gcable_pointers = (char **)NULL;
  31. static int gcable_pointers_index = 0;
  32. static int gcable_pointers_slots = 0;
  33.  
  34. /* Add POINTER to the list of garbage collectible pointers.  A pointer
  35.    is not actually garbage collected until no info window contains a node
  36.    whose contents member is equal to the pointer. */
  37. void
  38. add_gcable_pointer (pointer)
  39.      char *pointer;
  40. {
  41.   gc_pointers ();
  42.   add_pointer_to_array (pointer, gcable_pointers_index, gcable_pointers,
  43.             gcable_pointers_slots, 10, char *);
  44. }
  45.  
  46. /* Grovel the list of info windows and gc-able pointers finding those
  47.    node->contents which are collectible, and free them. */
  48. void
  49. gc_pointers ()
  50. {
  51.   register int i, j, k;
  52.   INFO_WINDOW *iw;
  53.   char **new = (char **)NULL;
  54.   int new_index = 0;
  55.   int new_slots = 0;
  56.  
  57.   if (!info_windows || !gcable_pointers_index)
  58.     return;
  59.  
  60.   for (i = 0; iw = info_windows[i]; i++)
  61.     {
  62.       for (j = 0; j < iw->nodes_index; j++)
  63.     {
  64.       NODE *node = iw->nodes[j];
  65.  
  66.       /* If this node->contents appears in our list of gcable_pointers,
  67.          it is not gc-able, so save it. */
  68.       for (k = 0; k < gcable_pointers_index; k++)
  69.         if (gcable_pointers[k] == node->contents)
  70.           {
  71.         add_pointer_to_array
  72.           (node->contents, new_index, new, new_slots, 10, char *);
  73.         break;
  74.           }
  75.     }
  76.     }
  77.  
  78.   /* We have gathered all of the pointers which need to be saved.  Free any
  79.      of the original pointers which do not appear in the new list. */
  80.   for (i = 0; i < gcable_pointers_index; i++)
  81.     {
  82.       for (j = 0; j < new_index; j++)
  83.     if (gcable_pointers[i] == new[j])
  84.       break;
  85.  
  86.       /* If we got all the way through the new list, then the old pointer
  87.      can be garbage collected. */
  88.       if (new && !new[j])
  89.     free (gcable_pointers[i]);
  90.     }
  91.  
  92.   free (gcable_pointers);
  93.   gcable_pointers = new;
  94.   gcable_pointers_slots = new_slots;
  95.   gcable_pointers_index = new_index;
  96. }
  97.