From db1a5cce471cb038953f4221401142da85c2412a Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Wed, 28 Sep 2016 14:22:12 -0700 Subject: Buffer pool C++ wrapper --- src/cpp/common/buffer_pool.cc | 50 +++++++++++++++++++++++++++++++++++++ src/cpp/common/channel_arguments.cc | 14 ++++++++++- src/cpp/server/server_builder.cc | 22 ++++++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/cpp/common/buffer_pool.cc (limited to 'src') diff --git a/src/cpp/common/buffer_pool.cc b/src/cpp/common/buffer_pool.cc new file mode 100644 index 0000000000..9d65bd7431 --- /dev/null +++ b/src/cpp/common/buffer_pool.cc @@ -0,0 +1,50 @@ +/* + * + * Copyright 2016, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +namespace grpc { + +BufferPool::BufferPool() : impl_(grpc_buffer_pool_create(nullptr)) {} + +BufferPool::BufferPool(const grpc::string& name) + : impl_(grpc_buffer_pool_create(name.c_str())) {} + +BufferPool::~BufferPool() { grpc_buffer_pool_unref(impl_); } + +void BufferPool::Resize(size_t new_size) { + grpc_buffer_pool_resize(impl_, new_size); +} + +} // namespace grpc diff --git a/src/cpp/common/channel_arguments.cc b/src/cpp/common/channel_arguments.cc index f297ae8587..afde513e1e 100644 --- a/src/cpp/common/channel_arguments.cc +++ b/src/cpp/common/channel_arguments.cc @@ -34,6 +34,7 @@ #include +#include #include #include #include "src/core/lib/channel/channel_args.h" @@ -113,6 +114,11 @@ void ChannelArguments::SetUserAgentPrefix( } } +void ChannelArguments::SetBufferPool(const grpc::BufferPool& buffer_pool) { + SetPointerWithVtable(GRPC_ARG_BUFFER_POOL, buffer_pool.c_buffer_pool(), + grpc_buffer_pool_arg_vtable()); +} + void ChannelArguments::SetInt(const grpc::string& key, int value) { grpc_arg arg; arg.type = GRPC_ARG_INTEGER; @@ -127,12 +133,18 @@ void ChannelArguments::SetPointer(const grpc::string& key, void* value) { static const grpc_arg_pointer_vtable vtable = { &PointerVtableMembers::Copy, &PointerVtableMembers::Destroy, &PointerVtableMembers::Compare}; + SetPointerWithVtable(key, value, &vtable); +} + +void ChannelArguments::SetPointerWithVtable( + const grpc::string& key, void* value, + const grpc_arg_pointer_vtable* vtable) { grpc_arg arg; arg.type = GRPC_ARG_POINTER; strings_.push_back(key); arg.key = const_cast(strings_.back().c_str()); arg.value.pointer.p = value; - arg.value.pointer.vtable = &vtable; + arg.value.pointer.vtable = vtable; args_.push_back(arg); } diff --git a/src/cpp/server/server_builder.cc b/src/cpp/server/server_builder.cc index 2980b16c56..be5b97846a 100644 --- a/src/cpp/server/server_builder.cc +++ b/src/cpp/server/server_builder.cc @@ -33,6 +33,7 @@ #include +#include #include #include #include @@ -54,6 +55,7 @@ static void do_plugin_list_init(void) { ServerBuilder::ServerBuilder() : max_receive_message_size_(-1), max_send_message_size_(-1), + buffer_pool_(nullptr), generic_service_(nullptr) { gpr_once_init(&once_init_plugin_list, do_plugin_list_init); for (auto it = g_plugin_factory_list->begin(); @@ -70,6 +72,12 @@ ServerBuilder::ServerBuilder() sizeof(maybe_default_compression_algorithm_)); } +ServerBuilder::~ServerBuilder() { + if (buffer_pool_ != nullptr) { + grpc_buffer_pool_unref(buffer_pool_); + } +} + std::unique_ptr ServerBuilder::AddCompletionQueue( bool is_frequently_polled) { ServerCompletionQueue* cq = new ServerCompletionQueue(is_frequently_polled); @@ -130,6 +138,16 @@ ServerBuilder& ServerBuilder::SetDefaultCompressionAlgorithm( return *this; } +ServerBuilder& ServerBuilder::SetBufferPool( + const grpc::BufferPool& buffer_pool) { + if (buffer_pool_ != nullptr) { + grpc_buffer_pool_unref(buffer_pool_); + } + buffer_pool_ = buffer_pool.c_buffer_pool(); + grpc_buffer_pool_ref(buffer_pool_); + return *this; +} + ServerBuilder& ServerBuilder::AddListeningPort( const grpc::string& addr, std::shared_ptr creds, int* selected_port) { @@ -178,6 +196,10 @@ std::unique_ptr ServerBuilder::BuildAndStart() { args.SetInt(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM, maybe_default_compression_algorithm_.algorithm); } + if (buffer_pool_ != nullptr) { + args.SetPointerWithVtable(GRPC_ARG_BUFFER_POOL, buffer_pool_, + grpc_buffer_pool_arg_vtable()); + } std::unique_ptr server(new Server(thread_pool.release(), true, max_receive_message_size_, &args)); ServerInitializer* initializer = server->initializer(); -- cgit v1.2.3