Skip to content

Commit

Permalink
Add ruby_upb_alloc using xrealloc/xfree so Ruby GC is aware of alloca…
Browse files Browse the repository at this point in the history
…ted memory for Arenas. (#9586)

* Add ruby-specific upb_alloc using xrealloc/xfree for use in Arena_alloc so Ruby GC is aware of allocated memory.
* Add RB_GC_GUARD to DescriptorPool_add_serialized_file  to ensure ruby does not aggressively garbage collect arena_rb due to lack of references.
  • Loading branch information
zhangskz committed Mar 8, 2022
1 parent 1cec803 commit 276add0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
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
1 change: 0 additions & 1 deletion ruby/tests/gc_test.rb
Expand Up @@ -94,7 +94,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

0 comments on commit 276add0

Please sign in to comment.