tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: proposal: some pointer and arithmetic utilities



On 22 March 2013 14:36, Taylor R Campbell <riastradh%netbsd.org@localhost> 
wrote:
>    Date: Fri, 22 Mar 2013 09:08:55 +0200
>    From: Alan Barrett <apb%cequrux.com@localhost>
>
>    I don't really see the point of offsetin -- if you have a struct
>    that ends with an array of 1 element, but you want to allocate
>    enough space for the struct plus an array of >1 elements, then why
>    not just do the arithmetic without a macro, and if you do need a
>    macro, then why not give it a name that sounds more like "sizeof"?
>
[.]
> This assumes that f_data is a C99 flexible array member, too, not a
> one-element array kludged pre-C99 into the same concept.  If you use
> one-element arrays like that and assume they work (which we do), the
> sizing becomes more complicated -- but offsetin remains the same:

Indeed there are seem to be uses of this in the tree, e.g., sasl.
Once i've overtaken maintainership of S-nail i've added these macros
to be compatible to ISO C99 (while still being compileable on C89):

#if defined __STDC_VERSION__ && __STDC_VERSION__ + 0 >= 199901L
  /* Variable size arrays and structure fields */
# define VFIELD_SIZE(X)
# define VFIELD_SIZEOF(T,F)     (0)
#else
# define VFIELD_SIZE(X)         (X)
# define VFIELD_SIZEOF(T,F)     SIZEOF_FIELD(T, F)
#endif

(you see, i thought they were named "variable array", just like variable
arguments, but ... oviously not) to be used like:

       char            b_buf[VFIELD_SIZE(SALIGN + 1)];
#define xy(STRUCTNAME, FIELD, SIZE) \
        ((sizeof(STRUCTNAME) - VFIELD_SIZEOF(STRUCTNAME, FIELD)) + (SIZE))

I think such a macro would be useful.
Me i always missed bitenums and automatic "superclass"-casts in C;
it's a pain that you have to you use a monster like C++ just for that!
In the end you end up writing some object-tree and hand-driven checked
castmacro...

--steffen
(from webinterface)


Home | Main Index | Thread Index | Old Index