Home | History | Annotate | Download | only in cgo
      1 // Copyright 2009 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 "libcgo.h"
      6 
      7 /* Stub for calling malloc from Go */
      8 void
      9 x_cgo_malloc(void *p)
     10 {
     11 	struct a {
     12 		long long n;
     13 		void *ret;
     14 	} *a = p;
     15 
     16 	a->ret = malloc(a->n);
     17 	if(a->ret == NULL && a->n == 0)
     18 		a->ret = malloc(1);
     19 }
     20 
     21 /* Stub for calling free from Go */
     22 void
     23 x_cgo_free(void *p)
     24 {
     25 	struct a {
     26 		void *arg;
     27 	} *a = p;
     28 
     29 	free(a->arg);
     30 }
     31 
     32 /* Stub for creating a new thread */
     33 void
     34 x_cgo_thread_start(ThreadStart *arg)
     35 {
     36 	ThreadStart *ts;
     37 
     38 	/* Make our own copy that can persist after we return. */
     39 	ts = malloc(sizeof *ts);
     40 	if(ts == nil) {
     41 		fprintf(stderr, "runtime/cgo: out of memory in thread_start\n");
     42 		abort();
     43 	}
     44 	*ts = *arg;
     45 
     46 	_cgo_sys_thread_start(ts);	/* OS-dependent half */
     47 }
     48