Better memory management for grpc_rb_byte_buffer_to_s.

pull/6004/head
Nicolas "Pixel" Noble 9 years ago
parent 97daf35add
commit 375d1f47ca
  1. 12
      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;
}

Loading…
Cancel
Save