1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2006 Free Software Foundation, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <errno.h>
#include <string.h>
#ifdef __ia64__
#include <ucontext.h>
extern unsigned long * __libc_ia64_register_backing_store_base;
#endif
#include "libguile/_scm.h"
#include "libguile/eval.h"
#include "libguile/stime.h"
#include "libguile/stackchk.h"
#include "libguile/struct.h"
#include "libguile/smob.h"
#include "libguile/unif.h"
#include "libguile/async.h"
#include "libguile/ports.h"
#include "libguile/root.h"
#include "libguile/strings.h"
#include "libguile/vectors.h"
#include "libguile/weaks.h"
#include "libguile/hashtab.h"
#include "libguile/tags.h"
#include "libguile/validate.h"
#include "libguile/deprecation.h"
#include "libguile/gc.h"
#include "libguile/private-gc.h"
#ifdef GUILE_DEBUG_MALLOC
#include "libguile/debug-malloc.h"
#endif
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
/*
INIT_MALLOC_LIMIT is the initial amount of malloc usage which will
trigger a GC.
After startup (at the guile> prompt), we have approximately 100k of
alloced memory, which won't go away on GC. Let's set the init such
that we get a nice yield on the next allocation:
*/
#define SCM_DEFAULT_INIT_MALLOC_LIMIT 200*1024
#define SCM_DEFAULT_MALLOC_MINYIELD 40
/* #define DEBUGINFO */
static int scm_i_minyield_malloc;
void
scm_gc_init_malloc (void)
{
scm_mtrigger = scm_getenv_int ("GUILE_INIT_MALLOC_LIMIT",
SCM_DEFAULT_INIT_MALLOC_LIMIT);
scm_i_minyield_malloc = scm_getenv_int ("GUILE_MIN_YIELD_MALLOC",
SCM_DEFAULT_MALLOC_MINYIELD);
if (scm_i_minyield_malloc >= 100)
scm_i_minyield_malloc = 99;
if (scm_i_minyield_malloc < 1)
scm_i_minyield_malloc = 1;
if (scm_mtrigger < 0)
scm_mtrigger = SCM_DEFAULT_INIT_MALLOC_LIMIT;
}
/* Function for non-cell memory management.
*/
void *
scm_realloc (void *mem, size_t size)
{
void *ptr;
scm_t_sweep_statistics sweep_stats;
SCM_SYSCALL (ptr = realloc (mem, size));
if (ptr)
return ptr;
scm_i_scm_pthread_mutex_lock (&scm_i_sweep_mutex);
scm_gc_running_p = 1;
scm_i_sweep_all_segments ("realloc", &sweep_stats);
SCM_SYSCALL (ptr = realloc (mem, size));
if (ptr)
{
scm_gc_running_p = 0;
scm_i_pthread_mutex_unlock (&scm_i_sweep_mutex);
return ptr;
}
scm_i_gc ("realloc");
scm_i_sweep_all_segments ("realloc", &sweep_stats);
scm_gc_running_p = 0;
scm_i_pthread_mutex_unlock (&scm_i_sweep_mutex);
SCM_SYSCALL (ptr = realloc (mem, size));
if (ptr)
return ptr;
scm_memory_error ("realloc");
}
void *
scm_malloc (size_t sz)
{
return scm_realloc (NULL, sz);
}
/*
Hmm. Should we use the C convention for arguments (i.e. N_ELTS,
SIZEOF_ELT)? --hwn
*/
void *
scm_calloc (size_t sz)
{
void * ptr;
/*
By default, try to use calloc, as it is likely more efficient than
calling memset by hand.
*/
SCM_SYSCALL (ptr = calloc (sz, 1));
if (ptr)
return ptr;
ptr = scm_realloc (NULL, sz);
memset (ptr, 0x0, sz);
return ptr;
}
char *
scm_strndup (const char *str, size_t n)
{
char *dst = scm_malloc (n + 1);
memcpy (dst, str, n);
dst[n] = 0;
return dst;
}
char *
scm_strdup (const char *str)
{
return scm_strndup (str, strlen (str));
}
static void
decrease_mtrigger (size_t size, const char * what)
{
scm_i_pthread_mutex_lock (&scm_i_gc_admin_mutex);
if (size > scm_mallocated)
{
fprintf (stderr, "`scm_mallocated' underflow. This means that more "
"memory was unregistered\n"
"via `scm_gc_unregister_collectable_memory ()' than "
"registered.\n");
abort ();
}
scm_mallocated -= size;
scm_gc_malloc_collected += size;
scm_i_pthread_mutex_unlock (&scm_i_gc_admin_mutex);
}
static void
increase_mtrigger (size_t size, const char *what)
{
size_t mallocated = 0;
int overflow = 0, triggered = 0;
scm_i_pthread_mutex_lock (&scm_i_gc_admin_mutex);
if (ULONG_MAX - size < scm_mallocated)
overflow = 1;
else
{
scm_mallocated += size;
mallocated = scm_mallocated;
if (scm_mallocated > scm_mtrigger)
triggered = 1;
}
scm_i_pthread_mutex_unlock (&scm_i_gc_admin_mutex);
if (overflow)
scm_memory_error ("Overflow of scm_mallocated: too much memory in use.");
/*
A program that uses a lot of malloced collectable memory (vectors,
strings), will use a lot of memory off the cell-heap; it needs to
do GC more often (before cells are exhausted), otherwise swapping
and malloc management will tie it down.
*/
if (triggered)
{
unsigned long prev_alloced;
float yield;
scm_t_sweep_statistics sweep_stats;
scm_i_scm_pthread_mutex_lock (&scm_i_sweep_mutex);
scm_gc_running_p = 1;
prev_alloced = mallocated;
scm_i_gc (what);
scm_i_sweep_all_segments ("mtrigger", &sweep_stats);
yield = (((float) prev_alloced - (float) scm_mallocated)
/ (float) prev_alloced);
scm_gc_malloc_yield_percentage = (int) (100 * yield);
#ifdef DEBUGINFO
fprintf (stderr, "prev %lud , now %lud, yield %4.2lf, want %d",
prev_alloced,
scm_mallocated,
100.0 * yield,
scm_i_minyield_malloc);
#endif
if (yield < scm_i_minyield_malloc / 100.0)
{
/*
We make the trigger a little larger, even; If you have a
program that builds up a lot of data in strings, then the
desired yield will never be satisfied.
Instead of getting bogged down, we let the mtrigger grow
strongly with it.
*/
float no_overflow_trigger = scm_mallocated * 110.0;
no_overflow_trigger /= (float) (100.0 - scm_i_minyield_malloc);
if (no_overflow_trigger >= (float) ULONG_MAX)
scm_mtrigger = ULONG_MAX;
else
scm_mtrigger = (unsigned long) no_overflow_trigger;
#ifdef DEBUGINFO
fprintf (stderr, "Mtrigger sweep: ineffective. New trigger %d\n",
scm_mtrigger);
#endif
}
scm_gc_running_p = 0;
scm_i_pthread_mutex_unlock (&scm_i_sweep_mutex);
}
}
void
scm_gc_register_collectable_memory (void *mem, size_t size, const char *what)
{
increase_mtrigger (size, what);
#ifdef GUILE_DEBUG_MALLOC
if (mem)
scm_malloc_register (mem, what);
#endif
}
void
scm_gc_unregister_collectable_memory (void *mem, size_t size, const char *what)
{
decrease_mtrigger (size, what);
#ifdef GUILE_DEBUG_MALLOC
if (mem)
scm_malloc_unregister (mem);
#endif
}
void *
scm_gc_malloc (size_t size, const char *what)
{
/*
The straightforward implementation below has the problem
that it might call the GC twice, once in scm_malloc and then
again in scm_gc_register_collectable_memory. We don't really
want the second GC since it will not find new garbage.
Note: this is a theoretical peeve. In reality, malloc() never
returns NULL. Usually, memory is overcommitted, and when you try
to write it the program is killed with signal 11. --hwn
*/
void *ptr = scm_malloc (size);
scm_gc_register_collectable_memory (ptr, size, what);
return ptr;
}
void *
scm_gc_calloc (size_t size, const char *what)
{
void *ptr = scm_gc_malloc (size, what);
memset (ptr, 0x0, size);
return ptr;
}
void *
scm_gc_realloc (void *mem, size_t old_size, size_t new_size, const char *what)
{
void *ptr;
/* XXX - see scm_gc_malloc. */
/*
scm_realloc() may invalidate the block pointed to by WHERE, eg. by
unmapping it from memory or altering the contents. Since
increase_mtrigger() might trigger a GC that would scan
MEM, it is crucial that this call precedes realloc().
*/
decrease_mtrigger (old_size, what);
increase_mtrigger (new_size, what);
ptr = scm_realloc (mem, new_size);
#ifdef GUILE_DEBUG_MALLOC
if (mem)
scm_malloc_reregister (mem, ptr, what);
#endif
return ptr;
}
void
scm_gc_free (void *mem, size_t size, const char *what)
{
scm_gc_unregister_collectable_memory (mem, size, what);
free (mem);
}
char *
scm_gc_strndup (const char *str, size_t n, const char *what)
{
char *dst = scm_gc_malloc (n+1, what);
memcpy (dst, str, n);
dst[n] = 0;
return dst;
}
char *
scm_gc_strdup (const char *str, const char *what)
{
return scm_gc_strndup (str, strlen (str), what);
}
#if SCM_ENABLE_DEPRECATED == 1
/* {Deprecated front end to malloc}
*
* scm_must_malloc, scm_must_realloc, scm_must_free, scm_done_malloc,
* scm_done_free
*
* These functions provide services comparable to malloc, realloc, and
* free. They should be used when allocating memory that will be under
* control of the garbage collector, i.e., if the memory may be freed
* during garbage collection.
*
* They are deprecated because they weren't really used the way
* outlined above, and making sure to return the right amount from
* smob free routines was sometimes difficult when dealing with nested
* data structures. We basically want everybody to review their code
* and use the more symmetrical scm_gc_malloc/scm_gc_free functions
* instead. In some cases, where scm_must_malloc has been used
* incorrectly (i.e. for non-GC-able memory), use scm_malloc/free.
*/
void *
scm_must_malloc (size_t size, const char *what)
{
scm_c_issue_deprecation_warning
("scm_must_malloc is deprecated. "
"Use scm_gc_malloc and scm_gc_free instead.");
return scm_gc_malloc (size, what);
}
void *
scm_must_realloc (void *where,
size_t old_size,
size_t size,
const char *what)
{
scm_c_issue_deprecation_warning
("scm_must_realloc is deprecated. "
"Use scm_gc_realloc and scm_gc_free instead.");
return scm_gc_realloc (where, old_size, size, what);
}
char *
scm_must_strndup (const char *str, size_t length)
{
scm_c_issue_deprecation_warning
("scm_must_strndup is deprecated. "
"Use scm_gc_strndup and scm_gc_free instead.");
return scm_gc_strndup (str, length, "string");
}
char *
scm_must_strdup (const char *str)
{
scm_c_issue_deprecation_warning
("scm_must_strdup is deprecated. "
"Use scm_gc_strdup and scm_gc_free instead.");
return scm_gc_strdup (str, "string");
}
void
scm_must_free (void *obj)
#define FUNC_NAME "scm_must_free"
{
scm_c_issue_deprecation_warning
("scm_must_free is deprecated. "
"Use scm_gc_malloc and scm_gc_free instead.");
#ifdef GUILE_DEBUG_MALLOC
scm_malloc_unregister (obj);
#endif
if (obj)
free (obj);
else
{
fprintf (stderr,"freeing NULL pointer");
abort ();
}
}
#undef FUNC_NAME
void
scm_done_malloc (long size)
{
scm_c_issue_deprecation_warning
("scm_done_malloc is deprecated. "
"Use scm_gc_register_collectable_memory instead.");
if (size >= 0)
scm_gc_register_collectable_memory (NULL, size, "foreign mallocs");
else
scm_gc_unregister_collectable_memory (NULL, -size, "foreign mallocs");
}
void
scm_done_free (long size)
{
scm_c_issue_deprecation_warning
("scm_done_free is deprecated. "
"Use scm_gc_unregister_collectable_memory instead.");
if (size >= 0)
scm_gc_unregister_collectable_memory (NULL, size, "foreign mallocs");
else
scm_gc_register_collectable_memory (NULL, -size, "foreign mallocs");
}
#endif /* SCM_ENABLE_DEPRECATED == 1 */
|