github.com/moontrade/unsafe@v0.9.1/memory/mem_std.go (about)

     1  //go:build !tinygo && !wasm && !wasi && !tinygo.wasm
     2  
     3  package memory
     4  
     5  import (
     6  	"reflect"
     7  	"unsafe"
     8  )
     9  
    10  func Compare(a, b unsafe.Pointer, n uintptr) int {
    11  	return Cmp(*(*string)(unsafe.Pointer(&reflect.StringHeader{Data: uintptr(a), Len: int(n)})),
    12  		*(*string)(unsafe.Pointer(&reflect.StringHeader{Data: uintptr(b), Len: int(n)})))
    13  }
    14  
    15  //go:linkname Cmp runtime.cmpstring
    16  func Cmp(a, b string) int
    17  
    18  func Copy(dst, src unsafe.Pointer, n uintptr) {
    19  	Move(dst, src, n)
    20  	//memcpySlow(dst, src, n)
    21  }
    22  
    23  func memcpySlow(dst, src unsafe.Pointer, n uintptr) {
    24  	//dstB := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
    25  	//	Data: uintptr(dst),
    26  	//	Len:  int(n),
    27  	//	Cap:  int(n),
    28  	//}))
    29  	//srcB := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
    30  	//	Data: uintptr(src),
    31  	//	Len:  int(n),
    32  	//	Cap:  int(n),
    33  	//}))
    34  	//copy(dstB, srcB)
    35  }
    36  
    37  // Move copies n bytes from "from" to "to".
    38  //
    39  // Move ensures that any pointer in "from" is written to "to" with
    40  // an indivisible write, so that racy reads cannot observe a
    41  // half-written pointer. This is necessary to prevent the garbage
    42  // collector from observing invalid pointers, and differs from Memmove
    43  // in unmanaged languages. However, Memmove is only required to do
    44  // this if "from" and "to" may contain pointers, which can only be the
    45  // case if "from", "to", and "n" are all be word-aligned.
    46  //
    47  // Implementations are in memmove_*.s.
    48  //
    49  //go:noescape
    50  //go:linkname Move runtime.memmove
    51  func Move(to, from unsafe.Pointer, n uintptr)
    52  
    53  func zeroSlow(ptr unsafe.Pointer, size uintptr) {
    54  	b := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
    55  		Data: uintptr(ptr),
    56  		Len:  int(size),
    57  		Cap:  int(size),
    58  	}))
    59  	switch {
    60  	case size < 8:
    61  		for i := 0; i < len(b); i++ {
    62  			b[i] = 0
    63  		}
    64  	case size == 8:
    65  		*(*uint64)(unsafe.Pointer(&b[0])) = 0
    66  	default:
    67  		var i = 0
    68  		for ; i <= len(b)-8; i += 8 {
    69  			*(*uint64)(unsafe.Pointer(&b[i])) = 0
    70  		}
    71  		for ; i < len(b); i++ {
    72  			b[i] = 0
    73  		}
    74  	}
    75  }
    76  
    77  //func Zero(ptr unsafe.Pointer, n uintptr) {
    78  //	memclrNoHeapPointers(ptr, n)
    79  //	//zeroSlow(ptr, n)
    80  //}
    81  
    82  // Zero clears n bytes starting at ptr.
    83  //
    84  // Usually you should use typedmemclr. memclrNoHeapPointers should be
    85  // used only when the caller knows that *ptr contains no heap pointers
    86  // because either:
    87  //
    88  // *ptr is initialized memory and its type is pointer-free, or
    89  //
    90  // *ptr is uninitialized memory (e.g., memory that's being reused
    91  // for a new allocation) and hence contains only "junk".
    92  //
    93  // memclrNoHeapPointers ensures that if ptr is pointer-aligned, and n
    94  // is a multiple of the pointer size, then any pointer-aligned,
    95  // pointer-sized portion is cleared atomically. Despite the function
    96  // name, this is necessary because this function is the underlying
    97  // implementation of typedmemclr and memclrHasPointers. See the doc of
    98  // Memmove for more details.
    99  //
   100  // The (CPU-specific) implementations of this function are in memclr_*.s.
   101  //
   102  //go:noescape
   103  //go:linkname Zero runtime.memclrNoHeapPointers
   104  func Zero(ptr unsafe.Pointer, n uintptr)
   105  
   106  //func Memequal(a, b unsafe.Pointer, n uintptr) bool {
   107  //	if a == nil {
   108  //		return b == nil
   109  //	}
   110  //	return *(*string)(unsafe.Pointer(&reflect.SliceHeader{
   111  //		Data: uintptr(a),
   112  //		Len:  int(n),
   113  //	})) == *(*string)(unsafe.Pointer(&reflect.SliceHeader{
   114  //		Data: uintptr(b),
   115  //		Len:  int(n),
   116  //	}))
   117  //}
   118  
   119  //go:linkname Equals runtime.memequal
   120  func Equals(a, b unsafe.Pointer, size uintptr) bool
   121  
   122  //go:linkname Fastrand runtime.fastrand
   123  func Fastrand() uint32