OLD | NEW |
1 /* | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. | 2 // Use of this source code is governed by a BSD-style license that can be |
3 * | 3 // found in the LICENSE file. |
4 * This library is free software; you can redistribute it and/or | |
5 * modify it under the terms of the GNU Library General Public | |
6 * License as published by the Free Software Foundation; either | |
7 * version 2 of the License, or (at your option) any later version. | |
8 * | |
9 * This library is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 * Library General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU Library General Public License | |
15 * along with this library; see the file COPYING.LIB. If not, write to | |
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
17 * Boston, MA 02110-1301, USA. | |
18 * | |
19 */ | |
20 | 4 |
21 #ifndef WTF_Alignment_h | 5 #include "platform/wtf/Alignment.h" |
22 #define WTF_Alignment_h | |
23 | 6 |
24 #include "wtf/Compiler.h" | 7 // The contents of this header was moved to platform/wtf as part of |
25 #include <algorithm> | 8 // WTF migration project. See the following post for details: |
26 #include <stdint.h> | 9 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gY
CAAJ |
27 #include <utility> | |
28 | |
29 namespace WTF { | |
30 | |
31 #if COMPILER(GCC) | |
32 #define WTF_ALIGN_OF(type) __alignof__(type) | |
33 #define WTF_ALIGNED(variable_type, variable, n) \ | |
34 variable_type variable __attribute__((__aligned__(n))) | |
35 #elif COMPILER(MSVC) | |
36 #define WTF_ALIGN_OF(type) __alignof(type) | |
37 #define WTF_ALIGNED(variable_type, variable, n) \ | |
38 __declspec(align(n)) variable_type variable | |
39 #else | |
40 #error WTF_ALIGN macros need alignment control. | |
41 #endif | |
42 | |
43 #if COMPILER(GCC) | |
44 typedef char __attribute__((__may_alias__)) AlignedBufferChar; | |
45 #else | |
46 typedef char AlignedBufferChar; | |
47 #endif | |
48 | |
49 template <size_t size, size_t alignment> | |
50 struct AlignedBuffer; | |
51 template <size_t size> | |
52 struct AlignedBuffer<size, 1> { | |
53 AlignedBufferChar buffer[size]; | |
54 }; | |
55 template <size_t size> | |
56 struct AlignedBuffer<size, 2> { | |
57 WTF_ALIGNED(AlignedBufferChar, buffer[size], 2); | |
58 }; | |
59 template <size_t size> | |
60 struct AlignedBuffer<size, 4> { | |
61 WTF_ALIGNED(AlignedBufferChar, buffer[size], 4); | |
62 }; | |
63 template <size_t size> | |
64 struct AlignedBuffer<size, 8> { | |
65 WTF_ALIGNED(AlignedBufferChar, buffer[size], 8); | |
66 }; | |
67 template <size_t size> | |
68 struct AlignedBuffer<size, 16> { | |
69 WTF_ALIGNED(AlignedBufferChar, buffer[size], 16); | |
70 }; | |
71 template <size_t size> | |
72 struct AlignedBuffer<size, 32> { | |
73 WTF_ALIGNED(AlignedBufferChar, buffer[size], 32); | |
74 }; | |
75 template <size_t size> | |
76 struct AlignedBuffer<size, 64> { | |
77 WTF_ALIGNED(AlignedBufferChar, buffer[size], 64); | |
78 }; | |
79 | |
80 template <size_t size, size_t alignment> | |
81 void swap(AlignedBuffer<size, alignment>& a, | |
82 AlignedBuffer<size, alignment>& b) { | |
83 for (size_t i = 0; i < size; ++i) | |
84 std::swap(a.buffer[i], b.buffer[i]); | |
85 } | |
86 | |
87 template <uintptr_t mask> | |
88 inline bool isAlignedTo(const void* pointer) { | |
89 return !(reinterpret_cast<uintptr_t>(pointer) & mask); | |
90 } | |
91 | |
92 } // namespace WTF | |
93 | |
94 #endif // WTF_Alignment_h | |
OLD | NEW |