varint.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_VARINT_H
20 #define GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_VARINT_H
21 
23 
24 #include <stdint.h>
25 
26 /* Helpers for hpack varint encoding */
27 
28 namespace grpc_core {
29 
30 /* maximum value that can be bitpacked with the opcode if the opcode has a
31  prefix of length prefix_bits */
32 constexpr uint32_t MaxInVarintPrefix(uint8_t prefix_bits) {
33  return (1 << (8 - prefix_bits)) - 1;
34 }
35 
36 /* length of a value that needs varint tail encoding (it's bigger than can be
37  bitpacked into the opcode byte) - returned value includes the length of the
38  opcode byte */
39 uint32_t VarintLength(uint32_t tail_value);
40 void VarintWriteTail(uint32_t tail_value, uint8_t* target,
41  uint32_t tail_length);
42 
43 template <uint8_t kPrefixBits>
44 class VarintWriter {
45  public:
46  static constexpr uint32_t kMaxInPrefix = MaxInVarintPrefix(kPrefixBits);
47 
49  : value_(value),
51  }
52 
53  uint32_t value() const { return value_; }
54  uint32_t length() const { return length_; }
55 
56  void Write(uint8_t prefix, uint8_t* target) const {
57  if (length_ == 1) {
58  target[0] = prefix | value_;
59  } else {
60  target[0] = prefix | kMaxInPrefix;
62  }
63  }
64 
65  private:
67  // length required to bitpack value_
69 };
70 
71 } // namespace grpc_core
72 
73 #endif /* GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_VARINT_H */
grpc_core::VarintWriteTail
void VarintWriteTail(uint32_t tail_value, uint8_t *target, uint32_t tail_length)
Definition: varint.cc:41
grpc_core
Definition: call_metric_recorder.h:31
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
grpc_core::MaxInVarintPrefix
constexpr uint32_t MaxInVarintPrefix(uint8_t prefix_bits)
Definition: varint.h:32
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
grpc_core::VarintWriter::value_
const uint32_t value_
Definition: varint.h:66
grpc_core::VarintWriter::length_
const uint32_t length_
Definition: varint.h:68
grpc_core::VarintWriter::Write
void Write(uint8_t prefix, uint8_t *target) const
Definition: varint.h:56
stdint.h
grpc_core::VarintWriter::value
uint32_t value() const
Definition: varint.h:53
grpc_core::VarintLength
uint32_t VarintLength(uint32_t tail_value)
Definition: varint.cc:27
grpc_core::VarintWriter::length
uint32_t length() const
Definition: varint.h:54
grpc_core::VarintWriter::VarintWriter
VarintWriter(uint32_t value)
Definition: varint.h:48
grpc_core::VarintWriter
Definition: varint.h:44
prefix
static const char prefix[]
Definition: head_of_line_blocking.cc:28
grpc_core::VarintWriter::kMaxInPrefix
static constexpr uint32_t kMaxInPrefix
Definition: varint.h:46
setup.target
target
Definition: third_party/bloaty/third_party/protobuf/python/setup.py:179
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:50