/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 | 832M | char* EncodeVarint32(char* dst, uint32_t v) { |
32 | | // Operate on characters as unsigneds |
33 | 832M | unsigned char* ptr = reinterpret_cast<unsigned char*>(dst); |
34 | 832M | static const int B = 128; |
35 | 832M | if (v < (1 << 7)) { |
36 | 786M | *(ptr++) = v; |
37 | 46.5M | } else if (v < (1 << 14)) { |
38 | 46.5M | *(ptr++) = v | B; |
39 | 46.5M | *(ptr++) = v >> 7; |
40 | 18.4E | } else if (v < (1 << 21)) { |
41 | 27.6k | *(ptr++) = v | B; |
42 | 27.6k | *(ptr++) = (v >> 7) | B; |
43 | 27.6k | *(ptr++) = v >> 14; |
44 | 18.4E | } else if (v < (1 << 28)) { |
45 | 67.9k | *(ptr++) = v | B; |
46 | 67.9k | *(ptr++) = (v >> 7) | B; |
47 | 67.9k | *(ptr++) = (v >> 14) | B; |
48 | 67.9k | *(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 | 832M | return reinterpret_cast<char*>(ptr); |
57 | 832M | } |
58 | | |
59 | | const char* GetVarint32PtrFallback(const char* p, const char* limit, |
60 | 164M | uint32_t* value) { |
61 | 164M | uint32_t result = 0; |
62 | 329M | for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) { |
63 | 329M | uint32_t byte = *(reinterpret_cast<const unsigned char*>(p)); |
64 | 329M | p++; |
65 | 329M | if (byte & 128) { |
66 | | // More bytes are present |
67 | 164M | result |= ((byte & 127) << shift); |
68 | 164M | } else { |
69 | 164M | result |= (byte << shift); |
70 | 164M | *value = result; |
71 | 164M | return reinterpret_cast<const char*>(p); |
72 | 164M | } |
73 | 329M | } |
74 | 18.4E | return nullptr; |
75 | 164M | } |
76 | | |
77 | 67.2M | const char* GetVarint64PtrFallback(const char* p, const char* limit, uint64_t* value) { |
78 | 67.2M | uint64_t result = 0; |
79 | 154M | for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) { |
80 | 154M | uint64_t byte = *(reinterpret_cast<const unsigned char*>(p)); |
81 | 154M | p++; |
82 | 154M | if (byte & 128) { |
83 | | // More bytes are present |
84 | 87.2M | result |= ((byte & 127) << shift); |
85 | 67.2M | } else { |
86 | 67.2M | result |= (byte << shift); |
87 | 67.2M | *value = result; |
88 | 67.2M | return reinterpret_cast<const char*>(p); |
89 | 67.2M | } |
90 | 154M | } |
91 | 18.0k | return nullptr; |
92 | 67.2M | } |
93 | | |
94 | | } // namespace rocksdb |