From f558654e09d8d87a674400a1ab91945d022c609f Mon Sep 17 00:00:00 2001 From: chai2010 Date: Tue, 14 Aug 2018 21:38:11 +0800 Subject: [PATCH] =?UTF-8?q?ch2:=20=E8=A7=84=E8=8C=83=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ch2-cgo/ch2-03-cgo-types.md | 6 +++--- ch2-cgo/ch2-05-internal.md | 6 +++--- ...ptr.plantuml => ch2.3-1-x-ptr-to-y-ptr.plantuml} | 0 ...y-ptr.uml.png => ch2.3-1-x-ptr-to-y-ptr.uml.png} | Bin ....plantuml => ch2.3-2-int32-to-char-ptr.plantuml} | 0 ...tr.uml.png => ch2.3-2-int32-to-char-ptr.uml.png} | Bin ...plantuml => ch2.3-3-x-slice-to-y-slice.plantuml} | 0 ...e.uml.png => ch2.3-3-x-slice-to-y-slice.uml.png} | Bin ...ed-files.dot => ch2.5-1-cgo-generated-files.dot} | 0 ....dot.png => ch2.5-1-cgo-generated-files.dot.png} | Bin ...m-v1.plantuml => ch2.5-2-call-c-sum-v1.plantuml} | 0 ...sum-v1.uml.png => ch2.5-2-call-c-sum-v1.uml.png} | Bin ...m-v2.plantuml => ch2.5-3-call-c-sum-v2.plantuml} | 0 ...sum-v2.uml.png => ch2.5-3-call-c-sum-v2.uml.png} | Bin 14 files changed, 6 insertions(+), 6 deletions(-) rename images/{ch2-x-ptr-to-y-ptr.plantuml => ch2.3-1-x-ptr-to-y-ptr.plantuml} (100%) rename images/{ch2-x-ptr-to-y-ptr.uml.png => ch2.3-1-x-ptr-to-y-ptr.uml.png} (100%) rename images/{ch2-int32-to-char-ptr.plantuml => ch2.3-2-int32-to-char-ptr.plantuml} (100%) rename images/{ch2-int32-to-char-ptr.uml.png => ch2.3-2-int32-to-char-ptr.uml.png} (100%) rename images/{ch2-x-slice-to-y-slice.plantuml => ch2.3-3-x-slice-to-y-slice.plantuml} (100%) rename images/{ch2-x-slice-to-y-slice.uml.png => ch2.3-3-x-slice-to-y-slice.uml.png} (100%) rename images/{ch2-cgo-generated-files.dot => ch2.5-1-cgo-generated-files.dot} (100%) rename images/{ch2-cgo-generated-files.dot.png => ch2.5-1-cgo-generated-files.dot.png} (100%) rename images/{ch2-call-c-sum-v1.plantuml => ch2.5-2-call-c-sum-v1.plantuml} (100%) rename images/{ch2-call-c-sum-v1.uml.png => ch2.5-2-call-c-sum-v1.uml.png} (100%) rename images/{ch2-call-c-sum-v2.plantuml => ch2.5-3-call-c-sum-v2.plantuml} (100%) rename images/{ch2-call-c-sum-v2.uml.png => ch2.5-3-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 2aab3c7..71d0310 100644 --- a/ch2-cgo/ch2-03-cgo-types.md +++ b/ch2-cgo/ch2-03-cgo-types.md @@ -378,7 +378,7 @@ p = (*X)(unsafe.Pointer(q)) // *Y => *X 下面是指针间的转换流程的示意图: -![](../images/ch2-x-ptr-to-y-ptr.uml.png) +![](../images/ch2.3-1-x-ptr-to-y-ptr.uml.png) 任何类型的指针都可以通过强制转换为`unsafe.Pointer`指针类型去掉原有的类型信息,然后再重新赋予新的指针类型而达到指针间的转换的目的。 @@ -390,7 +390,7 @@ p = (*X)(unsafe.Pointer(q)) // *Y => *X 下面流程图演示了如何实现int32类型到C语言的`char*`字符串指针类型的相互转换: -![](../images/ch2-int32-to-char-ptr.uml.png) +![](../images/ch2.3-2-int32-to-char-ptr.uml.png) 转换分为几个阶段,在每个阶段实现一个小目标:首先是int32到uintptr类型,然后是uintptr到`unsafe.Pointr`指针类型,最后是`unsafe.Pointr`指针类型到`*C.char`类型。 @@ -416,6 +416,6 @@ pHdr.Cap = qHdr.Cap * unsafe.Sizeof(q[0]) / unsafe.Sizeof(p[0]) 下面演示了切片间的转换的具体流程: -![](../images/ch2-x-slice-to-y-slice.uml.png) +![](../images/ch2.3-3-x-slice-to-y-slice.uml.png) 针对CGO中常用的功能,作者封装了 "github.com/chai2010/cgo" 包,提供基本的转换功能,具体的细节可以参考实现代码。 diff --git a/ch2-cgo/ch2-05-internal.md b/ch2-cgo/ch2-05-internal.md index c0ffaf4..8a7cfe8 100644 --- a/ch2-cgo/ch2-05-internal.md +++ b/ch2-cgo/ch2-05-internal.md @@ -8,7 +8,7 @@ 在一个Go源文件中,如果出现了`import "C"`指令则表示将调用cgo命令生成对应的中间文件。下图是cgo生成的中间文件的简单示意图: -![](../images/ch2-cgo-generated-files.dot.png) +![](../images/ch2.5-1-cgo-generated-files.dot.png) 包中有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语言的类型和函数。 @@ -132,7 +132,7 @@ void _cgo_506f45f9fa85_Cfunc_sum(void *v) { `C.sum`的整个调用流程图如下: -![](../images/ch2-call-c-sum-v1.uml.png) +![](../images/ch2.5-2-call-c-sum-v1.uml.png) 其中`runtime.cgocall`函数是实现Go语言到C语言函数跨界调用的关键。更详细的细节可以参考 https://golang.org/src/cmd/cgo/doc.go 内部的代码注释和 `runtime.cgocall` 函数的实现。 @@ -243,7 +243,7 @@ func runtime.cgocallback(fn, frame unsafe.Pointer, framesize, ctxt uintptr) 整个调用流程图如下: -![](../images/ch2-call-c-sum-v2.uml.png) +![](../images/ch2.5-3-call-c-sum-v2.uml.png) 其中`runtime.cgocallback`函数是实现C语言到Go语言函数跨界调用的关键。更详细的细节可以参考相关函数的实现。 diff --git a/images/ch2-x-ptr-to-y-ptr.plantuml b/images/ch2.3-1-x-ptr-to-y-ptr.plantuml similarity index 100% rename from images/ch2-x-ptr-to-y-ptr.plantuml rename to images/ch2.3-1-x-ptr-to-y-ptr.plantuml diff --git a/images/ch2-x-ptr-to-y-ptr.uml.png b/images/ch2.3-1-x-ptr-to-y-ptr.uml.png similarity index 100% rename from images/ch2-x-ptr-to-y-ptr.uml.png rename to images/ch2.3-1-x-ptr-to-y-ptr.uml.png diff --git a/images/ch2-int32-to-char-ptr.plantuml b/images/ch2.3-2-int32-to-char-ptr.plantuml similarity index 100% rename from images/ch2-int32-to-char-ptr.plantuml rename to images/ch2.3-2-int32-to-char-ptr.plantuml diff --git a/images/ch2-int32-to-char-ptr.uml.png b/images/ch2.3-2-int32-to-char-ptr.uml.png similarity index 100% rename from images/ch2-int32-to-char-ptr.uml.png rename to images/ch2.3-2-int32-to-char-ptr.uml.png diff --git a/images/ch2-x-slice-to-y-slice.plantuml b/images/ch2.3-3-x-slice-to-y-slice.plantuml similarity index 100% rename from images/ch2-x-slice-to-y-slice.plantuml rename to images/ch2.3-3-x-slice-to-y-slice.plantuml diff --git a/images/ch2-x-slice-to-y-slice.uml.png b/images/ch2.3-3-x-slice-to-y-slice.uml.png similarity index 100% rename from images/ch2-x-slice-to-y-slice.uml.png rename to images/ch2.3-3-x-slice-to-y-slice.uml.png diff --git a/images/ch2-cgo-generated-files.dot b/images/ch2.5-1-cgo-generated-files.dot similarity index 100% rename from images/ch2-cgo-generated-files.dot rename to images/ch2.5-1-cgo-generated-files.dot diff --git a/images/ch2-cgo-generated-files.dot.png b/images/ch2.5-1-cgo-generated-files.dot.png similarity index 100% rename from images/ch2-cgo-generated-files.dot.png rename to images/ch2.5-1-cgo-generated-files.dot.png diff --git a/images/ch2-call-c-sum-v1.plantuml b/images/ch2.5-2-call-c-sum-v1.plantuml similarity index 100% rename from images/ch2-call-c-sum-v1.plantuml rename to images/ch2.5-2-call-c-sum-v1.plantuml diff --git a/images/ch2-call-c-sum-v1.uml.png b/images/ch2.5-2-call-c-sum-v1.uml.png similarity index 100% rename from images/ch2-call-c-sum-v1.uml.png rename to images/ch2.5-2-call-c-sum-v1.uml.png diff --git a/images/ch2-call-c-sum-v2.plantuml b/images/ch2.5-3-call-c-sum-v2.plantuml similarity index 100% rename from images/ch2-call-c-sum-v2.plantuml rename to images/ch2.5-3-call-c-sum-v2.plantuml diff --git a/images/ch2-call-c-sum-v2.uml.png b/images/ch2.5-3-call-c-sum-v2.uml.png similarity index 100% rename from images/ch2-call-c-sum-v2.uml.png rename to images/ch2.5-3-call-c-sum-v2.uml.png