Linux kernel source code "two-way linked list list_head" continued, linuxlist_head
In the previous blog, the "two-way linked list list_head" of the Linux kernel source code introduced the usage of the list_head two-way linked list with an instance. Only the instance code is available, and the list_head linked list code is not available, taking into account the strong desire of students, today we put the list_head code as list. the h header file is pasted here for you to learn how to use.
I. source code of the list. h header file [root @ bdkyr 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") are useful when
* Manipulating whole lists rather than single entries,
* Sometimes we already know the next/prev entries and we can
* Generate better code by using them directly rather
* 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 two known consecutive entries.
*
* This is 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 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.
*/
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 is 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
2. Compile double_list.c [root @ bdkyr cstudy] # gcc double_list.c-o double_list
3. Run [root @ bdkyr cstudy] #./double_list
* Print the result of a recurring table **************
Val = 1, num = 1
Val = 2, num = 2
Val = 3, num = 3
* *********** Delete node B, repeat the chain table, and print the result *
Val = 1, num = 1
Val = 3, num = 3
******************
Val = 4, num = 4
Val = 5, num = 5
**************************************** ***
The list is not empty!
If you think this linked list is good, you can refer to the test instance to compile the code suitable for your own business application. Thank you!