github.com/rsc/go@v0.0.0-20150416155037-e040fd465409/src/runtime/cgo/callbacks.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package cgo 6 7 import "unsafe" 8 9 // These utility functions are available to be called from code 10 // compiled with gcc via crosscall2. 11 12 // cgocallback is defined in runtime 13 //go:linkname _runtime_cgocallback runtime.cgocallback 14 func _runtime_cgocallback(unsafe.Pointer, unsafe.Pointer, uintptr) 15 16 // The declaration of crosscall2 is: 17 // void crosscall2(void (*fn)(void *, int), void *, int); 18 // 19 // We need to export the symbol crosscall2 in order to support 20 // callbacks from shared libraries. This applies regardless of 21 // linking mode. 22 //go:cgo_export_static crosscall2 23 //go:cgo_export_dynamic crosscall2 24 25 // Allocate memory. This allocates the requested number of bytes in 26 // memory controlled by the Go runtime. The allocated memory will be 27 // zeroed. You are responsible for ensuring that the Go garbage 28 // collector can see a pointer to the allocated memory for as long as 29 // it is valid, e.g., by storing a pointer in a local variable in your 30 // C function, or in memory allocated by the Go runtime. If the only 31 // pointers are in a C global variable or in memory allocated via 32 // malloc, then the Go garbage collector may collect the memory. 33 34 // Call like this in code compiled with gcc: 35 // struct { size_t len; void *ret; } a; 36 // a.len = /* number of bytes to allocate */; 37 // crosscall2(_cgo_allocate, &a, sizeof a); 38 // /* Here a.ret is a pointer to the allocated memory. */ 39 40 //go:linkname _runtime_cgo_allocate_internal runtime._cgo_allocate_internal 41 var _runtime_cgo_allocate_internal byte 42 43 //go:linkname _cgo_allocate _cgo_allocate 44 //go:cgo_export_static _cgo_allocate 45 //go:cgo_export_dynamic _cgo_allocate 46 //go:nosplit 47 func _cgo_allocate(a unsafe.Pointer, n int32) { 48 _runtime_cgocallback(unsafe.Pointer(&_runtime_cgo_allocate_internal), a, uintptr(n)) 49 } 50 51 // Panic. The argument is converted into a Go string. 52 53 // Call like this in code compiled with gcc: 54 // struct { const char *p; } a; 55 // a.p = /* string to pass to panic */; 56 // crosscall2(_cgo_panic, &a, sizeof a); 57 // /* The function call will not return. */ 58 59 //go:linkname _runtime_cgo_panic_internal runtime._cgo_panic_internal 60 var _runtime_cgo_panic_internal byte 61 62 //go:linkname _cgo_panic _cgo_panic 63 //go:cgo_export_static _cgo_panic 64 //go:cgo_export_dynamic _cgo_panic 65 //go:nosplit 66 func _cgo_panic(a unsafe.Pointer, n int32) { 67 _runtime_cgocallback(unsafe.Pointer(&_runtime_cgo_panic_internal), a, uintptr(n)) 68 } 69 70 //go:cgo_import_static x_cgo_init 71 //go:linkname x_cgo_init x_cgo_init 72 //go:linkname _cgo_init _cgo_init 73 var x_cgo_init byte 74 var _cgo_init = &x_cgo_init 75 76 //go:cgo_import_static x_cgo_malloc 77 //go:linkname x_cgo_malloc x_cgo_malloc 78 //go:linkname _cgo_malloc _cgo_malloc 79 var x_cgo_malloc byte 80 var _cgo_malloc = &x_cgo_malloc 81 82 //go:cgo_import_static x_cgo_free 83 //go:linkname x_cgo_free x_cgo_free 84 //go:linkname _cgo_free _cgo_free 85 var x_cgo_free byte 86 var _cgo_free = &x_cgo_free 87 88 //go:cgo_import_static x_cgo_thread_start 89 //go:linkname x_cgo_thread_start x_cgo_thread_start 90 //go:linkname _cgo_thread_start _cgo_thread_start 91 var x_cgo_thread_start byte 92 var _cgo_thread_start = &x_cgo_thread_start 93 94 // Creates a new system thread without updating any Go state. 95 // 96 // This method is invoked during shared library loading to create a new OS 97 // thread to perform the runtime initialization. This method is similar to 98 // _cgo_sys_thread_start except that it doesn't update any Go state. 99 100 //go:cgo_import_static x_cgo_sys_thread_create 101 //go:linkname x_cgo_sys_thread_create x_cgo_sys_thread_create 102 //go:linkname _cgo_sys_thread_create _cgo_sys_thread_create 103 var x_cgo_sys_thread_create byte 104 var _cgo_sys_thread_create = &x_cgo_sys_thread_create 105 106 // Notifies that the runtime has been intialized. 107 // 108 // We currently block at every CGO entry point (via _cgo_wait_runtime_init_done) 109 // to ensure that the runtime has been initialized before the CGO call is 110 // executed. This is necessary for shared libraries where we kickoff runtime 111 // initialization in a separate thread and return without waiting for this 112 // thread to complete the init. 113 114 //go:cgo_import_static x_cgo_notify_runtime_init_done 115 //go:linkname x_cgo_notify_runtime_init_done x_cgo_notify_runtime_init_done 116 //go:linkname _cgo_notify_runtime_init_done _cgo_notify_runtime_init_done 117 var x_cgo_notify_runtime_init_done byte 118 var _cgo_notify_runtime_init_done = &x_cgo_notify_runtime_init_done 119 120 //go:cgo_export_static _cgo_topofstack 121 //go:cgo_export_dynamic _cgo_topofstack