include <stdio.h>
[bpt/guile.git] / libguile / gc-freelist.c
1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
41
42 #include <assert.h>
43 #include <stdio.h>
44
45 #include "libguile/private-gc.h"
46 #include "libguile/gc.h"
47 #include "libguile/deprecation.h"
48 #include "libguile/private-gc.h"
49
50 scm_t_cell_type_statistics scm_i_master_freelist;
51 scm_t_cell_type_statistics scm_i_master_freelist2;
52
53
54
55
56 /*
57
58 In older versions of GUILE GC there was extensive support for
59 debugging freelists. This was useful, since the freelist was kept
60 inside the heap, and writing to an object that was GC'd would mangle
61 the list. Mark bits are now separate, and checking for sane cell
62 access can be done much more easily by simply checking if the mark bit
63 is unset before allocation. --hwn
64
65
66
67 */
68
69 #if (SCM_ENABLE_DEPRECATED == 1)
70 #if defined(GUILE_DEBUG_FREELIST)
71
72 SCM_DEFINE (scm_map_free_list, "map-free-list", 0, 0, 0,
73 (),
74 "DEPRECATED\n")
75 #define FUNC_NAME s_scm_map_free_list
76 {
77 scm_c_issue_deprecation_warning ("map-free-list has been removed from GUILE. Doing nothing\n");
78 return SCM_UNSPECIFIED;
79 }
80
81 SCM_DEFINE (scm_gc_set_debug_check_freelist_x, "gc-set-debug-check-freelist!", 1, 0, 0,
82 (SCM flag),
83 "DEPRECATED.\n")
84 #define FUNC_NAME s_scm_gc_set_debug_check_freelist_x
85 {
86 scm_c_issue_deprecation_warning ("gc-set-debug-check-freelist! has been removed from GUILE. Doing nothing\n");
87 return SCM_UNSPECIFIED;
88 }
89 #undef FUNC_NAME
90
91
92 #endif /* defined (GUILE_DEBUG) */
93 #endif /* deprecated */
94
95
96
97
98 /*
99 This adjust FREELIST variables to decide wether or not to allocate
100 more heap in the next GC run. It uses scm_gc_cells_collected and scm_gc_cells_collected1
101 */
102
103 void
104 scm_i_adjust_min_yield (scm_t_cell_type_statistics *freelist)
105 {
106 /* min yield is adjusted upwards so that next predicted total yield
107 * (allocated cells actually freed by GC) becomes
108 * `min_yield_fraction' of total heap size. Note, however, that
109 * the absolute value of min_yield will correspond to `collected'
110 * on one master (the one which currently is triggering GC).
111 *
112 * The reason why we look at total yield instead of cells collected
113 * on one list is that we want to take other freelists into account.
114 * On this freelist, we know that (local) yield = collected cells,
115 * but that's probably not the case on the other lists.
116 *
117 * (We might consider computing a better prediction, for example
118 * by computing an average over multiple GC:s.)
119 */
120 if (freelist->min_yield_fraction)
121 {
122 /* Pick largest of last two yields. */
123 long delta = ((SCM_HEAP_SIZE * freelist->min_yield_fraction / 100)
124 - (long) SCM_MAX (scm_gc_cells_collected_1, scm_gc_cells_collected));
125 #ifdef DEBUGINFO
126 fprintf (stderr, " after GC = %lu, delta = %ld\n",
127 (unsigned long) scm_cells_allocated,
128 (long) delta);
129 #endif
130 if (delta > 0)
131 freelist->min_yield += delta;
132 }
133 }
134
135
136 static void
137 scm_init_freelist (scm_t_cell_type_statistics *freelist,
138 int span,
139 int min_yield)
140 {
141 freelist->heap_segment_idx = -1;
142 freelist->min_yield = 0;
143 freelist->min_yield_fraction = min_yield;
144 freelist->span = span;
145 freelist->collected = 0;
146 freelist->collected_1 = 0;
147 freelist->heap_size = 0;
148 }
149
150 #if (SCM_ENABLE_DEPRECATED == 1)
151 size_t scm_default_init_heap_size_1;
152 int scm_default_min_yield_1;
153 size_t scm_default_init_heap_size_2;
154 int scm_default_min_yield_2;
155 size_t scm_default_max_segment_size;
156 #endif
157
158 void
159 scm_gc_init_freelist (void)
160 {
161 size_t init_heap_size_1
162 = scm_getenv_int ("GUILE_INIT_SEGMENT_SIZE_1", SCM_DEFAULT_INIT_HEAP_SIZE_1);
163
164 size_t init_heap_size_2
165 = scm_getenv_int ("GUILE_INIT_SEGMENT_SIZE_2", SCM_DEFAULT_INIT_HEAP_SIZE_2);
166
167 scm_i_freelist = SCM_EOL;
168 scm_i_freelist2 = SCM_EOL;
169
170 scm_init_freelist (&scm_i_master_freelist2, 2,
171 scm_getenv_int ("GUILE_MIN_YIELD_2", SCM_DEFAULT_MIN_YIELD_2));
172 scm_init_freelist (&scm_i_master_freelist, 1,
173 scm_getenv_int ("GUILE_MIN_YIELD_1", SCM_DEFAULT_MIN_YIELD_1));
174
175
176 scm_max_segment_size = scm_getenv_int ("GUILE_MAX_SEGMENT_SIZE", SCM_DEFAULT_MAX_SEGMENT_SIZE);
177
178 scm_i_make_initial_segment (init_heap_size_1, &scm_i_master_freelist);
179 scm_i_make_initial_segment (init_heap_size_2, &scm_i_master_freelist2);
180
181
182 #if (SCM_ENABLE_DEPRECATED == 1)
183 if ( scm_default_init_heap_size_1 ||
184 scm_default_min_yield_1||
185 scm_default_init_heap_size_2||
186 scm_default_min_yield_2||
187 scm_default_max_segment_size)
188 {
189 scm_c_issue_deprecation_warning ("Tuning heap parameters with C variables is deprecated. Use environment variables instead.");
190 }
191 #endif
192 }
193
194
195 void
196 scm_i_gc_sweep_freelist_reset (scm_t_cell_type_statistics *freelist)
197 {
198 freelist->collected_1 = freelist->collected;
199 freelist->collected = 0;
200
201 /*
202 at the end we simply start with the lowest segment again.
203 */
204 freelist->heap_segment_idx = -1;
205 }
206
207 int
208 scm_i_gc_grow_heap_p (scm_t_cell_type_statistics * freelist)
209 {
210 return SCM_MAX (freelist->collected,freelist->collected_1) < freelist->min_yield;
211 }