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