]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdbsupport/packed.h
gdbsupport: assume that compiler supports std::{is_trivially_constructible,is_trivial...
[thirdparty/binutils-gdb.git] / gdbsupport / packed.h
CommitLineData
1d506c26 1/* Copyright (C) 2022-2024 Free Software Foundation, Inc.
68c0faca
PA
2
3 This file is part of GDB.
4
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.
9
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.
14
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/>. */
17
18#ifndef PACKED_H
19#define PACKED_H
20
02f0597c 21#include "traits.h"
e249e6b8 22#include <atomic>
02f0597c 23
68c0faca
PA
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. */
30
b669667d
PA
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. */
38
39/* Clang targeting Windows does not support attribute gcc_struct, so
33b5899f 40 we use the alternative byte array implementation there. */
b669667d
PA
41#if defined _WIN32 && defined __clang__
42# define PACKED_USE_ARRAY 1
43#else
44# define PACKED_USE_ARRAY 0
45#endif
46
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
50 gcc_struct. */
51#if !PACKED_USE_ARRAY && defined _WIN32 && !defined __clang__
4ca26ad7
PA
52# define ATTRIBUTE_GCC_STRUCT __attribute__((__gcc_struct__))
53#else
54# define ATTRIBUTE_GCC_STRUCT
55#endif
56
68c0faca 57template<typename T, size_t Bytes = sizeof (T)>
4ca26ad7 58struct ATTRIBUTE_GCC_STRUCT packed
68c0faca
PA
59{
60public:
2df41bda
TV
61 packed () noexcept = default;
62
68c0faca
PA
63 packed (T val)
64 {
69f6730d 65 static_assert (sizeof (ULONGEST) >= sizeof (T));
b669667d
PA
66
67#if PACKED_USE_ARRAY
68 ULONGEST tmp = val;
69 for (int i = (Bytes - 1); i >= 0; --i)
70 {
71 m_bytes[i] = (gdb_byte) tmp;
72 tmp >>= HOST_CHAR_BIT;
73 }
74#else
68c0faca 75 m_val = val;
b669667d 76#endif
68c0faca
PA
77
78 /* Ensure size and aligment are what we expect. */
69f6730d
TT
79 static_assert (sizeof (packed) == Bytes);
80 static_assert (alignof (packed) == 1);
68c0faca
PA
81
82 /* Make sure packed can be wrapped with std::atomic. */
69f6730d 83 static_assert (std::is_trivially_copyable<packed>::value);
69f6730d
TT
84 static_assert (std::is_copy_constructible<packed>::value);
85 static_assert (std::is_move_constructible<packed>::value);
86 static_assert (std::is_copy_assignable<packed>::value);
87 static_assert (std::is_move_assignable<packed>::value);
68c0faca
PA
88 }
89
90 operator T () const noexcept
91 {
b669667d
PA
92#if PACKED_USE_ARRAY
93 ULONGEST tmp = 0;
94 for (int i = 0;;)
95 {
96 tmp |= m_bytes[i];
97 if (++i == Bytes)
98 break;
99 tmp <<= HOST_CHAR_BIT;
100 }
101 return (T) tmp;
102#else
68c0faca 103 return m_val;
b669667d 104#endif
68c0faca
PA
105 }
106
107private:
b669667d
PA
108#if PACKED_USE_ARRAY
109 gdb_byte m_bytes[Bytes];
110#else
68c0faca 111 T m_val : (Bytes * HOST_CHAR_BIT) ATTRIBUTE_PACKED;
b669667d 112#endif
68c0faca
PA
113};
114
e249e6b8
PA
115/* Add some comparisons between std::atomic<packed<T>> and packed<T>
116 and T. We need this because even though std::atomic<T> doesn't
117 define these operators, the relational expressions still work via
118 implicit conversions. Those wouldn't work when wrapped in packed
119 without these operators, because they'd require two implicit
120 conversions to go from T to packed<T> to std::atomic<packed<T>>
121 (and back), and C++ only does one. */
122
123#define PACKED_ATOMIC_OP(OP) \
124 template<typename T, size_t Bytes> \
125 bool operator OP (const std::atomic<packed<T, Bytes>> &lhs, \
126 const std::atomic<packed<T, Bytes>> &rhs) \
127 { \
128 return lhs.load () OP rhs.load (); \
129 } \
130 \
131 template<typename T, size_t Bytes> \
132 bool operator OP (T lhs, const std::atomic<packed<T, Bytes>> &rhs) \
133 { \
134 return lhs OP rhs.load (); \
135 } \
136 \
137 template<typename T, size_t Bytes> \
138 bool operator OP (const std::atomic<packed<T, Bytes>> &lhs, T rhs) \
139 { \
140 return lhs.load () OP rhs; \
141 } \
142 \
143 template<typename T, size_t Bytes> \
144 bool operator OP (const std::atomic<packed<T, Bytes>> &lhs, \
145 packed<T, Bytes> rhs) \
146 { \
147 return lhs.load () OP rhs; \
148 } \
149 \
150 template<typename T, size_t Bytes> \
151 bool operator OP (packed<T, Bytes> lhs, \
152 const std::atomic<packed<T, Bytes>> &rhs) \
153 { \
154 return lhs OP rhs.load (); \
155 }
68c0faca 156
e249e6b8
PA
157PACKED_ATOMIC_OP (==)
158PACKED_ATOMIC_OP (!=)
159PACKED_ATOMIC_OP (>)
160PACKED_ATOMIC_OP (<)
161PACKED_ATOMIC_OP (>=)
162PACKED_ATOMIC_OP (<=)
68c0faca 163
e249e6b8 164#undef PACKED_ATOMIC_OP
68c0faca
PA
165
166#endif