aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Craig Tiller <craig.tiller@gmail.com>2015-06-02 16:59:00 -0700
committerGravatar Craig Tiller <craig.tiller@gmail.com>2015-06-02 16:59:00 -0700
commit669c139a4a41eaca7bff52071510258e65274de7 (patch)
tree60c3021aca6a3f936e251277f3ad2050ce427218 /src
parentf11214faa5a86df188d4631241b63ae90f1ea6a6 (diff)
parent02c677c6cb68d497ccf7bf90be32b2a238249e62 (diff)
Merge pull request #1866 from dgquintas/stack-allocate-bb-reader
Removed grpc_byte_buffer_reader_{create,destroy}.
Diffstat (limited to 'src')
-rw-r--r--src/core/surface/byte_buffer_reader.c15
-rw-r--r--src/cpp/proto/proto_utils.cc11
-rw-r--r--src/cpp/util/byte_buffer.cc7
-rw-r--r--src/csharp/ext/grpc_csharp_ext.c8
-rw-r--r--src/node/ext/byte_buffer.cc6
-rw-r--r--src/objective-c/GRPCClient/private/NSData+GRPC.m13
-rw-r--r--src/php/ext/grpc/byte_buffer.c6
-rw-r--r--src/python/src/grpc/_adapter/_c/utility.c7
-rw-r--r--src/ruby/ext/grpc/rb_byte_buffer.c7
9 files changed, 44 insertions, 36 deletions
diff --git a/src/core/surface/byte_buffer_reader.c b/src/core/surface/byte_buffer_reader.c
index fd5289bac3..41ad700274 100644
--- a/src/core/surface/byte_buffer_reader.c
+++ b/src/core/surface/byte_buffer_reader.c
@@ -39,15 +39,18 @@
#include <grpc/support/slice_buffer.h>
#include <grpc/byte_buffer.h>
-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;
+}
+
+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,
@@ -68,7 +71,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 <grpc/grpc.h>
#include <grpc/byte_buffer.h>
+#include <grpc/byte_buffer_reader.h>
#include <grpc/support/slice.h>
#include <grpc/support/slice_buffer.h>
#include <grpc/support/port_platform.h>
@@ -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 <grpc/byte_buffer_reader.h>
#include <grpc++/byte_buffer.h>
namespace grpc {
@@ -56,12 +57,12 @@ void ByteBuffer::Dump(std::vector<Slice>* 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 <grpc/byte_buffer_reader.h>
#include <grpc/support/port_platform.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
@@ -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 <node.h>
#include <nan.h>
#include "grpc/grpc.h"
+#include "grpc/byte_buffer_reader.h"
#include "grpc/support/slice.h"
#include "byte_buffer.h"
@@ -69,9 +70,10 @@ Handle<Value> ByteBufferToBuffer(grpc_byte_buffer *buffer) {
size_t length = grpc_byte_buffer_length(buffer);
char *result = reinterpret_cast<char *>(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 <grpc/byte_buffer.h>
+#include <grpc/byte_buffer_reader.h>
#include <string.h>
// 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 <grpc/grpc.h>
+#include <grpc/byte_buffer_reader.h>
#include <grpc/support/slice.h>
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 <Python.h>
#include <grpc/grpc.h>
+#include <grpc/byte_buffer_reader.h>
#include <grpc/support/alloc.h>
#include <grpc/support/slice.h>
#include <grpc/support/time.h>
@@ -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 <ruby/ruby.h>
#include <grpc/grpc.h>
+#include <grpc/byte_buffer_reader.h>
#include <grpc/support/slice.h>
#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);
}