Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #4106

Re: Extending unions and ABI?

From jt@toerring.de (Jens Thoms Toerring)
Newsgroups comp.lang.c
Subject Re: Extending unions and ABI?
Date 2011-05-16 19:12 +0000
Organization Freie Universitaet Berlin
Message-ID <93db8pFrevU1@mid.uni-berlin.de> (permalink)
References <yIdAp.1305$Wy2.200@newsfe23.ams2> <aa7aac67-f6dc-40b0-9147-b593d82a6203@w21g2000yqm.googlegroups.com>

Show all headers | View raw


Tom St Denis <tom@iahu.ca> wrote:
> On May 16, 2:06 pm, Jef Driesen <jefdrie...@hotmail.com.invalid>
> wrote:
> > Suppose I have defined a union:
> >
> > typedef struct foo_t {
> >     ...
> >
> > } foo_t;
> >
> > typedef struct bar_t {
> >     ...
> >
> > } bar_t;
> >
> > typedef union foobar_t {
> >     foo_t foo;
> >     bar_t bar;
> >
> > } foobar_t;
> >
> > Which is part of the public api of my project. The union is used to pass
> > different types of data (e.g. either a foo or a bar struct) back to the
> > caller through a callback function. The type is used distinguish between
> > the two:
> >
> > typedef enum foobar_type_t {
> >     FOO;
> >     BAR;
> >
> > } foobar_type_t;
> >
> > typedef void (*callback_t)(foobar_type_t type, const foobar_t *foobar);
> >
> > void
> > dosomething (callback_t callback)
> > {
> >
> >     foobar_t foobar;
> >
> >     foobar.foo = ...
> >     callback (FOO, &foobar);
> >
> >     foobar.bar = ...
> >     callback (BAR, &foobar);
> >
> > }
> >
> > Note that I only use pointers to the union in the public api. The union
> > itself appears only in the implementation (e.g. in the dosomething()
> > function). Now suppose I need to extend my api to support a third type of
> > data. Is it portable to just add a third struct to the union? Will it
> > break the ABI rules and thus backwards compatibility. Is this a violation
> > of the C standard?

> If you add things to the union you potentially change the size of it,
> so any code compiled against the definition already won't be able to
> work with the object properly.

Well, but the user gets, as far as the OP wrote, only pointers
to it and not copies, so I would think the size of what's pointef
to is irrelevant - the size of the pointer remains the same. The
only information used when compiling a program using the header
file declaring the union when only using pointers is thus what
members there are - and not being aware that there could be
others won't hurt (except that the user can't access them).

> IOW, don't do that.

I think it's completely safe to extend the union as long as the
user (i.e. the callback function written by the user) only gets
a pointer to an instance. Even if the user would create his own
instance of the union (using the old version without the new
members) and copies from the union pointed to he's still safe
since he will perhaps miss a few bytes but only for members he
can't access anyway because he doesn't know about them.

> If you must, pass a void* to your callback prototype and have the
> instances of the function deref it appropriately.  e.g.

> void callback_foo(void *data, ...)
> {
>    struct foo *foo_data = data;

Now you're talking structures, but the OP has a union that
contains different structures. And he's not going to change
the structures but just add another structure to the union.
I don't see how converting in the function that calls the
callback function to a void pointer and then converting back
in the user-written callback function would help.

                           Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Extending unions and ABI? Jef Driesen <jefdriesen@hotmail.com.invalid> - 2011-05-16 20:06 +0200
  Re: Extending unions and ABI? Tom St Denis <tom@iahu.ca> - 2011-05-16 11:20 -0700
    Re: Extending unions and ABI? jt@toerring.de (Jens Thoms Toerring) - 2011-05-16 19:12 +0000
  Re: Extending unions and ABI? Shao Miller <sha0.miller@gmail.com> - 2011-05-16 14:38 -0400
    Re: Extending unions and ABI? "Morris Keesan" <mkeesan@post.harvard.edu> - 2011-05-16 14:54 -0400
      Re: Extending unions and ABI? Shao Miller <sha0.miller@gmail.com> - 2011-05-16 15:13 -0400
    Re: Extending unions and ABI? Jef Driesen <jefdriesen@hotmail.com.invalid> - 2011-05-16 20:55 +0200
      Re: Extending unions and ABI? Shao Miller <sha0.miller@gmail.com> - 2011-05-16 15:43 -0400
        Re: Extending unions and ABI? Shao Miller <sha0.miller@gmail.com> - 2011-05-16 15:48 -0400
          Re: Extending unions and ABI? Jef Driesen <jefdriesen@hotmail.com.invalid> - 2011-05-17 20:10 +0200
            Re: Extending unions and ABI? Shao Miller <sha0.miller@gmail.com> - 2011-05-18 00:28 -0500
        Re: Extending unions and ABI? Jef Driesen <jefdriesen@hotmail.com.invalid> - 2011-05-16 21:54 +0200
  Re: Extending unions and ABI? Columbus sailed the ocean China Blue <chine.bleu@yahoo.com> - 2011-05-17 12:53 -0700
  Re: Extending unions and ABI? "io_x" <a@b.c.invalid> - 2011-05-18 19:31 +0200
  Re: Extending unions and ABI? gordonb.4d208@burditt.org (Gordon Burditt) - 2011-05-23 04:03 -0500
  Re: Extending unions and ABI? Tim Rentsch <txr@alumni.caltech.edu> - 2011-05-30 23:47 -0700

csiph-web