Find a better way to wrap structs with C unions
- Dominant language
- Julia
- Stars
- 1.4k
- Forks
- 281
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 30
Description
The way that structs containing C unions are being wrapped, besides not being very user-friendly, presents a potential bug. For example, `CUDA_RESOURCE_DESC` is a struct used to create textures that is being wrapped by CUDAdrv, which contains a C union in the field `res`. It seems that the biggest alternative type of the union is being picked (in this case `reserved`, 32 `Cint`s) to define the wrapping struct. From libcuda_common.jl:
```julia
struct ANONYMOUS2_reserved
reserved::NTuple{32, Cint}
end
struct ANONYMOUS1_res
reserved::ANONYMOUS2_reserved
end
struct CUDA_RESOURCE_DESC_st
resType::CUresourcetype
res::ANONYMOUS1_res
flags::UInt32
end
```
The problem here is that the original union contains, among others, an alternative type that has a 64bit pointer type inside, which makes all struct fields to be aligned to the 64bit boundaries.
Notice the following. This is the layout of the struct as libcuda_common.jl is creating it:
```julia
julia> sizeof(Tuple{Int32, NTuple{32, Int32}, Int32})
136
```
The `NTuple{32, Int32}` represents the `res` union field whose large alternative is 32 32bit-integers.
However, if the `res` contained 64bit types while still maintaining its size, for example `NTuple{16, Int64}` (16 64bit-integers), then:
```julia
julia> sizeof(Tuple{Int32, NTuple{16, Int64}, Int32})
144
```
since both first and last fields would occupy 64bits too, i.e. 4 bytes more each.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.