test-spark: add object-size assertions
This commit is contained in:
parent
2a71c8fa82
commit
30505c7977
|
@ -2,6 +2,11 @@ pragma Ada_2012;
|
||||||
|
|
||||||
package body Machinery is
|
package body Machinery is
|
||||||
|
|
||||||
|
function Number_Of_Bits (Machinery : Machinery_Type) return Number_Of_Bits_Type is
|
||||||
|
begin
|
||||||
|
return Machinery'Size;
|
||||||
|
end Number_Of_Bits;
|
||||||
|
|
||||||
procedure Initialize (Machinery : out Machinery_Type) is
|
procedure Initialize (Machinery : out Machinery_Type) is
|
||||||
begin
|
begin
|
||||||
Machinery := ( Temperature => 25 );
|
Machinery := ( Temperature => 25 );
|
||||||
|
|
|
@ -2,10 +2,17 @@ package Machinery is
|
||||||
|
|
||||||
pragma Pure;
|
pragma Pure;
|
||||||
|
|
||||||
|
type Number_Of_Bits_Type is mod 2**32 with Size => 32;
|
||||||
|
|
||||||
type Temperature_Type is mod 2**32 with Size => 32;
|
type Temperature_Type is mod 2**32 with Size => 32;
|
||||||
|
|
||||||
type Machinery_Type is private;
|
type Machinery_Type is private;
|
||||||
|
|
||||||
|
function Number_of_bits (Machinery : Machinery_Type) return Number_Of_Bits_Type
|
||||||
|
with Export,
|
||||||
|
Convention => C,
|
||||||
|
External_Name => "_ZN5Spark14number_of_bitsERKNS_9MachineryE";
|
||||||
|
|
||||||
procedure Initialize (Machinery : out Machinery_Type)
|
procedure Initialize (Machinery : out Machinery_Type)
|
||||||
with Export,
|
with Export,
|
||||||
Convention => C,
|
Convention => C,
|
||||||
|
|
|
@ -20,6 +20,13 @@ namespace Spark {
|
||||||
template <Genode::size_t BYTES>
|
template <Genode::size_t BYTES>
|
||||||
struct Object
|
struct Object
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Exception type
|
||||||
|
*/
|
||||||
|
struct Object_size_mismatch { };
|
||||||
|
|
||||||
|
static constexpr Genode::size_t bytes() { return BYTES; }
|
||||||
|
|
||||||
long _space[(BYTES + sizeof(long) - 1)/sizeof(long)] { };
|
long _space[(BYTES + sizeof(long) - 1)/sizeof(long)] { };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -31,6 +38,15 @@ namespace Spark {
|
||||||
|
|
||||||
Genode::uint32_t temperature() const;
|
Genode::uint32_t temperature() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Genode::size_t number_of_bits(Machinery const &);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static inline void assert_valid_object_size()
|
||||||
|
{
|
||||||
|
if (number_of_bits(*(T *)nullptr) > T::bytes()*8)
|
||||||
|
throw typename T::Object_size_mismatch();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -38,6 +54,8 @@ static inline void test_spark_object_construction()
|
||||||
{
|
{
|
||||||
using namespace Genode;
|
using namespace Genode;
|
||||||
|
|
||||||
|
Spark::assert_valid_object_size<Spark::Machinery>();
|
||||||
|
|
||||||
Spark::Machinery machinery { };
|
Spark::Machinery machinery { };
|
||||||
|
|
||||||
auto check = [&] (char const *msg, Genode::uint32_t expected)
|
auto check = [&] (char const *msg, Genode::uint32_t expected)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user