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