Alignment.h (1444B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 /* Functionality related to memory alignment. */ 8 9 #ifndef mozilla_Alignment_h 10 #define mozilla_Alignment_h 11 12 #include "mozilla/Attributes.h" 13 #include <cstddef> 14 #include <cstdint> 15 16 namespace mozilla { 17 18 /* 19 * AlignedElem<N> is a structure whose alignment is guaranteed to be at least N 20 * bytes. 21 */ 22 template <size_t Align> 23 struct alignas(Align) AlignedElem {}; 24 25 template <typename T> 26 struct MOZ_INHERIT_TYPE_ANNOTATIONS_FROM_TEMPLATE_ARGS AlignedStorage2 { 27 union { 28 unsigned char mBytes[sizeof(T)]; 29 uint64_t mDummy; 30 }; 31 32 const T* addr() const { return reinterpret_cast<const T*>(mBytes); } 33 T* addr() { return static_cast<T*>(static_cast<void*>(mBytes)); } 34 35 const void* bytes() const { return static_cast<const void*>(mBytes); } 36 void* bytes() { return static_cast<void*>(mBytes); } 37 38 AlignedStorage2() = default; 39 40 // AlignedStorage2 is non-copyable: the default copy constructor violates 41 // strict aliasing rules, per bug 1269319. 42 AlignedStorage2(const AlignedStorage2&) = delete; 43 void operator=(const AlignedStorage2&) = delete; 44 }; 45 46 } /* namespace mozilla */ 47 48 #endif /* mozilla_Alignment_h */