diff options
author | yang-g <yangg@google.com> | 2018-02-07 15:56:51 -0800 |
---|---|---|
committer | yang-g <yangg@google.com> | 2018-02-07 16:03:57 -0800 |
commit | 89464de6f04860b661d736c5645951be0b69357b (patch) | |
tree | 22968a587fa83fc6e3c82c840ade207715c833a4 /include/grpc++ | |
parent | fdc10b6ebd7aacaa933770b4561fdac601f0f06f (diff) |
Trim backup slice when it is larger than needed
Diffstat (limited to 'include/grpc++')
-rw-r--r-- | include/grpc++/impl/codegen/proto_utils.h | 14 |
1 files changed, 9 insertions, 5 deletions
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<size_t>(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 |