contributed by < mickey30606
>
因為使用 ({})
,所以前面加一個 __extension__
可以不顯示 gcc 的警告訊息。
沒看過的用法 __typeof__(((type *) 0)->member)
這個可以當 member 的 type 使用。
突然發現下面有清楚的解釋。
這樣就可以知道原本這個 struct 的位址。
建立一個 struct 的型別,用來作為 doubly-linked list 的節點。
宣告一個 list_head 然後初始化,將兩邊都指向自己。
初始化一個空的 list_head 。
將 node 加入整個 doubly-linked list 的最尾端。
將 node 在 doubly-linked list 中刪除。
檢查這個 doubly-linked list 中是不是沒有任何 node 。
檢查這個 doubly-linked list 中是不是只有一個 node 。
將 list 中的 node 接入 head 這個 doubly-linked list 的尾端。
這兩個:
會變成:
list 指到的 list_head 被拿掉了!
在 head_from 這個 doubly-linked list 中,以 node 為裁切點,把 node 以前的(包含 node 本身) doubly-linked list 存到 head_to 中,而其餘的仍舊待在 head_from 裡面。
這邊沒有檢查 list_head *head_to 是否有東西感覺很危險??
切割前:
切割後:
用來計算 node 所存在的 structure 的位址。
用來計算第一個 node 所存在的 structure 的位址。
跑過這個 linked list 中所有的 node 。
用來取得 linked list 中間的 node 。
而中間迴圈的中止條件應該是要 fast 在最後一個或是倒數第二個的時候中止。
所以 COND1 和 COND2 分別為 fast->next == list
和 fast->next->next == list
。