public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Struct member & __attribute__((aligned)) in public header
@ 2014-01-21  6:46 Ian Pilcher
  0 siblings, 0 replies; only message in thread
From: Ian Pilcher @ 2014-01-21  6:46 UTC (permalink / raw)
  To: gcc-help

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
========================================================================

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2014-01-21  6:46 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-01-21  6:46 Struct member & __attribute__((aligned)) in public header Ian Pilcher

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).