From 24f78ae1d8312ba39fb92699951d005451556bb1 Mon Sep 17 00:00:00 2001 From: chai2010 Date: Thu, 26 Apr 2018 07:32:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=86=E5=A4=87=20ch2-5=20=E4=BE=8B=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 - ch2-cgo/ch2-05-internal.md | 4 + ch2-cgo/ch2-06-qsort.md | 2 + .../Makefile | 0 .../01-cgo-gen-files/_obj/_cgo_export.c | 13 ++++ .../01-cgo-gen-files/_obj/_cgo_export.h | 69 +++++++++++++++++ .../01-cgo-gen-files/_obj/_cgo_flags | 1 + .../01-cgo-gen-files/_obj/_cgo_gotypes.go | 31 ++++++++ .../01-cgo-gen-files/_obj/_cgo_main.c | 9 +++ .../01-cgo-gen-files/_obj/hello.cgo1.go | 9 +++ .../01-cgo-gen-files/_obj/hello.cgo2.c | 53 +++++++++++++ .../01-cgo-gen-files/_obj/main.cgo1.go | 11 +++ .../01-cgo-gen-files/_obj/main.cgo2.c | 53 +++++++++++++ .../hello.go | 0 .../main.go | 0 .../nocgo_1.go | 0 .../nocgo_x.go | 0 .../02-go-call-c-func/Makefile | 9 +++ .../02-go-call-c-func/_obj/_cgo_export.c | 13 ++++ .../02-go-call-c-func/_obj/_cgo_export.h | 69 +++++++++++++++++ .../02-go-call-c-func/_obj/_cgo_flags | 1 + .../02-go-call-c-func/_obj/_cgo_gotypes.go | 47 ++++++++++++ .../02-go-call-c-func/_obj/_cgo_main.c | 9 +++ .../02-go-call-c-func/_obj/main.cgo1.go | 14 ++++ .../02-go-call-c-func/_obj/main.cgo2.c | 76 +++++++++++++++++++ .../ch2-05-internal/02-go-call-c-func/main.go | 11 +++ .../03-c-call-go-func/Makefile | 14 ++++ .../03-c-call-go-func/_obj/_cgo_export.c | 33 ++++++++ .../03-c-call-go-func/_obj/_cgo_export.h | 76 +++++++++++++++++++ .../03-c-call-go-func/_obj/_cgo_flags | 1 + .../03-c-call-go-func/_obj/_cgo_gotypes.go | 46 +++++++++++ .../03-c-call-go-func/_obj/_cgo_main.c | 10 +++ .../03-c-call-go-func/_obj/sum.cgo1.go | 17 +++++ .../03-c-call-go-func/_obj/sum.cgo2.c | 57 ++++++++++++++ .../ch2-05-internal/03-c-call-go-func/main.c | 10 +++ .../ch2-05-internal/03-c-call-go-func/sum.go | 14 ++++ .../ch2-05-internal/03-c-call-go-func/sum.h | 76 +++++++++++++++++++ 37 files changed, 858 insertions(+), 2 deletions(-) rename examples/ch2-05-internal/{cgo-gen-files => 01-cgo-gen-files}/Makefile (100%) create mode 100644 examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_export.c create mode 100644 examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_export.h create mode 100644 examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_flags create mode 100644 examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_gotypes.go create mode 100644 examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_main.c create mode 100644 examples/ch2-05-internal/01-cgo-gen-files/_obj/hello.cgo1.go create mode 100644 examples/ch2-05-internal/01-cgo-gen-files/_obj/hello.cgo2.c create mode 100644 examples/ch2-05-internal/01-cgo-gen-files/_obj/main.cgo1.go create mode 100644 examples/ch2-05-internal/01-cgo-gen-files/_obj/main.cgo2.c rename examples/ch2-05-internal/{cgo-gen-files => 01-cgo-gen-files}/hello.go (100%) rename examples/ch2-05-internal/{cgo-gen-files => 01-cgo-gen-files}/main.go (100%) rename examples/ch2-05-internal/{cgo-gen-files => 01-cgo-gen-files}/nocgo_1.go (100%) rename examples/ch2-05-internal/{cgo-gen-files => 01-cgo-gen-files}/nocgo_x.go (100%) create mode 100644 examples/ch2-05-internal/02-go-call-c-func/Makefile create mode 100644 examples/ch2-05-internal/02-go-call-c-func/_obj/_cgo_export.c create mode 100644 examples/ch2-05-internal/02-go-call-c-func/_obj/_cgo_export.h create mode 100644 examples/ch2-05-internal/02-go-call-c-func/_obj/_cgo_flags create mode 100644 examples/ch2-05-internal/02-go-call-c-func/_obj/_cgo_gotypes.go create mode 100644 examples/ch2-05-internal/02-go-call-c-func/_obj/_cgo_main.c create mode 100644 examples/ch2-05-internal/02-go-call-c-func/_obj/main.cgo1.go create mode 100644 examples/ch2-05-internal/02-go-call-c-func/_obj/main.cgo2.c create mode 100644 examples/ch2-05-internal/02-go-call-c-func/main.go create mode 100644 examples/ch2-05-internal/03-c-call-go-func/Makefile create mode 100644 examples/ch2-05-internal/03-c-call-go-func/_obj/_cgo_export.c create mode 100644 examples/ch2-05-internal/03-c-call-go-func/_obj/_cgo_export.h create mode 100644 examples/ch2-05-internal/03-c-call-go-func/_obj/_cgo_flags create mode 100644 examples/ch2-05-internal/03-c-call-go-func/_obj/_cgo_gotypes.go create mode 100644 examples/ch2-05-internal/03-c-call-go-func/_obj/_cgo_main.c create mode 100644 examples/ch2-05-internal/03-c-call-go-func/_obj/sum.cgo1.go create mode 100644 examples/ch2-05-internal/03-c-call-go-func/_obj/sum.cgo2.c create mode 100644 examples/ch2-05-internal/03-c-call-go-func/main.c create mode 100644 examples/ch2-05-internal/03-c-call-go-func/sum.go create mode 100644 examples/ch2-05-internal/03-c-call-go-func/sum.h diff --git a/.gitignore b/.gitignore index 7c5fb4d..50474a3 100644 --- a/.gitignore +++ b/.gitignore @@ -19,8 +19,6 @@ _book *.obj *.exe -_obj - # macOS .DS_Store diff --git a/ch2-cgo/ch2-05-internal.md b/ch2-cgo/ch2-05-internal.md index 557cfa4..5108165 100644 --- a/ch2-cgo/ch2-05-internal.md +++ b/ch2-cgo/ch2-05-internal.md @@ -14,8 +14,12 @@ ## Go调用C函数 +![](../images/ch2-call-c-sum-v1.uml.png) + TODO ## C调用Go函数 +![](../images/ch2-call-c-sum-v2.uml.png) + TODO diff --git a/ch2-cgo/ch2-06-qsort.md b/ch2-cgo/ch2-06-qsort.md index 0aec3be..adb6f26 100644 --- a/ch2-cgo/ch2-06-qsort.md +++ b/ch2-cgo/ch2-06-qsort.md @@ -1 +1,3 @@ # 2.6. 实战: 封装qsort + +![](../images/ch2-qsort-v2.uml.png) diff --git a/examples/ch2-05-internal/cgo-gen-files/Makefile b/examples/ch2-05-internal/01-cgo-gen-files/Makefile similarity index 100% rename from examples/ch2-05-internal/cgo-gen-files/Makefile rename to examples/ch2-05-internal/01-cgo-gen-files/Makefile diff --git a/examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_export.c b/examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_export.c new file mode 100644 index 0000000..7136366 --- /dev/null +++ b/examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_export.c @@ -0,0 +1,13 @@ +/* Created by cgo - DO NOT EDIT. */ +#include +#include "_cgo_export.h" + +extern void crosscall2(void (*fn)(void *, int, __SIZE_TYPE__), void *, int, __SIZE_TYPE__); +extern __SIZE_TYPE__ _cgo_wait_runtime_init_done(); +extern void _cgo_release_context(__SIZE_TYPE__); + +extern char* _cgo_topofstack(void); +#define CGO_NO_SANITIZE_THREAD +#define _cgo_tsan_acquire() +#define _cgo_tsan_release() + diff --git a/examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_export.h b/examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_export.h new file mode 100644 index 0000000..07a5878 --- /dev/null +++ b/examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_export.h @@ -0,0 +1,69 @@ +/* Created by "go tool cgo" - DO NOT EDIT. */ + +/* package main */ + + +#line 1 "cgo-builtin-prolog" + +#include /* for ptrdiff_t below */ + +#ifndef GO_CGO_EXPORT_PROLOGUE_H +#define GO_CGO_EXPORT_PROLOGUE_H + +typedef struct { const char *p; ptrdiff_t n; } _GoString_; + +#endif + +/* Start of preamble from import "C" comments. */ + + + +/* End of preamble from import "C" comments. */ + + +/* Start of boilerplate cgo prologue. */ +#line 1 "cgo-gcc-export-header-prolog" + +#ifndef GO_CGO_PROLOGUE_H +#define GO_CGO_PROLOGUE_H + +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 float _Complex GoComplex64; +typedef double _Complex GoComplex128; + +/* + static assertion to make sure the file is being used on architecture + at least with matching size of GoInt. +*/ +typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1]; + +typedef _GoString_ GoString; +typedef void *GoMap; +typedef void *GoChan; +typedef struct { void *t; void *v; } GoInterface; +typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; + +#endif + +/* End of boilerplate cgo prologue. */ + +#ifdef __cplusplus +extern "C" { +#endif + + +#ifdef __cplusplus +} +#endif diff --git a/examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_flags b/examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_flags new file mode 100644 index 0000000..3c91b0f --- /dev/null +++ b/examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_flags @@ -0,0 +1 @@ +_CGO_CFLAGS= diff --git a/examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_gotypes.go b/examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_gotypes.go new file mode 100644 index 0000000..f7d984f --- /dev/null +++ b/examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_gotypes.go @@ -0,0 +1,31 @@ +// Created by cgo - DO NOT EDIT + +package main + +import "unsafe" + +import _ "runtime/cgo" + +import "syscall" + +var _ syscall.Errno +func _Cgo_ptr(ptr unsafe.Pointer) unsafe.Pointer { return ptr } + +//go:linkname _Cgo_always_false runtime.cgoAlwaysFalse +var _Cgo_always_false bool +//go:linkname _Cgo_use runtime.cgoUse +func _Cgo_use(interface{}) +type _Ctype_void [0]byte + +//go:linkname _cgo_runtime_cgocall runtime.cgocall +func _cgo_runtime_cgocall(unsafe.Pointer, uintptr) int32 + +//go:linkname _cgo_runtime_cgocallback runtime.cgocallback +func _cgo_runtime_cgocallback(unsafe.Pointer, unsafe.Pointer, uintptr, uintptr) + +//go:linkname _cgoCheckPointer runtime.cgoCheckPointer +func _cgoCheckPointer(interface{}, ...interface{}) + +//go:linkname _cgoCheckResult runtime.cgoCheckResult +func _cgoCheckResult(interface{}) + diff --git a/examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_main.c b/examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_main.c new file mode 100644 index 0000000..3a6493c --- /dev/null +++ b/examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_main.c @@ -0,0 +1,9 @@ +int main() { return 0; } +void crosscall2(void(*fn)(void*, int, __SIZE_TYPE__), void *a, int c, __SIZE_TYPE__ ctxt) { } +__SIZE_TYPE__ _cgo_wait_runtime_init_done() { return 0; } +void _cgo_release_context(__SIZE_TYPE__ ctxt) { } +char* _cgo_topofstack(void) { return (char*)0; } +void _cgo_allocate(void *a, int c) { } +void _cgo_panic(void *a, int c) { } +void _cgo_reginit(void) { } +#line 1 "cgo-generated-wrappers" diff --git a/examples/ch2-05-internal/01-cgo-gen-files/_obj/hello.cgo1.go b/examples/ch2-05-internal/01-cgo-gen-files/_obj/hello.cgo1.go new file mode 100644 index 0000000..1c03918 --- /dev/null +++ b/examples/ch2-05-internal/01-cgo-gen-files/_obj/hello.cgo1.go @@ -0,0 +1,9 @@ +// Created by cgo - DO NOT EDIT + +//line hello.go:1 +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +package main + +import _ "unsafe" diff --git a/examples/ch2-05-internal/01-cgo-gen-files/_obj/hello.cgo2.c b/examples/ch2-05-internal/01-cgo-gen-files/_obj/hello.cgo2.c new file mode 100644 index 0000000..f4ca2bd --- /dev/null +++ b/examples/ch2-05-internal/01-cgo-gen-files/_obj/hello.cgo2.c @@ -0,0 +1,53 @@ + +#line 1 "cgo-builtin-prolog" +#include /* for ptrdiff_t and size_t below */ + +/* Define intgo when compiling with GCC. */ +typedef ptrdiff_t intgo; + +typedef struct { const char *p; intgo n; } _GoString_; +typedef struct { char *p; intgo n; intgo c; } _GoBytes_; +_GoString_ GoString(char *p); +_GoString_ GoStringN(char *p, int l); +_GoBytes_ GoBytes(void *p, int n); +char *CString(_GoString_); +void *CBytes(_GoBytes_); +void *_CMalloc(size_t); + +__attribute__ ((unused)) +static size_t _GoStringLen(_GoString_ s) { return s.n; } + +__attribute__ ((unused)) +static const char *_GoStringPtr(_GoString_ s) { return s.p; } + + + +#line 1 "cgo-gcc-prolog" +/* + 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) + +extern char* _cgo_topofstack(void); + +#include +#include + + +#define CGO_NO_SANITIZE_THREAD +#define _cgo_tsan_acquire() +#define _cgo_tsan_release() + diff --git a/examples/ch2-05-internal/01-cgo-gen-files/_obj/main.cgo1.go b/examples/ch2-05-internal/01-cgo-gen-files/_obj/main.cgo1.go new file mode 100644 index 0000000..702b4b1 --- /dev/null +++ b/examples/ch2-05-internal/01-cgo-gen-files/_obj/main.cgo1.go @@ -0,0 +1,11 @@ +// Created by cgo - DO NOT EDIT + +//line main.go:1 +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +package main + +import _ "unsafe" + +func main() {} diff --git a/examples/ch2-05-internal/01-cgo-gen-files/_obj/main.cgo2.c b/examples/ch2-05-internal/01-cgo-gen-files/_obj/main.cgo2.c new file mode 100644 index 0000000..f4ca2bd --- /dev/null +++ b/examples/ch2-05-internal/01-cgo-gen-files/_obj/main.cgo2.c @@ -0,0 +1,53 @@ + +#line 1 "cgo-builtin-prolog" +#include /* for ptrdiff_t and size_t below */ + +/* Define intgo when compiling with GCC. */ +typedef ptrdiff_t intgo; + +typedef struct { const char *p; intgo n; } _GoString_; +typedef struct { char *p; intgo n; intgo c; } _GoBytes_; +_GoString_ GoString(char *p); +_GoString_ GoStringN(char *p, int l); +_GoBytes_ GoBytes(void *p, int n); +char *CString(_GoString_); +void *CBytes(_GoBytes_); +void *_CMalloc(size_t); + +__attribute__ ((unused)) +static size_t _GoStringLen(_GoString_ s) { return s.n; } + +__attribute__ ((unused)) +static const char *_GoStringPtr(_GoString_ s) { return s.p; } + + + +#line 1 "cgo-gcc-prolog" +/* + 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) + +extern char* _cgo_topofstack(void); + +#include +#include + + +#define CGO_NO_SANITIZE_THREAD +#define _cgo_tsan_acquire() +#define _cgo_tsan_release() + diff --git a/examples/ch2-05-internal/cgo-gen-files/hello.go b/examples/ch2-05-internal/01-cgo-gen-files/hello.go similarity index 100% rename from examples/ch2-05-internal/cgo-gen-files/hello.go rename to examples/ch2-05-internal/01-cgo-gen-files/hello.go diff --git a/examples/ch2-05-internal/cgo-gen-files/main.go b/examples/ch2-05-internal/01-cgo-gen-files/main.go similarity index 100% rename from examples/ch2-05-internal/cgo-gen-files/main.go rename to examples/ch2-05-internal/01-cgo-gen-files/main.go diff --git a/examples/ch2-05-internal/cgo-gen-files/nocgo_1.go b/examples/ch2-05-internal/01-cgo-gen-files/nocgo_1.go similarity index 100% rename from examples/ch2-05-internal/cgo-gen-files/nocgo_1.go rename to examples/ch2-05-internal/01-cgo-gen-files/nocgo_1.go diff --git a/examples/ch2-05-internal/cgo-gen-files/nocgo_x.go b/examples/ch2-05-internal/01-cgo-gen-files/nocgo_x.go similarity index 100% rename from examples/ch2-05-internal/cgo-gen-files/nocgo_x.go rename to examples/ch2-05-internal/01-cgo-gen-files/nocgo_x.go diff --git a/examples/ch2-05-internal/02-go-call-c-func/Makefile b/examples/ch2-05-internal/02-go-call-c-func/Makefile new file mode 100644 index 0000000..f1ab967 --- /dev/null +++ b/examples/ch2-05-internal/02-go-call-c-func/Makefile @@ -0,0 +1,9 @@ +# Copyright © 2017 ChaiShushan . +# License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +default: clean + go tool cgo main.go + go run main.go + +clean: + -rm -rf ./_obj diff --git a/examples/ch2-05-internal/02-go-call-c-func/_obj/_cgo_export.c b/examples/ch2-05-internal/02-go-call-c-func/_obj/_cgo_export.c new file mode 100644 index 0000000..7136366 --- /dev/null +++ b/examples/ch2-05-internal/02-go-call-c-func/_obj/_cgo_export.c @@ -0,0 +1,13 @@ +/* Created by cgo - DO NOT EDIT. */ +#include +#include "_cgo_export.h" + +extern void crosscall2(void (*fn)(void *, int, __SIZE_TYPE__), void *, int, __SIZE_TYPE__); +extern __SIZE_TYPE__ _cgo_wait_runtime_init_done(); +extern void _cgo_release_context(__SIZE_TYPE__); + +extern char* _cgo_topofstack(void); +#define CGO_NO_SANITIZE_THREAD +#define _cgo_tsan_acquire() +#define _cgo_tsan_release() + diff --git a/examples/ch2-05-internal/02-go-call-c-func/_obj/_cgo_export.h b/examples/ch2-05-internal/02-go-call-c-func/_obj/_cgo_export.h new file mode 100644 index 0000000..07a5878 --- /dev/null +++ b/examples/ch2-05-internal/02-go-call-c-func/_obj/_cgo_export.h @@ -0,0 +1,69 @@ +/* Created by "go tool cgo" - DO NOT EDIT. */ + +/* package main */ + + +#line 1 "cgo-builtin-prolog" + +#include /* for ptrdiff_t below */ + +#ifndef GO_CGO_EXPORT_PROLOGUE_H +#define GO_CGO_EXPORT_PROLOGUE_H + +typedef struct { const char *p; ptrdiff_t n; } _GoString_; + +#endif + +/* Start of preamble from import "C" comments. */ + + + +/* End of preamble from import "C" comments. */ + + +/* Start of boilerplate cgo prologue. */ +#line 1 "cgo-gcc-export-header-prolog" + +#ifndef GO_CGO_PROLOGUE_H +#define GO_CGO_PROLOGUE_H + +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 float _Complex GoComplex64; +typedef double _Complex GoComplex128; + +/* + static assertion to make sure the file is being used on architecture + at least with matching size of GoInt. +*/ +typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1]; + +typedef _GoString_ GoString; +typedef void *GoMap; +typedef void *GoChan; +typedef struct { void *t; void *v; } GoInterface; +typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; + +#endif + +/* End of boilerplate cgo prologue. */ + +#ifdef __cplusplus +extern "C" { +#endif + + +#ifdef __cplusplus +} +#endif diff --git a/examples/ch2-05-internal/02-go-call-c-func/_obj/_cgo_flags b/examples/ch2-05-internal/02-go-call-c-func/_obj/_cgo_flags new file mode 100644 index 0000000..3c91b0f --- /dev/null +++ b/examples/ch2-05-internal/02-go-call-c-func/_obj/_cgo_flags @@ -0,0 +1 @@ +_CGO_CFLAGS= diff --git a/examples/ch2-05-internal/02-go-call-c-func/_obj/_cgo_gotypes.go b/examples/ch2-05-internal/02-go-call-c-func/_obj/_cgo_gotypes.go new file mode 100644 index 0000000..f7b7272 --- /dev/null +++ b/examples/ch2-05-internal/02-go-call-c-func/_obj/_cgo_gotypes.go @@ -0,0 +1,47 @@ +// Created by cgo - DO NOT EDIT + +package main + +import "unsafe" + +import _ "runtime/cgo" + +import "syscall" + +var _ syscall.Errno +func _Cgo_ptr(ptr unsafe.Pointer) unsafe.Pointer { return ptr } + +//go:linkname _Cgo_always_false runtime.cgoAlwaysFalse +var _Cgo_always_false bool +//go:linkname _Cgo_use runtime.cgoUse +func _Cgo_use(interface{}) +type _Ctype_int int32 + +type _Ctype_void [0]byte + +//go:linkname _cgo_runtime_cgocall runtime.cgocall +func _cgo_runtime_cgocall(unsafe.Pointer, uintptr) int32 + +//go:linkname _cgo_runtime_cgocallback runtime.cgocallback +func _cgo_runtime_cgocallback(unsafe.Pointer, unsafe.Pointer, uintptr, uintptr) + +//go:linkname _cgoCheckPointer runtime.cgoCheckPointer +func _cgoCheckPointer(interface{}, ...interface{}) + +//go:linkname _cgoCheckResult runtime.cgoCheckResult +func _cgoCheckResult(interface{}) + +//go:cgo_import_static _cgo_506f45f9fa85_Cfunc_sum +//go:linkname __cgofn__cgo_506f45f9fa85_Cfunc_sum _cgo_506f45f9fa85_Cfunc_sum +var __cgofn__cgo_506f45f9fa85_Cfunc_sum byte +var _cgo_506f45f9fa85_Cfunc_sum = unsafe.Pointer(&__cgofn__cgo_506f45f9fa85_Cfunc_sum) + +//go:cgo_unsafe_args +func _Cfunc_sum(p0 _Ctype_int, p1 _Ctype_int) (r1 _Ctype_int) { + _cgo_runtime_cgocall(_cgo_506f45f9fa85_Cfunc_sum, uintptr(unsafe.Pointer(&p0))) + if _Cgo_always_false { + _Cgo_use(p0) + _Cgo_use(p1) + } + return +} diff --git a/examples/ch2-05-internal/02-go-call-c-func/_obj/_cgo_main.c b/examples/ch2-05-internal/02-go-call-c-func/_obj/_cgo_main.c new file mode 100644 index 0000000..3a6493c --- /dev/null +++ b/examples/ch2-05-internal/02-go-call-c-func/_obj/_cgo_main.c @@ -0,0 +1,9 @@ +int main() { return 0; } +void crosscall2(void(*fn)(void*, int, __SIZE_TYPE__), void *a, int c, __SIZE_TYPE__ ctxt) { } +__SIZE_TYPE__ _cgo_wait_runtime_init_done() { return 0; } +void _cgo_release_context(__SIZE_TYPE__ ctxt) { } +char* _cgo_topofstack(void) { return (char*)0; } +void _cgo_allocate(void *a, int c) { } +void _cgo_panic(void *a, int c) { } +void _cgo_reginit(void) { } +#line 1 "cgo-generated-wrappers" diff --git a/examples/ch2-05-internal/02-go-call-c-func/_obj/main.cgo1.go b/examples/ch2-05-internal/02-go-call-c-func/_obj/main.cgo1.go new file mode 100644 index 0000000..8d7e20b --- /dev/null +++ b/examples/ch2-05-internal/02-go-call-c-func/_obj/main.cgo1.go @@ -0,0 +1,14 @@ +// Created by cgo - DO NOT EDIT + +//line main.go:1 +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +package main + +//int sum(int a, int b) { return a+b; } +import _ "unsafe" + +func main() { + println((_Cfunc_sum)(1, 1)) +} diff --git a/examples/ch2-05-internal/02-go-call-c-func/_obj/main.cgo2.c b/examples/ch2-05-internal/02-go-call-c-func/_obj/main.cgo2.c new file mode 100644 index 0000000..bb6fe2f --- /dev/null +++ b/examples/ch2-05-internal/02-go-call-c-func/_obj/main.cgo2.c @@ -0,0 +1,76 @@ + +#line 1 "cgo-builtin-prolog" +#include /* for ptrdiff_t and size_t below */ + +/* Define intgo when compiling with GCC. */ +typedef ptrdiff_t intgo; + +typedef struct { const char *p; intgo n; } _GoString_; +typedef struct { char *p; intgo n; intgo c; } _GoBytes_; +_GoString_ GoString(char *p); +_GoString_ GoStringN(char *p, int l); +_GoBytes_ GoBytes(void *p, int n); +char *CString(_GoString_); +void *CBytes(_GoBytes_); +void *_CMalloc(size_t); + +__attribute__ ((unused)) +static size_t _GoStringLen(_GoString_ s) { return s.n; } + +__attribute__ ((unused)) +static const char *_GoStringPtr(_GoString_ s) { return s.p; } + +#line 6 "/Users/chai/go/src/github.com/chai2010/advanced-go-programming-book/examples/ch2-05-internal/02-go-call-c-func/main.go" +int sum(int a, int b) { return a+b; } + +#line 1 "cgo-generated-wrapper" + + +#line 1 "cgo-gcc-prolog" +/* + 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) + +extern char* _cgo_topofstack(void); + +#include +#include + + +#define CGO_NO_SANITIZE_THREAD +#define _cgo_tsan_acquire() +#define _cgo_tsan_release() + +CGO_NO_SANITIZE_THREAD +void +_cgo_506f45f9fa85_Cfunc_sum(void *v) +{ + struct { + int p0; + int p1; + int r; + char __pad12[4]; + } __attribute__((__packed__)) *a = v; + char *stktop = _cgo_topofstack(); + __typeof__(a->r) r; + _cgo_tsan_acquire(); + r = sum(a->p0, a->p1); + _cgo_tsan_release(); + a = (void*)((char*)a + (_cgo_topofstack() - stktop)); + a->r = r; +} + diff --git a/examples/ch2-05-internal/02-go-call-c-func/main.go b/examples/ch2-05-internal/02-go-call-c-func/main.go new file mode 100644 index 0000000..199bcaf --- /dev/null +++ b/examples/ch2-05-internal/02-go-call-c-func/main.go @@ -0,0 +1,11 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +package main + +//int sum(int a, int b) { return a+b; } +import "C" + +func main() { + println(C.sum(1, 1)) +} diff --git a/examples/ch2-05-internal/03-c-call-go-func/Makefile b/examples/ch2-05-internal/03-c-call-go-func/Makefile new file mode 100644 index 0000000..dbf20a0 --- /dev/null +++ b/examples/ch2-05-internal/03-c-call-go-func/Makefile @@ -0,0 +1,14 @@ +# Copyright © 2017 ChaiShushan . +# License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +default: + go tool cgo sum.go + go build -buildmode=c-archive -o sum.a sum.go + gcc -o a.out main.c sum.a + ./a.out + +clean: + -rm -rf ./_obj + -rm sum.a sum.h + -rm a.out + diff --git a/examples/ch2-05-internal/03-c-call-go-func/_obj/_cgo_export.c b/examples/ch2-05-internal/03-c-call-go-func/_obj/_cgo_export.c new file mode 100644 index 0000000..229ba03 --- /dev/null +++ b/examples/ch2-05-internal/03-c-call-go-func/_obj/_cgo_export.c @@ -0,0 +1,33 @@ +/* Created by cgo - DO NOT EDIT. */ +#include +#include "_cgo_export.h" + +extern void crosscall2(void (*fn)(void *, int, __SIZE_TYPE__), void *, int, __SIZE_TYPE__); +extern __SIZE_TYPE__ _cgo_wait_runtime_init_done(); +extern void _cgo_release_context(__SIZE_TYPE__); + +extern char* _cgo_topofstack(void); +#define CGO_NO_SANITIZE_THREAD +#define _cgo_tsan_acquire() +#define _cgo_tsan_release() + +extern void _cgoexp_8313eaf44386_sum(void *, int, __SIZE_TYPE__); + +CGO_NO_SANITIZE_THREAD +int sum(int p0, int p1) +{ + __SIZE_TYPE__ _cgo_ctxt = _cgo_wait_runtime_init_done(); + struct { + int p0; + int p1; + int r0; + char __pad0[4]; + } __attribute__((__packed__)) a; + a.p0 = p0; + a.p1 = p1; + _cgo_tsan_release(); + crosscall2(_cgoexp_8313eaf44386_sum, &a, 16, _cgo_ctxt); + _cgo_tsan_acquire(); + _cgo_release_context(_cgo_ctxt); + return a.r0; +} diff --git a/examples/ch2-05-internal/03-c-call-go-func/_obj/_cgo_export.h b/examples/ch2-05-internal/03-c-call-go-func/_obj/_cgo_export.h new file mode 100644 index 0000000..de66b3f --- /dev/null +++ b/examples/ch2-05-internal/03-c-call-go-func/_obj/_cgo_export.h @@ -0,0 +1,76 @@ +/* Created by "go tool cgo" - DO NOT EDIT. */ + +/* package main */ + + +#line 1 "cgo-builtin-prolog" + +#include /* for ptrdiff_t below */ + +#ifndef GO_CGO_EXPORT_PROLOGUE_H +#define GO_CGO_EXPORT_PROLOGUE_H + +typedef struct { const char *p; ptrdiff_t n; } _GoString_; + +#endif + +/* Start of preamble from import "C" comments. */ + + +#line 6 "/Users/chai/go/src/github.com/chai2010/advanced-go-programming-book/examples/ch2-05-internal/03-c-call-go-func/sum.go" +int sum(int a, int b); + +#line 1 "cgo-generated-wrapper" + + +/* End of preamble from import "C" comments. */ + + +/* Start of boilerplate cgo prologue. */ +#line 1 "cgo-gcc-export-header-prolog" + +#ifndef GO_CGO_PROLOGUE_H +#define GO_CGO_PROLOGUE_H + +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 float _Complex GoComplex64; +typedef double _Complex GoComplex128; + +/* + static assertion to make sure the file is being used on architecture + at least with matching size of GoInt. +*/ +typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1]; + +typedef _GoString_ GoString; +typedef void *GoMap; +typedef void *GoChan; +typedef struct { void *t; void *v; } GoInterface; +typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; + +#endif + +/* End of boilerplate cgo prologue. */ + +#ifdef __cplusplus +extern "C" { +#endif + + +extern int sum(int p0, int p1); + +#ifdef __cplusplus +} +#endif diff --git a/examples/ch2-05-internal/03-c-call-go-func/_obj/_cgo_flags b/examples/ch2-05-internal/03-c-call-go-func/_obj/_cgo_flags new file mode 100644 index 0000000..3c91b0f --- /dev/null +++ b/examples/ch2-05-internal/03-c-call-go-func/_obj/_cgo_flags @@ -0,0 +1 @@ +_CGO_CFLAGS= diff --git a/examples/ch2-05-internal/03-c-call-go-func/_obj/_cgo_gotypes.go b/examples/ch2-05-internal/03-c-call-go-func/_obj/_cgo_gotypes.go new file mode 100644 index 0000000..36cf41f --- /dev/null +++ b/examples/ch2-05-internal/03-c-call-go-func/_obj/_cgo_gotypes.go @@ -0,0 +1,46 @@ +// Created by cgo - DO NOT EDIT + +package main + +import "unsafe" + +import _ "runtime/cgo" + +import "syscall" + +var _ syscall.Errno +func _Cgo_ptr(ptr unsafe.Pointer) unsafe.Pointer { return ptr } + +//go:linkname _Cgo_always_false runtime.cgoAlwaysFalse +var _Cgo_always_false bool +//go:linkname _Cgo_use runtime.cgoUse +func _Cgo_use(interface{}) +type _Ctype_int int32 + +type _Ctype_void [0]byte + +//go:linkname _cgo_runtime_cgocall runtime.cgocall +func _cgo_runtime_cgocall(unsafe.Pointer, uintptr) int32 + +//go:linkname _cgo_runtime_cgocallback runtime.cgocallback +func _cgo_runtime_cgocallback(unsafe.Pointer, unsafe.Pointer, uintptr, uintptr) + +//go:linkname _cgoCheckPointer runtime.cgoCheckPointer +func _cgoCheckPointer(interface{}, ...interface{}) + +//go:linkname _cgoCheckResult runtime.cgoCheckResult +func _cgoCheckResult(interface{}) + +//go:cgo_export_dynamic sum +//go:linkname _cgoexp_8313eaf44386_sum _cgoexp_8313eaf44386_sum +//go:cgo_export_static _cgoexp_8313eaf44386_sum +//go:nosplit +//go:norace +func _cgoexp_8313eaf44386_sum(a unsafe.Pointer, n int32, ctxt uintptr) { + fn := _cgoexpwrap_8313eaf44386_sum + _cgo_runtime_cgocallback(**(**unsafe.Pointer)(unsafe.Pointer(&fn)), a, uintptr(n), ctxt); +} + +func _cgoexpwrap_8313eaf44386_sum(p0 _Ctype_int, p1 _Ctype_int) (r0 _Ctype_int) { + return sum(p0, p1) +} diff --git a/examples/ch2-05-internal/03-c-call-go-func/_obj/_cgo_main.c b/examples/ch2-05-internal/03-c-call-go-func/_obj/_cgo_main.c new file mode 100644 index 0000000..a6a61d8 --- /dev/null +++ b/examples/ch2-05-internal/03-c-call-go-func/_obj/_cgo_main.c @@ -0,0 +1,10 @@ +int main() { return 0; } +void crosscall2(void(*fn)(void*, int, __SIZE_TYPE__), void *a, int c, __SIZE_TYPE__ ctxt) { } +__SIZE_TYPE__ _cgo_wait_runtime_init_done() { return 0; } +void _cgo_release_context(__SIZE_TYPE__ ctxt) { } +char* _cgo_topofstack(void) { return (char*)0; } +void _cgo_allocate(void *a, int c) { } +void _cgo_panic(void *a, int c) { } +void _cgo_reginit(void) { } +#line 1 "cgo-generated-wrappers" +int _cgoexp_8313eaf44386_sum; diff --git a/examples/ch2-05-internal/03-c-call-go-func/_obj/sum.cgo1.go b/examples/ch2-05-internal/03-c-call-go-func/_obj/sum.cgo1.go new file mode 100644 index 0000000..be7bfb8 --- /dev/null +++ b/examples/ch2-05-internal/03-c-call-go-func/_obj/sum.cgo1.go @@ -0,0 +1,17 @@ +// Created by cgo - DO NOT EDIT + +//line sum.go:1 +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +package main + +//int sum(int a, int b); +import _ "unsafe" + +//export sum +func sum(a, b _Ctype_int) _Ctype_int { + return a + b +} + +func main() {} diff --git a/examples/ch2-05-internal/03-c-call-go-func/_obj/sum.cgo2.c b/examples/ch2-05-internal/03-c-call-go-func/_obj/sum.cgo2.c new file mode 100644 index 0000000..147c17d --- /dev/null +++ b/examples/ch2-05-internal/03-c-call-go-func/_obj/sum.cgo2.c @@ -0,0 +1,57 @@ + +#line 1 "cgo-builtin-prolog" +#include /* for ptrdiff_t and size_t below */ + +/* Define intgo when compiling with GCC. */ +typedef ptrdiff_t intgo; + +typedef struct { const char *p; intgo n; } _GoString_; +typedef struct { char *p; intgo n; intgo c; } _GoBytes_; +_GoString_ GoString(char *p); +_GoString_ GoStringN(char *p, int l); +_GoBytes_ GoBytes(void *p, int n); +char *CString(_GoString_); +void *CBytes(_GoBytes_); +void *_CMalloc(size_t); + +__attribute__ ((unused)) +static size_t _GoStringLen(_GoString_ s) { return s.n; } + +__attribute__ ((unused)) +static const char *_GoStringPtr(_GoString_ s) { return s.p; } + +#line 6 "/Users/chai/go/src/github.com/chai2010/advanced-go-programming-book/examples/ch2-05-internal/03-c-call-go-func/sum.go" +int sum(int a, int b); + +#line 1 "cgo-generated-wrapper" + + +#line 1 "cgo-gcc-prolog" +/* + 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) + +extern char* _cgo_topofstack(void); + +#include +#include + + +#define CGO_NO_SANITIZE_THREAD +#define _cgo_tsan_acquire() +#define _cgo_tsan_release() + diff --git a/examples/ch2-05-internal/03-c-call-go-func/main.c b/examples/ch2-05-internal/03-c-call-go-func/main.c new file mode 100644 index 0000000..e2e706d --- /dev/null +++ b/examples/ch2-05-internal/03-c-call-go-func/main.c @@ -0,0 +1,10 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +#include + +int main() { + extern int sum(int a, int b); + printf("1+1=%d\n", sum(1, 1)); + return 0; +} diff --git a/examples/ch2-05-internal/03-c-call-go-func/sum.go b/examples/ch2-05-internal/03-c-call-go-func/sum.go new file mode 100644 index 0000000..ab79233 --- /dev/null +++ b/examples/ch2-05-internal/03-c-call-go-func/sum.go @@ -0,0 +1,14 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +package main + +//int sum(int a, int b); +import "C" + +//export sum +func sum(a, b C.int) C.int { + return a + b +} + +func main() {} diff --git a/examples/ch2-05-internal/03-c-call-go-func/sum.h b/examples/ch2-05-internal/03-c-call-go-func/sum.h new file mode 100644 index 0000000..3f51941 --- /dev/null +++ b/examples/ch2-05-internal/03-c-call-go-func/sum.h @@ -0,0 +1,76 @@ +/* Created by "go tool cgo" - DO NOT EDIT. */ + +/* package command-line-arguments */ + + +#line 1 "cgo-builtin-prolog" + +#include /* for ptrdiff_t below */ + +#ifndef GO_CGO_EXPORT_PROLOGUE_H +#define GO_CGO_EXPORT_PROLOGUE_H + +typedef struct { const char *p; ptrdiff_t n; } _GoString_; + +#endif + +/* Start of preamble from import "C" comments. */ + + +#line 6 "/Users/chai/go/src/github.com/chai2010/advanced-go-programming-book/examples/ch2-05-internal/03-c-call-go-func/sum.go" +int sum(int a, int b); + +#line 1 "cgo-generated-wrapper" + + +/* End of preamble from import "C" comments. */ + + +/* Start of boilerplate cgo prologue. */ +#line 1 "cgo-gcc-export-header-prolog" + +#ifndef GO_CGO_PROLOGUE_H +#define GO_CGO_PROLOGUE_H + +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 float _Complex GoComplex64; +typedef double _Complex GoComplex128; + +/* + static assertion to make sure the file is being used on architecture + at least with matching size of GoInt. +*/ +typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1]; + +typedef _GoString_ GoString; +typedef void *GoMap; +typedef void *GoChan; +typedef struct { void *t; void *v; } GoInterface; +typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; + +#endif + +/* End of boilerplate cgo prologue. */ + +#ifdef __cplusplus +extern "C" { +#endif + + +extern int sum(int p0, int p1); + +#ifdef __cplusplus +} +#endif