From 89464de6f04860b661d736c5645951be0b69357b Mon Sep 17 00:00:00 2001 From: yang-g Date: Wed, 7 Feb 2018 15:56:51 -0800 Subject: Trim backup slice when it is larger than needed --- include/grpc++/impl/codegen/proto_utils.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'include/grpc++') diff --git a/include/grpc++/impl/codegen/proto_utils.h b/include/grpc++/impl/codegen/proto_utils.h index b7636034d4..209250bba2 100644 --- a/include/grpc++/impl/codegen/proto_utils.h +++ b/include/grpc++/impl/codegen/proto_utils.h @@ -59,18 +59,22 @@ class GrpcBufferWriter : public ::grpc::protobuf::io::ZeroCopyOutputStream { bool Next(void** data, int* size) override { // Protobuf should not ask for more memory than total_size_. GPR_CODEGEN_ASSERT(byte_count_ < total_size_); + size_t remain = total_size_ - byte_count_; if (have_backup_) { slice_ = backup_slice_; have_backup_ = false; + if (GRPC_SLICE_LENGTH(slice_) > remain) { + GRPC_SLICE_SET_LENGTH(slice_, remain); + } } else { // When less than a whole block is needed, only allocate that much. // But make sure the allocated slice is not inlined. - size_t remain = total_size_ - byte_count_ > block_size_ - ? block_size_ - : total_size_ - byte_count_; + size_t allocate_length = + remain > static_cast(block_size_) ? block_size_ : remain; slice_ = g_core_codegen_interface->grpc_slice_malloc( - remain > GRPC_SLICE_INLINED_SIZE ? remain - : GRPC_SLICE_INLINED_SIZE + 1); + allocate_length > GRPC_SLICE_INLINED_SIZE + ? allocate_length + : GRPC_SLICE_INLINED_SIZE + 1); } *data = GRPC_SLICE_START_PTR(slice_); // On win x64, int is only 32bit -- cgit v1.2.3