* __scm.h (SCM_ALLOW_INTS_ONLY): Removed.
[bpt/guile.git] / libguile / gc-freelist.c
CommitLineData
c7743d02
HWN
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>
5bd4a949 43#include <stdio.h>
c7743d02
HWN
44
45#include "libguile/private-gc.h"
46#include "libguile/gc.h"
47#include "libguile/deprecation.h"
48#include "libguile/private-gc.h"
49
50scm_t_cell_type_statistics scm_i_master_freelist;
51scm_t_cell_type_statistics scm_i_master_freelist2;
52
53
54
55
56/*
57
58In older versions of GUILE GC there was extensive support for
59debugging freelists. This was useful, since the freelist was kept
60inside the heap, and writing to an object that was GC'd would mangle
61the list. Mark bits are now separate, and checking for sane cell
62access can be done much more easily by simply checking if the mark bit
63is unset before allocation. --hwn
64
65
66
67*/
68
69#if (SCM_ENABLE_DEPRECATED == 1)
70#if defined(GUILE_DEBUG_FREELIST)
71
72SCM_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
81SCM_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
103void
104scm_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",
f2893a25 127 (unsigned long) scm_cells_allocated,
c7743d02
HWN
128 (long) delta);
129#endif
130 if (delta > 0)
131 freelist->min_yield += delta;
132 }
133}
134
135
136static void
137scm_init_freelist (scm_t_cell_type_statistics *freelist,
138 int span,
139 int min_yield)
140{
dac04e9f
HWN
141 if (min_yield < 1)
142 min_yield = 1;
143 if (min_yield > 99)
144 min_yield = 99;
145
c7743d02
HWN
146 freelist->heap_segment_idx = -1;
147 freelist->min_yield = 0;
148 freelist->min_yield_fraction = min_yield;
149 freelist->span = span;
150 freelist->collected = 0;
151 freelist->collected_1 = 0;
152 freelist->heap_size = 0;
153}
154
155#if (SCM_ENABLE_DEPRECATED == 1)
156 size_t scm_default_init_heap_size_1;
157 int scm_default_min_yield_1;
158 size_t scm_default_init_heap_size_2;
159 int scm_default_min_yield_2;
160 size_t scm_default_max_segment_size;
161#endif
162
163void
164scm_gc_init_freelist (void)
165{
dac04e9f 166 int init_heap_size_1
c7743d02 167 = scm_getenv_int ("GUILE_INIT_SEGMENT_SIZE_1", SCM_DEFAULT_INIT_HEAP_SIZE_1);
dac04e9f 168 int init_heap_size_2
c7743d02
HWN
169 = scm_getenv_int ("GUILE_INIT_SEGMENT_SIZE_2", SCM_DEFAULT_INIT_HEAP_SIZE_2);
170
9bc4701c
MD
171 /* These are the thread-local freelists. */
172 scm_key_create (&scm_i_freelist, free);
173 scm_key_create (&scm_i_freelist2, free);
174 SCM_FREELIST_CREATE (scm_i_freelist);
175 SCM_FREELIST_CREATE (scm_i_freelist2);
c7743d02
HWN
176
177 scm_init_freelist (&scm_i_master_freelist2, 2,
178 scm_getenv_int ("GUILE_MIN_YIELD_2", SCM_DEFAULT_MIN_YIELD_2));
179 scm_init_freelist (&scm_i_master_freelist, 1,
180 scm_getenv_int ("GUILE_MIN_YIELD_1", SCM_DEFAULT_MIN_YIELD_1));
181
c7743d02 182 scm_max_segment_size = scm_getenv_int ("GUILE_MAX_SEGMENT_SIZE", SCM_DEFAULT_MAX_SEGMENT_SIZE);
dac04e9f
HWN
183
184 if (scm_max_segment_size <= 0)
185 scm_max_segment_size = SCM_DEFAULT_MAX_SEGMENT_SIZE;
186
c7743d02
HWN
187
188 scm_i_make_initial_segment (init_heap_size_1, &scm_i_master_freelist);
189 scm_i_make_initial_segment (init_heap_size_2, &scm_i_master_freelist2);
c7743d02
HWN
190
191#if (SCM_ENABLE_DEPRECATED == 1)
192 if ( scm_default_init_heap_size_1 ||
193 scm_default_min_yield_1||
194 scm_default_init_heap_size_2||
195 scm_default_min_yield_2||
196 scm_default_max_segment_size)
197 {
198 scm_c_issue_deprecation_warning ("Tuning heap parameters with C variables is deprecated. Use environment variables instead.");
199 }
200#endif
201}
202
203
204void
205scm_i_gc_sweep_freelist_reset (scm_t_cell_type_statistics *freelist)
206{
207 freelist->collected_1 = freelist->collected;
208 freelist->collected = 0;
209
210 /*
211 at the end we simply start with the lowest segment again.
212 */
213 freelist->heap_segment_idx = -1;
214}
215
216int
217scm_i_gc_grow_heap_p (scm_t_cell_type_statistics * freelist)
218{
219 return SCM_MAX (freelist->collected,freelist->collected_1) < freelist->min_yield;
220}