/********************************************************************************* * Copyright: (C) 2012 Guo Wenxue * All rights reserved. * * Filename: test_klist.c * Description: This file is for test kernel space double linked list. * * Version: 1.0.0(11/12/2012~) * Author: Guo Wenxue * ChangeLog: 1, Release initial version on "11/12/2012 04:26:39 PM" * ********************************************************************************/ #include #include #include #include "cp_klist.h" typedef struct node_s { int data; struct list_head link; } node_t; void travel_list(struct list_head *head) { node_t *new_node; node_t *node, *tmp; if( (new_node=malloc(sizeof(node_t))) ) { list_add_tail(&new_node->link, head); printf("Add new node %p to list \n", new_node); } /* Use list_for_each_entry to travel the list, we can not remove the node in it */ list_for_each_entry_safe(node, tmp, head, link) { printf("Travel2 list on node %p\n", node); } } /******************************************************************************** * Description: * Input Args: * Output Args: * Return Value: ********************************************************************************/ int main (int argc, char **argv) { int i; struct list_head header; node_t *node; node_t *tmp; INIT_LIST_HEAD(&header); for(i=0; i<10; i++) { node=malloc(sizeof(node_t)); memset(node, 0, sizeof(node_t)); node->data=i; if( node ) { list_add_tail(&node->link, &header); printf("Add node %p to list \n", node); } } /* Use list_for_each_entry to travel the list, we can not remove the node in it */ list_for_each_entry(node, &header, link) { printf("Travel list on node %p\n", node); } travel_list(&header); /* Use list_for_each_entry_safe to travel the list and destroy the node */ list_for_each_entry_safe(node, tmp, &header, link) { list_del(&node->link); printf("Remove and destroy node %p from list\n", node); free(node); } return 0; } /* ----- End of main() ----- */