runtime: add gc layout info for some basic types

This commit is contained in:
Damian Gryski
2024-10-03 12:03:32 -07:00
committed by Ayke
parent b8fe75a9dd
commit 6e6507bf77
6 changed files with 73 additions and 7 deletions
+21
View File
@@ -64,6 +64,7 @@
package reflect
import (
"internal/gclayout"
"internal/itoa"
"unsafe"
)
@@ -961,6 +962,26 @@ func (t *rawType) Align() int {
}
}
func (r *rawType) gcLayout() unsafe.Pointer {
kind := r.Kind()
if kind < String {
return gclayout.NoPtrs
}
switch kind {
case Pointer, UnsafePointer, Chan, Map:
return gclayout.Pointer
case String:
return gclayout.String
case Slice:
return gclayout.Slice
}
// Unknown (for now); let the conservative pointer scanning handle it
return nil
}
// FieldAlign returns the alignment if this type is used in a struct field. It
// is currently an alias for Align() but this might change in the future.
func (t *rawType) FieldAlign() int {
+5 -2
View File
@@ -1479,7 +1479,8 @@ func MakeSlice(typ Type, len, cap int) Value {
ulen := uint(len)
ucap := uint(cap)
maxSize := (^uintptr(0)) / 2
elementSize := rtype.elem().Size()
elem := rtype.elem()
elementSize := elem.Size()
if elementSize > 1 {
maxSize /= uintptr(elementSize)
}
@@ -1493,7 +1494,9 @@ func MakeSlice(typ Type, len, cap int) Value {
var slice sliceHeader
slice.cap = uintptr(ucap)
slice.len = uintptr(ulen)
slice.data = alloc(size, nil)
layout := elem.gcLayout()
slice.data = alloc(size, layout)
return Value{
typecode: rtype,