1 // Copyright 2022 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 "pw_containers/intrusive_list.h" 17 18 // 19 // Baseline data container structure. 20 // 21 // This structure is a container for data used to establish a baseline for size 22 // report diffs. 23 // 24 // The baseline contains one global instance of this structure. Other binaries 25 // should contain one global instance of a subclass of this structure, adding 26 // any data that should be a part of the size report diff. 27 // 28 // This structure may be used to load sections of code and data in order to 29 // establish a basline for size report diffs. This structure causes sections to 30 // be loaded by referencing symbols within those sections. 31 // 32 // Simple references to symbols can be optimized out if they don't have any 33 // external effects. The LoadData method uses functions and other symbols to 34 // generate and return a run-time value. In this way, the symbol references 35 // cannot be optimized out. 36 // 37 struct BaseContainer { 38 class BaseItem : public pw::IntrusiveList<BaseItem>::Item {}; 39 BaseContainerBaseContainer40 BaseContainer() { 41 static BaseItem item; 42 GetList().push_front(item); 43 } 44 45 // 46 // Causes code and data sections to be loaded. Returns a generated value 47 // based on symbols in those sections to force them to be loaded. The caller 48 // should ensure that the return value has some external effect (e.g., 49 // returning the value from the "main" function). 50 // LoadDataBaseContainer51 long LoadData() { return reinterpret_cast<long>(this) ^ GetList().size(); } 52 GetListBaseContainer53 static pw::IntrusiveList<BaseItem>& GetList() { 54 static pw::IntrusiveList<BaseItem> list; 55 return list; 56 } 57 58 // Explicit padding. If this structure is empty, the baseline will include 59 // padding that won't appear in subclasses, so the padding is explicitly 60 // added here so it appears in both the baseline and all subclasses. 61 char padding[8]; 62 }; 63