George Hartzell writes:
 > 
 > Quick summary, I'm trying to understand the Go structures that cgo
 > gives me, how they map back onto the C structures and why a Go struct
 > that doesn't quite match the cgo output seems to work anyway.
 > [...]

Ian gave me a lot of good things to chew on, but didn't answer the
main question about why the different versions of the `i2c_msg` struct
definition all seem to work.

For those of you who've been sitting on the edges of your seats
awaiting the answer...

It turns out that my assumption, that the `buf` pointer's alignment
requirements end up inserting padding automagically, seems to be true.

I convinced myself of this using the `structlayout` tools from
https://github.com/dominikh/go-tools.

Starting with this input file:

```go
package main

// #include <linux/i2c.h>
import "C"

// This is what `cgo -godefs` generates
type ViaGoDefs struct {
        addr  uint16
        flags uint16
        len   uint16
        buf   *uint8
}

// This is what @tetsu-koba used
type FromTetsu struct {
        addr      uint16
        flags     uint16
        len       uint16
        __padding uint16
        buf       uintptr
}

// This is the result of using cgo as Ian suggested
type ViaCGo C.struct_i2c_msg
```

you can see that all of the structs have the same shape:

```
pi@raspberrypi:~/temper/go/structures $ structlayout -json structures ViaGoDefs 
| structlayout-pretty
    +--------+
  0 |        | <- ViaGoDefs.addr uint16 (size 2, align 2)
    +--------+
  2 |        | <- ViaGoDefs.flags uint16 (size 2, align 2)
    +--------+
  4 |        | <- ViaGoDefs.len uint16 (size 2, align 2)
    +--------+
  6 |        | <- padding (size 2, align 0)
    +--------+
  8 |        | <- ViaGoDefs.buf *uint8 (size 4, align 4)
    +--------+
    -........-
    +--------+
 11 |        |
    +--------+
pi@raspberrypi:~/temper/go/structures $ structlayout -json structures FromTetsu 
| structlayout-pretty
    +--------+
  0 |        | <- FromTetsu.addr uint16 (size 2, align 2)
    +--------+
  2 |        | <- FromTetsu.flags uint16 (size 2, align 2)
    +--------+
  4 |        | <- FromTetsu.len uint16 (size 2, align 2)
    +--------+
  6 |        | <- FromTetsu.__padding uint16 (size 2, align 2)
    +--------+
  8 |        | <- FromTetsu.buf uintptr (size 4, align 4)
    +--------+
    -........-
    +--------+
 11 |        |
    +--------+
pi@raspberrypi:~/temper/go/structures $ structlayout -json structures ViaCGo | 
structlayout-pretty
    +--------+
  0 |        | <- ViaCGo.addr structures._Ctype_ushort (size 2, align 2)
    +--------+
  2 |        | <- ViaCGo.flags structures._Ctype_ushort (size 2, align 2)
    +--------+
  4 |        | <- ViaCGo.len structures._Ctype_ushort (size 2, align 2)
    +--------+
  6 |        | <- padding (size 2, align 0)
    +--------+
  8 |        | <- ViaCGo.buf *structures._Ctype_uchar (size 4, align 4)
    +--------+
    -........-
    +--------+
 11 |        |
    +--------+
pi@raspberrypi:~/temper/go/structures $
```

Phew.

g.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/23914.46306.691997.964218%40alice.local.

Reply via email to