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