]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdbsupport/packed.h
Add note about adding ChangeLog.git to src-release.sh
[thirdparty/binutils-gdb.git] / gdbsupport / packed.h
CommitLineData
213516ef 1/* Copyright (C) 2022-2023 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 {
b669667d
PA
65 gdb_static_assert (sizeof (ULONGEST) >= sizeof (T));
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. */
79 gdb_static_assert (sizeof (packed) == Bytes);
80 gdb_static_assert (alignof (packed) == 1);
81
82 /* Make sure packed can be wrapped with std::atomic. */
02f0597c 83#if HAVE_IS_TRIVIALLY_COPYABLE
68c0faca 84 gdb_static_assert (std::is_trivially_copyable<packed>::value);
02f0597c 85#endif
68c0faca
PA
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);
90 }
91
92 operator T () const noexcept
93 {
b669667d
PA
94#if PACKED_USE_ARRAY
95 ULONGEST tmp = 0;
96 for (int i = 0;;)
97 {
98 tmp |= m_bytes[i];
99 if (++i == Bytes)
100 break;
101 tmp <<= HOST_CHAR_BIT;
102 }
103 return (T) tmp;
104#else
68c0faca 105 return m_val;
b669667d 106#endif
68c0faca
PA
107 }
108
109private:
b669667d
PA
110#if PACKED_USE_ARRAY
111 gdb_byte m_bytes[Bytes];
112#else
68c0faca 113 T m_val : (Bytes * HOST_CHAR_BIT) ATTRIBUTE_PACKED;
b669667d 114#endif
68c0faca
PA
115};
116
e249e6b8
PA
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. */
124
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) \
129 { \
130 return lhs.load () OP rhs.load (); \
131 } \
132 \
133 template<typename T, size_t Bytes> \
134 bool operator OP (T lhs, const std::atomic<packed<T, Bytes>> &rhs) \
135 { \
136 return lhs OP rhs.load (); \
137 } \
138 \
139 template<typename T, size_t Bytes> \
140 bool operator OP (const std::atomic<packed<T, Bytes>> &lhs, T rhs) \
141 { \
142 return lhs.load () OP rhs; \
143 } \
144 \
145 template<typename T, size_t Bytes> \
146 bool operator OP (const std::atomic<packed<T, Bytes>> &lhs, \
147 packed<T, Bytes> rhs) \
148 { \
149 return lhs.load () OP rhs; \
150 } \
151 \
152 template<typename T, size_t Bytes> \
153 bool operator OP (packed<T, Bytes> lhs, \
154 const std::atomic<packed<T, Bytes>> &rhs) \
155 { \
156 return lhs OP rhs.load (); \
157 }
68c0faca 158
e249e6b8
PA
159PACKED_ATOMIC_OP (==)
160PACKED_ATOMIC_OP (!=)
161PACKED_ATOMIC_OP (>)
162PACKED_ATOMIC_OP (<)
163PACKED_ATOMIC_OP (>=)
164PACKED_ATOMIC_OP (<=)
68c0faca 165
e249e6b8 166#undef PACKED_ATOMIC_OP
68c0faca
PA
167
168#endif