以前在網上了解到 Linux 內核開發時用的是侵入式(intrusive)數據結構,就想了解下。而後讀了這篇介紹 Linux 內核中用到的雙向鏈表實現的文章 。html
看完那篇博客後,就想本身寫個小例子感覺下。數據結構
/** * 採用侵入式數據結構,實現一個簡單的單向鏈表。 * gcc -o test test.c */ #include <stdio.h> #include <stdlib.h> ////////////////////////////////////////////////////////////// // 鏈表的實現 struct list_head { struct list_head *next; }; #define LIST_HEAD_INIT(name) { &(name) } #define LIST_HEAD(name) \ struct list_head name = LIST_HEAD_INIT(name) static inline void INIT_LIST_HEAD(struct list_head *list) { list->next = list; } static inline void list_add(struct list_head *new, struct list_head *head) { new->next = head->next; head->next = new; } // 對外接口 // #define container_of(ptr, type, member) ({ const typeof( ((type *)0)->member ) *__mptr = (ptr); (type *)( (char *)__mptr - offsetof(type,member) );}) // 採用 typeof 編譯不過 #define list_entry(ptr, type, member) container_of(ptr, type, member) #define list_for_each(pos, head) for (pos = (head)->next; pos != (head); pos = pos->next) // 內部接口 #define container_of(ptr, type, member) ({ \ const __auto_type __mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) ////////////////////////////////////////////////////////////// // 遊戲中揹包的道具。 struct item_info { struct list_head list; int id; }; static LIST_HEAD(items); // 向背包中添加一個道具。 void additem(int id) { struct item_info *item = (struct item_info*)malloc(sizeof(struct item_info)); INIT_LIST_HEAD(&item->list); item->id = id; list_add(&item->list, &items); } int main(int argc, char *argv[]) { int count = 0; additem(200); additem(300); additem(0); struct item_info *nouse_item = NULL; struct list_head *pos = NULL, *nouse = NULL; list_for_each(pos, &items) { if (nouse) { // 要在本循環銷燬上一個循環獲取的道具 nouse_item = list_entry(nouse, struct item_info, list); printf("\nfree one item:%d\n", nouse_item->id); free(nouse_item); } count++; struct item_info *info = list_entry(pos, struct item_info, list); printf("id:%d ", info->id); nouse = pos; } if (nouse) { nouse_item = list_entry(nouse, struct item_info, list); printf("\nfree the last item:%d\n", nouse_item->id); free(nouse_item); } printf("total count:%d\n", count); return 0; }