ruby / fiddle

A libffi wrapper for Ruby.
BSD 2-Clause "Simplified" License
154 stars 37 forks source link

Support for Bit Field #132

Closed lukejahnke closed 1 year ago

lukejahnke commented 1 year ago

I would like to be able to deal with C bit fields. At the moment I am managing this manually.

For comparison, this is possible in a nice way with ruby-ffi with https://github.com/kojix2/ffi-bitfield

kou commented 1 year ago

Could you share your use case?

It seems that memory layout of bit field is implementation defined: https://wiki.sei.cmu.edu/confluence/display/c/EXP11-C.+Do+not+make+assumptions+regarding+the+layout+of+structures+with+bit-fields

It means that we can't create portable memory layout with Fiddle/Ruby-FFI.

lukejahnke commented 1 year ago

Ah, good point about it being implementation defined.

My use case is accessing shared memory (shmat) to communicate with another (not written by me) process. They're using bit fields for space efficiency. I guess for my use case I don't care about portability, but I can understand it being important for this project.

kou commented 1 year ago

OK. For your use case, it's better that you use bit operations such as Integer#& against a byte that includes the target data. The memory layout (which bit has your data) depends on your use case, you may need to do some try & errors. You may need to change your program when you run your program in another environment.

Can we close this because we can't implement bit field support in portable way? If we find a way to implement it in portable way, we can reopen this.

lukejahnke commented 1 year ago

Yes, happy to close it. Thank you for considering this and your feedback.