-3

golang 1.17 changed from stack to register to handle the params

is it safe when i use atomic.SwapPointer to change the pointer receiver's value

below is the code

//like this
func (this *A) Replace(a *A) {
    //other business logic
    //....
    //business logic end
    atomic.SwapPointer((*unsafe.Pointer)(unsafe.Pointer(&this)), unsafe.Pointer(a))
}
gnat
  • 6,213
  • 108
  • 53
  • 73
nobody
  • 1
  • 1

2 Answers2

0

[I]s it safe in golang 1.17 to change the pointer receiver's value with atomic swap pointer [?]

No. Use of package unsafe is unsafe by definition of unsafe.

("safe" in which sense?)

Volker
  • 40,468
  • 7
  • 81
  • 87
0

I don't think this is doing what you want it to. From the point-of-view of Go code, even "sensible" unsafe code, you cannot tell the difference between the register and stack ABI. If you take the address of a parameter, if it was not already in memory, it will be stored to memory, and that will be its address. If that address is passed to a function that "escapes" the address by storing it in a global or in the heap, then the memory for the parameter will actually be obtained by heap-allocating a slot for it and storing it there. If this happens, then subsequent reference to the parameter will indirect through the hidden pointer to its heap address.

David Chase
  • 301
  • 1
  • 6