From: Jeff King <peff@peff.net>
To: Karsten Blees <karsten.blees@gmail.com>
Cc: git@vger.kernel.org
Subject: struct hashmap_entry packing
Date: Mon, 28 Jul 2014 13:17:44 -0400 [thread overview]
Message-ID: <20140728171743.GA1927@peff.net> (raw)
Hi Karsten,
The hashmap_entry documentation claims:
`struct hashmap_entry`::
An opaque structure representing an entry in the hash table,
which must be used as first member of user data structures.
Ideally it should be followed by an int-sized member to prevent
unused memory on 64-bit systems due to alignment.
I'm not sure if the statement about alignment is true. If I have a
struct like:
struct magic {
struct hashmap_entry map;
int x;
};
the statement above implies that I should be able to fit this into only
16 bytes on an LP64 system. But I can't convince gcc to do it. And I
think that makes sense, if you consider code like:
memset(&magic.map, 0, sizeof(struct hashmap_entry));
The sizeof() has to be the same regardless of whether the hashmap_entry
is standalone or in another struct, and therefore must be padded up to
16 bytes. If we stored "x" in that padding in the combined struct, it
would be overwritten by our memset.
Am I missing anything? If this is the case, we should probably drop that
bit from the documentation. It's possible that we could get around it by
embedding the hashmap_entry elements directly into the parent struct,
but we would be counting on a reader dereferencing it as a hashmap_entry
seeing the members at the exact same offset. I'd imagine that's one of
those things that holds most of the time, but is violating the standard.
It's probably not worth it to save a few bytes.
-Peff
next reply other threads:[~2014-07-28 17:17 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-07-28 17:17 Jeff King [this message]
2014-07-29 20:40 ` struct hashmap_entry packing Karsten Blees
2014-08-01 22:37 ` Jeff King
2014-08-01 23:10 ` [PATCH] pack-bitmap: do not use gcc packed attribute Jeff King
2014-08-01 23:12 ` Jeff King
2014-08-04 19:19 ` Karsten Blees
2014-08-05 18:38 ` Vicent Martí
2014-08-05 18:47 ` Jeff King
2014-08-06 18:58 ` Karsten Blees
2014-08-06 19:32 ` Junio C Hamano
2014-08-04 19:20 ` struct hashmap_entry packing Karsten Blees
2014-08-05 18:51 ` Jeff King
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=20140728171743.GA1927@peff.net \
--to=peff@peff.net \
--cc=git@vger.kernel.org \
--cc=karsten.blees@gmail.com \
/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.
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 NNTP newsgroup(s).