github.com/lemon-mint/libuseful@v1.3.1-0.20220724073654-ee73785d5aa0/mem.go (about) 1 package libuseful 2 3 import ( 4 "unsafe" 5 ) 6 7 // MemMove copies n bytes from "from" to "to". 8 // 9 // MemMove ensures that any pointer in "from" is written to "to" with 10 // an indivisible write, so that racy reads cannot observe a 11 // half-written pointer. This is necessary to prevent the garbage 12 // collector from observing invalid pointers, and differs from MemMove 13 // in unmanaged languages. However, MemMove is only required to do 14 // this if "from" and "to" may contain pointers, which can only be the 15 // case if "from", "to", and "n" are all be word-aligned. 16 // 17 // Implementations are in memmove_*.s. 18 func MemMove(to, from unsafe.Pointer, n uintptr) 19 20 //MemEqual compares a and b 21 func MemEqual(a, b unsafe.Pointer, size uintptr) bool 22 23 // Add adds x to pointer p 24 func Add(p unsafe.Pointer, x uintptr) unsafe.Pointer 25 26 // SystemStack runs fn on a system stack. 27 // If SystemStack is called from the per-OS-thread (g0) stack, or 28 // if SystemStack is called from the signal handling (gsignal) stack, 29 // SystemStack calls fn directly and returns. 30 // Otherwise, SystemStack is being called from the limited stack 31 // of an ordinary goroutine. In this case, SystemStack switches 32 // to the per-OS-thread stack, calls fn, and switches back. 33 // It is common to use a func literal as the argument, in order 34 // to share inputs and outputs with the code around the call 35 // to system stack: 36 // 37 // ... set up y ... 38 // SystemStack(func() { 39 // x = bigcall(y) 40 // }) 41 // ... use x ... 42 // 43 func SystemStack(fn func()) 44 45 //go:linkname MemMove runtime.memmove 46 //go:linkname MemEqual runtime.memequal 47 //go:linkname Add runtime.add 48 //go:linkname SystemStack runtime.systemstack