diff --git a/libuavcan/include/uavcan/internal/marshal/type_util.hpp b/libuavcan/include/uavcan/internal/marshal/type_util.hpp index ad2c555834..f1f5613240 100644 --- a/libuavcan/include/uavcan/internal/marshal/type_util.hpp +++ b/libuavcan/include/uavcan/internal/marshal/type_util.hpp @@ -20,6 +20,10 @@ template <> struct IntegerBitLen<0> { enum { Result = 0 }; }; +template +struct BitLenToByteLen { enum { Result = (BitLen + 7) / 8 }; }; + + template struct StorageType { typedef T Type; }; diff --git a/libuavcan/include/uavcan/publisher.hpp b/libuavcan/include/uavcan/publisher.hpp index ea4d487a3e..882768cf9c 100644 --- a/libuavcan/include/uavcan/publisher.hpp +++ b/libuavcan/include/uavcan/publisher.hpp @@ -12,6 +12,7 @@ #include #include #include +#include namespace uavcan { @@ -54,8 +55,7 @@ private: IMarshalBuffer* getBuffer() { - const int size = (DataType::MaxBitLen + 7) / 8; - return buffer_provider_.getBuffer(size); + return buffer_provider_.getBuffer(BitLenToByteLen::Result); } int genericSend(const DataType& message, TransferType transfer_type, NodeID dst_node_id, diff --git a/libuavcan/test/marshal/array.cpp b/libuavcan/test/marshal/array.cpp index 5517079ef9..a423d16669 100644 --- a/libuavcan/test/marshal/array.cpp +++ b/libuavcan/test/marshal/array.cpp @@ -76,18 +76,6 @@ struct CustomType }; -TEST(Array, IntegerBitLen) -{ - using uavcan::IntegerBitLen; - - ASSERT_EQ(0, IntegerBitLen<0>::Result); - ASSERT_EQ(1, IntegerBitLen<1>::Result); - ASSERT_EQ(6, IntegerBitLen<42>::Result); - ASSERT_EQ(8, IntegerBitLen<232>::Result); - ASSERT_EQ(32, IntegerBitLen<0x81234567>::Result); -} - - TEST(Array, Basic) { typedef Array, ArrayModeStatic, 4> A1; diff --git a/libuavcan/test/marshal/type_util.cpp b/libuavcan/test/marshal/type_util.cpp new file mode 100644 index 0000000000..20bda6890f --- /dev/null +++ b/libuavcan/test/marshal/type_util.cpp @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2014 Pavel Kirienko + */ + +#include +#include + + +TEST(MarshalTypeUtil, IntegerBitLen) +{ + using uavcan::IntegerBitLen; + + ASSERT_EQ(0, IntegerBitLen<0>::Result); + ASSERT_EQ(1, IntegerBitLen<1>::Result); + ASSERT_EQ(6, IntegerBitLen<42>::Result); + ASSERT_EQ(8, IntegerBitLen<232>::Result); + ASSERT_EQ(32, IntegerBitLen<0x81234567>::Result); +} + + +TEST(MarshalTypeUtil, BitLenToByteLen) +{ + using uavcan::BitLenToByteLen; + + ASSERT_EQ(2, BitLenToByteLen<16>::Result); + ASSERT_EQ(1, BitLenToByteLen<8>::Result); + ASSERT_EQ(1, BitLenToByteLen<7>::Result); + ASSERT_EQ(1, BitLenToByteLen<1>::Result); + ASSERT_EQ(2, BitLenToByteLen<9>::Result); +}