contributed by < ldotrg
>
linux2019
在 header 中使用 inline 時,常搭配 static 出現,這樣的 static inline 函式搭配編譯器最佳化,則有 macro 的效果,並得以進行參數形態檢查
TODO: 做實驗檢查
Kernel Module 要註冊使用 char device 就必須新增一個 list head.
每次 user space 呼叫 open 裝置檔(/dev/xxx), 就會增加一個node.
Linux Code
typeof 允許我們傳入一個變數,代表的會是該變數的型態。
舉例來說:
Real world Example: 為了避免重複呼叫函式f
第三次
Why we need the container_of
?
If strcut x contains y, we could find the pointer to x given the pointer to y
__extension__
翻閱 gcc 文件中的 Alternative Keywords 提到
You can prevent such warnings within one expression by writing
__extension__
before the expression.
GNU C Extension裏面有使用到非標準的ANSI C的語法, 因此會產生警告.
如果不想要看見這個警告,只要在表達式之加入__extension__
修飾字.
__typeof__
gcc 文件中的 Alternative Keywords 可以得知__
是為了相容性考量而加的,作用和 keyword typeof
相同,在 Typeof 章節中可以得知 typeof 是用來獲取變數的型別。
((type *) 0)->member
照理來說 NULL pointer dereference 應該會出錯.
The operand of typeof is evaluated for its side effects if and only if it is an expression of variably modified type or the name of such a type.
黑人問號??
offsetof
找出 C99 規格中對應的說明。注意 offsetof
macro 定義
這裡的 &((TYPE *)0)->MEMBER
並不是求值後取地址,struct 中的成員變數地址是相對於 struct 的初始地址的,並且編譯時期就可以確定也就沒必要在運行時求值了,所以在此得到的是 0 + offset of MEMBER in struct
將 改成 ,會發現回傳值會增加
延伸閱讀: How to Use C's offsetof() Macro
{}
compound statement第一次看到時覺得很奇怪,這 macro 到底要怎麼 return(廣義上的) 結果阿?
gcc docs 的 Statement-Exprs 章節中提到,這被稱為 compound statement 且回傳值是最後一個 expression 的結果
The last thing in the compound statement should be an expression followed by a semicolon; the value of this subexpression serves as the value of the entire construct.
(type *) ((char *) __pmember - offsetof(type, member));
list.h
還定義一系列操作,為什麼呢?這些有什麼益處?LIST_POISONING
這樣的設計有何意義?故意將已刪除節點的指標指向特定的位址.
這些特別的位址可以作為分析kernel panic
Reference: Kernel Debugging wiki
list_add_tail
實做list_for_each_safe
和 list_for_each
的差異在哪?"safe" 在執行時期的影響為何?list_cut_position
分成左右兩半的listlist_splice_tail
,list_first_entry
, list_move_tail
printf
觀察排序結果.qtest
(ToDo)想要做的事情好多阿
Linux Linked List