emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Packing of union fields and bool_bf


From: Eli Zaretskii
Subject: Packing of union fields and bool_bf
Date: Sat, 13 Oct 2018 18:15:25 +0300

While working on bug#33017, I found out something unexpected: using
bool_bf in union members bloats the union.

A case in point is this part of 'struct glyph' (I removed the comments
for brevity):

  /* A union of sub-structures for different glyph types.  */
  union
  {
    unsigned ch;
    struct
    {
      bool_bf automatic : 1;
      unsigned id    : 31;
    } cmp;
    int img_id;
#ifdef HAVE_XWIDGETS
    struct xwidget *xwidget;
#endif
    struct
    {
      unsigned height  : 16;
      unsigned ascent  : 16;
    }
    stretch;
    struct
    {
      unsigned method : 2;
      bool_bf for_no_font : 1;
      unsigned len : 4;
      unsigned ch : 25;
    } glyphless;
    unsigned val;
  } u;

Here's how this is compiled with 32-bit MinGW GCC 7.3.0, according to
GDB:

  (gdb) ptype /o desired_glyph->u
  /* offset    |  size */  type = union {
  /*                 4 */    unsigned int ch;
  /*                 8 */    struct {
  /*    0: 7   |     1 */        bool_bf automatic : 1;
  /* XXX  7-bit hole  */
  /* XXX  3-byte hole */
  /*    4: 1   |     4 */        unsigned int id : 31;
  /* XXX  1-bit padding  */

                                 /* total size (bytes):    8 */
                             } cmp;
  /*                 4 */    int img_id;
  /*                 4 */    struct {
  /*    0:16   |     4 */        unsigned int height : 16;
  /*    0: 0   |     4 */        unsigned int ascent : 16;

                                 /* total size (bytes):    4 */
                             } stretch;
  /*                12 */    struct {
  /*    0:30   |     4 */        unsigned int method : 2;
  /* XXX  6-bit hole  */
  /* XXX  3-byte hole */
  /*    4: 7   |     1 */        bool_bf for_no_font : 1;
  /* XXX  7-bit hole  */
  /* XXX  3-byte hole */
  /*    8:28   |     4 */        unsigned int len : 4;
  /*    8: 3   |     4 */        unsigned int ch : 25;
  /* XXX  3-bit padding  */

                                 /* total size (bytes):   12 */
                             } glyphless;
  /*                 4 */    unsigned int val;

                             /* total size (bytes):   12 */
                           }

Note the difference between handling 'unsigned int' fields and
'bool_bf' fields: the former are nicely packed, whereas the latter are
not.  Which causes the size of union to be 12 bytes, instead of the
expected 4.  The 'bool' type is 1-byte wide on this platform.

Is this expected?  Should we continue using bool_bf in these cases?



reply via email to

[Prev in Thread] Current Thread [Next in Thread]