stub out your gc without hacking on druntime
H. S. Teoh
hsteoh at quickfur.ath.cx
Mon Jun 10 20:06:53 PDT 2013
On Mon, Jun 10, 2013 at 09:16:04PM +0200, Adam D. Ruppe wrote:
> I decided to boil going without gc down to one simple move: put this
> code into a file called nogc.d and then add it to your dmd command
> line:
>
> ===
>
> import core.stdc.stdio;
> import core.stdc.stdlib;
>
> extern(C):
>
> void* gc_malloc() {
> fprintf(stderr, "GC allocations are disabled\nProgram
> terminated\n");
> asm { hlt; }
> assert(0);
> }
>
> // druntime calls these, but we can just stub them out
> void gc_init() { }
> void gc_addRange() { }
> void gc_term() { }
Should these return errors too? Or is this just a quick-n-dirty way to
get a no-GC environment without needing hack druntime?
[...]
> extern(C) Object _d_newclass(const ClassInfo ci) {
> void* memory = malloc(ci.init.length);
> if(memory is null) {
> fprintf(stderr, "Out of memory to allocate
> class\n");
> asm { hlt; }
> assert(0);
> }
> (cast(byte*) memory)[0 .. ci.init.length] = ci.init[];
> return cast(Object) memory;
> }
Hmm. What if the user code instantiates classes? Will they leak memory
then? Though I suppose the point is really just to find *hidden*
allocations, and 'new' is a pretty blatant use of the GC.
[...]
> You shouldn't have to modify your code nor druntime/phobos (though
> you'll probably find them being killed by hidden allocations!),
> unlike the minimal D stuff I've been talking about the last few
> weeks which replaces them entirely.
>
> The reason it works is the gc functions come from a library file.
> .lib functions are overridden by functions with the same name in an
> object file.
>
> So this redefines crucial gc functions, and then the linker uses
> them instead of the ones druntime provides.Thereby stubbing out the
> garbage collector in this individual exe. I tried it on both Windows
> and Linux and it seemed to work as I expected.
[...]
Very nice! Clever way of quickly testing for GC-dependence in a piece of
code.
T
--
Questions are the beginning of intelligence, but the fear of God is the beginning of wisdom.
More information about the Digitalmars-d
mailing list