11/*
2- * Copyright (c) 2015, 2017 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2015, 2018 , Oracle and/or its affiliates. All rights reserved.
33 *
44 * This program is free software; you can redistribute it and/or modify
55 * it under the terms of the GNU General Public License, version 2.0,
3030
3131namespace ngs {
3232
33- Message_builder::Message_builder () : m_out_buffer(NULL ) {}
33+ Message_builder::Message_builder ()
34+ : m_out_buffer(nullptr ), m_out_stream(Stream_allocator().allocate(1 )) {}
3435
35- Message_builder::~Message_builder () {}
36+ Message_builder::~Message_builder () {
37+ Stream_allocator ().deallocate (m_out_stream, 1 );
38+ }
3639
3740void Message_builder::encode_uint32 (const uint32 value, const bool write) {
3841 ++m_field_number;
@@ -41,7 +44,7 @@ void Message_builder::encode_uint32(const uint32 value, const bool write) {
4144 google::protobuf::internal::WireFormatLite::WriteTag (
4245 m_field_number,
4346 google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT,
44- m_out_stream. get () );
47+ m_out_stream);
4548 m_out_stream->WriteVarint32 (value);
4649 }
4750}
@@ -53,7 +56,7 @@ void Message_builder::encode_uint64(const uint64 value, const bool write) {
5356 google::protobuf::internal::WireFormatLite::WriteTag (
5457 m_field_number,
5558 google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT,
56- m_out_stream. get () );
59+ m_out_stream);
5760 m_out_stream->WriteVarint64 (value);
5861 }
5962}
@@ -64,7 +67,7 @@ void Message_builder::encode_int32(const int32 value, const bool write) {
6467 google::protobuf::internal::WireFormatLite::WriteTag (
6568 m_field_number,
6669 google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT,
67- m_out_stream. get () );
70+ m_out_stream);
6871 m_out_stream->WriteVarint32SignExtended (value);
6972 }
7073}
@@ -77,7 +80,7 @@ void Message_builder::encode_string(const char *value, const size_t len,
7780 google::protobuf::internal::WireFormatLite::WriteTag (
7881 m_field_number,
7982 google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED,
80- m_out_stream. get () );
83+ m_out_stream);
8184 m_out_stream->WriteVarint32 (static_cast <google::protobuf::uint32>(len));
8285 m_out_stream->WriteRaw (value, static_cast <int >(len));
8386 }
@@ -87,6 +90,18 @@ void Message_builder::encode_string(const char *value, const bool write) {
8790 encode_string (value, write ? strlen (value) : 0 , write);
8891}
8992
93+ void Message_builder::construct_stream () {
94+ if (m_valid_out_stream) reset_stream ();
95+
96+ Stream_allocator ().construct (m_out_stream, m_out_buffer);
97+ m_valid_out_stream = true ;
98+ }
99+
100+ void Message_builder::reset_stream () {
101+ Stream_allocator ().destroy (m_out_stream);
102+ m_valid_out_stream = false ;
103+ }
104+
90105void Message_builder::start_message (Output_buffer *out_buffer,
91106 const uint8 type) {
92107 m_field_number = 0 ;
@@ -95,7 +110,8 @@ void Message_builder::start_message(Output_buffer *out_buffer,
95110 m_out_buffer->save_state ();
96111 m_out_buffer->reserve (5 );
97112 m_start_from = static_cast <uint32>(m_out_buffer->ByteCount ());
98- m_out_stream.reset (ngs::allocate_object<CodedOutputStream>(m_out_buffer));
113+
114+ construct_stream ();
99115
100116 // at this point we don't know the size but we need to reserve the space for
101117 // it it is possible that the size which is stored on 4-bytes will be split
@@ -128,7 +144,7 @@ void Message_builder::end_message() {
128144 // here we already know the buffer size, so write it at the beginning of the
129145 // buffer the order is important here as the stream's destructor calls
130146 // buffer's BackUp() validating ByteCount
131- m_out_stream. reset ();
147+ reset_stream ();
132148
133149 uint32 msg_size = static_cast <uint32>(m_out_buffer->ByteCount ()) -
134150 m_start_from - sizeof (google::protobuf::uint32);
0 commit comments