packed.h revision 1.1.1.2 1 1.1.1.2 christos /* Copyright (C) 2022-2024 Free Software Foundation, Inc.
2 1.1 christos
3 1.1 christos This file is part of GDB.
4 1.1 christos
5 1.1 christos This program is free software; you can redistribute it and/or modify
6 1.1 christos it under the terms of the GNU General Public License as published by
7 1.1 christos the Free Software Foundation; either version 3 of the License, or
8 1.1 christos (at your option) any later version.
9 1.1 christos
10 1.1 christos This program is distributed in the hope that it will be useful,
11 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
12 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 1.1 christos GNU General Public License for more details.
14 1.1 christos
15 1.1 christos You should have received a copy of the GNU General Public License
16 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 1.1 christos
18 1.1 christos #ifndef PACKED_H
19 1.1 christos #define PACKED_H
20 1.1 christos
21 1.1 christos #include "traits.h"
22 1.1 christos #include <atomic>
23 1.1 christos
24 1.1 christos /* Each instantiation and full specialization of the packed template
25 1.1 christos defines a type that behaves like a given scalar type, but that has
26 1.1 christos byte alignment, and, may optionally have a smaller size than the
27 1.1 christos given scalar type. This is typically used as alternative to
28 1.1 christos bit-fields (and ENUM_BITFIELD), when the fields must have separate
29 1.1 christos memory locations to avoid data races. */
30 1.1 christos
31 1.1 christos /* There are two implementations here -- one standard compliant, using
32 1.1 christos a byte array for internal representation, and another that relies
33 1.1 christos on bitfields and attribute packed (and attribute gcc_struct on
34 1.1 christos Windows). The latter is preferable, as it is more convenient when
35 1.1 christos debugging GDB -- printing a struct packed variable prints its field
36 1.1 christos using its natural type, which is particularly useful if the type is
37 1.1 christos an enum -- but may not work on all compilers. */
38 1.1 christos
39 1.1 christos /* Clang targeting Windows does not support attribute gcc_struct, so
40 1.1.1.2 christos we use the alternative byte array implementation there. */
41 1.1 christos #if defined _WIN32 && defined __clang__
42 1.1 christos # define PACKED_USE_ARRAY 1
43 1.1 christos #else
44 1.1 christos # define PACKED_USE_ARRAY 0
45 1.1 christos #endif
46 1.1 christos
47 1.1 christos /* For the preferred implementation, we need gcc_struct on Windows, as
48 1.1 christos otherwise the size of e.g., "packed<int, 1>" will be larger than
49 1.1 christos what we want. Clang targeting Windows does not support attribute
50 1.1 christos gcc_struct. */
51 1.1 christos #if !PACKED_USE_ARRAY && defined _WIN32 && !defined __clang__
52 1.1 christos # define ATTRIBUTE_GCC_STRUCT __attribute__((__gcc_struct__))
53 1.1 christos #else
54 1.1 christos # define ATTRIBUTE_GCC_STRUCT
55 1.1 christos #endif
56 1.1 christos
57 1.1 christos template<typename T, size_t Bytes = sizeof (T)>
58 1.1 christos struct ATTRIBUTE_GCC_STRUCT packed
59 1.1 christos {
60 1.1 christos public:
61 1.1 christos packed () noexcept = default;
62 1.1 christos
63 1.1 christos packed (T val)
64 1.1 christos {
65 1.1.1.2 christos static_assert (sizeof (ULONGEST) >= sizeof (T));
66 1.1 christos
67 1.1 christos #if PACKED_USE_ARRAY
68 1.1 christos ULONGEST tmp = val;
69 1.1 christos for (int i = (Bytes - 1); i >= 0; --i)
70 1.1 christos {
71 1.1 christos m_bytes[i] = (gdb_byte) tmp;
72 1.1 christos tmp >>= HOST_CHAR_BIT;
73 1.1 christos }
74 1.1 christos #else
75 1.1 christos m_val = val;
76 1.1 christos #endif
77 1.1 christos
78 1.1 christos /* Ensure size and aligment are what we expect. */
79 1.1.1.2 christos static_assert (sizeof (packed) == Bytes);
80 1.1.1.2 christos static_assert (alignof (packed) == 1);
81 1.1 christos
82 1.1 christos /* Make sure packed can be wrapped with std::atomic. */
83 1.1.1.2 christos static_assert (std::is_trivially_copyable<packed>::value);
84 1.1.1.2 christos static_assert (std::is_copy_constructible<packed>::value);
85 1.1.1.2 christos static_assert (std::is_move_constructible<packed>::value);
86 1.1.1.2 christos static_assert (std::is_copy_assignable<packed>::value);
87 1.1.1.2 christos static_assert (std::is_move_assignable<packed>::value);
88 1.1 christos }
89 1.1 christos
90 1.1 christos operator T () const noexcept
91 1.1 christos {
92 1.1 christos #if PACKED_USE_ARRAY
93 1.1 christos ULONGEST tmp = 0;
94 1.1 christos for (int i = 0;;)
95 1.1 christos {
96 1.1 christos tmp |= m_bytes[i];
97 1.1 christos if (++i == Bytes)
98 1.1 christos break;
99 1.1 christos tmp <<= HOST_CHAR_BIT;
100 1.1 christos }
101 1.1 christos return (T) tmp;
102 1.1 christos #else
103 1.1 christos return m_val;
104 1.1 christos #endif
105 1.1 christos }
106 1.1 christos
107 1.1 christos private:
108 1.1 christos #if PACKED_USE_ARRAY
109 1.1 christos gdb_byte m_bytes[Bytes];
110 1.1 christos #else
111 1.1 christos T m_val : (Bytes * HOST_CHAR_BIT) ATTRIBUTE_PACKED;
112 1.1 christos #endif
113 1.1 christos };
114 1.1 christos
115 1.1 christos /* Add some comparisons between std::atomic<packed<T>> and packed<T>
116 1.1 christos and T. We need this because even though std::atomic<T> doesn't
117 1.1 christos define these operators, the relational expressions still work via
118 1.1 christos implicit conversions. Those wouldn't work when wrapped in packed
119 1.1 christos without these operators, because they'd require two implicit
120 1.1 christos conversions to go from T to packed<T> to std::atomic<packed<T>>
121 1.1 christos (and back), and C++ only does one. */
122 1.1 christos
123 1.1 christos #define PACKED_ATOMIC_OP(OP) \
124 1.1 christos template<typename T, size_t Bytes> \
125 1.1 christos bool operator OP (const std::atomic<packed<T, Bytes>> &lhs, \
126 1.1 christos const std::atomic<packed<T, Bytes>> &rhs) \
127 1.1 christos { \
128 1.1 christos return lhs.load () OP rhs.load (); \
129 1.1 christos } \
130 1.1 christos \
131 1.1 christos template<typename T, size_t Bytes> \
132 1.1 christos bool operator OP (T lhs, const std::atomic<packed<T, Bytes>> &rhs) \
133 1.1 christos { \
134 1.1 christos return lhs OP rhs.load (); \
135 1.1 christos } \
136 1.1 christos \
137 1.1 christos template<typename T, size_t Bytes> \
138 1.1 christos bool operator OP (const std::atomic<packed<T, Bytes>> &lhs, T rhs) \
139 1.1 christos { \
140 1.1 christos return lhs.load () OP rhs; \
141 1.1 christos } \
142 1.1 christos \
143 1.1 christos template<typename T, size_t Bytes> \
144 1.1 christos bool operator OP (const std::atomic<packed<T, Bytes>> &lhs, \
145 1.1 christos packed<T, Bytes> rhs) \
146 1.1 christos { \
147 1.1 christos return lhs.load () OP rhs; \
148 1.1 christos } \
149 1.1 christos \
150 1.1 christos template<typename T, size_t Bytes> \
151 1.1 christos bool operator OP (packed<T, Bytes> lhs, \
152 1.1 christos const std::atomic<packed<T, Bytes>> &rhs) \
153 1.1 christos { \
154 1.1 christos return lhs OP rhs.load (); \
155 1.1 christos }
156 1.1 christos
157 1.1 christos PACKED_ATOMIC_OP (==)
158 1.1 christos PACKED_ATOMIC_OP (!=)
159 1.1 christos PACKED_ATOMIC_OP (>)
160 1.1 christos PACKED_ATOMIC_OP (<)
161 1.1 christos PACKED_ATOMIC_OP (>=)
162 1.1 christos PACKED_ATOMIC_OP (<=)
163 1.1 christos
164 1.1 christos #undef PACKED_ATOMIC_OP
165 1.1 christos
166 1.1 christos #endif
167