github.com/moontrade/nogc@v0.1.7/mem_tinygowasm.go (about)

     1  //go:build tinygo && tinygo.wasm
     2  // +build tinygo,tinygo.wasm
     3  
     4  package nogc
     5  
     6  import (
     7  	"unsafe"
     8  )
     9  
    10  func Compare(a, b unsafe.Pointer, n uintptr) int {
    11  	if a == nil {
    12  		if b == nil {
    13  			return 0
    14  		}
    15  		return -1
    16  	}
    17  	ab := *(*string)(unsafe.Pointer(&_bytes{
    18  		Data: uintptr(a),
    19  		Len:  int(n),
    20  	}))
    21  	bb := *(*string)(unsafe.Pointer(&_bytes{
    22  		Data: uintptr(b),
    23  		Len:  int(n),
    24  	}))
    25  	if ab < bb {
    26  		return -1
    27  	}
    28  	if ab == bb {
    29  		return 0
    30  	}
    31  	return 1
    32  }
    33  
    34  ////go:linkname gcMemcpy runtime.gcMemcpy
    35  //func gcMemcpy(dst, src unsafe.Pointer, n uintptr)
    36  
    37  //go:linkname Copy runtime.gcMemcpy
    38  func Copy(dst, src unsafe.Pointer, n uintptr)
    39  
    40  //go:linkname Move runtime.gcMemmove
    41  func Move(dst, src unsafe.Pointer, size uintptr)
    42  
    43  //func Memmove(dst, src unsafe.Pointer, size uintptr) {
    44  //	gcMemmove(dst, src, size)
    45  //}
    46  
    47  //go:linkname Zero runtime.gcZero
    48  func Zero(ptr unsafe.Pointer, size uintptr)
    49  
    50  //func Memzero(ptr unsafe.Pointer, size uintptr) {
    51  //	gcZero(ptr, size)
    52  //}
    53  
    54  func zeroSlow(ptr unsafe.Pointer, size uintptr) {
    55  	b := *(*[]byte)(unsafe.Pointer(&_bytes{
    56  		Data: uintptr(ptr),
    57  		Len:  int(size),
    58  		Cap:  int(size),
    59  	}))
    60  	switch {
    61  	case size < 8:
    62  		for i := 0; i < len(b); i++ {
    63  			b[i] = 0
    64  		}
    65  	case size == 8:
    66  		*(*uint64)(unsafe.Pointer(&b[0])) = 0
    67  	default:
    68  		var i = 0
    69  		for ; i <= len(b)-8; i += 8 {
    70  			*(*uint64)(unsafe.Pointer(&b[i])) = 0
    71  		}
    72  		for ; i < len(b); i++ {
    73  			b[i] = 0
    74  		}
    75  	}
    76  }
    77  
    78  //go:linkname Equals runtime.gcMemequal
    79  func Equals(x, y unsafe.Pointer, size uintptr) bool