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