• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 PANDA_LIBPANDABASE_UTILS_ALIGNED_STORAGE_H_
17 #define PANDA_LIBPANDABASE_UTILS_ALIGNED_STORAGE_H_
18 
19 #include "libpandabase/utils/bit_utils.h"
20 
21 namespace panda {
22 
23 /**
24  * Aligned storage with aligned elements.
25  *
26  * @tparam StructAlign Alignment of the structure
27  * @tparam ElementsAlign Alignment of the elements
28  * @tparam ElementsNum Number of elements in the structure, used for static checkers
29  */
30 template <size_t StructAlign, size_t ElementsAlign, size_t ElementsNum>
31 struct alignas(StructAlign) AlignedStorage {
32     template <typename T>
33     using Aligned __attribute__((aligned(ElementsAlign))) = T;
34 
35     static constexpr size_t STRUCT_ALIGN = StructAlign;
36     static constexpr size_t ELEMENTS_ALIGN = ElementsAlign;
37     static constexpr size_t ELEMENTS_NUM = ElementsNum;
38 
GetSizeAlignedStorage39     static constexpr size_t GetSize()
40     {
41         return RoundUp(ElementsNum * ElementsAlign, StructAlign);
42     }
43 
ConvertOffsetAlignedStorage44     static constexpr size_t ConvertOffset(size_t dst_align, size_t offset)
45     {
46         return (dst_align > ElementsAlign) ? (offset / (dst_align / ElementsAlign))
47                                            : (offset / (ElementsAlign / dst_align));
48     }
49 };
50 
51 }  // namespace panda
52 
53 #endif  // PANDA_LIBPANDABASE_UTILS_ALIGNED_STORAGE_H_
54