/Users/deen/code/yugabyte-db/src/yb/util/coding-inl.h
Line | Count | Source (jump to first uncovered line) |
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 | | // Some portions Copyright (c) 2011 The LevelDB Authors. |
18 | | // |
19 | | // The following only applies to changes made to this file as part of YugaByte development. |
20 | | // |
21 | | // Portions Copyright (c) YugaByte, Inc. |
22 | | // |
23 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
24 | | // in compliance with the License. You may obtain a copy of the License at |
25 | | // |
26 | | // http://www.apache.org/licenses/LICENSE-2.0 |
27 | | // |
28 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
29 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
30 | | // or implied. See the License for the specific language governing permissions and limitations |
31 | | // under the License. |
32 | | // |
33 | | // Endian-neutral encoding: |
34 | | // * Fixed-length numbers are encoded with least-significant byte first |
35 | | // * In addition we support variable length "varint" encoding |
36 | | // * Strings are encoded prefixed by their length in varint format |
37 | | |
38 | | #ifndef YB_UTIL_CODING_INL_H |
39 | | #define YB_UTIL_CODING_INL_H |
40 | | |
41 | | #include <stdint.h> |
42 | | #include <string.h> |
43 | | |
44 | | namespace yb { |
45 | | |
46 | 0 | inline uint8_t *InlineEncodeVarint32(uint8_t *dst, uint32_t v) { |
47 | | // Operate on characters as unsigneds |
48 | 0 | uint8_t *ptr = dst; |
49 | 0 | static const int B = 128; |
50 | 0 | if (v < (1<<7)) { |
51 | 0 | *(ptr++) = v; |
52 | 0 | } else if (v < (1<<14)) { |
53 | 0 | *(ptr++) = v | B; |
54 | 0 | *(ptr++) = v>>7; |
55 | 0 | } else if (v < (1<<21)) { |
56 | 0 | *(ptr++) = v | B; |
57 | 0 | *(ptr++) = (v>>7) | B; |
58 | 0 | *(ptr++) = v>>14; |
59 | 0 | } else if (v < (1<<28)) { |
60 | 0 | *(ptr++) = v | B; |
61 | 0 | *(ptr++) = (v>>7) | B; |
62 | 0 | *(ptr++) = (v>>14) | B; |
63 | 0 | *(ptr++) = v>>21; |
64 | 0 | } else { |
65 | 0 | *(ptr++) = v | B; |
66 | 0 | *(ptr++) = (v>>7) | B; |
67 | 0 | *(ptr++) = (v>>14) | B; |
68 | 0 | *(ptr++) = (v>>21) | B; |
69 | 0 | *(ptr++) = v>>28; |
70 | 0 | } |
71 | 0 | return ptr; |
72 | 0 | } |
73 | | |
74 | 85.9M | inline void InlineEncodeFixed32(uint8_t *buf, uint32_t value) { |
75 | 85.9M | #if __BYTE_ORDER == __LITTLE_ENDIAN |
76 | 85.9M | memcpy(buf, &value, sizeof(value)); |
77 | | #else |
78 | | buf[0] = value & 0xff; |
79 | | buf[1] = (value >> 8) & 0xff; |
80 | | buf[2] = (value >> 16) & 0xff; |
81 | | buf[3] = (value >> 24) & 0xff; |
82 | | #endif |
83 | 85.9M | } |
84 | | |
85 | 0 | inline void InlineEncodeFixed64(uint8_t *buf, uint64_t value) { |
86 | 0 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
87 | 0 | memcpy(buf, &value, sizeof(value)); |
88 | | #else |
89 | | buf[0] = value & 0xff; |
90 | | buf[1] = (value >> 8) & 0xff; |
91 | | buf[2] = (value >> 16) & 0xff; |
92 | | buf[3] = (value >> 24) & 0xff; |
93 | | buf[4] = (value >> 32) & 0xff; |
94 | | buf[5] = (value >> 40) & 0xff; |
95 | | buf[6] = (value >> 48) & 0xff; |
96 | | buf[7] = (value >> 56) & 0xff; |
97 | | #endif |
98 | 0 | } |
99 | | |
100 | | |
101 | | // Standard Put... routines append to a string |
102 | | template <class StrType> |
103 | 245k | inline void InlinePutFixed32(StrType *dst, uint32_t value) { |
104 | 245k | uint8_t buf[sizeof(value)]; |
105 | 245k | InlineEncodeFixed32(buf, value); |
106 | 245k | dst->append(buf, sizeof(buf)); |
107 | 245k | } |
108 | | |
109 | | template <class StrType> |
110 | 0 | inline void InlinePutFixed64(StrType *dst, uint64_t value) { |
111 | 0 | uint8_t buf[sizeof(value)]; |
112 | 0 | InlineEncodeFixed64(buf, value); |
113 | 0 | dst->append(buf, sizeof(buf)); |
114 | 0 | } |
115 | | |
116 | | template <class StrType> |
117 | | inline void InlinePutVarint32(StrType* dst, uint32_t v) { |
118 | | // We resize the array and then size it back down as appropriate |
119 | | // rather than using append(), since the generated code ends up |
120 | | // being substantially shorter. |
121 | | int old_size = dst->size(); |
122 | | dst->resize(old_size + 5); |
123 | | uint8_t* p = &(*dst)[old_size]; |
124 | | uint8_t *ptr = InlineEncodeVarint32(p, v); |
125 | | |
126 | | dst->resize(old_size + ptr - p); |
127 | | } |
128 | | |
129 | | } // namespace yb |
130 | | |
131 | | #endif |