github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/pkg/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 "../../../cmd/ld/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 static void 37 _cgo_allocate_internal(uintptr len, byte *ret) 38 { 39 CgoMal *c; 40 41 ret = runtime·mal(len); 42 c = runtime·mal(sizeof(*c)); 43 c->next = m->cgomal; 44 c->alloc = ret; 45 m->cgomal = c; 46 FLUSH(&ret); 47 } 48 49 #pragma cgo_export_static _cgo_allocate 50 #pragma cgo_export_dynamic _cgo_allocate 51 #pragma textflag NOSPLIT 52 void 53 _cgo_allocate(void *a, int32 n) 54 { 55 runtime·cgocallback((void(*)(void))_cgo_allocate_internal, a, n); 56 } 57 58 // Panic. The argument is converted into a Go string. 59 60 // Call like this in code compiled with gcc: 61 // struct { const char *p; } a; 62 // a.p = /* string to pass to panic */; 63 // crosscall2(_cgo_panic, &a, sizeof a); 64 // /* The function call will not return. */ 65 66 extern void ·cgoStringToEface(String, Eface*); 67 68 static void 69 _cgo_panic_internal(byte *p) 70 { 71 String s; 72 Eface err; 73 74 s = runtime·gostring(p); 75 ·cgoStringToEface(s, &err); 76 runtime·panic(err); 77 } 78 79 #pragma cgo_export_static _cgo_panic 80 #pragma cgo_export_dynamic _cgo_panic 81 #pragma textflag NOSPLIT 82 void 83 _cgo_panic(void *a, int32 n) 84 { 85 runtime·cgocallback((void(*)(void))_cgo_panic_internal, a, n); 86 } 87 88 #pragma cgo_import_static x_cgo_init 89 extern void x_cgo_init(G*); 90 void (*_cgo_init)(G*) = x_cgo_init; 91 92 #pragma cgo_import_static x_cgo_malloc 93 extern void x_cgo_malloc(void*); 94 void (*_cgo_malloc)(void*) = x_cgo_malloc; 95 96 #pragma cgo_import_static x_cgo_free 97 extern void x_cgo_free(void*); 98 void (*_cgo_free)(void*) = x_cgo_free; 99 100 #pragma cgo_import_static x_cgo_thread_start 101 extern void x_cgo_thread_start(void*); 102 void (*_cgo_thread_start)(void*) = x_cgo_thread_start;