print: Fix printing of weak vectors.
[bpt/guile.git] / libguile / gc-malloc.c
CommitLineData
8fc5ef7d 1/* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
c2247b78 2 * 2004, 2006, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
c7743d02 3 *
73be1d9e 4 * This library is free software; you can redistribute it and/or
53befeb7
NJ
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
c7743d02 8 *
53befeb7
NJ
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
c7743d02 13 *
73be1d9e
MV
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
53befeb7
NJ
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
73be1d9e 18 */
c7743d02
HWN
19
20
21\f
dbb605f5 22#ifdef HAVE_CONFIG_H
132fa21f
RB
23# include <config.h>
24#endif
25
c7743d02
HWN
26#include <stdio.h>
27#include <errno.h>
28#include <string.h>
34cf38c3 29#include <stdlib.h>
c7743d02
HWN
30
31#ifdef __ia64__
32#include <ucontext.h>
33extern unsigned long * __libc_ia64_register_backing_store_base;
34#endif
35
36#include "libguile/_scm.h"
37#include "libguile/eval.h"
38#include "libguile/stime.h"
39#include "libguile/stackchk.h"
40#include "libguile/struct.h"
41#include "libguile/smob.h"
2fa901a5 42#include "libguile/arrays.h"
c7743d02
HWN
43#include "libguile/async.h"
44#include "libguile/ports.h"
45#include "libguile/root.h"
46#include "libguile/strings.h"
47#include "libguile/vectors.h"
c7743d02
HWN
48#include "libguile/hashtab.h"
49#include "libguile/tags.h"
50
51#include "libguile/validate.h"
52#include "libguile/deprecation.h"
53#include "libguile/gc.h"
54
c7743d02
HWN
55#ifdef GUILE_DEBUG_MALLOC
56#include "libguile/debug-malloc.h"
57#endif
58
c7743d02
HWN
59#ifdef HAVE_UNISTD_H
60#include <unistd.h>
61#endif
62
63/*
64 INIT_MALLOC_LIMIT is the initial amount of malloc usage which will
65 trigger a GC.
66
67 After startup (at the guile> prompt), we have approximately 100k of
68 alloced memory, which won't go away on GC. Let's set the init such
69 that we get a nice yield on the next allocation:
70*/
c2cbcc57 71#define SCM_DEFAULT_INIT_MALLOC_LIMIT 200*1024
c7743d02
HWN
72#define SCM_DEFAULT_MALLOC_MINYIELD 40
73
61ef9c1f 74/* #define DEBUGINFO */
c7743d02 75
c7743d02
HWN
76
77\f
817307cc
AW
78
79static void*
80do_realloc (void *from, size_t new_size)
81{
82 scm_gc_register_allocation (new_size);
83 return realloc (from, new_size);
84}
85
86static void*
87do_calloc (size_t n, size_t size)
88{
89 scm_gc_register_allocation (size);
90 return calloc (n, size);
91}
92
93static void*
94do_gc_malloc (size_t size, const char *what)
95{
96 /* Ensure nonzero size to be compatible with always-nonzero return of
97 glibc malloc. */
98 return GC_MALLOC (size ? size : sizeof (void *));
99}
100
101static void*
102do_gc_malloc_atomic (size_t size, const char *what)
103{
104 return GC_MALLOC_ATOMIC (size ? size : sizeof (void *));
105}
106
107static void*
108do_gc_realloc (void *from, size_t size, const char *what)
109{
110 return GC_REALLOC (from, size ? size : sizeof (void *));
111}
112
113static void
114do_gc_free (void *ptr)
115{
116 GC_FREE (ptr);
117}
118
119
120\f
c7743d02
HWN
121/* Function for non-cell memory management.
122 */
123
c7743d02
HWN
124void *
125scm_realloc (void *mem, size_t size)
126{
127 void *ptr;
128
817307cc 129 ptr = do_realloc (mem, size);
fd51e661 130
817307cc 131 if (ptr || size == 0)
c7743d02
HWN
132 return ptr;
133
26224b3f 134 /* Time is hard: trigger a full, ``stop-the-world'' GC, and try again. */
fd51e661 135 GC_gcollect_and_unmap ();
b17e0ac3 136
817307cc 137 ptr = do_realloc (mem, size);
c7743d02
HWN
138 if (ptr)
139 return ptr;
140
c2247b78
AW
141 scm_report_out_of_memory ();
142
143 /* Not reached. */
144 return NULL;
c7743d02
HWN
145}
146
39e8f371
HWN
147void *
148scm_malloc (size_t sz)
149{
150 return scm_realloc (NULL, sz);
151}
ba1b2226
HWN
152
153/*
154 Hmm. Should we use the C convention for arguments (i.e. N_ELTS,
155 SIZEOF_ELT)? --hwn
156 */
157void *
158scm_calloc (size_t sz)
159{
1383773b
HWN
160 void * ptr;
161
162 /*
163 By default, try to use calloc, as it is likely more efficient than
164 calling memset by hand.
165 */
817307cc
AW
166 ptr = do_calloc (sz, 1);
167 if (ptr || sz == 0)
1383773b 168 return ptr;
26224b3f 169
1383773b 170 ptr = scm_realloc (NULL, sz);
ba1b2226
HWN
171 memset (ptr, 0x0, sz);
172 return ptr;
173}
174
39e8f371 175
c7743d02
HWN
176char *
177scm_strndup (const char *str, size_t n)
178{
fb50ef08 179 char *dst = scm_malloc (n + 1);
c7743d02
HWN
180 memcpy (dst, str, n);
181 dst[n] = 0;
182 return dst;
183}
184
185char *
186scm_strdup (const char *str)
187{
188 return scm_strndup (str, strlen (str));
189}
190
b17e0ac3 191
cbfe8e62
HWN
192
193void
194scm_gc_register_collectable_memory (void *mem, size_t size, const char *what)
195{
76f3ee77
AW
196 scm_gc_register_allocation (size);
197
c7743d02
HWN
198#ifdef GUILE_DEBUG_MALLOC
199 if (mem)
8fc5ef7d 200 scm_malloc_register (mem, what);
c7743d02
HWN
201#endif
202}
203
cbfe8e62 204
c7743d02
HWN
205void
206scm_gc_unregister_collectable_memory (void *mem, size_t size, const char *what)
207{
c5018a2b 208 /* Nothing to do. */
c7743d02
HWN
209#ifdef GUILE_DEBUG_MALLOC
210 if (mem)
211 scm_malloc_unregister (mem);
212#endif
213}
214
3ef6650d
AW
215/* Allocate SIZE bytes of memory whose contents should not be scanned
216 for pointers (useful, e.g., for strings). Note though that this
217 memory is *not* cleared; be sure to initialize it to prevent
218 information leaks. */
c5018a2b
LC
219void *
220scm_gc_malloc_pointerless (size_t size, const char *what)
221{
817307cc 222 return do_gc_malloc_atomic (size, what);
c5018a2b
LC
223}
224
c7743d02
HWN
225void *
226scm_gc_malloc (size_t size, const char *what)
227{
817307cc 228 return do_gc_malloc (size, what);
c7743d02
HWN
229}
230
39e8f371
HWN
231void *
232scm_gc_calloc (size_t size, const char *what)
233{
b1f6293e 234 /* `GC_MALLOC ()' always returns a zeroed buffer. */
817307cc 235 return do_gc_malloc (size, what);
39e8f371
HWN
236}
237
c7743d02
HWN
238void *
239scm_gc_realloc (void *mem, size_t old_size, size_t new_size, const char *what)
240{
817307cc 241 return do_gc_realloc (mem, new_size, what);
c7743d02
HWN
242}
243
244void
245scm_gc_free (void *mem, size_t size, const char *what)
246{
817307cc 247 do_gc_free (mem);
c7743d02
HWN
248}
249
250char *
251scm_gc_strndup (const char *str, size_t n, const char *what)
252{
817307cc 253 char *dst = do_gc_malloc_atomic (n + 1, what);
c7743d02
HWN
254 memcpy (dst, str, n);
255 dst[n] = 0;
256 return dst;
257}
258
259char *
260scm_gc_strdup (const char *str, const char *what)
261{
262 return scm_gc_strndup (str, strlen (str), what);
263}