From 30bd4eb5cc268c0faa7a7b89f7be0af61598785f Mon Sep 17 00:00:00 2001 From: David Garcia Quintas Date: Mon, 1 Jun 2015 21:08:59 -0700 Subject: Removed grpc_byte_buffer_reader_{create,destroy}. Introduced grpc_byte_buffer_init instead. It's now the user's responsibility to manage memory. --- src/core/surface/byte_buffer_reader.c | 10 ++-------- src/cpp/proto/proto_utils.cc | 11 +++++------ src/cpp/util/byte_buffer.cc | 7 ++++--- src/csharp/ext/grpc_csharp_ext.c | 8 ++++---- src/node/ext/byte_buffer.cc | 6 ++++-- src/objective-c/GRPCClient/private/NSData+GRPC.m | 13 ++++++++----- src/php/ext/grpc/byte_buffer.c | 6 ++++-- src/python/src/grpc/_adapter/_c/utility.c | 7 ++++--- src/ruby/ext/grpc/rb_byte_buffer.c | 7 ++++--- 9 files changed, 39 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/core/surface/byte_buffer_reader.c b/src/core/surface/byte_buffer_reader.c index fd5289bac3..5d97609aaa 100644 --- a/src/core/surface/byte_buffer_reader.c +++ b/src/core/surface/byte_buffer_reader.c @@ -39,15 +39,13 @@ #include #include -grpc_byte_buffer_reader *grpc_byte_buffer_reader_create( - grpc_byte_buffer *buffer) { - grpc_byte_buffer_reader *reader = malloc(sizeof(grpc_byte_buffer_reader)); +void grpc_byte_buffer_reader_init(grpc_byte_buffer_reader *reader, + grpc_byte_buffer *buffer) { reader->buffer = buffer; switch (buffer->type) { case GRPC_BB_SLICE_BUFFER: reader->current.index = 0; } - return reader; } int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader *reader, @@ -68,7 +66,3 @@ int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader *reader, } return 0; } - -void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader *reader) { - free(reader); -} diff --git a/src/cpp/proto/proto_utils.cc b/src/cpp/proto/proto_utils.cc index b9554c4bb7..7a7e73bba4 100644 --- a/src/cpp/proto/proto_utils.cc +++ b/src/cpp/proto/proto_utils.cc @@ -36,6 +36,7 @@ #include #include +#include #include #include #include @@ -100,11 +101,9 @@ class GrpcBufferReader GRPC_FINAL public: explicit GrpcBufferReader(grpc_byte_buffer* buffer) : byte_count_(0), backup_count_(0) { - reader_ = grpc_byte_buffer_reader_create(buffer); - } - ~GrpcBufferReader() GRPC_OVERRIDE { - grpc_byte_buffer_reader_destroy(reader_); + grpc_byte_buffer_reader_init(&reader_, buffer); } + ~GrpcBufferReader() GRPC_OVERRIDE {} bool Next(const void** data, int* size) GRPC_OVERRIDE { if (backup_count_ > 0) { @@ -114,7 +113,7 @@ class GrpcBufferReader GRPC_FINAL backup_count_ = 0; return true; } - if (!grpc_byte_buffer_reader_next(reader_, &slice_)) { + if (!grpc_byte_buffer_reader_next(&reader_, &slice_)) { return false; } gpr_slice_unref(slice_); @@ -147,7 +146,7 @@ class GrpcBufferReader GRPC_FINAL private: gpr_int64 byte_count_; gpr_int64 backup_count_; - grpc_byte_buffer_reader* reader_; + grpc_byte_buffer_reader reader_; gpr_slice slice_; }; diff --git a/src/cpp/util/byte_buffer.cc b/src/cpp/util/byte_buffer.cc index ac2657472c..45eaa2fe5b 100644 --- a/src/cpp/util/byte_buffer.cc +++ b/src/cpp/util/byte_buffer.cc @@ -31,6 +31,7 @@ * */ +#include #include namespace grpc { @@ -56,12 +57,12 @@ void ByteBuffer::Dump(std::vector* slices) { if (!buffer_) { return; } - grpc_byte_buffer_reader* reader = grpc_byte_buffer_reader_create(buffer_); + grpc_byte_buffer_reader reader; + grpc_byte_buffer_reader_init(&reader,buffer_); gpr_slice s; - while (grpc_byte_buffer_reader_next(reader, &s)) { + while (grpc_byte_buffer_reader_next(&reader, &s)) { slices->push_back(Slice(s, Slice::STEAL_REF)); } - grpc_byte_buffer_reader_destroy(reader); } size_t ByteBuffer::Length() { diff --git a/src/csharp/ext/grpc_csharp_ext.c b/src/csharp/ext/grpc_csharp_ext.c index cea23f019e..25d8469fe7 100644 --- a/src/csharp/ext/grpc_csharp_ext.c +++ b/src/csharp/ext/grpc_csharp_ext.c @@ -33,6 +33,7 @@ #include "src/core/support/string.h" +#include #include #include #include @@ -236,13 +237,13 @@ GPR_EXPORT gpr_intptr GPR_CALLTYPE grpcsharp_batch_context_recv_message_length( */ GPR_EXPORT void GPR_CALLTYPE grpcsharp_batch_context_recv_message_to_buffer( const grpcsharp_batch_context *ctx, char *buffer, size_t buffer_len) { - grpc_byte_buffer_reader *reader; + grpc_byte_buffer_reader reader; gpr_slice slice; size_t offset = 0; - reader = grpc_byte_buffer_reader_create(ctx->recv_message); + grpc_byte_buffer_reader_init(&reader, ctx->recv_message); - while (grpc_byte_buffer_reader_next(reader, &slice)) { + while (grpc_byte_buffer_reader_next(&reader, &slice)) { size_t len = GPR_SLICE_LENGTH(slice); GPR_ASSERT(offset + len <= buffer_len); memcpy(buffer + offset, GPR_SLICE_START_PTR(slice), @@ -250,7 +251,6 @@ GPR_EXPORT void GPR_CALLTYPE grpcsharp_batch_context_recv_message_to_buffer( offset += len; gpr_slice_unref(slice); } - grpc_byte_buffer_reader_destroy(reader); } GPR_EXPORT grpc_status_code GPR_CALLTYPE diff --git a/src/node/ext/byte_buffer.cc b/src/node/ext/byte_buffer.cc index 01bd92ea52..2c84099069 100644 --- a/src/node/ext/byte_buffer.cc +++ b/src/node/ext/byte_buffer.cc @@ -36,6 +36,7 @@ #include #include #include "grpc/grpc.h" +#include "grpc/byte_buffer_reader.h" #include "grpc/support/slice.h" #include "byte_buffer.h" @@ -69,9 +70,10 @@ Handle ByteBufferToBuffer(grpc_byte_buffer *buffer) { size_t length = grpc_byte_buffer_length(buffer); char *result = reinterpret_cast(calloc(length, sizeof(char))); size_t offset = 0; - grpc_byte_buffer_reader *reader = grpc_byte_buffer_reader_create(buffer); + grpc_byte_buffer_reader reader; + grpc_byte_buffer_reader_init(&reader, buffer); gpr_slice next; - while (grpc_byte_buffer_reader_next(reader, &next) != 0) { + while (grpc_byte_buffer_reader_next(&reader, &next) != 0) { memcpy(result + offset, GPR_SLICE_START_PTR(next), GPR_SLICE_LENGTH(next)); offset += GPR_SLICE_LENGTH(next); } diff --git a/src/objective-c/GRPCClient/private/NSData+GRPC.m b/src/objective-c/GRPCClient/private/NSData+GRPC.m index 6ea4ce979e..3a7f76887b 100644 --- a/src/objective-c/GRPCClient/private/NSData+GRPC.m +++ b/src/objective-c/GRPCClient/private/NSData+GRPC.m @@ -34,23 +34,26 @@ #import "NSData+GRPC.h" #include +#include #include // TODO(jcanizales): Move these two incantations to the C library. static void CopyByteBufferToCharArray(grpc_byte_buffer *buffer, char *array) { size_t offset = 0; - grpc_byte_buffer_reader *reader = grpc_byte_buffer_reader_create(buffer); + grpc_byte_buffer_reader reader; + grpc_byte_buffer_reader_init(&reader, buffer); gpr_slice next; - while (grpc_byte_buffer_reader_next(reader, &next) != 0){ - memcpy(array + offset, GPR_SLICE_START_PTR(next), (size_t) GPR_SLICE_LENGTH(next)); + while (grpc_byte_buffer_reader_next(&reader, &next) != 0){ + memcpy(array + offset, GPR_SLICE_START_PTR(next), + (size_t)GPR_SLICE_LENGTH(next)); offset += GPR_SLICE_LENGTH(next); gpr_slice_unref(next); } - grpc_byte_buffer_reader_destroy(reader); } -static grpc_byte_buffer *CopyCharArrayToNewByteBuffer(const char *array, size_t length) { +static grpc_byte_buffer *CopyCharArrayToNewByteBuffer(const char *array, + size_t length) { gpr_slice slice = gpr_slice_from_copied_buffer(array, length); grpc_byte_buffer *buffer = grpc_byte_buffer_create(&slice, 1); gpr_slice_unref(slice); diff --git a/src/php/ext/grpc/byte_buffer.c b/src/php/ext/grpc/byte_buffer.c index 4f3e6b67af..bb9d3f5337 100644 --- a/src/php/ext/grpc/byte_buffer.c +++ b/src/php/ext/grpc/byte_buffer.c @@ -46,6 +46,7 @@ #include "byte_buffer.h" #include +#include #include grpc_byte_buffer *string_to_byte_buffer(char *string, size_t length) { @@ -65,9 +66,10 @@ void byte_buffer_to_string(grpc_byte_buffer *buffer, char **out_string, size_t length = grpc_byte_buffer_length(buffer); char *string = ecalloc(length + 1, sizeof(char)); size_t offset = 0; - grpc_byte_buffer_reader *reader = grpc_byte_buffer_reader_create(buffer); + grpc_byte_buffer_reader reader; + grpc_byte_buffer_reader_init(&reader, buffer); gpr_slice next; - 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)); offset += GPR_SLICE_LENGTH(next); } diff --git a/src/python/src/grpc/_adapter/_c/utility.c b/src/python/src/grpc/_adapter/_c/utility.c index 6d228c73fe..42d3370cf2 100644 --- a/src/python/src/grpc/_adapter/_c/utility.c +++ b/src/python/src/grpc/_adapter/_c/utility.c @@ -36,6 +36,7 @@ #define PY_SSIZE_T_CLEAN #include #include +#include #include #include #include @@ -443,18 +444,18 @@ PyObject *pygrpc_cast_metadata_array_to_pylist(grpc_metadata_array metadata) { void pygrpc_byte_buffer_to_bytes( grpc_byte_buffer *buffer, char **result, size_t *result_size) { - grpc_byte_buffer_reader *reader = grpc_byte_buffer_reader_create(buffer); + grpc_byte_buffer_reader reader; + grpc_byte_buffer_reader_init(&reader, buffer); gpr_slice slice; char *read_result = NULL; size_t size = 0; - while (grpc_byte_buffer_reader_next(reader, &slice)) { + while (grpc_byte_buffer_reader_next(&reader, &slice)) { read_result = gpr_realloc(read_result, size + GPR_SLICE_LENGTH(slice)); memcpy(read_result + size, GPR_SLICE_START_PTR(slice), GPR_SLICE_LENGTH(slice)); size = size + GPR_SLICE_LENGTH(slice); gpr_slice_unref(slice); } - grpc_byte_buffer_reader_destroy(reader); *result_size = size; *result = read_result; } diff --git a/src/ruby/ext/grpc/rb_byte_buffer.c b/src/ruby/ext/grpc/rb_byte_buffer.c index edf0d3b115..1cc22f4aff 100644 --- a/src/ruby/ext/grpc/rb_byte_buffer.c +++ b/src/ruby/ext/grpc/rb_byte_buffer.c @@ -36,6 +36,7 @@ #include #include +#include #include #include "rb_grpc.h" @@ -50,7 +51,7 @@ VALUE grpc_rb_byte_buffer_to_s(grpc_byte_buffer *buffer) { size_t length = 0; char *string = NULL; size_t offset = 0; - grpc_byte_buffer_reader *reader = NULL; + grpc_byte_buffer_reader reader; gpr_slice next; if (buffer == NULL) { return Qnil; @@ -58,8 +59,8 @@ VALUE grpc_rb_byte_buffer_to_s(grpc_byte_buffer *buffer) { } length = grpc_byte_buffer_length(buffer); string = xmalloc(length + 1); - reader = grpc_byte_buffer_reader_create(buffer); - while (grpc_byte_buffer_reader_next(reader, &next) != 0) { + 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); } -- cgit v1.2.3 From 02c677c6cb68d497ccf7bf90be32b2a238249e62 Mon Sep 17 00:00:00 2001 From: David Garcia Quintas Date: Tue, 2 Jun 2015 14:40:07 -0700 Subject: Reintroduced grpc_byte_buffer_reader_destroy. As a no-op for the time being. --- include/grpc/grpc.h | 5 ++++- src/core/surface/byte_buffer_reader.c | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/include/grpc/grpc.h b/include/grpc/grpc.h index f0fa85a77b..57bf2fad5a 100644 --- a/include/grpc/grpc.h +++ b/include/grpc/grpc.h @@ -93,7 +93,7 @@ typedef struct { } grpc_arg; /** An array of arguments that can be passed around. - + Used to set optional channel-level configuration. These configuration options are modelled as key-value pairs as defined by grpc_arg; keys are strings to allow easy backwards-compatible extension @@ -174,6 +174,9 @@ typedef struct grpc_byte_buffer_reader grpc_byte_buffer_reader; void grpc_byte_buffer_reader_init(grpc_byte_buffer_reader *reader, grpc_byte_buffer *buffer); +/** Cleanup and destroy \a reader */ +void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader *reader); + /* At the end of the stream, returns 0. Otherwise, returns 1 and sets slice to be the returned slice. Caller is responsible for calling gpr_slice_unref on the result. */ diff --git a/src/core/surface/byte_buffer_reader.c b/src/core/surface/byte_buffer_reader.c index 5d97609aaa..41ad700274 100644 --- a/src/core/surface/byte_buffer_reader.c +++ b/src/core/surface/byte_buffer_reader.c @@ -48,6 +48,11 @@ void grpc_byte_buffer_reader_init(grpc_byte_buffer_reader *reader, } } +void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader *reader) { + /* no-op: the user is responsible for memory deallocation. + * Other cleanup operations would go here if needed. */ +} + int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader *reader, gpr_slice *slice) { grpc_byte_buffer *buffer = reader->buffer; -- cgit v1.2.3