YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/coding.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style license that can be
3
// found in the LICENSE file. See the AUTHORS file for names of contributors.
4
//
5
// The following only applies to changes made to this file as part of YugaByte development.
6
//
7
// Portions Copyright (c) YugaByte, Inc.
8
//
9
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
10
// in compliance with the License.  You may obtain a copy of the License at
11
//
12
// http://www.apache.org/licenses/LICENSE-2.0
13
//
14
// Unless required by applicable law or agreed to in writing, software distributed under the License
15
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
16
// or implied.  See the License for the specific language governing permissions and limitations
17
// under the License.
18
//
19
// Endian-neutral encoding:
20
// * Fixed-length numbers are encoded with least-significant byte first
21
// * In addition we support variable length "varint" encoding
22
// * Strings are encoded prefixed by their length in varint format
23
24
#ifndef YB_UTIL_CODING_H
25
#define YB_UTIL_CODING_H
26
27
#include <stdint.h>
28
#include <string.h>
29
30
#include <string>
31
32
#include <boost/container/small_vector.hpp>
33
34
#include "yb/util/faststring.h"
35
#include "yb/util/slice.h"
36
37
namespace yb {
38
39
extern void PutFixed32(faststring* dst, uint32_t value);
40
extern void PutFixed64(faststring* dst, uint64_t value);
41
extern void PutVarint32(faststring* dst, uint32_t value);
42
extern void PutVarint64(faststring* dst, uint64_t value);
43
extern void PutVarint64(boost::container::small_vector_base<uint8_t>* dst, uint64_t value);
44
45
// Put a length-prefixed Slice into the buffer. The length prefix
46
// is varint-encoded.
47
extern void PutLengthPrefixedSlice(faststring* dst, const Slice& value);
48
49
// Put a length-prefixed Slice into the buffer. The length prefix
50
// is 32-bit fixed encoded in little endian.
51
extern void PutFixed32LengthPrefixedSlice(faststring* dst, const Slice& value);
52
53
// Standard Get... routines parse a value from the beginning of a Slice
54
// and advance the slice past the parsed value.
55
extern bool GetVarint32(Slice* input, uint32_t* value);
56
extern bool GetVarint64(Slice* input, uint64_t* value);
57
extern bool GetLengthPrefixedSlice(Slice* input, Slice* result);
58
59
// Pointer-based variants of GetVarint...  These either store a value
60
// in *v and return a pointer just past the parsed value, or return
61
// NULL on error.  These routines only look at bytes in the range
62
// [p..limit-1]
63
extern const uint8_t *GetVarint32Ptr(const uint8_t *p, const uint8_t *limit, uint32_t* v);
64
extern const uint8_t *GetVarint64Ptr(const uint8_t *p, const uint8_t *limit, uint64_t* v);
65
66
// Returns the length of the varint32 or varint64 encoding of "v"
67
extern int VarintLength(uint64_t v);
68
69
// Lower-level versions of Put... that write directly into a character buffer
70
// REQUIRES: dst has enough space for the value being written
71
extern void EncodeFixed32(uint8_t *dst, uint32_t value);
72
extern void EncodeFixed64(uint8_t *dst, uint64_t value);
73
74
// Lower-level versions of Put... that write directly into a character buffer
75
// and return a pointer just past the last byte written.
76
// REQUIRES: dst has enough space for the value being written
77
extern uint8_t *EncodeVarint32(uint8_t *dst, uint32_t value);
78
extern uint8_t *EncodeVarint64(uint8_t *dst, uint64_t value);
79
80
// Lower-level versions of Get... that read directly from a character buffer
81
// without any bounds checking.
82
83
8.06M
inline uint32_t DecodeFixed32(const uint8_t *ptr) {
84
8.06M
#if __BYTE_ORDER == __LITTLE_ENDIAN
85
    // Load the raw bytes
86
8.06M
    uint32_t result;
87
8.06M
    memcpy(&result, ptr, sizeof(result));  // gcc optimizes this to a plain load
88
8.06M
    return result;
89
#else
90
    return ((static_cast<uint32_t>(static_cast<unsigned char>(ptr[0])))
91
        | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[1])) << 8)
92
        | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[2])) << 16)
93
        | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[3])) << 24));
94
#endif
95
8.06M
}
96
97
0
inline uint64_t DecodeFixed64(const uint8_t *ptr) {
98
0
#if __BYTE_ORDER == __LITTLE_ENDIAN
99
0
    // Load the raw bytes
100
0
    uint64_t result;
101
0
    memcpy(&result, ptr, sizeof(result));  // gcc optimizes this to a plain load
102
0
    return result;
103
0
#else
104
0
    uint64_t lo = DecodeFixed32(ptr);
105
0
    uint64_t hi = DecodeFixed32(ptr + 4);
106
0
    return (hi << 32) | lo;
107
0
#endif
108
0
}
109
110
// Internal routine for use by fallback path of GetVarint32Ptr
111
extern const uint8_t *GetVarint32PtrFallback(const uint8_t *p,
112
                                             const uint8_t *limit,
113
                                             uint32_t* value);
114
inline const uint8_t *GetVarint32Ptr(const uint8_t *p,
115
                                     const uint8_t *limit,
116
0
                                     uint32_t* value) {
117
0
  if (PREDICT_TRUE(p < limit)) {
118
0
    uint32_t result = *p;
119
0
    if (PREDICT_TRUE((result & 128) == 0)) {
120
0
      *value = result;
121
0
      return p + 1;
122
0
    }
123
0
  }
124
0
  return GetVarint32PtrFallback(p, limit, value);
125
0
}
Unexecuted instantiation: _ZN2yb14GetVarint32PtrEPKhS1_Pj
Unexecuted instantiation: _ZN2yb14GetVarint32PtrEPKhS1_Pj
126
127
}  // namespace yb
128
129
#endif  // YB_UTIL_CODING_H