summaryrefslogtreecommitdiffstats
path: root/src/inkgc/gc.cpp
blob: a290423062fb159b4d45503e1d9c4e0942144565 (plain)
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
// SPDX-License-Identifier: GPL-2.0-or-later
/** @file
 * Wrapper for Boehm GC.
 */
/* Authors:
 *   MenTaLguY <mental@rydia.net>
 *
 * Copyright (C) 2004 MenTaLguY
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include "inkgc/gc-core.h"
#include <stdexcept>
#include <cstring>
#include <string>
#include <glib.h>
#include <sigc++/functors/ptr_fun.h>
#include <glibmm/main.h>
#include <cstddef>

namespace Inkscape {
namespace GC {

namespace {

void display_warning(char *msg, GC_word arg) {
    g_warning(msg, arg);
}

void do_init() {
    GC_set_no_dls(1);
    GC_set_all_interior_pointers(1);
    GC_set_finalize_on_demand(0);

    GC_INIT();

    GC_set_warn_proc(&display_warning);
}

void *debug_malloc(std::size_t size) {
    return GC_debug_malloc(size, GC_EXTRAS);
}

void *debug_malloc_atomic(std::size_t size) {
    return GC_debug_malloc_atomic(size, GC_EXTRAS);
}

void *debug_malloc_uncollectable(std::size_t size) {
    return GC_debug_malloc_uncollectable(size, GC_EXTRAS);
}

void *debug_malloc_atomic_uncollectable(std::size_t size) {
    return GC_debug_malloc_uncollectable(size, GC_EXTRAS);
}

std::ptrdiff_t compute_debug_base_fixup() {
    char *base=reinterpret_cast<char *>(GC_debug_malloc(1, GC_EXTRAS));
    char *real_base=reinterpret_cast<char *>(GC_base(base));
    GC_debug_free(base);
    return base - real_base;
}

inline std::ptrdiff_t const &debug_base_fixup() {
    static std::ptrdiff_t fixup=compute_debug_base_fixup();
    return fixup;
}

void *debug_base(void *ptr) {
    char *base=reinterpret_cast<char *>(GC_base(ptr));
    return base + debug_base_fixup();
}

int debug_general_register_disappearing_link(void **p_ptr, void const *base) {
    char const *real_base = reinterpret_cast<char const *>(base) - debug_base_fixup();
#if (GC_MAJOR_VERSION >= 7 && GC_MINOR_VERSION >= 4)
    return GC_general_register_disappearing_link(p_ptr, real_base);
#else // compatibility with older Boehm GC versions
    return GC_general_register_disappearing_link(p_ptr, const_cast<char *>(real_base));
#endif
}

void dummy_do_init() {}

void *dummy_base(void *) { return nullptr; }

void dummy_register_finalizer(void *, CleanupFunc, void *,
                                      CleanupFunc *old_func, void **old_data)
{
    if (old_func) {
        *old_func = nullptr;
    }
    if (old_data) {
        *old_data = nullptr;
    }
}

int dummy_general_register_disappearing_link(void **, void const *) { return false; }

int dummy_unregister_disappearing_link(void **/*link*/) { return false; }

std::size_t dummy_get_heap_size() { return 0; }

std::size_t dummy_get_free_bytes() { return 0; }

void dummy_gcollect() {}

void dummy_enable() {}

void dummy_disable() {}

Ops enabled_ops = {
    &do_init,
    &GC_malloc,
    &GC_malloc_atomic,
    &GC_malloc_uncollectable,
    &GC_malloc_atomic_uncollectable,
    &GC_base,
    &GC_register_finalizer_ignore_self,
#if (GC_MAJOR_VERSION >= 7 && GC_MINOR_VERSION >= 4)
     &GC_general_register_disappearing_link,
#else // compatibility with older Boehm GC versions
    (int (*)(void**, const void*))(&GC_general_register_disappearing_link),
#endif
    &GC_unregister_disappearing_link,
    &GC_get_heap_size,
    &GC_get_free_bytes,
    &GC_gcollect,
    &GC_enable,
    &GC_disable,
    &GC_free
};

Ops debug_ops = {
    &do_init,
    &debug_malloc,
    &debug_malloc_atomic,
    &debug_malloc_uncollectable,
    &debug_malloc_atomic_uncollectable,
    &debug_base,
    &GC_debug_register_finalizer_ignore_self,
    &debug_general_register_disappearing_link,
    &GC_unregister_disappearing_link,
    &GC_get_heap_size,
    &GC_get_free_bytes,
    &GC_gcollect,
    &GC_enable,
    &GC_disable,
    &GC_debug_free
};

Ops disabled_ops = {
    &dummy_do_init,
    &std::malloc,
    &std::malloc,
    &std::malloc,
    &std::malloc,
    &dummy_base,
    &dummy_register_finalizer,
    &dummy_general_register_disappearing_link,
    &dummy_unregister_disappearing_link,
    &dummy_get_heap_size,
    &dummy_get_free_bytes,
    &dummy_gcollect,
    &dummy_enable,
    &dummy_disable,
    &std::free
};

class InvalidGCModeError : public std::runtime_error {
public:
    InvalidGCModeError(const char *mode)
    : runtime_error(std::string("Unknown GC mode \"") + mode + "\"")
    {}
};

Ops const &get_ops() {
    char *mode_string=std::getenv("_INKSCAPE_GC");
    if (mode_string) {
        if (!std::strcmp(mode_string, "enable")) {
            return enabled_ops;
        } else if (!std::strcmp(mode_string, "debug")) {
            return debug_ops;
        } else if (!std::strcmp(mode_string, "disable")) {
            return disabled_ops;
        } else {
            throw InvalidGCModeError(mode_string);
        }
    } else {
        return enabled_ops;
    }
}

void die_because_not_initialized() {
    g_error("Attempt to use GC allocator before call to Inkscape::GC::init()");
}

void *stub_malloc(std::size_t) {
    die_because_not_initialized();
    return nullptr;
}

void *stub_base(void *) {
    die_because_not_initialized();
    return nullptr;
}

void stub_register_finalizer_ignore_self(void *, CleanupFunc, void *,
                                                 CleanupFunc *, void **)
{
    die_because_not_initialized();
}

int stub_general_register_disappearing_link(void **, void const *) {
    die_because_not_initialized();
    return 0;
}

int stub_unregister_disappearing_link(void **) {
    die_because_not_initialized();
    return 0;
}

std::size_t stub_get_heap_size() {
    die_because_not_initialized();
    return 0;
}

std::size_t stub_get_free_bytes() {
    die_because_not_initialized();
    return 0;
}

void stub_gcollect() {
    die_because_not_initialized();
}

void stub_enable() {
    die_because_not_initialized();
}

void stub_disable() {
    die_because_not_initialized();
}

void stub_free(void *) {
    die_because_not_initialized();
}

}

Ops Core::_ops = {
    nullptr,
    &stub_malloc,
    &stub_malloc,
    &stub_malloc,
    &stub_malloc,
    &stub_base,
    &stub_register_finalizer_ignore_self,
    &stub_general_register_disappearing_link,
    &stub_unregister_disappearing_link,
    &stub_get_heap_size,
    &stub_get_free_bytes,
    &stub_gcollect,
    &stub_enable,
    &stub_disable,
    &stub_free
};

void Core::init() {
    try {
        _ops = get_ops();
    } catch (InvalidGCModeError &e) {
        g_warning("%s; enabling normal collection", e.what());
        _ops = enabled_ops;
    }

    _ops.do_init();
}


namespace {

bool collection_requested=false;
bool collection_task() {
    Core::gcollect();
    Core::gcollect();
    collection_requested=false;
    return false;
}

}

void request_early_collection() {
    if (!collection_requested) {
        collection_requested=true;
        Glib::signal_idle().connect(sigc::ptr_fun(&collection_task));
    }
}

}
}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :