all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Eli Zaretskii <eliz@gnu.org>
To: emacs-devel@gnu.org
Subject: Packing of union fields and bool_bf
Date: Sat, 13 Oct 2018 18:15:25 +0300	[thread overview]
Message-ID: <838t31vpwy.fsf@gnu.org> (raw)

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	other threads:[~2018-10-13 15:15 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-13 15:15 Eli Zaretskii [this message]
2018-10-13 15:58 ` Packing of union fields and bool_bf Tom Tromey
2018-10-13 16:22   ` Paul Eggert
2018-10-13 16:54     ` Eli Zaretskii
2018-10-13 16:43   ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=838t31vpwy.fsf@gnu.org \
    --to=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.