From: sean Date: Wed, 29 Oct 2008 00:15:37 +0000 (+0000) Subject: Added static data segment support to the new Runtime.loadLibrary() features. This... X-Git-Url: https://git.llucax.com/software/druntime.git/commitdiff_plain/97701d90b6977c5fb6b9a738cc1f152d80a422c9?ds=sidebyside Added static data segment support to the new Runtime.loadLibrary() features. This also replaces static data segment scanning for all apps by dropping rt_scanStaticData() in favor of a new initialization step that calls gc_addRange() for each static data segment. The upshot of this is that all GCs must track add/remove Root and Range calls so the info can be passed to a proxy if gc_setProxy() is called. See src/gc/stub/gc.d for a simple implementation of this. git-svn-id: http://svn.dsource.org/projects/druntime/trunk@39 4a9d5153-6564-4b3f-b5e1-7e8e9dac548f --- diff --git a/src/gc/basic/gc.d b/src/gc/basic/gc.d index 6b24182..596d3ba 100644 --- a/src/gc/basic/gc.d +++ b/src/gc/basic/gc.d @@ -26,20 +26,86 @@ module gc.gc; -private import gcx; -private import gcstats; -private import core.stdc.stdlib; +private +{ + import gcx; + import gcstats; + import core.stdc.stdlib; + + version = GCCLASS; + + version( GCCLASS ) + alias GC gc_t; + else + alias GC* gc_t; + + gc_t _gc; -version=GCCLASS; + extern (C) void thread_init(); -version (GCCLASS) - alias GC gc_t; -else - alias GC* gc_t; + struct Proxy + { + extern (C) void function() gc_enable; + extern (C) void function() gc_disable; + extern (C) void function() gc_collect; + extern (C) void function() gc_minimize; -gc_t _gc; + extern (C) uint function(void*) gc_getAttr; + extern (C) uint function(void*, uint) gc_setAttr; + extern (C) uint function(void*, uint) gc_clrAttr; -extern (C) void thread_init(); + extern (C) void* function(size_t, uint) gc_malloc; + extern (C) void* function(size_t, uint) gc_calloc; + extern (C) void* function(void*, size_t, uint ba) gc_realloc; + extern (C) size_t function(void*, size_t, size_t) gc_extend; + extern (C) size_t function(size_t) gc_reserve; + extern (C) void function(void*) gc_free; + + extern (C) void* function(void*) gc_addrOf; + extern (C) size_t function(void*) gc_sizeOf; + + extern (C) BlkInfo function(void*) gc_query; + + extern (C) void function(void*) gc_addRoot; + extern (C) void function(void*, size_t) gc_addRange; + + extern (C) void function(void*) gc_removeRoot; + extern (C) void function(void*) gc_removeRange; + } + + Proxy pthis; + Proxy* proxy; + + void initProxy() + { + pthis.gc_enable = &gc_enable; + pthis.gc_disable = &gc_disable; + pthis.gc_collect = &gc_collect; + pthis.gc_minimize = &gc_minimize; + + pthis.gc_getAttr = &gc_getAttr; + pthis.gc_setAttr = &gc_setAttr; + pthis.gc_clrAttr = &gc_clrAttr; + + pthis.gc_malloc = &gc_malloc; + pthis.gc_calloc = &gc_calloc; + pthis.gc_realloc = &gc_realloc; + pthis.gc_extend = &gc_extend; + pthis.gc_reserve = &gc_reserve; + pthis.gc_free = &gc_free; + + pthis.gc_addrOf = &gc_addrOf; + pthis.gc_sizeOf = &gc_sizeOf; + + pthis.gc_query = &gc_query; + + pthis.gc_addRoot = &gc_addRoot; + pthis.gc_addRange = &gc_addRange; + + pthis.gc_removeRoot = &gc_removeRoot; + pthis.gc_removeRange = &gc_removeRange; + } +} extern (C) void gc_init() { @@ -59,6 +125,7 @@ extern (C) void gc_init() // NOTE: The GC must initialize the thread library // before its first collection. thread_init(); + initProxy(); } extern (C) void gc_term() @@ -234,74 +301,11 @@ extern (C) void gc_removeRange( void* p ) return proxy.gc_removeRange( p ); } -struct Proxy -{ - extern (C) void function() gc_enable; - extern (C) void function() gc_disable; - extern (C) void function() gc_collect; - extern (C) void function() gc_minimize; - - extern (C) uint function(void*) gc_getAttr; - extern (C) uint function(void*, uint) gc_setAttr; - extern (C) uint function(void*, uint) gc_clrAttr; - - extern (C) void* function(size_t, uint) gc_malloc; - extern (C) void* function(size_t, uint) gc_calloc; - extern (C) void* function(void*, size_t, uint ba) gc_realloc; - extern (C) size_t function(void*, size_t, size_t) gc_extend; - extern (C) size_t function(size_t) gc_reserve; - extern (C) void function(void*) gc_free; - - extern (C) void* function(void*) gc_addrOf; - extern (C) size_t function(void*) gc_sizeOf; - - extern (C) BlkInfo function(void*) gc_query; - - extern (C) void function(void*) gc_addRoot; - extern (C) void function(void*, size_t) gc_addRange; - - extern (C) void function(void*) gc_removeRoot; - extern (C) void function(void*) gc_removeRange; -} - -Proxy pthis; -Proxy* proxy; - -static this() -{ - pthis.gc_enable = &gc_enable; - pthis.gc_disable = &gc_disable; - pthis.gc_collect = &gc_collect; - pthis.gc_minimize = &gc_minimize; - - pthis.gc_getAttr = &gc_getAttr; - pthis.gc_setAttr = &gc_setAttr; - pthis.gc_clrAttr = &gc_clrAttr; - - pthis.gc_malloc = &gc_malloc; - pthis.gc_calloc = &gc_calloc; - pthis.gc_realloc = &gc_realloc; - pthis.gc_extend = &gc_extend; - pthis.gc_reserve = &gc_reserve; - pthis.gc_free = &gc_free; - - pthis.gc_addrOf = &gc_addrOf; - pthis.gc_sizeOf = &gc_sizeOf; - - pthis.gc_query = &gc_query; - - pthis.gc_addRoot = &gc_addRoot; - pthis.gc_addRange = &gc_addRange; - - pthis.gc_removeRoot = &gc_removeRoot; - pthis.gc_removeRange = &gc_removeRange; -} - extern (C) Proxy* gc_getProxy() { return &pthis; } - +import core.stdc.stdio; export extern (C) void gc_setProxy( Proxy* p ) { if( proxy !is null ) @@ -309,11 +313,17 @@ export extern (C) void gc_setProxy( Proxy* p ) // TODO: Decide if this is an error condition. } proxy = p; - // TODO: Add known ranges and roots to the proxy. + foreach( r; _gc.rootIter ) + proxy.gc_addRoot( r ); + foreach( r; _gc.rangeIter ) + proxy.gc_addRange( r.pbot, r.ptop - r.pbot ); } export extern (C) void gc_clrProxy() { - // TODO: Remove known ranges and roots from the proxy. + foreach( r; _gc.rangeIter ) + proxy.gc_removeRange( r.pbot ); + foreach( r; _gc.rootIter ) + proxy.gc_removeRoot( r ); proxy = null; } diff --git a/src/gc/basic/gcx.d b/src/gc/basic/gcx.d index 4c6396e..97a8b07 100644 --- a/src/gc/basic/gcx.d +++ b/src/gc/basic/gcx.d @@ -1163,6 +1163,22 @@ class GC } + /** + * + */ + int delegate(int delegate(inout void*)) rootIter() + { + if (!thread_needLock()) + { + return &gcx.rootIter; + } + else synchronized (gcLock) + { + return &gcx.rootIter; + } + } + + /** * add range to scan for roots */ @@ -1207,6 +1223,22 @@ class GC } + /** + * + */ + int delegate(int delegate(inout Range)) rangeIter() + { + if (!thread_needLock()) + { + return &gcx.rangeIter; + } + else synchronized (gcLock) + { + return &gcx.rangeIter; + } + } + + /** * do full garbage collection */ @@ -1570,6 +1602,22 @@ struct Gcx } + /** + * + */ + int rootIter(int delegate(inout void*) dg) + { + int result = 0; + for( size_t i = 0; i < nroots; ++i ) + { + result = dg(roots[i]); + if (result) + break; + } + return result; + } + + /** * */ @@ -1623,6 +1671,22 @@ struct Gcx } + /** + * + */ + int rangeIter(int delegate(inout Range) dg) + { + int result = 0; + for( size_t i = 0; i < nranges; ++i ) + { + result = dg(ranges[i]); + if (result) + break; + } + return result; + } + + /** * Find Pool that pointer is in. * Return null if not in a Pool. diff --git a/src/gc/stub/gc.d b/src/gc/stub/gc.d index a5426ff..45ecbde 100644 --- a/src/gc/stub/gc.d +++ b/src/gc/stub/gc.d @@ -20,10 +20,10 @@ module gc.gc; -private import core.stdc.stdlib; - private { + import core.stdc.stdlib; + enum BlkAttr : uint { FINALIZE = 0b0000_0001, @@ -41,6 +41,81 @@ private extern (C) void thread_init(); extern (C) void onOutOfMemoryError(); + + struct Proxy + { + extern (C) void function() gc_enable; + extern (C) void function() gc_disable; + extern (C) void function() gc_collect; + extern (C) void function() gc_minimize; + + extern (C) uint function(void*) gc_getAttr; + extern (C) uint function(void*, uint) gc_setAttr; + extern (C) uint function(void*, uint) gc_clrAttr; + + extern (C) void* function(size_t, uint) gc_malloc; + extern (C) void* function(size_t, uint) gc_calloc; + extern (C) void* function(void*, size_t, uint ba) gc_realloc; + extern (C) size_t function(void*, size_t, size_t) gc_extend; + extern (C) size_t function(size_t) gc_reserve; + extern (C) void function(void*) gc_free; + + extern (C) void* function(void*) gc_addrOf; + extern (C) size_t function(void*) gc_sizeOf; + + extern (C) BlkInfo function(void*) gc_query; + + extern (C) void function(void*) gc_addRoot; + extern (C) void function(void*, size_t) gc_addRange; + + extern (C) void function(void*) gc_removeRoot; + extern (C) void function(void*) gc_removeRange; + } + + Proxy pthis; + Proxy* proxy; + + void initProxy() + { + pthis.gc_enable = &gc_enable; + pthis.gc_disable = &gc_disable; + pthis.gc_collect = &gc_collect; + pthis.gc_minimize = &gc_minimize; + + pthis.gc_getAttr = &gc_getAttr; + pthis.gc_setAttr = &gc_setAttr; + pthis.gc_clrAttr = &gc_clrAttr; + + pthis.gc_malloc = &gc_malloc; + pthis.gc_calloc = &gc_calloc; + pthis.gc_realloc = &gc_realloc; + pthis.gc_extend = &gc_extend; + pthis.gc_reserve = &gc_reserve; + pthis.gc_free = &gc_free; + + pthis.gc_addrOf = &gc_addrOf; + pthis.gc_sizeOf = &gc_sizeOf; + + pthis.gc_query = &gc_query; + + pthis.gc_addRoot = &gc_addRoot; + pthis.gc_addRange = &gc_addRange; + + pthis.gc_removeRoot = &gc_removeRoot; + pthis.gc_removeRange = &gc_removeRange; + } + + void** roots = null; + size_t nroots = 0; + + struct Range + { + void* pos; + size_t len; + } + + Range* ranges = null; + size_t nranges = 0; } extern (C) void gc_init() @@ -48,11 +123,13 @@ extern (C) void gc_init() // NOTE: The GC must initialize the thread library before its first // collection, and always before returning from gc_init(). thread_init(); + initProxy(); } extern (C) void gc_term() { - + free( roots ); + free( ranges ); } extern (C) void gc_enable() @@ -185,100 +262,71 @@ extern (C) BlkInfo gc_query( void* p ) return proxy.gc_query( p ); } -// TODO: Implement range storage. -// TODO: Implement root storage. - extern (C) void gc_addRoot( void* p ) { if( proxy is null ) - return; + { + void** r = cast(void**) realloc( roots, + (nroots+1) * roots[0].sizeof ); + if( r is null ) + onOutOfMemoryError(); + r[nroots++] = p; + roots = r; + + } return proxy.gc_addRoot( p ); } extern (C) void gc_addRange( void* p, size_t sz ) { if( proxy is null ) - return; + { + Range* r = cast(Range*) realloc( ranges, + (nranges+1) * ranges[0].sizeof ); + if( r is null ) + onOutOfMemoryError(); + r[nranges].pos = p; + r[nranges].len = sz; + ranges = r; + ++nranges; + } return proxy.gc_addRange( p, sz ); } extern (C) void gc_removeRoot( void *p ) { if( proxy is null ) - return; + { + for( size_t i = 0; i < nroots; ++i ) + { + if( roots[i] is p ) + { + roots[i] = roots[--nroots]; + return; + } + } + assert( false ); + } return proxy.gc_removeRoot( p ); } extern (C) void gc_removeRange( void *p ) { if( proxy is null ) - return; + { + for( size_t i = 0; i < nranges; ++i ) + { + if( ranges[i].pos is p ) + { + ranges[i] = ranges[--nranges]; + return; + } + } + assert( false ); + } return proxy.gc_removeRange( p ); } -struct Proxy -{ - extern (C) void function() gc_enable; - extern (C) void function() gc_disable; - extern (C) void function() gc_collect; - extern (C) void function() gc_minimize; - - extern (C) uint function(void*) gc_getAttr; - extern (C) uint function(void*, uint) gc_setAttr; - extern (C) uint function(void*, uint) gc_clrAttr; - - extern (C) void* function(size_t, uint) gc_malloc; - extern (C) void* function(size_t, uint) gc_calloc; - extern (C) void* function(void*, size_t, uint ba) gc_realloc; - extern (C) size_t function(void*, size_t, size_t) gc_extend; - extern (C) size_t function(size_t) gc_reserve; - extern (C) void function(void*) gc_free; - - extern (C) void* function(void*) gc_addrOf; - extern (C) size_t function(void*) gc_sizeOf; - - extern (C) BlkInfo function(void*) gc_query; - - extern (C) void function(void*) gc_addRoot; - extern (C) void function(void*, size_t) gc_addRange; - - extern (C) void function(void*) gc_removeRoot; - extern (C) void function(void*) gc_removeRange; -} - -Proxy pthis; -Proxy* proxy; - -static this() -{ - pthis.gc_enable = &gc_enable; - pthis.gc_disable = &gc_disable; - pthis.gc_collect = &gc_collect; - pthis.gc_minimize = &gc_minimize; - - pthis.gc_getAttr = &gc_getAttr; - pthis.gc_setAttr = &gc_setAttr; - pthis.gc_clrAttr = &gc_clrAttr; - - pthis.gc_malloc = &gc_malloc; - pthis.gc_calloc = &gc_calloc; - pthis.gc_realloc = &gc_realloc; - pthis.gc_extend = &gc_extend; - pthis.gc_reserve = &gc_reserve; - pthis.gc_free = &gc_free; - - pthis.gc_addrOf = &gc_addrOf; - pthis.gc_sizeOf = &gc_sizeOf; - - pthis.gc_query = &gc_query; - - pthis.gc_addRoot = &gc_addRoot; - pthis.gc_addRange = &gc_addRange; - - pthis.gc_removeRoot = &gc_removeRoot; - pthis.gc_removeRange = &gc_removeRange; -} - extern (C) Proxy* gc_getProxy() { return &pthis; @@ -288,20 +336,20 @@ export extern (C) void gc_setProxy( Proxy* p ) { if( proxy !is null ) { - // error? + // TODO: Decide if this is an error condition. } proxy = p; - ///foreach range - //proxy.addRange(); - //foreach root - //proxy.addRoot() + foreach( r; roots[0 .. nroots] ) + proxy.gc_addRoot( r ); + foreach( r; ranges[0 .. nranges] ) + proxy.gc_addRange( r.pos, r.len ); } export extern (C) void gc_clrProxy() { - // foreach root - // proxy.removeRoot(); - // foreach range - // proxy.removeReange(); + foreach( r; ranges[0 .. nranges] ) + proxy.gc_removeRange( r.pos ); + foreach( r; roots[0 .. nroots] ) + proxy.gc_removeRoot( r ); proxy = null; -} \ No newline at end of file +}