aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Nicolas "Pixel" Noble <pixel@nobis-crew.org>2016-03-30 01:53:01 +0200
committerGravatar Nicolas "Pixel" Noble <pixel@nobis-crew.org>2016-03-30 02:08:49 +0200
commit375d1f47cad2f678da49e3e3d0a5e6c79c5e0f30 (patch)
tree781ea1c2d47f8536a7484e0eb7be90c159026244 /src
parent97daf35addf51951748d2954580fe10dffd3be36 (diff)
Better memory management for grpc_rb_byte_buffer_to_s.
Diffstat (limited to 'src')
-rw-r--r--src/ruby/ext/grpc/rb_byte_buffer.c12
1 files changed, 3 insertions, 9 deletions
diff --git a/src/ruby/ext/grpc/rb_byte_buffer.c b/src/ruby/ext/grpc/rb_byte_buffer.c
index 52523b8319..9b617e13d3 100644
--- a/src/ruby/ext/grpc/rb_byte_buffer.c
+++ b/src/ruby/ext/grpc/rb_byte_buffer.c
@@ -50,24 +50,18 @@ grpc_byte_buffer* grpc_rb_s_to_byte_buffer(char *string, size_t length) {
}
VALUE grpc_rb_byte_buffer_to_s(grpc_byte_buffer *buffer) {
- size_t length = 0;
- char *string = NULL;
VALUE rb_string;
- size_t offset = 0;
grpc_byte_buffer_reader reader;
gpr_slice next;
if (buffer == NULL) {
return Qnil;
}
- length = grpc_byte_buffer_length(buffer);
- string = xmalloc(length + 1);
+ rb_string = rb_str_buf_new(grpc_byte_buffer_length(buffer));
grpc_byte_buffer_reader_init(&reader, buffer);
while (grpc_byte_buffer_reader_next(&reader, &next) != 0) {
- memcpy(string + offset, GPR_SLICE_START_PTR(next), GPR_SLICE_LENGTH(next));
- offset += GPR_SLICE_LENGTH(next);
+ rb_str_cat(rb_string, (const char *) GPR_SLICE_START_PTR(next),
+ GPR_SLICE_LENGTH(next));
gpr_slice_unref(next);
}
- rb_string = rb_str_new(string, length);
- xfree(string);
return rb_string;
}