From 98f681deb05d81c63667e7ffc98fd67224744339 Mon Sep 17 00:00:00 2001 From: Daniel Lin Date: Thu, 18 Jan 2018 08:51:57 -0800 Subject: [PATCH] Author: Daniel Lin (#4595) Date: Mon Jan 15 11:38:20 2018 -0200 Compilation failure with grpc.h If cmake run with flag FLATBUFFERS_BUILD_GRPCTEST=ON compilation fails. Fix : -Fix argument list for overriden function in grpc. -Fix member function name called by FlatBufferBuilder from buf() to scratch_data() --- include/flatbuffers/flatbuffers.h | 16 ++++++++++++++-- include/flatbuffers/grpc.h | 8 +++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index 9d2e2486a..528cb5758 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -370,11 +370,23 @@ class Allocator { size_t in_use_front) { assert(new_size > old_size); // vector_downward only grows uint8_t *new_p = allocate(new_size); + memcpy_downward(old_p, old_size, new_p, new_size, in_use_back, + in_use_front); + deallocate(old_p, old_size); + return new_p; + } + + protected: + // Called by `reallocate_downward` to copy memory from `old_p` of `old_size` + // to `new_p` of `new_size`. Only memory of size `in_use_front` and + // `in_use_back` will be copied from the front and back of the old memory + // allocation. + void memcpy_downward(uint8_t *old_p, size_t old_size, + uint8_t *new_p, size_t new_size, + size_t in_use_back, size_t in_use_front) { memcpy(new_p + new_size - in_use_back, old_p + old_size - in_use_back, in_use_back); memcpy(new_p, old_p, in_use_front); - deallocate(old_p, old_size); - return new_p; } }; diff --git a/include/flatbuffers/grpc.h b/include/flatbuffers/grpc.h index ac55efd59..e40ab7645 100644 --- a/include/flatbuffers/grpc.h +++ b/include/flatbuffers/grpc.h @@ -104,14 +104,16 @@ class SliceAllocator : public Allocator { } virtual uint8_t *reallocate_downward(uint8_t *old_p, size_t old_size, - size_t new_size) override { + size_t new_size, size_t in_use_back, + size_t in_use_front) override { assert(old_p == GRPC_SLICE_START_PTR(slice_)); assert(old_size == GRPC_SLICE_LENGTH(slice_)); assert(new_size > old_size); grpc_slice old_slice = slice_; grpc_slice new_slice = grpc_slice_malloc(new_size); uint8_t *new_p = GRPC_SLICE_START_PTR(new_slice); - memcpy(new_p + (new_size - old_size), old_p, old_size); + memcpy_downward(old_p, old_size, new_p, new_size, in_use_back, + in_use_front); slice_ = new_slice; grpc_slice_unref(old_slice); return new_p; @@ -155,7 +157,7 @@ class MessageBuilder : private detail::SliceAllocatorMember, // flatbuffers-encoded region and wraps it in a `Message` to handle buffer // ownership. template Message GetMessage() { - auto buf_data = buf_.buf(); // pointer to memory + auto buf_data = buf_.scratch_data(); // pointer to memory auto buf_size = buf_.capacity(); // size of memory auto msg_data = buf_.data(); // pointer to msg auto msg_size = buf_.size(); // size of msg