public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Ian Pilcher <arequipeno@gmail.com>
To: gcc-help@gcc.gnu.org
Subject: Struct member & __attribute__((aligned)) in public header
Date: Tue, 21 Jan 2014 06:46:00 -0000	[thread overview]
Message-ID: <lbl52j$9e4$1@ger.gmane.org> (raw)

I have a struct that uses a flexible array member to store values of
application-defined types.  Since the flexible array member is defined
as an array of unsigned char, I am using __attribute__((aligned)) to
give it a reasonable alignment.

  struct generic_value {
      const char *name;
      const struct value_type *type;
      unsigned char flags;
      unsigned char value[] __attribute__((aligned));
  };

This is all well and good, but the definition of this struct may have to
become part of the public API of this library.  Obviously, it would be
very, very bad if an application and the library had different ideas
about the alignment of the 'value' member.

I found this discussion from 2009:

  http://comments.gmane.org/gmane.comp.gcc.devel/102830

Unfortunately, that thread doesn't really discuss the "best practices"
of a shared library in this situation.  I see a few possibilities:

1)  This isn't actually a problem.  It was fixed back in '09, and I can
    use __attribute__((aligned)) freely.

2)  Use something like __attribute__((aligned(sizeof(void *)))).  I'm
    not writing performance-critical code, so this should really be
    more than adequate.

3)  Use offsetof to capture the offset of the value element at the time
    my library is compiled (in a const size_t) and provide some sort of
    macro/inline function to retrieve it in a "safe" manner -- along
    with comments in the header file about the dangers of accessing it
    directly.

Any thoughts on what the best approach is?

Thanks!

-- 
========================================================================
Ian Pilcher                                         arequipeno@gmail.com
           Sent from the cloud -- where it's already tomorrow
========================================================================

                 reply	other threads:[~2014-01-21  6:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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='lbl52j$9e4$1@ger.gmane.org' \
    --to=arequipeno@gmail.com \
    --cc=gcc-help@gcc.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.
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).