Menu

#169 Encapsulate declaration of packed structures within a macro

1.4.0.0
wont-fix
None
2017-11-22
2017-08-18
No

Instead of

typedef struct
{
    UINT8   ver;   
    UINT8   rel;  
} GNU_PACKED TRDP_SHORT_VERSION_T;

use something like this:

VOS_PACKED(
{
    UINT8   ver;    
    UINT8   rel;    
} , TRDP_SHORT_VERSION_T);

The macro should be implemented in the VOS and for a gcc it would look like:

#define VOS_PACKED(members, name) \
GNU_PACKED struct name members; \
typedef struct name name

Discussion

  • Bernd Löhr

    Bernd Löhr - 2017-08-18

    Why?
    What is the reason behind this request?

    Regards

     
  • Joachim Lillich

    Joachim Lillich - 2017-08-18

    There are several reasons:

    1. If you are using for example a compiler which is not supported by tcnopen, the syntax for packed structures may be different: different pragmas, different attributes, different position of the keyword.
      With a common macro, you only have to provide one macro instead of searching each occurrence in the code.
    2. We use a static analysis tool which generates a false-positve for mixing typedef with the packed attribute. So structure declaration and typedef are separated.
    3. It would not be necessary to use different packing strategies within each header file like
    #ifdef WIN32
    #pragma pack(push, 1)
    #endif
    /* moved this typedefinition from trdp_types.h to this place, as it belongs to the  */
    /* communication buffer definitions, which need a packed struct                     */
    
    /** Version information for communication buffers */
    typedef struct
    {
        UINT8   ver;    /**< Version    - incremented for incompatible changes */
        UINT8   rel;    /**< Release    - incremented for compatible changes   */
    } GNU_PACKED TRDP_SHORT_VERSION_T;
    
    #ifdef WIN32
    #pragma pack(pop)
    #endif
    
     
  • Bernd Löhr

    Bernd Löhr - 2017-11-17
    • status: open --> accepted
    • assigned_to: Bernd Löhr
     
  • Bernd Löhr

    Bernd Löhr - 2017-11-22
    • status: accepted --> wont-fix
     
  • Bernd Löhr

    Bernd Löhr - 2017-11-22

    Declaring a #pragma pack within the proposed VOS_PACKED macro is rejected by MS compilers (VS 2010...2015).
    Sorry, but we decided to leave it like it is for now!

     

Log in to post a comment.

MongoDB Logo MongoDB