github.com/neilotoole/jsoncolor@v0.7.2-0.20231115150201-1637fae69be1/reflect_optimize.go (about)

     1  //go:build !go1.15
     2  // +build !go1.15
     3  
     4  package jsoncolor
     5  
     6  import (
     7  	"reflect"
     8  	"unsafe"
     9  )
    10  
    11  //go:linkname unsafe_NewArray reflect.unsafe_NewArray
    12  func unsafe_NewArray(rtype unsafe.Pointer, length int) unsafe.Pointer
    13  
    14  //go:linkname typedslicecopy reflect.typedslicecopy
    15  //go:noescape
    16  func typedslicecopy(elemType unsafe.Pointer, dst, src slice) int
    17  
    18  func extendSlice(t reflect.Type, s *slice, n int) slice {
    19  	elemTypeRef := t.Elem()
    20  	elemTypePtr := ((*iface)(unsafe.Pointer(&elemTypeRef))).ptr
    21  
    22  	d := slice{
    23  		data: unsafe_NewArray(elemTypePtr, n),
    24  		len:  s.len,
    25  		cap:  n,
    26  	}
    27  
    28  	typedslicecopy(elemTypePtr, d, *s)
    29  	return d
    30  }