mirror of
https://github.com/chai2010/advanced-go-programming-book.git
synced 2025-05-24 04:22:22 +00:00
准备 ch2-5 例子
This commit is contained in:
parent
fe604df575
commit
24f78ae1d8
2
.gitignore
vendored
2
.gitignore
vendored
@ -19,8 +19,6 @@ _book
|
||||
*.obj
|
||||
*.exe
|
||||
|
||||
_obj
|
||||
|
||||
# macOS
|
||||
.DS_Store
|
||||
|
||||
|
@ -14,8 +14,12 @@
|
||||
|
||||
## Go调用C函数
|
||||
|
||||

|
||||
|
||||
TODO
|
||||
|
||||
## C调用Go函数
|
||||
|
||||

|
||||
|
||||
TODO
|
||||
|
@ -1 +1,3 @@
|
||||
# 2.6. 实战: 封装qsort
|
||||
|
||||

|
||||
|
13
examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_export.c
Normal file
13
examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_export.c
Normal file
@ -0,0 +1,13 @@
|
||||
/* Created by cgo - DO NOT EDIT. */
|
||||
#include <stdlib.h>
|
||||
#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()
|
||||
|
69
examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_export.h
Normal file
69
examples/ch2-05-internal/01-cgo-gen-files/_obj/_cgo_export.h
Normal file
@ -0,0 +1,69 @@
|
||||
/* Created by "go tool cgo" - DO NOT EDIT. */
|
||||
|
||||
/* package main */
|
||||
|
||||
|
||||
#line 1 "cgo-builtin-prolog"
|
||||
|
||||
#include <stddef.h> /* 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
|
@ -0,0 +1 @@
|
||||
_CGO_CFLAGS=
|
@ -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{})
|
||||
|
@ -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"
|
@ -0,0 +1,9 @@
|
||||
// Created by cgo - DO NOT EDIT
|
||||
|
||||
//line hello.go:1
|
||||
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
package main
|
||||
|
||||
import _ "unsafe"
|
53
examples/ch2-05-internal/01-cgo-gen-files/_obj/hello.cgo2.c
Normal file
53
examples/ch2-05-internal/01-cgo-gen-files/_obj/hello.cgo2.c
Normal file
@ -0,0 +1,53 @@
|
||||
|
||||
#line 1 "cgo-builtin-prolog"
|
||||
#include <stddef.h> /* 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 <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#define CGO_NO_SANITIZE_THREAD
|
||||
#define _cgo_tsan_acquire()
|
||||
#define _cgo_tsan_release()
|
||||
|
11
examples/ch2-05-internal/01-cgo-gen-files/_obj/main.cgo1.go
Normal file
11
examples/ch2-05-internal/01-cgo-gen-files/_obj/main.cgo1.go
Normal file
@ -0,0 +1,11 @@
|
||||
// Created by cgo - DO NOT EDIT
|
||||
|
||||
//line main.go:1
|
||||
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
package main
|
||||
|
||||
import _ "unsafe"
|
||||
|
||||
func main() {}
|
53
examples/ch2-05-internal/01-cgo-gen-files/_obj/main.cgo2.c
Normal file
53
examples/ch2-05-internal/01-cgo-gen-files/_obj/main.cgo2.c
Normal file
@ -0,0 +1,53 @@
|
||||
|
||||
#line 1 "cgo-builtin-prolog"
|
||||
#include <stddef.h> /* 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 <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#define CGO_NO_SANITIZE_THREAD
|
||||
#define _cgo_tsan_acquire()
|
||||
#define _cgo_tsan_release()
|
||||
|
9
examples/ch2-05-internal/02-go-call-c-func/Makefile
Normal file
9
examples/ch2-05-internal/02-go-call-c-func/Makefile
Normal file
@ -0,0 +1,9 @@
|
||||
# Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||
# 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
|
@ -0,0 +1,13 @@
|
||||
/* Created by cgo - DO NOT EDIT. */
|
||||
#include <stdlib.h>
|
||||
#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()
|
||||
|
@ -0,0 +1,69 @@
|
||||
/* Created by "go tool cgo" - DO NOT EDIT. */
|
||||
|
||||
/* package main */
|
||||
|
||||
|
||||
#line 1 "cgo-builtin-prolog"
|
||||
|
||||
#include <stddef.h> /* 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
|
@ -0,0 +1 @@
|
||||
_CGO_CFLAGS=
|
@ -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
|
||||
}
|
@ -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"
|
14
examples/ch2-05-internal/02-go-call-c-func/_obj/main.cgo1.go
Normal file
14
examples/ch2-05-internal/02-go-call-c-func/_obj/main.cgo1.go
Normal file
@ -0,0 +1,14 @@
|
||||
// Created by cgo - DO NOT EDIT
|
||||
|
||||
//line main.go:1
|
||||
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||
// 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))
|
||||
}
|
76
examples/ch2-05-internal/02-go-call-c-func/_obj/main.cgo2.c
Normal file
76
examples/ch2-05-internal/02-go-call-c-func/_obj/main.cgo2.c
Normal file
@ -0,0 +1,76 @@
|
||||
|
||||
#line 1 "cgo-builtin-prolog"
|
||||
#include <stddef.h> /* 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 <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#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;
|
||||
}
|
||||
|
11
examples/ch2-05-internal/02-go-call-c-func/main.go
Normal file
11
examples/ch2-05-internal/02-go-call-c-func/main.go
Normal file
@ -0,0 +1,11 @@
|
||||
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||
// 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))
|
||||
}
|
14
examples/ch2-05-internal/03-c-call-go-func/Makefile
Normal file
14
examples/ch2-05-internal/03-c-call-go-func/Makefile
Normal file
@ -0,0 +1,14 @@
|
||||
# Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||
# 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
|
||||
|
@ -0,0 +1,33 @@
|
||||
/* Created by cgo - DO NOT EDIT. */
|
||||
#include <stdlib.h>
|
||||
#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;
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
/* Created by "go tool cgo" - DO NOT EDIT. */
|
||||
|
||||
/* package main */
|
||||
|
||||
|
||||
#line 1 "cgo-builtin-prolog"
|
||||
|
||||
#include <stddef.h> /* 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
|
@ -0,0 +1 @@
|
||||
_CGO_CFLAGS=
|
@ -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)
|
||||
}
|
10
examples/ch2-05-internal/03-c-call-go-func/_obj/_cgo_main.c
Normal file
10
examples/ch2-05-internal/03-c-call-go-func/_obj/_cgo_main.c
Normal file
@ -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;
|
17
examples/ch2-05-internal/03-c-call-go-func/_obj/sum.cgo1.go
Normal file
17
examples/ch2-05-internal/03-c-call-go-func/_obj/sum.cgo1.go
Normal file
@ -0,0 +1,17 @@
|
||||
// Created by cgo - DO NOT EDIT
|
||||
|
||||
//line sum.go:1
|
||||
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||
// 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() {}
|
57
examples/ch2-05-internal/03-c-call-go-func/_obj/sum.cgo2.c
Normal file
57
examples/ch2-05-internal/03-c-call-go-func/_obj/sum.cgo2.c
Normal file
@ -0,0 +1,57 @@
|
||||
|
||||
#line 1 "cgo-builtin-prolog"
|
||||
#include <stddef.h> /* 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 <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#define CGO_NO_SANITIZE_THREAD
|
||||
#define _cgo_tsan_acquire()
|
||||
#define _cgo_tsan_release()
|
||||
|
10
examples/ch2-05-internal/03-c-call-go-func/main.c
Normal file
10
examples/ch2-05-internal/03-c-call-go-func/main.c
Normal file
@ -0,0 +1,10 @@
|
||||
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
extern int sum(int a, int b);
|
||||
printf("1+1=%d\n", sum(1, 1));
|
||||
return 0;
|
||||
}
|
14
examples/ch2-05-internal/03-c-call-go-func/sum.go
Normal file
14
examples/ch2-05-internal/03-c-call-go-func/sum.go
Normal file
@ -0,0 +1,14 @@
|
||||
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||
// 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() {}
|
76
examples/ch2-05-internal/03-c-call-go-func/sum.h
Normal file
76
examples/ch2-05-internal/03-c-call-go-func/sum.h
Normal file
@ -0,0 +1,76 @@
|
||||
/* Created by "go tool cgo" - DO NOT EDIT. */
|
||||
|
||||
/* package command-line-arguments */
|
||||
|
||||
|
||||
#line 1 "cgo-builtin-prolog"
|
||||
|
||||
#include <stddef.h> /* 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
|
Loading…
x
Reference in New Issue
Block a user