scala-native / scala-native-bindgen

Scala Native Binding Generator

Home Page:https://scala-native.github.io/scala-native-bindgen/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Field offset in bits is not always divisible by 8

kornilova203 opened this issue · comments

Field offsets are not always divisible by 8.

Running bindgen on signal.h give assertion error on struct __darwin_fp_control because second field __denorm has 1 bit offset.

Solution is to remove assertion and not generate getters/setters for such fields.

It looks like it is caused by explicit struct field layout, which is a quite common pattern to optimize memory usage:

struct __darwin_fp_control
{
    unsigned short              __invalid       :1,
                                __denorm        :1,
                                __zdiv          :1,
                                __ovrfl         :1,
                                __undfl         :1,
                                __precis        :1,
                                                :2,
                                __pc            :2
    // ...
}

@ekrich Did you figure out a way to support such structs in Scala Native? One option would be to read in the whole 32-bit/64-bit word and do the bit masking in Scala.

Seems like we cannot get real offset for such fields because it is implementation-defined

Multiple adjacent bit fields are usually packed together (although this behavior is implementation-defined)

https://en.cppreference.com/w/cpp/language/bit_field

@jonas In certain places the POSIX standard allows a type to differ because the underlying structure is based on the system or OS. They could not standardize all the way down. I believe the solution is provide a Ptr[Byte] as a generic pointer. I think this will also require some glue code in C if #ifdef conditionals for each platform. I haven't quite gotten that far yet.

Example: the ucontext_t struct contains mcontext_t which points to platform specific code. See http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html

I don't think this is related to this issue.