Back to index

tetex-bin  3.0
gc.c
Go to the documentation of this file.
00001 /* gc.c -- Functions to remember and garbage collect unused node contents.
00002    $Id: gc.c,v 1.3 2004/04/11 17:56:45 karl Exp $
00003 
00004    Copyright (C) 1993, 2004 Free Software Foundation, Inc.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2, or (at your option)
00009    any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00019 
00020    Written by Brian Fox (bfox@ai.mit.edu). */
00021 
00022 #include "info.h"
00023 
00024 /* Array of pointers to the contents of gc-able nodes.  A pointer on this
00025    list can be garbage collected when no info window contains a node whose
00026    contents member match the pointer. */
00027 static char **gcable_pointers = (char **)NULL;
00028 static int gcable_pointers_index = 0;
00029 static int gcable_pointers_slots = 0;
00030 
00031 /* Add POINTER to the list of garbage collectible pointers.  A pointer
00032    is not actually garbage collected until no info window contains a node
00033    whose contents member is equal to the pointer. */
00034 void
00035 add_gcable_pointer (char *pointer)
00036 {
00037   gc_pointers ();
00038   add_pointer_to_array (pointer, gcable_pointers_index, gcable_pointers,
00039                      gcable_pointers_slots, 10, char *);
00040 }
00041 
00042 /* Grovel the list of info windows and gc-able pointers finding those
00043    node->contents which are collectible, and free them. */
00044 void
00045 gc_pointers (void)
00046 {
00047   register int i, j, k;
00048   INFO_WINDOW *iw;
00049   char **new = (char **)NULL;
00050   int new_index = 0;
00051   int new_slots = 0;
00052 
00053   if (!info_windows || !gcable_pointers_index)
00054     return;
00055 
00056   for (i = 0; (iw = info_windows[i]); i++)
00057     {
00058       for (j = 0; j < iw->nodes_index; j++)
00059        {
00060          NODE *node = iw->nodes[j];
00061 
00062          /* If this node->contents appears in our list of gcable_pointers,
00063             it is not gc-able, so save it. */
00064          for (k = 0; k < gcable_pointers_index; k++)
00065            if (gcable_pointers[k] == node->contents)
00066              {
00067               add_pointer_to_array
00068                 (node->contents, new_index, new, new_slots, 10, char *);
00069               break;
00070              }
00071        }
00072     }
00073 
00074   /* We have gathered all of the pointers which need to be saved.  Free any
00075      of the original pointers which do not appear in the new list. */
00076   for (i = 0; i < gcable_pointers_index; i++)
00077     {
00078       for (j = 0; j < new_index; j++)
00079        if (gcable_pointers[i] == new[j])
00080          break;
00081 
00082       /* If we got all the way through the new list, then the old pointer
00083         can be garbage collected. */
00084       if (new && !new[j])
00085        free (gcable_pointers[i]);
00086     }
00087 
00088   free (gcable_pointers);
00089   gcable_pointers = new;
00090   gcable_pointers_slots = new_slots;
00091   gcable_pointers_index = new_index;
00092 }