From 67ed19bdd512e15da56e6719b741835fcb62b099 Mon Sep 17 00:00:00 2001 From: chai2010 Date: Fri, 14 Dec 2018 21:41:22 +0800 Subject: [PATCH] =?UTF-8?q?ch2:=20=E8=A7=84=E8=8C=83=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E7=BC=96=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ch2-cgo/ch2-03-cgo-types.md | 12 ++++++------ ch2-cgo/ch2-05-internal.md | 12 ++++++------ ...y-ptr.plantuml => ch2-1-x-ptr-to-y-ptr.plantuml} | 0 ...o-y-ptr.uml.png => ch2-1-x-ptr-to-y-ptr.uml.png} | Bin ...tr.plantuml => ch2-2-int32-to-char-ptr.plantuml} | 0 ...-ptr.uml.png => ch2-2-int32-to-char-ptr.uml.png} | Bin ...e.plantuml => ch2-3-x-slice-to-y-slice.plantuml} | 0 ...ice.uml.png => ch2-3-x-slice-to-y-slice.uml.png} | Bin ...ated-files.dot => ch2-4-cgo-generated-files.dot} | 0 ...es.dot.png => ch2-4-cgo-generated-files.dot.png} | Bin ...sum-v1.plantuml => ch2-5-call-c-sum-v1.plantuml} | 0 ...c-sum-v1.uml.png => ch2-5-call-c-sum-v1.uml.png} | Bin ...sum-v2.plantuml => ch2-6-call-c-sum-v2.plantuml} | 0 ...c-sum-v2.uml.png => ch2-6-call-c-sum-v2.uml.png} | Bin 14 files changed, 12 insertions(+), 12 deletions(-) rename images/{ch2.3-1-x-ptr-to-y-ptr.plantuml => ch2-1-x-ptr-to-y-ptr.plantuml} (100%) rename images/{ch2.3-1-x-ptr-to-y-ptr.uml.png => ch2-1-x-ptr-to-y-ptr.uml.png} (100%) rename images/{ch2.3-2-int32-to-char-ptr.plantuml => ch2-2-int32-to-char-ptr.plantuml} (100%) rename images/{ch2.3-2-int32-to-char-ptr.uml.png => ch2-2-int32-to-char-ptr.uml.png} (100%) rename images/{ch2.3-3-x-slice-to-y-slice.plantuml => ch2-3-x-slice-to-y-slice.plantuml} (100%) rename images/{ch2.3-3-x-slice-to-y-slice.uml.png => ch2-3-x-slice-to-y-slice.uml.png} (100%) rename images/{ch2.5-1-cgo-generated-files.dot => ch2-4-cgo-generated-files.dot} (100%) rename images/{ch2.5-1-cgo-generated-files.dot.png => ch2-4-cgo-generated-files.dot.png} (100%) rename images/{ch2.5-2-call-c-sum-v1.plantuml => ch2-5-call-c-sum-v1.plantuml} (100%) rename images/{ch2.5-2-call-c-sum-v1.uml.png => ch2-5-call-c-sum-v1.uml.png} (100%) rename images/{ch2.5-3-call-c-sum-v2.plantuml => ch2-6-call-c-sum-v2.plantuml} (100%) rename images/{ch2.5-3-call-c-sum-v2.uml.png => ch2-6-call-c-sum-v2.uml.png} (100%) diff --git a/ch2-cgo/ch2-03-cgo-types.md b/ch2-cgo/ch2-03-cgo-types.md index 1e277f4..5c3416a 100644 --- a/ch2-cgo/ch2-03-cgo-types.md +++ b/ch2-cgo/ch2-03-cgo-types.md @@ -378,9 +378,9 @@ p = (*X)(unsafe.Pointer(q)) // *Y => *X 下面是指针间的转换流程的示意图: -![](../images/ch2.3-1-x-ptr-to-y-ptr.uml.png) +![](../images/ch2-1-x-ptr-to-y-ptr.uml.png) -*图 2.3-1 X类型指针转Y类型指针* +*图 2-1 X类型指针转Y类型指针* 任何类型的指针都可以通过强制转换为`unsafe.Pointer`指针类型去掉原有的类型信息,然后再重新赋予新的指针类型而达到指针间的转换的目的。 @@ -393,9 +393,9 @@ p = (*X)(unsafe.Pointer(q)) // *Y => *X 下面流程图演示了如何实现int32类型到C语言的`char*`字符串指针类型的相互转换: -![](../images/ch2.3-2-int32-to-char-ptr.uml.png) +![](../images/ch2-2-int32-to-char-ptr.uml.png) -*图 2.3-2 int32和`char*`指针转换* +*图 2-2 int32和`char*`指针转换* 转换分为几个阶段,在每个阶段实现一个小目标:首先是int32到uintptr类型,然后是uintptr到`unsafe.Pointr`指针类型,最后是`unsafe.Pointr`指针类型到`*C.char`类型。 @@ -422,9 +422,9 @@ pHdr.Cap = qHdr.Cap * unsafe.Sizeof(q[0]) / unsafe.Sizeof(p[0]) 下面演示了切片间的转换的具体流程: -![](../images/ch2.3-3-x-slice-to-y-slice.uml.png) +![](../images/ch2-3-x-slice-to-y-slice.uml.png) -*图 2.3-3 X类型切片转Y类型切片* +*图 2-3 X类型切片转Y类型切片* 针对CGO中常用的功能,作者封装了 "github.com/chai2010/cgo" 包,提供基本的转换功能,具体的细节可以参考实现代码。 diff --git a/ch2-cgo/ch2-05-internal.md b/ch2-cgo/ch2-05-internal.md index 6bff6f1..1dc0777 100644 --- a/ch2-cgo/ch2-05-internal.md +++ b/ch2-cgo/ch2-05-internal.md @@ -8,9 +8,9 @@ 在一个Go源文件中,如果出现了`import "C"`指令则表示将调用cgo命令生成对应的中间文件。下图是cgo生成的中间文件的简单示意图: -![](../images/ch2.5-1-cgo-generated-files.dot.png) +![](../images/ch2-4-cgo-generated-files.dot.png) -*图 2.5-1 cgo生成的中间文件* +*图 2-4 cgo生成的中间文件* 包中有4个Go文件,其中nocgo开头的文件中没有`import "C"`指令,其它的2个文件则包含了cgo代码。cgo命令会为每个包含了cgo代码的Go文件创建2个中间文件,比如 main.go 会分别创建 main.cgo1.go 和 main.cgo2.c 两个中间文件。然后会为整个包创建一个 `_cgo_gotypes.go` Go文件,其中包含Go语言部分辅助代码。此外还会创建一个 `_cgo_export.h` 和 `_cgo_export.c` 文件,对应Go语言导出到C语言的类型和函数。 @@ -135,9 +135,9 @@ void _cgo_506f45f9fa85_Cfunc_sum(void *v) { `C.sum`的整个调用流程图如下: -![](../images/ch2.5-2-call-c-sum-v1.uml.png) +![](../images/ch2-5-call-c-sum-v1.uml.png) -*图 2.5-2 调用C函数* +*图 2-5 调用C函数* 其中`runtime.cgocall`函数是实现Go语言到C语言函数跨界调用的关键。更详细的细节可以参考 https://golang.org/src/cmd/cgo/doc.go 内部的代码注释和 `runtime.cgocall` 函数的实现。 @@ -248,9 +248,9 @@ func runtime.cgocallback(fn, frame unsafe.Pointer, framesize, ctxt uintptr) 整个调用流程图如下: -![](../images/ch2.5-3-call-c-sum-v2.uml.png) +![](../images/ch2-6-call-c-sum-v2.uml.png) -*图 2.5-3 调用导出的Go函数* +*图 2-6 调用导出的Go函数* 其中`runtime.cgocallback`函数是实现C语言到Go语言函数跨界调用的关键。更详细的细节可以参考相关函数的实现。 diff --git a/images/ch2.3-1-x-ptr-to-y-ptr.plantuml b/images/ch2-1-x-ptr-to-y-ptr.plantuml similarity index 100% rename from images/ch2.3-1-x-ptr-to-y-ptr.plantuml rename to images/ch2-1-x-ptr-to-y-ptr.plantuml diff --git a/images/ch2.3-1-x-ptr-to-y-ptr.uml.png b/images/ch2-1-x-ptr-to-y-ptr.uml.png similarity index 100% rename from images/ch2.3-1-x-ptr-to-y-ptr.uml.png rename to images/ch2-1-x-ptr-to-y-ptr.uml.png diff --git a/images/ch2.3-2-int32-to-char-ptr.plantuml b/images/ch2-2-int32-to-char-ptr.plantuml similarity index 100% rename from images/ch2.3-2-int32-to-char-ptr.plantuml rename to images/ch2-2-int32-to-char-ptr.plantuml diff --git a/images/ch2.3-2-int32-to-char-ptr.uml.png b/images/ch2-2-int32-to-char-ptr.uml.png similarity index 100% rename from images/ch2.3-2-int32-to-char-ptr.uml.png rename to images/ch2-2-int32-to-char-ptr.uml.png diff --git a/images/ch2.3-3-x-slice-to-y-slice.plantuml b/images/ch2-3-x-slice-to-y-slice.plantuml similarity index 100% rename from images/ch2.3-3-x-slice-to-y-slice.plantuml rename to images/ch2-3-x-slice-to-y-slice.plantuml diff --git a/images/ch2.3-3-x-slice-to-y-slice.uml.png b/images/ch2-3-x-slice-to-y-slice.uml.png similarity index 100% rename from images/ch2.3-3-x-slice-to-y-slice.uml.png rename to images/ch2-3-x-slice-to-y-slice.uml.png diff --git a/images/ch2.5-1-cgo-generated-files.dot b/images/ch2-4-cgo-generated-files.dot similarity index 100% rename from images/ch2.5-1-cgo-generated-files.dot rename to images/ch2-4-cgo-generated-files.dot diff --git a/images/ch2.5-1-cgo-generated-files.dot.png b/images/ch2-4-cgo-generated-files.dot.png similarity index 100% rename from images/ch2.5-1-cgo-generated-files.dot.png rename to images/ch2-4-cgo-generated-files.dot.png diff --git a/images/ch2.5-2-call-c-sum-v1.plantuml b/images/ch2-5-call-c-sum-v1.plantuml similarity index 100% rename from images/ch2.5-2-call-c-sum-v1.plantuml rename to images/ch2-5-call-c-sum-v1.plantuml diff --git a/images/ch2.5-2-call-c-sum-v1.uml.png b/images/ch2-5-call-c-sum-v1.uml.png similarity index 100% rename from images/ch2.5-2-call-c-sum-v1.uml.png rename to images/ch2-5-call-c-sum-v1.uml.png diff --git a/images/ch2.5-3-call-c-sum-v2.plantuml b/images/ch2-6-call-c-sum-v2.plantuml similarity index 100% rename from images/ch2.5-3-call-c-sum-v2.plantuml rename to images/ch2-6-call-c-sum-v2.plantuml diff --git a/images/ch2.5-3-call-c-sum-v2.uml.png b/images/ch2-6-call-c-sum-v2.uml.png similarity index 100% rename from images/ch2.5-3-call-c-sum-v2.uml.png rename to images/ch2-6-call-c-sum-v2.uml.png