Hans Aberg
2015-01-02 22:16:28 UTC
FYI: C++ global objects initialization on platforms where GC_INIT() is necessary, as on OS X 10.10, there is the problem that the latter must be called before memory allocations by GC_malloc(). So it can’t be in main().
I found a workaround, that perhaps might be used to avoid GC_INIT() in the API (disregarding complications with multiple allocators): the GC_malloc() replacement calls a pointer which first is an initialization function, but is then changed to just the old GC_malloc().
In the code below, the GC_malloc() replacement is called gc_f. Then it seems to work in C++11 with the operator new() replacements below, also with standard library containers global objects.
----
typedef void* (*malloc_ptr)(size_t);
extern malloc_ptr gc_f;
void* gc_uninitialized(size_t n) {
gc_f = &GC_malloc;
GC_INIT();
return gc_f(n);
}
malloc_ptr gc_f = gc_uninitialized;
void* operator new(std::size_t n) { return gc_f(n); }
void operator delete(void*) noexcept {}
void* operator new[](std::size_t n) { return gc_f(n); }
void operator delete[](void*) noexcept {}
----
I found a workaround, that perhaps might be used to avoid GC_INIT() in the API (disregarding complications with multiple allocators): the GC_malloc() replacement calls a pointer which first is an initialization function, but is then changed to just the old GC_malloc().
In the code below, the GC_malloc() replacement is called gc_f. Then it seems to work in C++11 with the operator new() replacements below, also with standard library containers global objects.
----
typedef void* (*malloc_ptr)(size_t);
extern malloc_ptr gc_f;
void* gc_uninitialized(size_t n) {
gc_f = &GC_malloc;
GC_INIT();
return gc_f(n);
}
malloc_ptr gc_f = gc_uninitialized;
void* operator new(std::size_t n) { return gc_f(n); }
void operator delete(void*) noexcept {}
void* operator new[](std::size_t n) { return gc_f(n); }
void operator delete[](void*) noexcept {}
----