Home | History | Annotate | Line # | Download | only in gdbsupport
packed.h revision 1.1.1.1
      1 /* Copyright (C) 2022-2023 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 implemention 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     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
     75     m_val = val;
     76 #endif
     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.  */
     83 #if HAVE_IS_TRIVIALLY_COPYABLE
     84     gdb_static_assert (std::is_trivially_copyable<packed>::value);
     85 #endif
     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   {
     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
    105     return m_val;
    106 #endif
    107   }
    108 
    109 private:
    110 #if PACKED_USE_ARRAY
    111   gdb_byte m_bytes[Bytes];
    112 #else
    113   T m_val : (Bytes * HOST_CHAR_BIT) ATTRIBUTE_PACKED;
    114 #endif
    115 };
    116 
    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   }
    158 
    159 PACKED_ATOMIC_OP (==)
    160 PACKED_ATOMIC_OP (!=)
    161 PACKED_ATOMIC_OP (>)
    162 PACKED_ATOMIC_OP (<)
    163 PACKED_ATOMIC_OP (>=)
    164 PACKED_ATOMIC_OP (<=)
    165 
    166 #undef PACKED_ATOMIC_OP
    167 
    168 #endif
    169