Windows驱动开发小练习-内存分配与链表操作

 1 typedef struct _MYDATA
 2 {
 3     LIST_ENTRY    ListEntry;
 4     LONG        number;
 5 }MYDATA, *PMYDATA;
 6 
 7 void LinkedListTest()
 8 {
 9     LIST_ENTRY head;
10     MYDATA *pMyData;
11     PLIST_ENTRY pListEntry;
12     InitializeListHead(&head);
13 
14     // 创建链表
15     for (int i = 0; i < 10; i++)
16     {
17         // 分配分页内存
18         pMyData = (PMYDATA)ExAllocatePool(PagedPool, sizeof(MYDATA));
19         pMyData->number = i;
20         InsertTailList(&head, &pMyData->ListEntry);
21     }
22 
23     // 遍历链表
24     pMyData = (PMYDATA)head.Flink;
25     while (pMyData != (PMYDATA)&head)
26     {
27         KdPrint(("%d\n", pMyData->number));
28         pMyData = (PMYDATA)pMyData->ListEntry.Flink;
29     }
30 
31     // 释放链表
32     KdPrint(("now removing linked list..."));
33     while (!IsListEmpty(&head))
34     {
35         pListEntry = RemoveTailList(&head); // 从尾部删除一个节点(如果需要从中间删除,使用RemoveEntryList函数)
36         pMyData = (PMYDATA)pListEntry;
37         KdPrint(("Removed data:%d\n", pMyData->number));
38         ExFreePool(pListEntry);                // 释放删除掉的节点所占用的内存
39     }
40 }
posted @ 2012-09-20 09:56  特洛伊人  阅读(609)  评论(0编辑  收藏  举报