Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Types in byteorder.h need a cleanup #14737

@maribu

Description

@maribu

Description

The types in byteorder.h like be_uint64_t look like this:

/**                                                                              
 * @brief          A 64 bit integer in big endian aka network byte order.        
 * @details        This is a wrapper around an uint64_t to catch missing conversions
 *                 between different byte orders at compile time.                
 */                                                                              
typedef union __attribute__((packed)) {                                          
    uint64_t u64;       /**< 64 bit representation */                            
    uint8_t u8[8];      /**< 8 bit representation */                             
    uint16_t u16[4];    /**< 16 bit representation */                            
    uint32_t u32[2];    /**< 32 bit representation */                            
    be_uint16_t b16[4]; /**< big endian 16 bit representation */                 
    be_uint32_t b32[2]; /**< big endian 32 bit representation */                 
} be_uint64_t; 
  • Why does the type contain __attribute__((packed))? This will for the compiler to assume all accesses are unaligned. This reduces performance and increases ROM size when accessing it.
    • When used in a some foo_hdr_t structure, that structure should have the __attribute__((packed)) attribute instead. This would prevent unaligned accesses to incorrectly be assumed to be aligned when casting a uint8_t *-buffer to a header struct. But at the same time, be_uint64_t used outside of headers won't be effected by the performance penalty for no reason.
  • Why does it contain both uint16_t u16[4] and be_uint16_t b16[4]?
    • Especially with the poor member documentation, this could easily mislead people to assume the u16 member contains host byte order and the b16 member contains network byte order.

Versions

Up to current master

Metadata

Metadata

Assignees

No one assigned

    Labels

    State: don't staleState: Tell state-bot to ignore this issueType: bugThe issue reports a bug / The PR fixes a bug (including spelling errors)Type: cleanupThe issue proposes a clean-up / The PR cleans-up parts of the codebase / documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions