内核链表是在Linux系统里面自带的一个提供给系统调用的控制双向循环链表这种数据结构一系列函数接口,它的原型其实就是我们之前学的双向循环链表,我们可以在Linux官方网下载源码,内核源码里面就要内核链表的相关功能函数接口</include/linux/list.h>,不言而喻,这些函数接口就是我们一直学的对链表进行增、删、改、查...的API,也就是说内核链表也就是提供对双向循环链表指针的操作的函数接口。
代码项目文件:my_kernel_list.h、list.h、my_kernel_list.c、main.c
第一部分:my_kernel_list.h
#ifndef _MY_KERNEL_LIST_H
#define _MY_KERNEL_LIST_H
#include "list.h"
#define CHAR_LENGTH 50
typedef char CHAR_KIND;
typedef struct my_kernel_list
{
CHAR_KIND data[CHAR_LENGTH];
LIST_NODE list;
}BIG_NODE,*P_BIG_NODE;
#define STRUCT_LENGTH sizeof(BIG_NODE)
P_BIG_NODE My_Kernel_List_Init();
int My_Kernel_List_Add(P_BIG_NODE Head);
int My_Kernel_List_Del(P_BIG_NODE Head);
int My_Kernel_List_Display(P_BIG_NODE Head);
#endif
第二部分 :list.h
#ifndef _LINUX_LIST_H
#define _LINUX_LIST_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
* Simple doubly linked list implementation.
*
* Some of the internal functions ("__xxx") are useful when
* manipulating whole lists rather than single entries, as
* sometimes we already know the next/prev entries and we can
* generate better code by using them directly rather than
* using the generic single-entry routines.
*/
typedef struct list_head {
struct list_head *next, *prev;
}LIST_NODE,*P_LIST_NODE;
#define LIST_HEAD_INIT(name) { &(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;
list->prev = list;
}
/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
#ifndef CONFIG_DEBUG_LIST
static inline void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
#else
extern void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next);
#endif
/**
* list_add - add a new entry
* @new: new entry to be added
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
/**
* list_add_tail - add a new entry
* @new: new entry to be added
* @head: list head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
/*
* Delete a list entry by making the prev/next entries
* point to each other.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
/**
* list_del - deletes entry from list.
* @entry: the element to delete from the list.
* Note: list_empty() on entry does not return true after this, the entry is
* in an undefined state.
*/
#ifndef CONFIG_DEBUG_LIST
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = NULL;
entry->prev = NULL;
}
#else
extern void list_del(struct list_head *entry);
#endif
/**
* list_replace - replace old entry by new one
* @old : the element to be replaced
* @new : the new element to insert
*
* If @old was empty, it will be overwritten.
*/
static inline void list_replace(struct list_head *old,
struct list_head *new)
{
new->next = old->next;
new->next->prev = new;
new->prev = old->prev;
new->prev->next = new;
}
static inline void list_replace_init(struct list_head *old,
struct list_head *new)
{
list_replace(old, new);
INIT_LIST_HEAD(old);
}
/**
* list_del_init - deletes entry from list and reinitialize it.
* @entry: the element to delete from the list.
*/
static inline void list_del_init(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
}
/**
* list_move - delete from one list and add as another's head
* @list: the entry to move
* @head: the head that will precede our entry
*/
static inline void list_move(struct list_head *list, struct list_head *head)
{
__list_del(list->prev, list->next);
list_add(list, head);
}
/**
* list_move_tail - delete from one li