-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for bitfields in structs #406
Comments
This is a great feature to add to package:ffigen indeed. |
It may not be possible to generate bitfields for structs, because of padding issues. Consider a struct S - typedef struct
{
int *a; // Can be 4 or 8 bytes
long long b:35; // Takes 5 bytes
char c; // Takes 1 byte
} A; The memory layout for this should be similar to this, The offset of char c is This 64-bit layout can be confirmed by clang's output $ clang -Xclang -fdump-record-layouts test.c
*** Dumping AST Record Layout
0 | A
0 | int * a
8:0-34 | long long b
13 | char c
| [sizeof=16, align=8] For generating 32-bit layout on my 64-bit machine, I simply replaced clang -Xclang -fdump-record-layouts test.c
*** Dumping AST Record Layout
0 | A
0 | int a
8:0-34 | long long b
13 | char c
| [sizeof=16, align=8] Here's why we cannot generate platform-agnostic bindings for this struct with bitfield -for representing
Also, it's possible to pack the structure tightly (using Considering all that, it seems more viable to have bitfields in |
Good find. A good middle ground solution would be to add some alignment directives to class A extends Struct {
Pointer<Int> a;
@AlignedTo(8)
Int8()
int _b0;
Int8()
int _b1;
// ...
Int8()
int c;
} I filed dart-lang/sdk#43257 to support that. |
I am not sure if we will be able to tell which alignment to use. typedef struct
{
char a;
long long b:30;
} A; has the following layout *** Dumping AST Record Layout
0 | A
0 | char a
1:0-29 | long long b
| [sizeof=8, align=8] I would have expected Here we must use
|
I would suggest omitting the bitfield information when generating the code. That's how I've called my C code with bitfield information and it worked well.
would be interpreted as
|
Any news about this? |
Ignoring the bitfields would fix a lot of codes and it is perfectly compatible with the C standard. |
As @mannprerak2 mentioned above, it will not be working in all cases. However, I think it would be fine to add what you're asking for under a config flag, given that it will work for a (large?) subset of all bitfields. That way you can just put the flag in your FFIgen config instead of replace all bitfields in your source header. Feel free to submit a PR! |
If there is no general solution, I would suggest custom writing the Structs yourself and using |
As long as the C/C++ compiler itself is not compiled with bitfields it should work in all cases. Is there a compiler flag for this? |
Bitfields are not directly supported in
dart:ffi
but it may be possible to generate code to work with it. See dart-lang/sdk#38954.The text was updated successfully, but these errors were encountered: