歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux內核 >> Linux內核中的一些基本編程操作

Linux內核中的一些基本編程操作

日期:2017/3/1 10:12:49   编辑:Linux內核

1. 前言

本文介紹linux內核中一些常用的數據結構和操作。

2. 雙向鏈表(list)

linux內核中的雙向鏈表通過結構 struct list_head來將各個節點連接起來,此結構會作為鏈表元素結構中的一個參數:
struct list_head {
struct list_head *next, *prev;
};

鏈表頭的初始化,注意,結構中的指針為NULL並不是初始化,而是指向自身才是初始化,如果只是按普通情況下的置為NULL,而不是指向自身,系統會崩潰,這是一個容易犯的錯誤:

#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
#define INIT_LIST_HEAD(ptr) do { \
(ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)

最常用的鏈表操作:
插入到鏈表頭:
void list_add(struct list_head *new, struct list_head *head);

插入到鏈表尾:
void list_add_tail(struct list_head *new, struct list_head *head);

刪除鏈表節點:
void list_del(struct list_head *entry);

將節點移動到另一鏈表:
void list_move(struct list_head *list, struct list_head *head);

將節點移動到鏈表尾:
void list_move_tail(struct list_head *list,struct list_head *head);

判斷鏈表是否為空,返回1為空,0非空
int list_empty(struct list_head *head);

把兩個鏈表拼接起來:
void list_splice(struct list_head *list, struct list_head *head);

取得節點指針:
#define list_entry(ptr, type, member) \
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))

遍歷鏈表中每個節點:
#define list_for_each(pos, head) \
for (pos = (head)->next, prefetch(pos->next); pos != (head); \
pos = pos->next, prefetch(pos->next))

逆向循環鏈表中每個節點:
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
pos = pos->prev, prefetch(pos->prev))

舉例:

LISH_HEAD(mylist);

struct my_list{
struct list_head list;
int data;
};

static int ini_list(void)
{
struct my_list *p;
int i;
for(i=0; i<100; i++){
p=kmalloc(sizeof(struct my_list), GFP_KERNEL);
list_add(&p->list, &mylist);
}
}

在內存中形成如下結構的一個雙向鏈表:

+---------------------------------------------------------------+
| |
| mylist 99 98 0 |
| +----+ +---------+ +---------+ +---------+ |
+->|next|--->|list.next|--->|list.next|--->...--->|list.next|---+
|----| |---------| |---------| |---------|
+--|prev|<---|list.prev|<---|list.prev|<---...<---|list.prev|<--+
| +----+ |---------| |---------| |---------| |
| | data | | data | | data | |
| +---------+ +---------+ +---------+ |
| |
+---------------------------------------------------------------+

知道了鏈表頭就能遍歷整個鏈表,如果是用list_add()插入新節點的話,從鏈表頭的next方向看是一個堆棧型。

從鏈表中刪除節點很容易:
static void del_item(struct my_list *p)
{
list_del(&p->list, &mylist);
kfree(p);
}

最重要的宏是list_entry,這個宏的思路是根據鏈表元素結構中鏈表頭結構list_head的地址推算出鏈表元素結構的實際地址:

#define list_entry(ptr, type, member) \
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))

ptr是鏈表元素結構(如struct my_list)中鏈表頭結構list_head的地址
member是鏈表元素結構(如struct my_list)中鏈表頭結構list_head參數的名稱
type是鏈表元素結構類型(如struct my_list)

計算原理是根據鏈表頭結構list_head的地址減去其在鏈表元素結構中的偏移位置而得到鏈表元素結構的地址。

例如:
static void print_list(void)
{
struct list_head *cur;
struct my_list *p;
list_for_each(cur, &mylist){
p=list_entry(cur, struct my_list, list);
printk("data=%d\n", p->data);
}
}

優點:

這樣就可以用相同的數據處理方式來描述所有雙向鏈表,不用再單獨為各個鏈表編寫各種編輯函數。

缺點:
1) 鏈表頭中元素置為NULL不是初始化,與普通習慣不同;
2) 仍然需要單獨編寫各自的刪除整個鏈表的函數,不能統一處理,因為不能保證所有鏈表元素結構中鏈表頭結構list_head的偏移地址都是相同的,當然如果把鏈表頭結構list_head都作為鏈表元素結構的第一個參數,就可以用統一的刪除整個鏈表的函數。

3. HASH表

HASH表適用於不需要對整個空間元素進行排序,而是只需要能快速找到某個元素的場合,是一種以空間換時間的方法,本質也是線性表,但由一個大的線性表拆分為了多個小線性表,由於只需要查找小表,因此搜索速度就會線性查整個大表提高很多,理想情況下,有多少個小線性表,搜索速度就提高了多少倍,通常把小線性表的表頭綜合為一個數組,大小就是HASH表的數量。

HASH表速度的關鍵是HASH函數的設計,HASH函數根據每個元素中固定的參數進行計算,算出一個不大於HASH表數量的索引值,表示該元素需要放在該索引號對應的那個表中,對於固定的參數,計算結果始終是固定的,但對於不同的參數值,希望計算出來的結果能盡可能地平均到每個索引值,HASH函數計算得越平均,表示每個小表中元素的數量都會差不多,這樣搜索性能將越好。HASH函數也要盡可能的簡單,以減少計算時間,常用的算法是將參數累加求模,在include/linux/jhash.h中已經定義了一些HASH計算函數,可直接使用。

HASH表在路由cache表,狀態連接表等處用得很多。

舉例,連接跟蹤中根據tuple值計算HASH:
// net/ipv4/netfilter/ip_conntrack_core.c
u_int32_t
hash_conntrack(const struct ip_conntrack_tuple *tuple)
{
#if 0
dump_tuple(tuple);
#endif
return (jhash_3words(tuple->src.ip,
(tuple->dst.ip ^ tuple->dst.protonum),
(tuple->src.u.all | (tuple->dst.u.all << 16)),
ip_conntrack_hash_rnd) % ip_conntrack_htable_size);
}

// include/linux/jhash.h
static inline u32 jhash_3words(u32 a, u32 b, u32 c, u32 initval)
{
a += JHASH_GOLDEN_RATIO;
b += JHASH_GOLDEN_RATIO;
c += initval;
__jhash_mix(a, b, c);
return c;
}

Copyright © Linux教程網 All Rights Reserved