YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/coding.cc
Line
Count
Source
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
//  This source code is licensed under the BSD-style license found in the
3
//  LICENSE file in the root directory of this source tree. An additional grant
4
//  of patent rights can be found in the PATENTS file in the same directory.
5
//
6
// The following only applies to changes made to this file as part of YugaByte development.
7
//
8
// Portions Copyright (c) YugaByte, Inc.
9
//
10
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
11
// in compliance with the License.  You may obtain a copy of the License at
12
//
13
// http://www.apache.org/licenses/LICENSE-2.0
14
//
15
// Unless required by applicable law or agreed to in writing, software distributed under the License
16
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
17
// or implied.  See the License for the specific language governing permissions and limitations
18
// under the License.
19
//
20
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
21
// Use of this source code is governed by a BSD-style license that can be
22
// found in the LICENSE file. See the AUTHORS file for names of contributors.
23
24
#include "yb/rocksdb/util/coding.h"
25
26
#include <algorithm>
27
#include "yb/util/slice.h"
28
29
namespace rocksdb {
30
31
1.79G
char* EncodeVarint32(char* dst, uint32_t v) {
32
  // Operate on characters as unsigneds
33
1.79G
  unsigned char* ptr = reinterpret_cast<unsigned char*>(dst);
34
1.79G
  static const int B = 128;
35
1.79G
  if (v < (1 << 7)) {
36
1.74G
    *(ptr++) = v;
37
1.74G
  } else 
if (49.9M
v < (1 << 14)49.9M
) {
38
49.9M
    *(ptr++) = v | B;
39
49.9M
    *(ptr++) = v >> 7;
40
49.9M
  } else 
if (29.2k
v < (1 << 21)29.2k
) {
41
79.2k
    *(ptr++) = v | B;
42
79.2k
    *(ptr++) = (v >> 7) | B;
43
79.2k
    *(ptr++) = v >> 14;
44
18.4E
  } else if (v < (1 << 28)) {
45
67.7k
    *(ptr++) = v | B;
46
67.7k
    *(ptr++) = (v >> 7) | B;
47
67.7k
    *(ptr++) = (v >> 14) | B;
48
67.7k
    *(ptr++) = v >> 21;
49
18.4E
  } else {
50
18.4E
    *(ptr++) = v | B;
51
18.4E
    *(ptr++) = (v >> 7) | B;
52
18.4E
    *(ptr++) = (v >> 14) | B;
53
18.4E
    *(ptr++) = (v >> 21) | B;
54
18.4E
    *(ptr++) = v >> 28;
55
18.4E
  }
56
1.79G
  return reinterpret_cast<char*>(ptr);
57
1.79G
}
58
59
const char* GetVarint32PtrFallback(const char* p, const char* limit,
60
339M
                                   uint32_t* value) {
61
339M
  uint32_t result = 0;
62
679M
  for (uint32_t shift = 0; shift <= 28 && 
p < limit678M
;
shift += 7340M
) {
63
679M
    uint32_t byte = *(reinterpret_cast<const unsigned char*>(p));
64
679M
    p++;
65
679M
    if (byte & 128) {
66
      // More bytes are present
67
340M
      result |= ((byte & 127) << shift);
68
340M
    } else {
69
338M
      result |= (byte << shift);
70
338M
      *value = result;
71
338M
      return reinterpret_cast<const char*>(p);
72
338M
    }
73
679M
  }
74
337k
  return nullptr;
75
339M
}
76
77
134M
const char* GetVarint64PtrFallback(const char* p, const char* limit, uint64_t* value) {
78
134M
  uint64_t result = 0;
79
343M
  for (uint32_t shift = 0; shift <= 63 && 
p < limit343M
;
shift += 7209M
) {
80
343M
    uint64_t byte = *(reinterpret_cast<const unsigned char*>(p));
81
343M
    p++;
82
343M
    if (byte & 128) {
83
      // More bytes are present
84
209M
      result |= ((byte & 127) << shift);
85
209M
    } else {
86
134M
      result |= (byte << shift);
87
134M
      *value = result;
88
134M
      return reinterpret_cast<const char*>(p);
89
134M
    }
90
343M
  }
91
12.0k
  return nullptr;
92
134M
}
93
94
}  // namespace rocksdb