Re: G_ARRAY_LENGTH for glib.h?



Hi Tim,

> > > even if this *would* work, i wouldn't put it in glib.h, simply
> > > because
> > > 1) the test is not fully reliable (try it with gpointer p = &p;)
> >
> > This would fail as well, because the compiler wouldn't notice at
> > compile-time, that p is equal &p.
> 
> the compiler can't do any such things at compile time.

Ok it can't do it, where a constant expression is expected.

> > > 2) the macro is provided for convenience of people so far doing
> > >    sizeof (array) / sizeof (array[0]), and those already know
> > >    that this kind of thing only works for arrays
> >
> > True, but wasn't it you, who pointed out, that it would be nice to
> > have a check to prevent this case.
> 
> not that i remember.

Sorry, I mixed that up.

> > > 4) your definition simply doesn't work, try compiling the following
> > >    example:
> >
> > Yeah, thats why I used #if defined(__GNUC__) && defined(__OPTIMIZE__),
> > which isn't defined in your example.
> 
> sorry, i don't follow, the same example doesn't work for -O1 as well,
> would please try to actually get it compiled?

Ok, Mea Culpa. You are right. This macro can't be used, where a constant is
expected. So lets just disregard all my comments so far....

The reason I at all jumped into this was, that I played similar games with
ORBit and there those macros weren't used as constants so it worked out quite
well. This is how it works there:

/* If the following macro is defined, a check for the right endianness
 * conversion is performed at link time. This only works with
 * optimization turned on. */
#define TEST_FOR_UNDEFINED_ENDIANNESS_CONVERSION_AT_LINK_TIME 1
#ifdef TEST_FOR_UNDEFINED_ENDIANNESS_CONVERSION_AT_LINK_TIME
void unknown_size_for_endianess(gulong size);
#else
#define unknown_size_for_endianess(size) \
  g_error("Unknown size %d for endianess conversion.", size)
#endif

/* This macro gets mostly opimized away, as the size should be a
 * sizeof expression and thus is known at compile time. */
#define CONVERT_ENDIANNESS(dest, src, size)                             \
  (((size) == 1) ? (*(guint8*)(dest) = *(guint8*)(src)) , 0 :           \
   ((size) == 2) ? (CONVERT_ENDIANESS_16 ((dest), (src))) , 0 :         \
   ((size) == 4) ? (CONVERT_ENDIANESS_32 ((dest), (src))) , 0 :         \
   ((size) == 8) ? (CONVERT_ENDIANESS_64 ((dest), (src))) , 0 :         \
   unknown_size_for_endianess (sizeof (dest)), 0)

Thus I got to know all places, where this macro was used on
compile-time-unknown sizes or on sizes, not supported (BTW: there weren't any
luckily)
 
Bye,
Sebastian
-- 
Sebastian Wilhelmi                   |            här ovanför alla molnen
mailto:wilhelmi@ira.uka.de           |     är himmlen sĺ förunderligt blĺ
http://goethe.ira.uka.de/~wilhelmi   |



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]