serializer-inl.h 8.6 KB
Newer Older
gejun's avatar
gejun committed
1
// mcpack2pb - Make protobuf be front-end of mcpack/compack
gejun's avatar
gejun committed
2
// Copyright (c) 2015 Baidu, Inc.
gejun's avatar
gejun committed
3 4 5 6 7 8 9 10 11 12 13 14
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
gejun's avatar
gejun committed
15

gejun's avatar
gejun committed
16
// Author: Ge,Jun (gejun@baidu.com)
gejun's avatar
gejun committed
17 18
// Date: Mon Oct 19 17:17:36 CST 2015

gejun's avatar
gejun committed
19 20
#ifndef MCPACK2PB_MCPACK_SERIALIZER_INL_H
#define MCPACK2PB_MCPACK_SERIALIZER_INL_H
gejun's avatar
gejun committed
21 22 23 24 25 26 27 28 29 30 31 32 33

void* fast_memcpy(void *__restrict dest, const void *__restrict src, size_t n);

namespace mcpack2pb {

inline OutputStream::Area::Area(const Area& rhs) 
    : _addr1(rhs._addr1)
    , _addr2(rhs._addr2)
    , _size1(rhs._size1)
    , _size2(rhs._size2)
    , _addional_area(NULL) {

    if (rhs._addional_area) {
34
        _addional_area = new std::vector<butil::StringPiece>(*rhs._addional_area);
gejun's avatar
gejun committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    }
}

inline OutputStream::Area::~Area() {
    if (_addional_area) {
        delete _addional_area;
        _addional_area = NULL;
    }
}

inline OutputStream::Area& OutputStream::Area::operator=(const OutputStream::Area& rhs) {
    if (this == &rhs) {
        return *this;
    }
    this->~Area();
    new (this) Area(rhs);
    return *this;
}

inline void OutputStream::Area::add(void* data, size_t n) {
    if (!data) {
        return;
    }
    if (_addr1 == NULL) {
        _addr1 = data;
        _size1 = n;
    } else if (_addr2 == NULL) {
        _addr2 = data;
        _size2 = n;
    } else {
        if (_addional_area == NULL) {
66
            _addional_area = new std::vector<butil::StringPiece>;
gejun's avatar
gejun committed
67
        }
68
        _addional_area->push_back(butil::StringPiece((const char*)data, n));
gejun's avatar
gejun committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82
    }
}

inline void OutputStream::Area::assign(const void* data) const {
    if (_addr1) {
        fast_memcpy(_addr1, data, _size1);
        if (!_addr2) {
            return;
        }
        fast_memcpy(_addr2, (const char*)data + _size1, _size2);
        if (!_addional_area) {
            return;
        }
        size_t offset = _size1 + _size2;
83
        for (std::vector<butil::StringPiece>::const_iterator iter =
gejun's avatar
gejun committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
                _addional_area->begin(); iter != _addional_area->end(); ++iter) {
            fast_memcpy((void*)iter->data(), (const char*)data + offset, iter->size());
            offset += iter->size();
        }
    }
}

inline void OutputStream::done() {
    if (_good && _size) {
        _zc_stream->BackUp(_size);
        _size = 0;
        _fullsize = 0;
    }
}

inline void OutputStream::append(const void* data, int n) {
    const int saved_n = n;
    do {
        if (n <= _size) {
            fast_memcpy(_data, data, n);
            _data = (char*)_data + n;
            _size -= n;
            _pushed_bytes += saved_n;
            return;
        }
        fast_memcpy(_data, data, _size);
        data = (const char*)data + _size;
        n -= _size;
        if (!_zc_stream->Next(&_data, &_size)) {
            break;
        }
        _fullsize = _size;
    } while (1);
    _data = NULL;
    _size = 0;
    _fullsize = 0;
    _pushed_bytes += (saved_n - n);
    if (n) {
        set_bad();
    }
}

template <typename T>
inline void OutputStream::append_packed_pod(const T& packed_pod) {
    // if (sizeof(T) <= _size) {
    //     *(T*)_data = packed_pod;
    //     _data = (char*)_data + sizeof(T);
    //     _size -= sizeof(T);
    //     _pushed_bytes += sizeof(T);
    //     return;
    // }
    return append(&packed_pod, sizeof(T));
}

inline void OutputStream::push_back(char c) {
    do {
        if (_size > 0) {
            *(char*)_data = c;
            _data = (char*)_data + 1;
            --_size;
            ++_pushed_bytes;
            return;
        }
        if (!_zc_stream->Next(&_data, &_size)) {
            break;
        }
        _fullsize = _size;
    } while (1);
    _data = NULL;
    _size = 0;
    _fullsize = 0;
    set_bad();
}

inline void* OutputStream::skip_continuous(int n) {
    if (_size >= n) {
        void* ret = _data;
        _data = (char*)_data + n;
        _size -= n;
        _pushed_bytes += n;
        return ret;
    }
    return NULL;
}

inline OutputStream::Area OutputStream::reserve(int n) {
    Area area;
    const int saved_n = n;
    do {
        if (n <= _size) {
            area.add(_data, n);
            _data = (char*)_data + n;
            _size -= n;
            _pushed_bytes += saved_n;
            return area;
        }
        area.add(_data, _size);
        n -= _size;
        if (!_zc_stream->Next(&_data, &_size)) {
            break;
        }
        _fullsize = _size;
    } while (1);
    _data = NULL;
    _size = 0;
    _fullsize = 0;
    _pushed_bytes += (saved_n - n);
    if (n) {
        set_bad();
    }
    return area;
}

inline void OutputStream::assign(const Area& area, const void* data) {
    area.assign(data);
}

inline void OutputStream::backup(int n) {
    if (_fullsize >= _size + n) {
        _size += n;
        _data = (char*)_data - n;
        _pushed_bytes -= n;
        return;
    }
    const int64_t saved_bytecount = _zc_stream->ByteCount();
    // Backup the remaining size + what user requests. The implementation
    // <= r33563 backups n + _size - _fullsize which is wrong.
    _zc_stream->BackUp(n + _size);
    const int64_t nbackup = saved_bytecount - _zc_stream->ByteCount();
    if (nbackup != n + _size) {
        CHECK(false) << "Expect output stream backward for " << n + _size
gejun's avatar
gejun committed
215
                     << " bytes, actually " << nbackup << " bytes";
gejun's avatar
gejun committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
    }
    _size = 0;
    _fullsize = 0;
    _data = NULL;
    _pushed_bytes -= n;
}

inline Serializer::GroupInfo* Serializer::push_group_info() {
    if (_ndepth + 1 < (int)arraysize(_group_info_fast)) {
        return &_group_info_fast[++_ndepth];
    }
    if (_ndepth >= MAX_DEPTH) {
        return NULL;
    }
    if (_group_info_more == NULL) {
        _group_info_more =
            (GroupInfo*)malloc((MAX_DEPTH + 1 - arraysize(_group_info_fast))
                               * sizeof(GroupInfo));
        if (_group_info_more == NULL) {
            return NULL;
        }
    }
    return &_group_info_more[++_ndepth - arraysize(_group_info_fast)];
}

inline Serializer::GroupInfo& Serializer::peek_group_info() {
    if (_ndepth < (int)arraysize(_group_info_fast)) {
        return _group_info_fast[_ndepth];
    } else {
        return _group_info_more[_ndepth - arraysize(_group_info_fast)];
    }
}

inline void Serializer::add_multiple_int8(const uint8_t* values, size_t count) {
    return add_multiple_int8((const int8_t*)values, count);
}
inline void Serializer::add_multiple_int16(const uint16_t* values, size_t count) {
    return add_multiple_int16((const int16_t*)values, count);
}
inline void Serializer::add_multiple_int32(const uint32_t* values, size_t count) {
    return add_multiple_int32((const int32_t*)values, count);
}
inline void Serializer::add_multiple_int64(const uint64_t* values, size_t count) {
    return add_multiple_int64((const int64_t*)values, count);
}

inline void Serializer::add_multiple_uint8(const int8_t* values, size_t count) {
    return add_multiple_uint8((const uint8_t*)values, count);
}
inline void Serializer::add_multiple_uint16(const int16_t* values, size_t count) {
    return add_multiple_uint16((const uint16_t*)values, count);
}
inline void Serializer::add_multiple_uint32(const int32_t* values, size_t count) {
    return add_multiple_uint32((const uint32_t*)values, count);
}
inline void Serializer::add_multiple_uint64(const int64_t* values, size_t count) {
    return add_multiple_uint64((const uint64_t*)values, count);
}

inline void Serializer::begin_mcpack_array(const StringWrapper& name, FieldType item_type)
{ begin_array_internal(name, item_type, false); }

inline void Serializer::begin_mcpack_array(FieldType item_type)
{ begin_array_internal(item_type, false); }

inline void Serializer::begin_compack_array(const StringWrapper& name, FieldType item_type)
{ begin_array_internal(name, item_type, true); }

inline void Serializer::begin_compack_array(FieldType item_type)
{ begin_array_internal(item_type, true); }

inline void Serializer::begin_object(const StringWrapper& name)
{ begin_object_internal(name); }

inline void Serializer::begin_object()
{ begin_object_internal(); }

inline void Serializer::end_object()
{ end_object_internal(false); }

inline void Serializer::end_object_iso()
{ end_object_internal(true); }

}  // namespace mcpack2pb

gejun's avatar
gejun committed
301
#endif  // MCPACK2PB_MCPACK_SERIALIZER_INL_H