/Users/deen/code/yugabyte-db/src/yb/util/alignment.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | // Macros for dealing with memory alignment. |
33 | | #ifndef YB_UTIL_ALIGNMENT_H |
34 | | #define YB_UTIL_ALIGNMENT_H |
35 | | |
36 | | #include <cstddef> |
37 | | |
38 | | namespace yb { |
39 | | |
40 | | // Round down 'x' to the nearest 'align' boundary |
41 | | #define YB_ALIGN_DOWN(x, align) ((x) & (-(align))) |
42 | | |
43 | | template<class T> |
44 | | class AlignmentTraits { |
45 | | public: |
46 | | typedef T value_type; |
47 | | |
48 | 23 | static value_type to_int(T x) { |
49 | 23 | return x; |
50 | 23 | } |
51 | | |
52 | 23 | static T from_int(value_type value) { |
53 | 23 | return value; |
54 | 23 | } |
55 | | }; |
56 | | |
57 | | template<class T> |
58 | | class AlignmentTraits<T*> { |
59 | | public: |
60 | | typedef size_t value_type; |
61 | | |
62 | 81.5M | static value_type to_int(T* ptr) { |
63 | 81.5M | return reinterpret_cast<value_type>(ptr); |
64 | 81.5M | } |
65 | | |
66 | 81.5M | static T* from_int(value_type value) { |
67 | 81.5M | return reinterpret_cast<T*>(value); |
68 | 81.5M | } |
69 | | }; |
70 | | |
71 | | // Round up 'ptr' to the nearest 'align' boundary. |
72 | | // T should be pointer or integer type. |
73 | | template<class T> |
74 | 81.5M | T align_up(T ptr, typename AlignmentTraits<T>::value_type alignment_bytes) { |
75 | 81.5M | typedef AlignmentTraits<T> Traits; |
76 | 81.5M | typedef typename Traits::value_type int_type; |
77 | 81.5M | static_assert(sizeof(T) == sizeof(int_type), "Invalid source size in align_up"); |
78 | 81.5M | auto x = Traits::to_int(ptr); |
79 | 81.5M | return Traits::from_int((x + alignment_bytes - 1) & -alignment_bytes); |
80 | 81.5M | } _ZN2yb8align_upImEET_S1_NS_15AlignmentTraitsIS1_E10value_typeE Line | Count | Source | 74 | 23 | T align_up(T ptr, typename AlignmentTraits<T>::value_type alignment_bytes) { | 75 | 23 | typedef AlignmentTraits<T> Traits; | 76 | 23 | typedef typename Traits::value_type int_type; | 77 | 23 | static_assert(sizeof(T) == sizeof(int_type), "Invalid source size in align_up"); | 78 | 23 | auto x = Traits::to_int(ptr); | 79 | 23 | return Traits::from_int((x + alignment_bytes - 1) & -alignment_bytes); | 80 | 23 | } |
_ZN2yb8align_upIPhEET_S2_NS_15AlignmentTraitsIS2_E10value_typeE Line | Count | Source | 74 | 81.5M | T align_up(T ptr, typename AlignmentTraits<T>::value_type alignment_bytes) { | 75 | 81.5M | typedef AlignmentTraits<T> Traits; | 76 | 81.5M | typedef typename Traits::value_type int_type; | 77 | 81.5M | static_assert(sizeof(T) == sizeof(int_type), "Invalid source size in align_up"); | 78 | 81.5M | auto x = Traits::to_int(ptr); | 79 | 81.5M | return Traits::from_int((x + alignment_bytes - 1) & -alignment_bytes); | 80 | 81.5M | } |
|
81 | | |
82 | | } // namespace yb |
83 | | |
84 | | #endif // YB_UTIL_ALIGNMENT_H |