Skip to content

Commit

Permalink
Merge branch 'pull-request/#989-Packed-unaligned_type' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
jwellbelove committed Dec 16, 2024
2 parents d6f7d28 + 0568293 commit f9b2494
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
17 changes: 17 additions & 0 deletions include/etl/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,22 @@ SOFTWARE.
#define ETL_HAS_INITIALIZER_LIST 0
#endif

//*************************************
// Determine if the ETL should use __attribute__((packed).
#if defined(ETL_COMPILER_CLANG) || defined(ETL_COMPILER_GCC) || defined(ETL_COMPILER_INTEL) || defined(ETL_COMPILER_ARM6)
#define ETL_PACKED __attribute__((packed))
#define ETL_END_PACKED
#define ETL_HAS_PACKED 1
#elif defined(ETL_COMPILER_MICROSOFT)
#define ETL_PACKED __pragma(pack(push, 1))
#define ETL_END_PACKED __pragma(pack(pop))
#define ETL_HAS_PACKED 1
#else
#define ETL_PACKED
#define ETL_END_PACKED
#define ETL_HAS_PACKED 0
#endif

//*************************************
// Check for availability of certain builtins
#include "profiles/determine_builtin_support.h"
Expand Down Expand Up @@ -518,6 +534,7 @@ namespace etl
static ETL_CONSTANT bool has_mutable_array_view = (ETL_HAS_MUTABLE_ARRAY_VIEW == 1);
static ETL_CONSTANT bool has_ideque_repair = (ETL_HAS_IDEQUE_REPAIR == 1);
static ETL_CONSTANT bool has_virtual_messages = (ETL_HAS_VIRTUAL_MESSAGES == 1);
static ETL_CONSTANT bool has_packed = (ETL_HAS_PACKED == 1);

// Is...
static ETL_CONSTANT bool is_debug_build = (ETL_IS_DEBUG_BUILD == 1);
Expand Down
8 changes: 4 additions & 4 deletions include/etl/unaligned_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace etl
/// Contains all functionality that doesn't require the type.
//*************************************************************************
template <size_t Size_>
class unaligned_type_common
class ETL_PACKED unaligned_type_common
{
public:

Expand Down Expand Up @@ -214,7 +214,7 @@ namespace etl
protected:

unsigned char storage[Size];
};
}; ETL_END_PACKED

template <size_t Size_>
ETL_CONSTANT size_t unaligned_type_common<Size_>::Size;
Expand All @@ -227,7 +227,7 @@ namespace etl
///\tparam Endian The endianness of the arithmetic type.
//*************************************************************************
template <typename T, int Endian_>
class unaligned_type : public private_unaligned_type::unaligned_type_common<sizeof(T)>
class ETL_PACKED unaligned_type : public private_unaligned_type::unaligned_type_common<sizeof(T)>
{
public:

Expand Down Expand Up @@ -729,7 +729,7 @@ namespace etl
}
}
};
};
}; ETL_END_PACKED

template <typename T, int Endian_>
ETL_CONSTANT int unaligned_type<T, Endian_>::Endian;
Expand Down
1 change: 1 addition & 0 deletions test/test_etl_traits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ namespace
CHECK_EQUAL((ETL_HAS_IDEQUE_REPAIR == 1), etl::traits::has_ideque_repair);
CHECK_EQUAL((ETL_HAS_MUTABLE_ARRAY_VIEW == 1), etl::traits::has_mutable_array_view);
CHECK_EQUAL((ETL_HAS_VIRTUAL_MESSAGES == 1), etl::traits::has_virtual_messages);
CHECK_EQUAL((ETL_HAS_PACKED == 1), etl::traits::has_packed);

CHECK_EQUAL((ETL_IS_DEBUG_BUILD == 1), etl::traits::is_debug_build);
CHECK_EQUAL(__cplusplus, etl::traits::cplusplus);
Expand Down
26 changes: 26 additions & 0 deletions test/test_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -718,5 +718,31 @@ namespace
CHECK_EQUAL(forward_like_call_type::ConstRValue, template_function_fl<TFL&&>(etl::move(u4)));
CHECK_EQUAL(forward_like_call_type::ConstRValue, template_function_fl<const TFL&&>(etl::move(u4)));
}

#if ETL_HAS_PACKED
//*********************************
TEST(test_packed)
{
struct Unpacked
{
uint32_t a = 0x12345678;
uint8_t b = 0x9A;
uint32_t c = 0x87654321;
};

struct ETL_PACKED Packed
{
uint32_t a = 0x12345678;
uint8_t b = 0x9A;
uint32_t c = 0x87654321;
}; ETL_END_PACKED

Unpacked unpacked;
Packed packed;

CHECK_TRUE(sizeof(unpacked) > sizeof(packed));
CHECK_EQUAL(9U, sizeof(packed));
}
#endif
};
}

0 comments on commit f9b2494

Please sign in to comment.