contributed by < lineagech
>
typeof
typeof is a compiler extension
offsetof
Using 0 address and cast it into type pointer, and then points to member to get the offset from 0 address, and finally cast to size_t.
Suffix rules are rules of the form .c.o. They are a way of telling make that any time you see, say, a .f file (the source file), you can make a .o file (the target file) from it by following that rule, where $< indicates the source file and $@ represents the target file.
Static pattern rules are rules which specify multiple targets and construct the prerequisite names for each target based on the target name. They are more general than ordinary rules with multiple targets because the targets do not have to have identical prerequisites. Their prerequisites must be analogous, but not necessarily identical.
$(objects): %.o: %.c
$(CC) -c $(CFLAGS) $< -o $@
列出 Linux 核心程式碼時,需要標注版本資訊,避免資訊過時
:notes: jserv
list_head
in task_struct in include/linux/sched.h
, and there is a run_list
, and its operation like real-time scheduling: push a list of entities to a priority queue according to the entity's prioritypage
according to its list_head lru
. Here I guess it sorts pages according to lru policy and then can find relative struct ebased on the list_head noe:arch/arm/mm/mmu.c
, linux memory management maintains a static virtual memory which may be used by ioremap/vmalloc(?) list. The follwoing code fill dummy vm entries by traversing the list.GNU extension 的 typeof 有何作用?在程式碼中扮演什麼角色?
It can refer to type of an expression.
e.g.
#define pointer(T) typeof(T *)
#define array(T, N) typeof(T [N])
In pratice we can just get the type we wanted dynamically and like use it in macro.
解釋以下巨集的原理
__extension__: GCC uses the extension attribute when using the -ansi/-pedantic flag to avoid warnings in headers with GCC extensions, such as ({…}).
we can get the pointer __pmember
type of type->member
in struct by using typeof so that we dont need to know what type it is explicitly.
And subtract the offset, from starting address to address of member
by offsetof
.
Finally it gets the address of object containing member whose address is ptr
.
除了你熟悉的 add 和 delete 操作,list.h
還定義一系列操作,為什麼呢?這些有什麼益處?
Ans. In addition to add and delete, there are other operations like:
I think kernel maintains lots of lists which is sorted based on some criterions. Due to those macro operations, it can add or remove a node even a list to or from another more efficiently.
LIST_POISONING
這樣的設計有何意義?
It is defined in linux/position.h
. And comment is
Linux purposely let head point to specific addresses and page faults will happen. The design can make kernel know someone has accessed empty list, easy to debug(?), not very sure.
linked list 採用環狀是基於哪些考量?
I think it is a performance issue. Based on the operations defined in linux.h
, sometimes it needs to add/delete a list at the head of tail. So circular list will make operations easier.
什麼情境會需要對 linked list 做排序呢?舉出真實世界的應用,最好在作業系統核心出現
Like priority-based scheduling, kernel will schedule process according to its priority value, and also the number of process is not fixed. So better using list and sort it.
什麼情境會需要需要找到 第 k 大/小元素 呢?又,你打算如何實作?
*
list_for_each_safe
和 list_for_each
的差異在哪?"safe" 在執行時期的影響為何?
list_for_each_safe additionaly memorize next pointer so that it allows users to delete node after getting it and will not affect traversing.
for_each 風格的開發方式對程式開發者的影響為何?
for_each is more like python as:
It hide for-loop details and need simple one line. I think it is a more concise way for programmer.
程式註解裡頭大量存在 @
符號,這有何意義?你能否應用在後續的程式開發呢?
It is specified by Doxygen rules:
Like function parameters, we can just specify @param etc. It is a clearer way for others tracing code afterwards
tests/
目錄底下的 unit test 的作用為何?就軟體工程來說的精神為何?
Unit test means that testing every single function you have and write some test programs or scripts to test it. Like those *.c
files in the tests/
, every *.c
is just a prgram to verify each list operation using assert
tests/
目錄的 unit test 可如何持續精進和改善呢?
Under example/
folder, I just modify the source c file to get user input indicating unsorted array size and Makefile to do multiple unit tests automatically, the same as applying to tests/
:
不需要張貼完整程式碼 (置於 GitHub 即可),相反的,你應該闡述實作背後的思維及考量點,特別要談如何驗證功能
:notes: jserv
merge-sort.c
:
list_mergesort
list_split
list_merge
.c
files. Type make
to compile all source files and type make test
to do unit test automatically: