github.com/ccccaoqing/test@v0.0.0-20220510085219-3985d23445c0/src/runtime/cgo/callbacks.c (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 #include "../runtime.h" 6 #include "../cgocall.h" 7 #include "textflag.h" 8 9 // These utility functions are available to be called from code 10 // compiled with gcc via crosscall2. 11 12 // The declaration of crosscall2 is: 13 // void crosscall2(void (*fn)(void *, int), void *, int); 14 // 15 // We need to export the symbol crosscall2 in order to support 16 // callbacks from shared libraries. This applies regardless of 17 // linking mode. 18 #pragma cgo_export_static crosscall2 19 #pragma cgo_export_dynamic crosscall2 20 21 // Allocate memory. This allocates the requested number of bytes in 22 // memory controlled by the Go runtime. The allocated memory will be 23 // zeroed. You are responsible for ensuring that the Go garbage 24 // collector can see a pointer to the allocated memory for as long as 25 // it is valid, e.g., by storing a pointer in a local variable in your 26 // C function, or in memory allocated by the Go runtime. If the only 27 // pointers are in a C global variable or in memory allocated via 28 // malloc, then the Go garbage collector may collect the memory. 29 30 // Call like this in code compiled with gcc: 31 // struct { size_t len; void *ret; } a; 32 // a.len = /* number of bytes to allocate */; 33 // crosscall2(_cgo_allocate, &a, sizeof a); 34 // /* Here a.ret is a pointer to the allocated memory. */ 35 36 void runtime·_cgo_allocate_internal(void); 37 38 #pragma cgo_export_static _cgo_allocate 39 #pragma cgo_export_dynamic _cgo_allocate 40 #pragma textflag NOSPLIT 41 void 42 _cgo_allocate(void *a, int32 n) 43 { 44 runtime·cgocallback((void(*)(void))runtime·_cgo_allocate_internal, a, n); 45 } 46 47 // Panic. The argument is converted into a Go string. 48 49 // Call like this in code compiled with gcc: 50 // struct { const char *p; } a; 51 // a.p = /* string to pass to panic */; 52 // crosscall2(_cgo_panic, &a, sizeof a); 53 // /* The function call will not return. */ 54 55 void runtime·_cgo_panic_internal(void); 56 57 #pragma cgo_export_static _cgo_panic 58 #pragma cgo_export_dynamic _cgo_panic 59 #pragma textflag NOSPLIT 60 void 61 _cgo_panic(void *a, int32 n) 62 { 63 runtime·cgocallback((void(*)(void))runtime·_cgo_panic_internal, a, n); 64 } 65 66 #pragma cgo_import_static x_cgo_init 67 extern void x_cgo_init(G*); 68 void (*_cgo_init)(G*) = x_cgo_init; 69 70 #pragma cgo_import_static x_cgo_malloc 71 extern void x_cgo_malloc(void*); 72 void (*_cgo_malloc)(void*) = x_cgo_malloc; 73 74 #pragma cgo_import_static x_cgo_free 75 extern void x_cgo_free(void*); 76 void (*_cgo_free)(void*) = x_cgo_free; 77 78 #pragma cgo_import_static x_cgo_thread_start 79 extern void x_cgo_thread_start(void*); 80 void (*_cgo_thread_start)(void*) = x_cgo_thread_start; 81 82 #pragma cgo_export_static _cgo_topofstack 83 #pragma cgo_export_dynamic _cgo_topofstack