Upgrade Texinfo from 4.8 to 4.13 on the vendor branch
[dragonfly.git] / contrib / texinfo / info / gc.c
1 /* gc.c -- Functions to remember and garbage collect unused node contents.
2    $Id: gc.c,v 1.7 2008/06/11 09:55:42 gray Exp $
3
4    Copyright (C) 1993, 2004, 2007, 2008 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 3 of the License, or (at
9    your option) 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, see <http://www.gnu.org/licenses/>.
18
19    Written by Brian Fox (bfox@ai.mit.edu). */
20
21 #include "info.h"
22
23 /* Array of pointers to the contents of gc-able nodes.  A pointer on this
24    list can be garbage collected when no info window contains a node whose
25    contents member match the pointer. */
26 static char **gcable_pointers = NULL;
27 static int gcable_pointers_index = 0;
28 static int gcable_pointers_slots = 0;
29
30 /* Add POINTER to the list of garbage collectible pointers.  A pointer
31    is not actually garbage collected until no info window contains a node
32    whose contents member is equal to the pointer. */
33 void
34 add_gcable_pointer (char *pointer)
35 {
36   gc_pointers ();
37   add_pointer_to_array (pointer, gcable_pointers_index, gcable_pointers,
38                         gcable_pointers_slots, 10, char *);
39 }
40
41 /* Grovel the list of info windows and gc-able pointers finding those
42    node->contents which are collectible, and free them. */
43 void
44 gc_pointers (void)
45 {
46   register int i, j, k;
47   INFO_WINDOW *iw;
48   char **new = NULL;
49   int new_index = 0;
50   int new_slots = 0;
51
52   if (!info_windows || !gcable_pointers_index)
53     return;
54
55   for (i = 0; (iw = info_windows[i]); i++)
56     {
57       for (j = 0; j < iw->nodes_index; j++)
58         {
59           NODE *node = iw->nodes[j];
60
61           /* If this node->contents appears in our list of gcable_pointers,
62              it is not gc-able, so save it. */
63           for (k = 0; k < gcable_pointers_index; k++)
64             if (gcable_pointers[k] == node->contents)
65               {
66                 add_pointer_to_array
67                   (node->contents, new_index, new, new_slots, 10, char *);
68                 break;
69               }
70         }
71     }
72
73   /* We have gathered all of the pointers which need to be saved.  Free any
74      of the original pointers which do not appear in the new list. */
75   for (i = 0; i < gcable_pointers_index; i++)
76     {
77       for (j = 0; j < new_index; j++)
78         if (gcable_pointers[i] == new[j])
79           break;
80
81       /* If we got all the way through the new list, then the old pointer
82          can be garbage collected. */
83       if (new && !new[j])
84         free (gcable_pointers[i]);
85     }
86
87   free (gcable_pointers);
88   gcable_pointers = new;
89   gcable_pointers_slots = new_slots;
90   gcable_pointers_index = new_index;
91 }