1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://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, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef COMMON_COMPONENTS_COMMON_RUN_TYPE_H 17 #define COMMON_COMPONENTS_COMMON_RUN_TYPE_H 18 19 #include "common_components/log/log.h" 20 21 namespace common { 22 // slot with size no more than RUN_ALLOC_SMALL_SIZE is small slot. 23 // small slot is allocated from cache run in thread-local buffer first. 24 // slot with size (RUN_ALLOC_SMALL_SIZE, RUN_ALLOC_LARGE_SIZE] is allocated from global buffer. 25 constexpr size_t RUN_ALLOC_SMALL_SIZE = 104; 26 // slot with size larger than RUN_ALLOC_LARGE_SIZE is large slot. 27 constexpr size_t RUN_ALLOC_LARGE_SIZE = 2016; 28 29 // this is a short cut of RUNTYPE_SIZE_TO_RUN_IDX, only works under certain configs, see TYPES def 30 #define RUNTYPE_FAST_RUN_IDX(size) (((size) >> 3) - 2) 31 32 struct RunType { 33 public: 34 // this supports a maximum of (256 * 8 == 2048 byte) run 35 // we need to extend this if we want to config multiple-page run 36 static constexpr uint32_t MAX_NUM_OF_RUN_TYPES = 256; 37 38 // REMEMBER TO CHANGE THIS WHEN YOU ADD/REMOVE CONFIGS 39 static constexpr uint32_t NUM_OF_RUN_TYPES = 53; 40 41 static constexpr uint32_t NUM_OF_LOCAL_TYPES = RUNTYPE_FAST_RUN_IDX(RUN_ALLOC_SMALL_SIZE) + 1; 42 43 // REMEMBER TO CHANGE NUM_OF_RUN_TYPES WHEN YOU ADD/REMOVE CONFIGS 44 // this stores a config for each kind of run (represented by an index) 45 static const RunType TYPES[NUM_OF_RUN_TYPES]; 46 47 // this map maps a size ((size >> 3 - 1) to be precise) to a run config 48 // this map takes 4 * MAX_NUM_OF_RUN_TYPES == 1k 49 static uint32_t g_size2Idx[MAX_NUM_OF_RUN_TYPES]; // all zero-initialised 50 51 const bool isSmall; // this kind of run is composed of small-sized slots. 52 53 const uint8_t numPagesPerRun; // pages per run 54 55 const uint32_t size; // slot size of this kind of run 56 57 static void InitRunTypeMap(); 58 }; 59 60 // assume(size <= (TYPES[N_RUN_CONFIGS - 1].size << 3)) 61 #define RUNTYPE_SIZE_TO_RUN_IDX(size) RunType::g_size2Idx[((size) >> 3) - 1] 62 63 #define RUNTYPE_RUN_IDX_TO_SIZE(idx) (RunType::TYPES[(idx)].size) 64 65 constexpr int DEFAULT_PAGE_PER_RUN = 1; 66 } // namespace common 67 68 #endif // COMMON_COMPONENTS_COMMON_RUN_TYPE_H 69