contributed by < tinyynoob
>
int ceil_log2(uint32_t x)
{
uint32_t r, shift;
x--;
r = (x > 0xFFFF) << 4;
x >>= r;
shift = (x > 0xFF) << 3;
x >>= shift;
r |= shift;
shift = (x > 0xF) << 2;
x >>= shift;
r |= shift;
shift = (x > 0x3) << 1;
x >>= shift;
return (EXP1) + 1;
}
對照 quiz3 測驗 7
struct foo_consumer {
int (*handler)(struct foo_consumer *self, void *);
struct foo_consumer *next;
};
struct foo {
struct foo_consumer *consumers;
unsigned long flags;
};
#include <stdbool.h>
/*
* For foo @foo, delete the consumer @fc.
* Return true if the @fc is deleted sfccessfully
* or return false.
*/
static bool consumer_del(struct foo *foo, struct foo_consumer *fc)
{
struct foo_consumer **con;
bool ret = false;
for (con = &foo->consumers; *con; EXP4) {
if (*con == fc) {
*con = EXP5;
ret = true;
break;
}
}
return ret;
}
這顯然是個從 singly-linked list 移除節點的問題,前面結構中的 handler
和 flags
都與答題無關。
走訪 list 並使用 indirect pointer 來刪除節點,EXP4
應為 con = &(*con)->next
,而其中的 if statement 用於判斷是否已找到目標,EXP5
應為 (*con)->next
。
解說如下:
假設現在要刪除 b 節點
接著第二圈的 if statement 會發現已找到目標 b
並結束搜尋。
在 linked list 中,head 與 node 扮演著不同的角色,常見的情況為:
既然目的不同,實作的方式也就不相同,那麼使用不同的定義也是自然而然。
藉由 head
的獨立定義,也可以做出把 consumers 分放到不同條 list 的功能等等。
搭配服用 http://www.cs.cmu.edu/afs/cs/academic/class/15213-f15/www/schedule.html 辭典 :::spoiler {state="open"} >> 收縮點此 << 繁體中文 english 作業系統
Jul 21, 2022大部分材料都來自於 Linux 核心設計/實作 (Linux Kernel Internals), 你所不知道的 C 語言 前篇:link Perf performance event 背景執行 ./<> &
Jul 16, 2022contributed by < tinyynoob > 作業要求 最近的方向可以跳到 June rework 開始看 因為一篇筆記放不下了,再開一篇:fibdrv 2 研讀資料 Fibonacci 相關性質
Jul 16, 2022contributed by < tinyynoob > 作業要求 因為一篇筆記放不下了,所以開第二 前篇:link 繼續改進 fibdrv
Jul 16, 2022or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up