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