在编译时使用-Wall –Wstrict-prototypes选项编译可以防止大多数的代码缺陷。
使用标准C语言类型
内核中的普通内存地址通常是unsigned long ,因为:至少在当前linux支持的所有平台上,指针和long整型的大小是一样的。
为数据项分配确定的空间大小
在<asm/types.h>中定义若干类型:u8, u16, u32, u64。
使用新的编译器可以定义如下类型等等:uint8_t, uint32_t。
其他有关移植性的问题
页分配常见代码:假设需要16KB空间来存储临时数据,则分配页的代码是
#include <asm/pages.h>
int order = get_order(16*1024);
buf = get_free_pages(FGP_KERNEL, order);
传给order的值必须是2的幂。
链表
相关类型以及API
struct list_head{struct list_head *next, *prev;};
初始化
sturct list_head todo_list;
INIT_LIST_HEAD(&todo_list);
也可以这样初始化:
LIST_HEAD(todo_list);
API:
list_add(struct list_head *new, struct list_head *head);
list_add_tail(struct list_head *new, struct list_head *head);
list_del(struct list_head *entry);
list_del_init(struct list_head *entry);
list_empty(struct list_head *head);
list_entry(entry, type, name);
list_move(struct list_head *entry, struct list_head *head);
list_move_tail(struct list_head *entry, struct list_head *head);
list_splice(struct list_head *list, struct list_head *head);