Coverage Report

Created: 2025-12-02 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/brpc/src/mcpack2pb/serializer-inl.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
// mcpack2pb - Make protobuf be front-end of mcpack/compack
19
20
// Date: Mon Oct 19 17:17:36 CST 2015
21
22
#ifndef MCPACK2PB_MCPACK_SERIALIZER_INL_H
23
#define MCPACK2PB_MCPACK_SERIALIZER_INL_H
24
25
void* fast_memcpy(void *__restrict dest, const void *__restrict src, size_t n);
26
27
namespace mcpack2pb {
28
29
inline OutputStream::Area::Area(const Area& rhs) 
30
0
    : _addr1(rhs._addr1)
31
0
    , _addr2(rhs._addr2)
32
0
    , _size1(rhs._size1)
33
0
    , _size2(rhs._size2)
34
0
    , _addional_area(NULL) {
35
36
0
    if (rhs._addional_area) {
37
0
        _addional_area = new std::vector<butil::StringPiece>(*rhs._addional_area);
38
0
    }
39
0
}
40
41
0
inline OutputStream::Area::~Area() {
42
0
    if (_addional_area) {
43
0
        delete _addional_area;
44
0
        _addional_area = NULL;
45
0
    }
46
0
}
47
48
0
inline OutputStream::Area& OutputStream::Area::operator=(const OutputStream::Area& rhs) {
49
0
    if (this == &rhs) {
50
0
        return *this;
51
0
    }
52
0
    this->~Area();
53
0
    new (this) Area(rhs);
54
0
    return *this;
55
0
}
56
57
0
inline void OutputStream::Area::add(void* data, size_t n) {
58
0
    if (!data) {
59
0
        return;
60
0
    }
61
0
    if (_addr1 == NULL) {
62
0
        _addr1 = data;
63
0
        _size1 = n;
64
0
    } else if (_addr2 == NULL) {
65
0
        _addr2 = data;
66
0
        _size2 = n;
67
0
    } else {
68
0
        if (_addional_area == NULL) {
69
0
            _addional_area = new std::vector<butil::StringPiece>;
70
0
        }
71
0
        _addional_area->push_back(butil::StringPiece((const char*)data, n));
72
0
    }
73
0
}
74
75
0
inline void OutputStream::Area::assign(const void* data) const {
76
0
    if (_addr1) {
77
0
        fast_memcpy(_addr1, data, _size1);
78
0
        if (!_addr2) {
79
0
            return;
80
0
        }
81
0
        fast_memcpy(_addr2, (const char*)data + _size1, _size2);
82
0
        if (!_addional_area) {
83
0
            return;
84
0
        }
85
0
        size_t offset = _size1 + _size2;
86
0
        for (std::vector<butil::StringPiece>::const_iterator iter =
87
0
                _addional_area->begin(); iter != _addional_area->end(); ++iter) {
88
0
            fast_memcpy((void*)iter->data(), (const char*)data + offset, iter->size());
89
0
            offset += iter->size();
90
0
        }
91
0
    }
92
0
}
93
94
0
inline void OutputStream::done() {
95
0
    if (_good && _size) {
96
0
        _zc_stream->BackUp(_size);
97
0
        _size = 0;
98
0
        _fullsize = 0;
99
0
    }
100
0
}
101
102
0
inline void OutputStream::append(const void* data, int n) {
103
0
    const int saved_n = n;
104
0
    do {
105
0
        if (n <= _size) {
106
0
            fast_memcpy(_data, data, n);
107
0
            _data = (char*)_data + n;
108
0
            _size -= n;
109
0
            _pushed_bytes += saved_n;
110
0
            return;
111
0
        }
112
0
        fast_memcpy(_data, data, _size);
113
0
        data = (const char*)data + _size;
114
0
        n -= _size;
115
0
        if (!_zc_stream->Next(&_data, &_size)) {
116
0
            break;
117
0
        }
118
0
        _fullsize = _size;
119
0
    } while (1);
120
0
    _data = NULL;
121
0
    _size = 0;
122
0
    _fullsize = 0;
123
0
    _pushed_bytes += (saved_n - n);
124
0
    if (n) {
125
0
        set_bad();
126
0
    }
127
0
}
128
129
template <typename T>
130
0
inline void OutputStream::append_packed_pod(const T& packed_pod) {
131
    // if (sizeof(T) <= _size) {
132
    //     *(T*)_data = packed_pod;
133
    //     _data = (char*)_data + sizeof(T);
134
    //     _size -= sizeof(T);
135
    //     _pushed_bytes += sizeof(T);
136
    //     return;
137
    // }
138
0
    return append(&packed_pod, sizeof(T));
139
0
}
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<mcpack2pb::FieldShortHead>(mcpack2pb::FieldShortHead const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<mcpack2pb::FieldLongHead>(mcpack2pb::FieldLongHead const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<mcpack2pb::FieldFixedHead>(mcpack2pb::FieldFixedHead const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<mcpack2pb::ItemsHead>(mcpack2pb::ItemsHead const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<mcpack2pb::ArrayHead>(mcpack2pb::ArrayHead const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<signed char>(signed char const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<short>(short const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<int>(int const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<long>(long const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<unsigned char>(unsigned char const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<unsigned short>(unsigned short const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<unsigned int>(unsigned int const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<unsigned long>(unsigned long const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<bool>(bool const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<float>(float const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<double>(double const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<mcpack2pb::FixedHeadAndValue<signed char> >(mcpack2pb::FixedHeadAndValue<signed char> const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<mcpack2pb::FixedHeadAndValue<short> >(mcpack2pb::FixedHeadAndValue<short> const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<mcpack2pb::FixedHeadAndValue<int> >(mcpack2pb::FixedHeadAndValue<int> const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<mcpack2pb::FixedHeadAndValue<long> >(mcpack2pb::FixedHeadAndValue<long> const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<mcpack2pb::FixedHeadAndValue<unsigned char> >(mcpack2pb::FixedHeadAndValue<unsigned char> const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<mcpack2pb::FixedHeadAndValue<unsigned short> >(mcpack2pb::FixedHeadAndValue<unsigned short> const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<mcpack2pb::FixedHeadAndValue<unsigned int> >(mcpack2pb::FixedHeadAndValue<unsigned int> const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<mcpack2pb::FixedHeadAndValue<unsigned long> >(mcpack2pb::FixedHeadAndValue<unsigned long> const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<mcpack2pb::FixedHeadAndValue<bool> >(mcpack2pb::FixedHeadAndValue<bool> const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<mcpack2pb::FixedHeadAndValue<float> >(mcpack2pb::FixedHeadAndValue<float> const&)
Unexecuted instantiation: void mcpack2pb::OutputStream::append_packed_pod<mcpack2pb::FixedHeadAndValue<double> >(mcpack2pb::FixedHeadAndValue<double> const&)
140
141
0
inline void OutputStream::push_back(char c) {
142
0
    do {
143
0
        if (_size > 0) {
144
0
            *(char*)_data = c;
145
0
            _data = (char*)_data + 1;
146
0
            --_size;
147
0
            ++_pushed_bytes;
148
0
            return;
149
0
        }
150
0
        if (!_zc_stream->Next(&_data, &_size)) {
151
0
            break;
152
0
        }
153
0
        _fullsize = _size;
154
0
    } while (1);
155
0
    _data = NULL;
156
0
    _size = 0;
157
0
    _fullsize = 0;
158
0
    set_bad();
159
0
}
160
161
0
inline void* OutputStream::skip_continuous(int n) {
162
0
    if (_size >= n) {
163
0
        void* ret = _data;
164
0
        _data = (char*)_data + n;
165
0
        _size -= n;
166
0
        _pushed_bytes += n;
167
0
        return ret;
168
0
    }
169
0
    return NULL;
170
0
}
171
172
0
inline OutputStream::Area OutputStream::reserve(int n) {
173
0
    Area area;
174
0
    const int saved_n = n;
175
0
    do {
176
0
        if (n <= _size) {
177
0
            area.add(_data, n);
178
0
            _data = (char*)_data + n;
179
0
            _size -= n;
180
0
            _pushed_bytes += saved_n;
181
0
            return area;
182
0
        }
183
0
        area.add(_data, _size);
184
0
        n -= _size;
185
0
        if (!_zc_stream->Next(&_data, &_size)) {
186
0
            break;
187
0
        }
188
0
        _fullsize = _size;
189
0
    } while (1);
190
0
    _data = NULL;
191
0
    _size = 0;
192
0
    _fullsize = 0;
193
0
    _pushed_bytes += (saved_n - n);
194
0
    if (n) {
195
0
        set_bad();
196
0
    }
197
0
    return area;
198
0
}
199
200
0
inline void OutputStream::assign(const Area& area, const void* data) {
201
0
    area.assign(data);
202
0
}
203
204
0
inline void OutputStream::backup(int n) {
205
0
    if (_fullsize >= _size + n) {
206
0
        _size += n;
207
0
        _data = (char*)_data - n;
208
0
        _pushed_bytes -= n;
209
0
        return;
210
0
    }
211
0
    const int64_t saved_bytecount = _zc_stream->ByteCount();
212
    // Backup the remaining size + what user requests. The implementation
213
    // <= r33563 backups n + _size - _fullsize which is wrong.
214
0
    _zc_stream->BackUp(n + _size);
215
0
    const int64_t nbackup = saved_bytecount - _zc_stream->ByteCount();
216
0
    if (nbackup != n + _size) {
217
0
        CHECK(false) << "Expect output stream backward for " << n + _size
218
0
                     << " bytes, actually " << nbackup << " bytes";
219
0
    }
220
0
    _size = 0;
221
0
    _fullsize = 0;
222
0
    _data = NULL;
223
0
    _pushed_bytes -= n;
224
0
}
225
226
0
inline Serializer::GroupInfo* Serializer::push_group_info() {
227
0
    if (_ndepth + 1 < (int)arraysize(_group_info_fast)) {
228
0
        return &_group_info_fast[++_ndepth];
229
0
    }
230
0
    if (_ndepth >= MAX_DEPTH) {
231
0
        return NULL;
232
0
    }
233
0
    if (_group_info_more == NULL) {
234
0
        _group_info_more =
235
0
            (GroupInfo*)malloc((MAX_DEPTH + 1 - arraysize(_group_info_fast))
236
0
                               * sizeof(GroupInfo));
237
0
        if (_group_info_more == NULL) {
238
0
            return NULL;
239
0
        }
240
0
    }
241
0
    return &_group_info_more[++_ndepth - arraysize(_group_info_fast)];
242
0
}
243
244
0
inline Serializer::GroupInfo& Serializer::peek_group_info() {
245
0
    if (_ndepth < (int)arraysize(_group_info_fast)) {
246
0
        return _group_info_fast[_ndepth];
247
0
    } else {
248
0
        return _group_info_more[_ndepth - arraysize(_group_info_fast)];
249
0
    }
250
0
}
251
252
0
inline void Serializer::add_multiple_int8(const uint8_t* values, size_t count) {
253
0
    return add_multiple_int8((const int8_t*)values, count);
254
0
}
255
0
inline void Serializer::add_multiple_int16(const uint16_t* values, size_t count) {
256
0
    return add_multiple_int16((const int16_t*)values, count);
257
0
}
258
0
inline void Serializer::add_multiple_int32(const uint32_t* values, size_t count) {
259
0
    return add_multiple_int32((const int32_t*)values, count);
260
0
}
261
0
inline void Serializer::add_multiple_int64(const uint64_t* values, size_t count) {
262
0
    return add_multiple_int64((const int64_t*)values, count);
263
0
}
264
265
0
inline void Serializer::add_multiple_uint8(const int8_t* values, size_t count) {
266
0
    return add_multiple_uint8((const uint8_t*)values, count);
267
0
}
268
0
inline void Serializer::add_multiple_uint16(const int16_t* values, size_t count) {
269
0
    return add_multiple_uint16((const uint16_t*)values, count);
270
0
}
271
0
inline void Serializer::add_multiple_uint32(const int32_t* values, size_t count) {
272
0
    return add_multiple_uint32((const uint32_t*)values, count);
273
0
}
274
0
inline void Serializer::add_multiple_uint64(const int64_t* values, size_t count) {
275
0
    return add_multiple_uint64((const uint64_t*)values, count);
276
0
}
277
278
inline void Serializer::begin_mcpack_array(const StringWrapper& name, FieldType item_type)
279
0
{ begin_array_internal(name, item_type, false); }
280
281
inline void Serializer::begin_mcpack_array(FieldType item_type)
282
0
{ begin_array_internal(item_type, false); }
283
284
inline void Serializer::begin_compack_array(const StringWrapper& name, FieldType item_type)
285
0
{ begin_array_internal(name, item_type, true); }
286
287
inline void Serializer::begin_compack_array(FieldType item_type)
288
0
{ begin_array_internal(item_type, true); }
289
290
inline void Serializer::begin_object(const StringWrapper& name)
291
0
{ begin_object_internal(name); }
292
293
inline void Serializer::begin_object()
294
0
{ begin_object_internal(); }
295
296
inline void Serializer::end_object()
297
0
{ end_object_internal(false); }
298
299
inline void Serializer::end_object_iso()
300
0
{ end_object_internal(true); }
301
302
}  // namespace mcpack2pb
303
304
#endif  // MCPACK2PB_MCPACK_SERIALIZER_INL_H