The previous blog "Linux kernel source" Two-way list List_head "" In an example of the use of the List_head doubly linked list, only the code of the instance, and there is no list_head linked list of code, considering the eager friends of the strong desire, today List_ The head code is list.h header file paste here, for you to learn to use Bo friends.
First, list.h head file source[email protected] cstudy]# cat list.h#list. h header File
#ifndef _linux_list_h
#define _linux_list_h
#include <stdlib.h>
#undef Offsetof
#ifdef __compiler_offsetof
#define OFFSETOF (Type,member) __compiler_offsetof (Type,member)
#else
#define OFFSETOF (Type, MEMBER) ((size_t) & ((TYPE *) 0)->member)
#endif
#define CONTAINER_OF (PTR, type, member) ({\
Const typeof (((type *) 0)->member) *__mptr = (PTR); \
(Type *) ((char *) __mptr-offsetof (Type,member));})
/*
* Simple doubly linked list implementation.
*
* Some of the internal functions ("__xxx") is 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.
*/
struct List_head {
struct List_head *next, *prev;
};
#define FLIST_HEAD_INIT (name) {& (name), & (name)}
#define FLIST_HEAD (name) \
struct List_head name = Flist_head_init (name)
#define Init_list_head (PTR) do {\
(PTR)->next = (PTR); (PTR)->prev = (PTR); \
} while (0)
/*
* Insert A new entry between, known consecutive entries.
*
* This was only for internal list manipulation where we know
* The Prev/next entries already!
*/
static inline void __list_add (struct list_head *new_entry,
struct List_head *prev,
struct List_head *next)
{
Next->prev = New_entry;
New_entry->next = Next;
New_entry->prev = prev;
Prev->next = New_entry;
}
/**
* List_add-add a new entry
* @new_entry: 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_entry,
struct List_head *head)
{
__list_add (New_entry, Head, Head->next);
}
static inline void List_add_tail (struct list_head *new_entry,
struct List_head *head)
{
__list_add (New_entry, Head->prev, head);
}
/*
* Delete A list entry by making the Prev/next entries
* point to each of the other.
*
* This was 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 no return true after this, the entry is
* In a undefined state.
*/
static inline void List_del (struct list_head *entry)
{
__list_del (Entry->prev, Entry->next);
Entry->next = NULL;
Entry->prev = NULL;
}
/**
* 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_empty-tests Whether a list is empty
* @head: The list to test.
*/
static inline int list_empty (const struct List_head *head)
{
return head->next = = head;
}
static inline void __list_splice (const struct List_head *list,
struct List_head *prev,
struct List_head *next)
{
struct List_head *first = list->next;
struct List_head *last = list->prev;
First->prev = prev;
Prev->next = First;
Last->next = Next;
Next->prev = Last;
}
static inline void List_splice (const struct List_head *list,
struct List_head *head)
{
if (!list_empty (list))
__list_splice (list, head, Head->next);
}
static inline void List_splice_init (struct list_head *list,
struct List_head *head)
{
if (!list_empty (list)) {
__list_splice (list, head, Head->next);
Init_list_head (LIST);
}
}
/**
* List_entry-get the struct for this entry
* @ptr: The &struct list_head pointer.
* @type: The type of the struct this was embedded in.
* @member: The name of the list_struct within the struct.
*/
#define List_entry (PTR, type, member) \
Container_of (PTR, type, member)
/**
* List_for_each-iterate over a list
* @pos: The &struct list_head to use as a loop counter.
* @head: The head for your list.
*/
#define List_for_each (POS, head) \
for (pos = (head)->next; pos! = (head); pos = pos->next)
/**
* List_for_each_safe-iterate over a list safe against removal of list entry
* @pos: The &struct list_head to use as a loop counter.
* @n:another &struct List_head to use as temporary storage
* @head: The head for your list.
*/
#define LIST_FOR_EACH_SAFE (POS, n, head) \
for (pos = (head)->next, n = pos->next; pos! = (head); \
pos = N, n = pos->next)
extern void List_sort (void *priv, struct list_head *head,
Int (*cmp) (void *priv, struct list_head *a, struct list_head *b));
#endif
Second, compiling double_list.c[email protected] cstudy]# gcc double_list.c-o double_list
Third, the operation[email protected] cstudy]#./double_list
Traverse linked list, print results **************
val = 1, num = 1
val = 2, num = 2
val = 3, num = 3
Delete Node B, re-traverse the linked list, print the results *
val = 1, num = 1
val = 3, num = 3
Print linked list head1******************
val = 4, num = 4
val = 5, num = 5
*******************************************
The list is not empty!
If Bo friends think this list is good, you can refer to test examples, write code suitable for their business applications, thank you!
Linux kernel source "bidirectional linked list List_head" continued