Skip to content

Instantly share code, notes, and snippets.

@benbjohnson benbjohnson/README.md Secret
Last active Aug 29, 2015

Embed
What would you like to do?
CGO Overhead

Building

  1. Create an empty directory and add main.go.

  2. Run: go build -work

  3. Open the WORK directory that gets printed out.

#include "runtime.h"
#include "cgocall.h"
void ·_Cerrno(void*, int32);
void
·_Cfunc_GoString(int8 *p, String s)
{
s = runtime·gostring((byte*)p);
FLUSH(&s);
}
void
·_Cfunc_GoStringN(int8 *p, int32 l, String s)
{
s = runtime·gostringn((byte*)p, l);
FLUSH(&s);
}
void
·_Cfunc_GoBytes(int8 *p, int32 l, Slice s)
{
s = runtime·gobytes((byte*)p, l);
FLUSH(&s);
}
void
·_Cfunc_CString(String s, int8 *p)
{
p = runtime·cmalloc(s.len+1);
runtime·memmove((byte*)p, s.str, s.len);
p[s.len] = 0;
FLUSH(&p);
}
void
·_Cfunc__CMalloc(uintptr n, int8 *p)
{
p = runtime·cmalloc(n);
FLUSH(&p);
}
#pragma cgo_import_static _cgo_ca50a1aa6e8a_Cfunc_increment
void _cgo_ca50a1aa6e8a_Cfunc_increment(void*);
void
·_Cfunc_increment(struct{void *y[2];}p)
{
runtime·cgocall(_cgo_ca50a1aa6e8a_Cfunc_increment, &p);
}
/* Created by cgo - DO NOT EDIT. */
#include "_cgo_export.h"
extern void crosscall2(void (*fn)(void *, int), void *, int);
/* Created by cgo - DO NOT EDIT. */
typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef __SIZE_TYPE__ GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
typedef __complex float GoComplex64;
typedef __complex double GoComplex128;
typedef struct { char *p; GoInt n; } GoString;
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
_CGO_CFLAGS=-I /var/folders/p3/nqt2bgp17fn7_t9hr6tr_1m00000gn/T/go-build066973304/_/private/tmp/cgo/_obj/
// Created by cgo - DO NOT EDIT
package main
import "unsafe"
import "syscall"
import _ "runtime/cgo"
type _ unsafe.Pointer
func _Cerrno(dst *error, x int32) { *dst = syscall.Errno(x) }
type _Ctype_int int32
type _Ctype_void [0]byte
func _Cfunc_increment(_Ctype_int) _Ctype_int
#pragma cgo_import_dynamic dyld_stub_binder dyld_stub_binder ""
#pragma cgo_import_dynamic _ _ "/usr/lib/libSystem.B.dylib"
int main() { return 0; }
void crosscall2(void(*fn)(void*, int), void *a, int c) { }
void _cgo_allocate(void *a, int c) { }
void _cgo_panic(void *a, int c) { }
// Created by cgo - DO NOT EDIT
//line /private/tmp/cgo/main.go:1
package main
//line /private/tmp/cgo/main.go:11
//line /private/tmp/cgo/main.go:10
import "fmt"
//line /private/tmp/cgo/main.go:13
//line /private/tmp/cgo/main.go:12
func main() {
value := _Cfunc_increment(_Ctype_int(1))
fmt.Printf("value=%d\n", value)
}
#line 3 "/private/tmp/cgo/main.go"
int increment(int num) {
return num + 1;
}
// Usual nonsense: if x and y are not equal, the type will be invalid
// (have a negative array count) and an inscrutable error will come
// out of the compiler and hopefully mention "name".
#define __cgo_compile_assert_eq(x, y, name) typedef char name[(x-y)*(x-y)*-2+1];
// Check at compile time that the sizes we use match our expectations.
#define __cgo_size_assert(t, n) __cgo_compile_assert_eq(sizeof(t), n, _cgo_sizeof_##t##_is_not_##n)
__cgo_size_assert(char, 1)
__cgo_size_assert(short, 2)
__cgo_size_assert(int, 4)
typedef long long __cgo_long_long;
__cgo_size_assert(__cgo_long_long, 8)
__cgo_size_assert(float, 4)
__cgo_size_assert(double, 8)
#include <errno.h>
#include <string.h>
void
_cgo_ca50a1aa6e8a_Cfunc_increment(void *v)
{
struct {
int p0;
char __pad4[4];
int r;
char __pad12[4];
} __attribute__((__packed__)) *a = v;
a->r = increment(a->p0);
}
package main
/*
int increment(int num) {
return num + 1;
}
*/
import "C"
import "fmt"
func main() {
value := C.increment(C.int(1))
fmt.Printf("value=%d\n", value)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.