1 | /* |
2 | * Copyright 2016-present Facebook, Inc. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | #pragma once |
18 | |
19 | #include <cstddef> |
20 | #include <utility> |
21 | |
22 | #include <folly/lang/Align.h> |
23 | |
24 | namespace folly { |
25 | |
26 | /** |
27 | * Holds a type T, in addition to enough padding to ensure that it isn't subject |
28 | * to false sharing within the range used by folly. |
29 | * |
30 | * If `sizeof(T) <= alignof(T)` then the inner `T` will be entirely within one |
31 | * false sharing range (AKA cache line). |
32 | * |
33 | * CachelinePadded may add padding both before and after the value. Consider |
34 | * whether alignas(folly::hardware_destructive_interference_size) suffices. |
35 | */ |
36 | template <typename T> |
37 | class CachelinePadded { |
38 | static_assert( |
39 | alignof(T) <= max_align_v, |
40 | "CachelinePadded does not support over-aligned types." ); |
41 | |
42 | public: |
43 | template <typename... Args> |
44 | explicit CachelinePadded(Args&&... args) |
45 | : inner_(std::forward<Args>(args)...) {} |
46 | |
47 | T* get() { |
48 | return &inner_; |
49 | } |
50 | |
51 | const T* get() const { |
52 | return &inner_; |
53 | } |
54 | |
55 | T* operator->() { |
56 | return get(); |
57 | } |
58 | |
59 | const T* operator->() const { |
60 | return get(); |
61 | } |
62 | |
63 | T& operator*() { |
64 | return *get(); |
65 | } |
66 | |
67 | const T& operator*() const { |
68 | return *get(); |
69 | } |
70 | |
71 | private: |
72 | static constexpr size_t paddingSize() noexcept { |
73 | return hardware_destructive_interference_size - |
74 | (alignof(T) % hardware_destructive_interference_size); |
75 | } |
76 | char paddingPre_[paddingSize()]; |
77 | T inner_; |
78 | char paddingPost_[paddingSize()]; |
79 | }; |
80 | } // namespace folly |
81 | |