2 * Copyright 2016-present Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <folly/lang/Align.h>
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.
30 * If `sizeof(T) <= alignof(T)` then the inner `T` will be entirely within one
31 * false sharing range (AKA cache line).
34 class CachelinePadded {
36 alignof(T) <= max_align_v,
37 "CachelinePadded does not support over-aligned types.");
40 template <typename... Args>
41 explicit CachelinePadded(Args&&... args)
42 : inner_(std::forward<Args>(args)...) {}
48 const T* get() const {
56 const T* operator->() const {
64 const T& operator*() const {
69 static constexpr size_t paddingSize() noexcept {
70 return hardware_destructive_interference_size -
71 (alignof(T) % hardware_destructive_interference_size);
73 char paddingPre_[paddingSize()];
75 char paddingPost_[paddingSize()];