diff --git a/libuavcan/dsdl_compiler/data_type_template.hpp b/libuavcan/dsdl_compiler/data_type_template.hpp index 351ecb6d7d..709ceebd5f 100644 --- a/libuavcan/dsdl_compiler/data_type_template.hpp +++ b/libuavcan/dsdl_compiler/data_type_template.hpp @@ -46,6 +46,10 @@ namespace ${nsc} { % endfor +#if UAVCAN_PACK_STRUCTS +UAVCAN_PACKED_BEGIN +#endif + struct ${t.short_name} { <%def name="generate_primary_body(type_name, max_bitlen, fields, constants)" buffered="True"> @@ -174,6 +178,10 @@ ${'::uavcan::TailArrayOptDisabled' if (idx + 1) < len(fields) else 'tao_mode'}); } }; +#if UAVCAN_PACK_STRUCTS +UAVCAN_PACKED_END +#endif + // TODO Stream operator % if t.has_default_dtid: @@ -181,6 +189,8 @@ namespace { ::uavcan::DefaultDataTypeRegistrator<${t.short_name}> _uavcan_gdtr_registrator_${t.short_name}; } +% else: +// No default registration % endif % for nsc in t.cpp_namespace_components: diff --git a/libuavcan/include/uavcan/internal/impl_constants.hpp b/libuavcan/include/uavcan/internal/impl_constants.hpp index f10a6cb3f0..ae32f5f258 100644 --- a/libuavcan/include/uavcan/internal/impl_constants.hpp +++ b/libuavcan/include/uavcan/internal/impl_constants.hpp @@ -18,6 +18,17 @@ namespace uavcan # endif #endif +/** + * Struct layout control. + * Define UAVCAN_PACK_STRUCTS = 1 to reduce memory usage. + */ +#ifndef UAVCAN_PACKED_BEGIN +# define UAVCAN_PACKED_BEGIN _Pragma("pack(push, 1)") +#endif +#ifndef UAVCAN_PACKED_END +# define UAVCAN_PACKED_END _Pragma("pack(pop)") +#endif + /** * Should be OK for any target arch up to AMD64 and any compiler. * The library leverages compile-time checks to ensure that all types that are subject to dynamic allocation