concurrencykit / ck

Concurrency primitives, safe memory reclamation mechanisms and non-blocking (including lock-free) data structures designed to aid in the research, design and implementation of high performance concurrent systems developed in C99+.
http://concurrencykit.org/
Other
2.38k stars 313 forks source link

Flexible array member not at end of struct #157

Closed johnflux closed 4 years ago

johnflux commented 4 years ago

I'm trying to compile some other codebase that uses ck, and it no longer compiles (breaks with gcc >6) because of the way that they've used ck.

Here is a small self-contained snippet:

#include <ck_bitmap.h>
struct Foo {
    using MyBitmap = CK_BITMAP_INSTANCE(100);
    MyBitmap bitmap1;
    MyBitmap bitmap2;
}
int main() { return 0; }

The compile error is:

/usr/include/ck_bitmap.h:119:15: error: flexible array member ‘ck_bitmap::map’ not at end of ‘struct Foo’

My question is: Should this work, and is there an easy fix? Thanks.

sbahra commented 4 years ago

This is expected behavior but there is a viable work-around for it probably. I'll check Sunday!

sbahra commented 4 years ago

Big project at work, sorry about that! So the above error is expected. You'll need to change the location of MyBitmap to the end due to the union used by CK_BITMAP_INSTANCE. Is that a viable option for you to unblock you immediately?

Otherwise, a simple fix is to just do pointer casting in CK_BITMAP macros, which removes the need to rely on the union + FAM. I would want to additional safety checks for that and make sure it doesn't break too annoyingly for existing consumers (it's convenient now for us to be to reference .bitmap on a bitmap instance, removing union would make that annoying but it can be solved with a new convenience macro).

The last option is we can also just use 0-sized array for the bitmap definition itself (change struct ck_bitmap from map[] to map[0]. We won't be taking this route as it does prevent certain classes of bugs.

Patches are also welcome for this, happy to review.