Skip to content
Snippets Groups Projects
Commit 375d1f47 authored by Nicolas "Pixel" Noble's avatar Nicolas "Pixel" Noble
Browse files

Better memory management for grpc_rb_byte_buffer_to_s.

parent 97daf35a
No related branches found
No related tags found
No related merge requests found
...@@ -50,24 +50,18 @@ grpc_byte_buffer* grpc_rb_s_to_byte_buffer(char *string, size_t length) { ...@@ -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) { VALUE grpc_rb_byte_buffer_to_s(grpc_byte_buffer *buffer) {
size_t length = 0;
char *string = NULL;
VALUE rb_string; VALUE rb_string;
size_t offset = 0;
grpc_byte_buffer_reader reader; grpc_byte_buffer_reader reader;
gpr_slice next; gpr_slice next;
if (buffer == NULL) { if (buffer == NULL) {
return Qnil; return Qnil;
} }
length = grpc_byte_buffer_length(buffer); rb_string = rb_str_buf_new(grpc_byte_buffer_length(buffer));
string = xmalloc(length + 1);
grpc_byte_buffer_reader_init(&reader, buffer); grpc_byte_buffer_reader_init(&reader, buffer);
while (grpc_byte_buffer_reader_next(&reader, &next) != 0) { while (grpc_byte_buffer_reader_next(&reader, &next) != 0) {
memcpy(string + offset, GPR_SLICE_START_PTR(next), GPR_SLICE_LENGTH(next)); rb_str_cat(rb_string, (const char *) GPR_SLICE_START_PTR(next),
offset += GPR_SLICE_LENGTH(next); GPR_SLICE_LENGTH(next));
gpr_slice_unref(next); gpr_slice_unref(next);
} }
rb_string = rb_str_new(string, length);
xfree(string);
return rb_string; return rb_string;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment