#include #include #include #include "kernel_list.h" struct fsync_inode_entry { struct list_head list; /* list head */ int data; }; struct list_head my_list; int main(void) { struct list_head *this; struct fsync_inode_entry *entry, *tmp; int i; INIT_LIST_HEAD(&my_list); for (i = 0; i < 10; i++) { entry = malloc(sizeof(*entry)); entry->data = i; list_add(&entry->list, &my_list); } list_for_each(this, &my_list) { entry = list_entry(this, struct fsync_inode_entry, list); printf("%d %p %p %p\n", entry->data, this, entry, &entry->list); } /* this way of deleting will segfault on the second iteration */ list_for_each(this, &my_list) { entry = list_entry(this, struct fsync_inode_entry, list); printf("deleting %p\n", &entry->list); list_del(&entry->list); } /* this is the right way to delete the list */ list_for_each_entry_safe(entry, tmp, &my_list, list) { list_del(&entry->list); } printf("deleted list\n"); /* this will never print anything because we deleted the list. */ list_for_each(this, &my_list) { entry = list_entry(this, struct fsync_inode_entry, list); printf("%d\n", entry->data); } return 0; }