diff options
author | Jean Boussier <jean.boussier@gmail.com> | 2023-03-06 19:44:45 +0100 |
---|---|---|
committer | Jean Boussier <jean.boussier@gmail.com> | 2023-03-06 19:44:45 +0100 |
commit | 5f1cbba44319d66d71eba6753c3e3e950c6f8efa (patch) | |
tree | ee92600a9cddc1f852262cad31f20f89e36079ee /ext/ffi_c/DynamicLibrary.c | |
parent | d54ea5a985425d163860b9723ed9e93a82aac2c5 (diff) | |
download | ffi-5f1cbba44319d66d71eba6753c3e3e950c6f8efa.tar.gz |
Implement the last missing Write Barriers and dsize
Ref: https://github.com/ffi/ffi/pull/991
Write barrier protected objects are allowed to be promoted to the old generation,
which means they only get marked on major GC.
The downside is that the RB_BJ_WRITE macro MUST be used to set references,
otherwise the referenced object may be garbaged collected.
This commit also implement a `dsize` function so that these instance report
a more relevant size in various memory profilers. It's not counting everything
because some types are opaque right now, so a larger refactoring would be needed.
Diffstat (limited to 'ext/ffi_c/DynamicLibrary.c')
-rw-r--r-- | ext/ffi_c/DynamicLibrary.c | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/ext/ffi_c/DynamicLibrary.c b/ext/ffi_c/DynamicLibrary.c index c69074a..2e0bf4b 100644 --- a/ext/ffi_c/DynamicLibrary.c +++ b/ext/ffi_c/DynamicLibrary.c @@ -56,7 +56,7 @@ typedef struct LibrarySymbol_ { static VALUE library_initialize(VALUE self, VALUE libname, VALUE libflags); static void library_free(void *); - +static size_t library_memsize(const void *); static VALUE symbol_allocate(VALUE klass); static VALUE symbol_new(VALUE library, void* address, VALUE name); @@ -68,9 +68,11 @@ static const rb_data_type_t rbffi_library_data_type = { .function = { .dmark = NULL, .dfree = library_free, - .dsize = NULL, + .dsize = library_memsize, }, - .flags = RUBY_TYPED_FREE_IMMEDIATELY + // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE() + // macro to update VALUE references, as to trigger write barriers. + .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED }; static const rb_data_type_t library_symbol_data_type = { @@ -200,6 +202,12 @@ library_free(void *data) xfree(library); } +static size_t +library_memsize(const void *data) +{ + return sizeof(Library); +} + #if (defined(_WIN32) || defined(__WIN32__)) && !defined(__CYGWIN__) static void* dl_open(const char* name, int flags) |