1 /* Copyright (C) 2022-2023 Free Software Foundation, Inc.
3 This file is part of GDB.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 /* Each instantiation and full specialization of the packed template
25 defines a type that behaves like a given scalar type, but that has
26 byte alignment, and, may optionally have a smaller size than the
27 given scalar type. This is typically used as alternative to
28 bit-fields (and ENUM_BITFIELD), when the fields must have separate
29 memory locations to avoid data races. */
31 /* There are two implementations here -- one standard compliant, using
32 a byte array for internal representation, and another that relies
33 on bitfields and attribute packed (and attribute gcc_struct on
34 Windows). The latter is preferable, as it is more convenient when
35 debugging GDB -- printing a struct packed variable prints its field
36 using its natural type, which is particularly useful if the type is
37 an enum -- but may not work on all compilers. */
39 /* Clang targeting Windows does not support attribute gcc_struct, so
40 we use the alternative byte array implementation there. */
41 #if defined _WIN32 && defined __clang__
42 # define PACKED_USE_ARRAY 1
44 # define PACKED_USE_ARRAY 0
47 /* For the preferred implementation, we need gcc_struct on Windows, as
48 otherwise the size of e.g., "packed<int, 1>" will be larger than
49 what we want. Clang targeting Windows does not support attribute
51 #if !PACKED_USE_ARRAY && defined _WIN32 && !defined __clang__
52 # define ATTRIBUTE_GCC_STRUCT __attribute__((__gcc_struct__))
54 # define ATTRIBUTE_GCC_STRUCT
57 template<typename T
, size_t Bytes
= sizeof (T
)>
58 struct ATTRIBUTE_GCC_STRUCT packed
61 packed () noexcept
= default;
65 gdb_static_assert (sizeof (ULONGEST
) >= sizeof (T
));
69 for (int i
= (Bytes
- 1); i
>= 0; --i
)
71 m_bytes
[i
] = (gdb_byte
) tmp
;
72 tmp
>>= HOST_CHAR_BIT
;
78 /* Ensure size and aligment are what we expect. */
79 gdb_static_assert (sizeof (packed
) == Bytes
);
80 gdb_static_assert (alignof (packed
) == 1);
82 /* Make sure packed can be wrapped with std::atomic. */
83 #if HAVE_IS_TRIVIALLY_COPYABLE
84 gdb_static_assert (std::is_trivially_copyable
<packed
>::value
);
86 gdb_static_assert (std::is_copy_constructible
<packed
>::value
);
87 gdb_static_assert (std::is_move_constructible
<packed
>::value
);
88 gdb_static_assert (std::is_copy_assignable
<packed
>::value
);
89 gdb_static_assert (std::is_move_assignable
<packed
>::value
);
92 operator T () const noexcept
101 tmp
<<= HOST_CHAR_BIT
;
111 gdb_byte m_bytes
[Bytes
];
113 T m_val
: (Bytes
* HOST_CHAR_BIT
) ATTRIBUTE_PACKED
;
117 /* Add some comparisons between std::atomic<packed<T>> and packed<T>
118 and T. We need this because even though std::atomic<T> doesn't
119 define these operators, the relational expressions still work via
120 implicit conversions. Those wouldn't work when wrapped in packed
121 without these operators, because they'd require two implicit
122 conversions to go from T to packed<T> to std::atomic<packed<T>>
123 (and back), and C++ only does one. */
125 #define PACKED_ATOMIC_OP(OP) \
126 template<typename T, size_t Bytes> \
127 bool operator OP (const std::atomic<packed<T, Bytes>> &lhs, \
128 const std::atomic<packed<T, Bytes>> &rhs) \
130 return lhs.load () OP rhs.load (); \
133 template<typename T, size_t Bytes> \
134 bool operator OP (T lhs, const std::atomic<packed<T, Bytes>> &rhs) \
136 return lhs OP rhs.load (); \
139 template<typename T, size_t Bytes> \
140 bool operator OP (const std::atomic<packed<T, Bytes>> &lhs, T rhs) \
142 return lhs.load () OP rhs; \
145 template<typename T, size_t Bytes> \
146 bool operator OP (const std::atomic<packed<T, Bytes>> &lhs, \
147 packed<T, Bytes> rhs) \
149 return lhs.load () OP rhs; \
152 template<typename T, size_t Bytes> \
153 bool operator OP (packed<T, Bytes> lhs, \
154 const std::atomic<packed<T, Bytes>> &rhs) \
156 return lhs OP rhs.load (); \
159 PACKED_ATOMIC_OP (==)
160 PACKED_ATOMIC_OP (!=)
163 PACKED_ATOMIC_OP (>=)
164 PACKED_ATOMIC_OP (<=)
166 #undef PACKED_ATOMIC_OP