Skip to content

Commit

Permalink
use ox hugo to edit book chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
DreamAndDead committed Feb 23, 2021
1 parent bd61054 commit 8c359eb
Show file tree
Hide file tree
Showing 160 changed files with 10,062 additions and 2,011 deletions.
12 changes: 7 additions & 5 deletions book/api/api.org
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#+SETUPFILE: setup.org
#+setupfile: ../setup.org
#+export_file_name: index

#+hugo_bundle: api
#+TITLE: c api
#+DATE: <2021-01-15 五 17:48>

Expand Down Expand Up @@ -51,7 +53,7 @@ api 在外部使用,可以将 Lua 作为 C lib 来使用;
实际上,几乎全部 api 都是对栈的操作。

#+caption: lua.h
#+include: ../lua-5.1.5/src/lua.h src C -n 107 :lines "107-216"
#+include: ../../lua-5.1.5/src/lua.h src C -n 107 :lines "107-216"

在官方文档[fn:1]中,有对所有 api 功能的绝佳描述。

Expand All @@ -65,7 +67,7 @@ api 在外部使用,可以将 Lua 作为 C lib 来使用;
api 内部使用一种自定义的映射关系,将整数映射到元素的栈位置。

#+caption: lapi.c
#+include: ../lua-5.1.5/src/lapi.c src C -n 49 :lines "49-77"
#+include: ../../lua-5.1.5/src/lapi.c src C -n 49 :lines "49-77"

#+begin_src dot :file api-index2addr.png
digraph {
Expand Down Expand Up @@ -170,7 +172,7 @@ digraph {
在上面的规则之外,api 内部使用几个特别定义的索引值,

#+caption: lua.h
#+include: ../lua-5.1.5/src/lua.h src C -n 33 :lines "33-40"
#+include: ../../lua-5.1.5/src/lua.h src C -n 33 :lines "33-40"

- =LUA_REGISTRYINDEX= 索引全局状态的 =l_registry=
- =LUA_ENVIRONINDEX= 索引当前 closure 的环境
Expand All @@ -188,7 +190,7 @@ LClosure 即从 Lua 代码编译得到的函数,而 CClosure 是通过 C api
首先,CClosure 在 C 语言中需要定义为 =lua_CFunction= 类型,

#+caption: lua.h
#+include: ../lua-5.1.5/src/lua.h src C -n 52 :lines "52-53"
#+include: ../../lua-5.1.5/src/lua.h src C -n 52 :lines "52-53"

其中,在调用时,从 base 到 top 都是 CClosure 的参数,

Expand Down
24 changes: 13 additions & 11 deletions book/gc/gc.org
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#+SETUPFILE: setup.org
#+setupfile: ../setup.org
#+export_file_name: index

#+hugo_bundle: gc
#+TITLE: gc
#+DATE: <2021-01-19 二 15:16>

Expand Down Expand Up @@ -324,12 +326,12 @@ digraph {
回忆 object 章节,

#+caption: lobject.h
#+include: ../lua-5.1.5/src/lobject.h src C -n 39 :lines "39-44"
#+include: ../../lua-5.1.5/src/lobject.h src C -n 39 :lines "39-44"

每一个 GCObject 都有共同的 CommonHeader 字段,其中 marked 就是用来标识对象在 gc 过程中的状态。

#+caption: lgc.h
#+include: ../lua-5.1.5/src/lgc.h src C -n 41 :lines "41-62"
#+include: ../../lua-5.1.5/src/lgc.h src C -n 41 :lines "41-62"

#+begin_src dot :file gc-bit-mark.png
digraph {
Expand Down Expand Up @@ -357,7 +359,7 @@ marked 字节中,前 3 位标识了颜色,任意时刻最多只有 1 位为
Lua 内部的 gc 过程分为如下几个状态,

#+caption: lgc.h
#+include: ../lua-5.1.5/src/lgc.h src C -n 14 :lines "14-22"
#+include: ../../lua-5.1.5/src/lgc.h src C -n 14 :lines "14-22"

不同状态间执行不同阶段的 gc 操作,

Expand All @@ -384,20 +386,20 @@ Lua 内部通过 =g->gcstate= 来记录当前的状态。
gc 模块内部通过 =luaC_step= 来推动整个 gc 过程,

#+caption: lgc.c
#+include: ../lua-5.1.5/src/lgc.c src C -n 610 :lines "610-633"
#+include: ../../lua-5.1.5/src/lgc.c src C -n 610 :lines "610-633"

其中调用 singlestep 来进行每个 phase 操作,其中统计处理的对象空间大小的和,
达到阈值就结束此次增量 gc 过程。

#+caption: lgc.c
#+include: ../lua-5.1.5/src/lgc.c src C -n 556 :lines "556-608"
#+include: ../../lua-5.1.5/src/lgc.c src C -n 556 :lines "556-608"

** push

push 阶段从 root 开始,

#+caption: lgc.c
#+include: ../lua-5.1.5/src/lgc.c src C -n 500 :lines "500-513"
#+include: ../../lua-5.1.5/src/lgc.c src C -n 500 :lines "500-513"

从 markroot 可以看出,gc 中的 root 从 mainthread registry globalState 开始。

Expand All @@ -406,7 +408,7 @@ push 阶段从 root 开始,
pop 阶段的主要入口在 propagatemark,

#+caption: lgc.c
#+include: ../lua-5.1.5/src/lgc.c src C -n 273 :lines "273-321"
#+include: ../../lua-5.1.5/src/lgc.c src C -n 273 :lines "273-321"

其中针对不同的对象类型,进行不同的处理。

Expand All @@ -415,19 +417,19 @@ pop 阶段的主要入口在 propagatemark,
sweep 阶段通过 sweeplist 遍历并回收白色对象,

#+caption: lgc.c
#+include: ../lua-5.1.5/src/lgc.c src C -n 404 :lines "404-429"
#+include: ../../lua-5.1.5/src/lgc.c src C -n 404 :lines "404-429"

最终通过 freeobj 回收相应内存空间,

#+caption: lgc.c
#+include: ../lua-5.1.5/src/lgc.c src C -n 378 :lines "378-401"
#+include: ../../lua-5.1.5/src/lgc.c src C -n 378 :lines "378-401"

* barrier

=luaC_barrierf= 和 =luaC_barrierback= 提供了 forward barrier 和 backward barrier 的实现。

#+caption: lgc.c
#+include: ../lua-5.1.5/src/lgc.c src C -n 661 :lines "661-683"
#+include: ../../lua-5.1.5/src/lgc.c src C -n 661 :lines "661-683"

在 Lua 内部,只有 table 对象使用 backward barrier,因为其作为容器,
引用其它可变动的对象比较多,置为灰色就不用一直触发写屏障,提高效率。
Expand Down
Loading

0 comments on commit 8c359eb

Please sign in to comment.