I'm reading map.go to have a deeper look at how map is implemented in Go.
I'm confused about what this piece of code does:
func (b *bmap) overflow(t *maptype) *bmap {
return *(**bmap)(add(unsafe.Pointer(b), uintptr(t.bucketsize)-sys.PtrSize))
}
Could anyone help me understand what *(**bmap) does here? Thanks in advance.
If T is a type then *T is also a type, specifically a pointer type, and since *T is a type then **T is also a type. https://golang.org/ref/spec#Pointer_types
If x is an expression that produces a value then *x is a pointer indirection, or pointer dereference. https://golang.org/ref/spec#Address_operators
And if T is a type and x is an expression then T(x) is a conversion. https://golang.org/ref/spec#Conversions
So **bmap is a type, specifically a "pointer to a pointer to bmap" type.
Then (**bmap)(add(...)) is a conversion that converts whatever the add(...) expression returns to a value of type **bmap.
And finally *(**bmap)(...) dereferences the result of that conversion by one pointer, leaving you with a value of type *bmap.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With