I don’t think the code works at all - at least not from an alignment perspective. For instance, assume T is one byte, and you use align 64, it is aligned on 65 byte boundaries. (This may not be fully correct need to read spec on structure member alignment - but the gist should be true) 

On Jun 10, 2025, at 10:04 AM, Timur Celik <clk...@gmail.com> wrote:


Consider the following generic type, which ensures to be large enough by allocating enough padding A and the to-be-aligned type T:

type Alignment interface {
Align16 | Align64
}
type Align16 = [16]byte
type Align64 = [64]byte

type Aligned[T any, A Alignment] struct {
pad A
val T
}

func (p *Aligned[T, A]) Value() *T {
size := unsafe.Sizeof(*new(A))
align := size - uintptr(unsafe.Pointer(&p.pad[0]))&(size-1)
return (*T)(unsafe.Pointer(&p.pad[align]))
}


Under the assumption that T has no heap pointers, is this safe? Or asked in another way, under which circumstances would this break?

--
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 visit https://groups.google.com/d/msgid/golang-nuts/d00058ec-cb9b-495f-807d-54397be94df2n%40googlegroups.com.

--
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 visit https://groups.google.com/d/msgid/golang-nuts/D368E0E8-DE85-43D3-813E-60D359578DF8%40ix.netcom.com.

Reply via email to