On Tue, Aug 21, 2018 at 4:08 PM, 'Carl Mastrangelo' via golang-nuts
<[email protected]> wrote:
>
> The answer must be more nuanced than that, because it is possible to take a
> nil pointer and construct an unsafe.Pointer from it.
Yes, OK, nil is an exception, as it is for any pointer type.
> The reason I am interested in this is (and please don't judge too early) is
> I'm toying around with implementing some atomic primitives. In particular,
> I would like to play around with with the cmpxchg16b instruction which needs
> 16 byte alignment. Go does not provide a way to enforce a data structure
> has such alignment, so I am attempting to define a struct that I can index
> into. (assume 64bit words). For example, the datastructure I want is this:
>
> // alignment of foo is 16
> type foo struct {
> uintptr
> unsafe.Pointer
> }
>
> But I can't assert this. The next best thing is to make a struct 2x the
> size, and make a pointer to the first aligned part:
>
> type foo struct {
> [4]uintptr
> }
>
> This way I can get an aligned address pointing into the middle of this array
> for using cmpxchg16b. The problem with this is that if any of the interior
> values are not seen a pointers by the GC. In order to keep the values alive
> they need to be unsafe.Pointer:
>
> type foo struct {
> [4]unsafe.Pointer
> }
>
> Now this is a problem. There is really only one pointer in here, the other
> value is just some arbitrary bytes. Since the GC now things the addresses
> are real, it will crash. What is the correct way to get an aligned struct
> that contains pointers?
There isn't one. There is some discussion at https://golang.org/issue/19057 .
Ian
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.