unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Packing of union fields and bool_bf
@ 2018-10-13 15:15 Eli Zaretskii
  2018-10-13 15:58 ` Tom Tromey
  0 siblings, 1 reply; 5+ messages in thread
From: Eli Zaretskii @ 2018-10-13 15:15 UTC (permalink / raw)
  To: emacs-devel

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?



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-10-13 16:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-10-13 15:15 Packing of union fields and bool_bf Eli Zaretskii
2018-10-13 15:58 ` Tom Tromey
2018-10-13 16:22   ` Paul Eggert
2018-10-13 16:54     ` Eli Zaretskii
2018-10-13 16:43   ` Eli Zaretskii

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).