Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ruby_upb_alloc using xrealloc/xfree so Ruby GC is aware of allocated memory for Arenas. #9586

Merged
merged 5 commits into from Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions ruby/ext/google/protobuf_c/defs.c
Expand Up @@ -159,6 +159,7 @@ VALUE DescriptorPool_add_serialized_file(VALUE _self,
rb_raise(cTypeError, "Unable to build file to DescriptorPool: %s",
upb_Status_ErrorMessage(&status));
}
RB_GC_GUARD(arena_rb);
return get_filedef_obj(_self, filedef);
}

Expand Down
13 changes: 12 additions & 1 deletion ruby/ext/google/protobuf_c/protobuf.c
Expand Up @@ -193,9 +193,20 @@ const rb_data_type_t Arena_type = {
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
};

static void* ruby_upb_allocfunc(upb_alloc* alloc, void* ptr, size_t oldsize, size_t size) {
if (size == 0) {
xfree(ptr);
return NULL;
} else {
return xrealloc(ptr, size);
}
}

upb_alloc ruby_upb_alloc = {&ruby_upb_allocfunc};

static VALUE Arena_alloc(VALUE klass) {
Arena *arena = ALLOC(Arena);
arena->arena = upb_Arena_New();
arena->arena = upb_Arena_Init(NULL, 0, &ruby_upb_alloc);
arena->pinned_objs = Qnil;
return TypedData_Wrap_Struct(klass, &Arena_type, arena);
}
Expand Down
5 changes: 1 addition & 4 deletions ruby/tests/gc_test.rb
Expand Up @@ -4,9 +4,7 @@
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))

old_gc = GC.stress
# Ruby 2.7.0 - 2.7.1 has a GC bug in its parser, so turn off stress for now
# See https://bugs.ruby-lang.org/issues/16807
GC.stress = 0x01 | 0x04 unless RUBY_VERSION.match?(/^2\.7\./)
GC.stress = 0x01 | 0x04
require 'generated_code_pb'
require 'generated_code_proto2_pb'
GC.stress = old_gc
Expand Down Expand Up @@ -94,7 +92,6 @@ def test_generated_msg
from = get_msg_proto3
data = A::B::C::TestMessage.encode(from)
to = A::B::C::TestMessage.decode(data)

# This doesn't work for proto2 on JRuby because there is a nested required message.
# A::B::Proto2::TestMessage has :required_msg which is of type:
# A::B::Proto2::TestMessage so there is no way to generate a valid
Expand Down