• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 // Configuration macros for the protobuf module.
16 #pragma once
17 
18 #include <cstddef>
19 
20 // When encoding nested messages, the number of bytes to reserve for the varint
21 // submessage length. Nested messages are limited in size to the maximum value
22 // that can be varint-encoded into this reserved space.
23 //
24 // The values that can be set, and their corresponding maximum submessage
25 // lengths, are outlined below.
26 //
27 //   1 byte  => 127
28 //   2 bytes => 16,383 or < 16KiB
29 //   3 bytes => 2,097,151 or < 2048KiB
30 //   4 bytes => 268,435,455 or < 256MiB
31 //   5 bytes => 4,294,967,295 or < 4GiB (max uint32_t)
32 //
33 #ifndef PW_PROTOBUF_CFG_MAX_VARINT_SIZE
34 #define PW_PROTOBUF_CFG_MAX_VARINT_SIZE 4
35 #endif  // PW_PROTOBUF_MAX_VARINT_SIZE
36 
37 static_assert(PW_PROTOBUF_CFG_MAX_VARINT_SIZE > 0 &&
38               PW_PROTOBUF_CFG_MAX_VARINT_SIZE <= 5);
39 
40 namespace pw::protobuf::config {
41 
42 inline constexpr size_t kMaxVarintSize = PW_PROTOBUF_CFG_MAX_VARINT_SIZE;
43 
44 // TODO(frolv): This converts the configured varint length to the legacy encoder
45 // SizeType. Remove this with the encoder rewrite.
46 #if PW_PROTOBUF_CFG_MAX_VARINT_SIZE == 1
47 using SizeType = uint8_t;
48 #elif PW_PROTOBUF_CFG_MAX_VARINT_SIZE == 2
49 using SizeType = uint16_t;
50 #elif PW_PROTOBUF_CFG_MAX_VARINT_SIZE <= 4
51 using SizeType = uint32_t;
52 #else
53 using SizeType = uint64_t;
54 #endif
55 
56 }  // namespace pw::protobuf::config
57