xref: /netbsd-src/external/gpl3/gdb/dist/gdbsupport/packed.h (revision 5ba1f45f2a09259cc846f20c7c5501604d633c90)
1 /* Copyright (C) 2022-2024 Free Software Foundation, Inc.
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 
21 #include "traits.h"
22 #include <atomic>
23 
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 
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
40    we use the alternative byte array implementation there. */
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__
52 # define ATTRIBUTE_GCC_STRUCT __attribute__((__gcc_struct__))
53 #else
54 # define ATTRIBUTE_GCC_STRUCT
55 #endif
56 
57 template<typename T, size_t Bytes = sizeof (T)>
58 struct ATTRIBUTE_GCC_STRUCT packed
59 {
60 public:
61   packed () noexcept = default;
62 
63   packed (T val)
64   {
65     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
75     m_val = val;
76 #endif
77 
78     /* Ensure size and aligment are what we expect.  */
79     static_assert (sizeof (packed) == Bytes);
80     static_assert (alignof (packed) == 1);
81 
82     /* Make sure packed can be wrapped with std::atomic.  */
83     static_assert (std::is_trivially_copyable<packed>::value);
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);
88   }
89 
90   operator T () const noexcept
91   {
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
103     return m_val;
104 #endif
105   }
106 
107 private:
108 #if PACKED_USE_ARRAY
109   gdb_byte m_bytes[Bytes];
110 #else
111   T m_val : (Bytes * HOST_CHAR_BIT) ATTRIBUTE_PACKED;
112 #endif
113 };
114 
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   }
156 
157 PACKED_ATOMIC_OP (==)
158 PACKED_ATOMIC_OP (!=)
159 PACKED_ATOMIC_OP (>)
160 PACKED_ATOMIC_OP (<)
161 PACKED_ATOMIC_OP (>=)
162 PACKED_ATOMIC_OP (<=)
163 
164 #undef PACKED_ATOMIC_OP
165 
166 #endif
167