1 // Copyright 2025 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 #pragma once
15
16 #include <array>
17 #include <cstddef>
18 #include <cstdint>
19 #include <utility>
20
21 #include "pw_bloat/bloat_this_binary.h"
22
23 namespace pw::containers::size_report {
24
25 static constexpr size_t kNumItems = 10;
26
27 // A few type aliases for convenience in the size reports.
28 using K1 = uint32_t;
29 using K2 = uint16_t;
30 using V1 = uint32_t;
31 using V2 = uint64_t;
32
33 /// Returns a reference to a std::array of statically-allocated items.
34 template <typename T>
GetItems()35 std::array<T, kNumItems>& GetItems() {
36 static std::array<T, kNumItems> items = {
37 T(0), T(1), T(2), T(3), T(4), T(5), T(6), T(7), T(8), T(9)};
38 return items;
39 }
40
41 /// Returns a reference to a std::array of statically-allocated pairs of keys
42 /// and values.
43 template <typename PairType>
GetPairs()44 std::array<PairType, kNumItems>& GetPairs() {
45 static std::array<PairType, kNumItems> pairs = {
46 PairType{0, 1},
47 PairType{1, 1},
48 PairType{2, 2},
49 PairType{3, 3},
50 PairType{4, 5},
51 PairType{5, 8},
52 PairType{6, 13},
53 PairType{7, 21},
54 PairType{8, 34},
55 PairType{9, 55},
56 };
57 return pairs;
58 }
59
60 /// Returns a statically allocated container of the given type.
61 template <typename Container>
GetContainer()62 Container& GetContainer() {
63 static Container container;
64 return container;
65 }
66
67 /// Returns a statically allocated container of the given type, constructed with
68 /// the provided arguments.
69 template <typename Container, typename Args>
GetContainer(const Args & args)70 Container& GetContainer(const Args& args) {
71 static Container container(args);
72 return container;
73 }
74
75 /// Measures the size of common functions and data without any containers.
SetBaseline(uint32_t mask)76 uint32_t SetBaseline(uint32_t mask) {
77 pw::bloat::BloatThisBinary();
78 PW_BLOAT_COND(!GetItems<K1>().empty(), mask);
79 PW_BLOAT_COND(!GetItems<K2>().empty(), mask);
80 PW_BLOAT_COND(!GetItems<V1>().empty(), mask);
81 PW_BLOAT_COND(!GetItems<V2>().empty(), mask);
82 return mask;
83 }
84
85 /// Invokes methods common to all containers.
86 template <typename Container>
MeasureContainer(Container & container,uint32_t mask)87 uint32_t MeasureContainer(Container& container, uint32_t mask) {
88 PW_BLOAT_COND(container.empty(), mask);
89 PW_BLOAT_COND(container.size() <= container.max_size(), mask);
90 return mask;
91 }
92
93 } // namespace pw::containers::size_report
94