github.com/ugorji/go/codec@v1.2.13-0.20240307214044-07c54c229a5a/helper_unsafe_compiler_not_gc.go (about)

     1  // Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved.
     2  // Use of this source code is governed by a MIT license found in the LICENSE file.
     3  
     4  //go:build !safe && !codec.safe && !appengine && go1.9 && !gc
     5  // +build !safe,!codec.safe,!appengine,go1.9,!gc
     6  
     7  package codec
     8  
     9  import (
    10  	"reflect"
    11  	_ "runtime" // needed for go linkname(s)
    12  	"unsafe"
    13  )
    14  
    15  var unsafeZeroArr [1024]byte
    16  
    17  // runtime.growslice does not work with gccgo, failing with "growslice: cap out of range" error.
    18  // consequently, we just call newarray followed by typedslicecopy directly.
    19  
    20  func unsafeGrowslice(typ unsafe.Pointer, old unsafeSlice, cap, incr int) (v unsafeSlice) {
    21  	size := rtsize2(typ)
    22  	if size == 0 {
    23  		return unsafeSlice{unsafe.Pointer(&unsafeZeroArr[0]), old.Len, cap + incr}
    24  	}
    25  	newcap := int(growCap(uint(cap), uint(size), uint(incr)))
    26  	v = unsafeSlice{Data: newarray(typ, newcap), Len: old.Len, Cap: newcap}
    27  	if old.Len > 0 {
    28  		typedslicecopy(typ, v, old)
    29  	}
    30  	// memmove(v.Data, old.Data, size*uintptr(old.Len))
    31  	return
    32  }
    33  
    34  // func unsafeNew(t reflect.Type, typ unsafe.Pointer) unsafe.Pointer {
    35  // 	rv := reflect.New(t)
    36  // 	return ((*unsafeReflectValue)(unsafe.Pointer(&rv))).ptr
    37  // }
    38  
    39  // runtime.{mapassign_fastXXX, mapaccess2_fastXXX} are not supported in gollvm,
    40  // failing with "error: undefined reference" error.
    41  // so we just use runtime.{mapassign, mapaccess2} directly
    42  
    43  func mapStoresElemIndirect(elemsize uintptr) bool { return false }
    44  
    45  func mapSet(m, k, v reflect.Value, _ mapKeyFastKind, _, valIsRef bool) {
    46  	var urv = (*unsafeReflectValue)(unsafe.Pointer(&k))
    47  	var kptr = unsafeMapKVPtr(urv)
    48  	urv = (*unsafeReflectValue)(unsafe.Pointer(&v))
    49  	var vtyp = urv.typ
    50  	var vptr = unsafeMapKVPtr(urv)
    51  
    52  	urv = (*unsafeReflectValue)(unsafe.Pointer(&m))
    53  	mptr := rvRefPtr(urv)
    54  
    55  	vvptr := mapassign(urv.typ, mptr, kptr)
    56  	typedmemmove(vtyp, vvptr, vptr)
    57  }
    58  
    59  func mapGet(m, k, v reflect.Value, _ mapKeyFastKind, _, valIsRef bool) (_ reflect.Value) {
    60  	var urv = (*unsafeReflectValue)(unsafe.Pointer(&k))
    61  	var kptr = unsafeMapKVPtr(urv)
    62  	urv = (*unsafeReflectValue)(unsafe.Pointer(&m))
    63  	mptr := rvRefPtr(urv)
    64  
    65  	vvptr, ok := mapaccess2(urv.typ, mptr, kptr)
    66  
    67  	if !ok {
    68  		return
    69  	}
    70  
    71  	urv = (*unsafeReflectValue)(unsafe.Pointer(&v))
    72  
    73  	if helperUnsafeDirectAssignMapEntry || valIsRef {
    74  		urv.ptr = vvptr
    75  	} else {
    76  		typedmemmove(urv.typ, urv.ptr, vvptr)
    77  	}
    78  
    79  	return v
    80  }