linux kernel 常用編碼模式

linux kernel 常用編碼模式

Memory Caches

    當需要經常分配和釋放內存時,內核通常使用cache進行管理。而不是使用開銷非常大的kmalloc和kfee。

kmem_cache_alloc

kmem_cache_free

 更多詳細的內容應該查看slab分配器 。
 

 向量定位

struct abc {
    int age;
    char *name[20];
    ...
    char    placeholder[0];
}

The optional block starts with placeholder. Note that placeholder is defined as a vector of size 0. This means that when abc is allocated with the optional block, placeholder points to the beginning of the block. When no optional block is required, placeholder is just a pointer to the end of the structure;

 

條件語句優化

    對於if判斷語句,根據概率可以使用likely and unlikely 進行優化,當true的概率大時使用likely,否則使用unlikely
 

捕捉bug

    當BUG_TRAP 的條件 爲false時,kernel將會打印警告信息。
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章