From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.devel Subject: Packing of union fields and bool_bf Date: Sat, 13 Oct 2018 18:15:25 +0300 Message-ID: <838t31vpwy.fsf@gnu.org> NNTP-Posting-Host: blaine.gmane.org X-Trace: blaine.gmane.org 1539443609 4199 195.159.176.226 (13 Oct 2018 15:13:29 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 13 Oct 2018 15:13:29 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Oct 13 17:13:25 2018 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gBLbY-0000zg-OU for ged-emacs-devel@m.gmane.org; Sat, 13 Oct 2018 17:13:24 +0200 Original-Received: from localhost ([::1]:45305 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gBLdf-0006C0-9X for ged-emacs-devel@m.gmane.org; Sat, 13 Oct 2018 11:15:35 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:53869) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gBLdV-00069R-HR for emacs-devel@gnu.org; Sat, 13 Oct 2018 11:15:26 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gBLdR-0003yy-UJ for emacs-devel@gnu.org; Sat, 13 Oct 2018 11:15:25 -0400 Original-Received: from fencepost.gnu.org ([2001:4830:134:3::e]:60012) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gBLdR-0003yu-Qk for emacs-devel@gnu.org; Sat, 13 Oct 2018 11:15:21 -0400 Original-Received: from [176.228.60.248] (port=2151 helo=home-c4e4a596f7) by fencepost.gnu.org with esmtpsa (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1gBLdQ-0007cO-Lw for emacs-devel@gnu.org; Sat, 13 Oct 2018 11:15:21 -0400 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2001:4830:134:3::e X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:230367 Archived-At: 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?