I’ve been doing a lot of work with Doubly linked lists recently so it is fresh in my mind. This is a good thing to get your head around using as they crop up everywhere in driver development.
Taking your questions in order
“When I want to insert an Entry to the list with InsertListHead/InsertListTail do I have to get memory for that Entry first?”
Yes. Firstly you would create the Linked list in DriverEntry using InitialiseListHead, and this list would be stored on the global heap. You would also create a spinlock on the global heap. Spinlocks are used for synchronising access to the list, as this is most likely running on a multiple cpu system.
InitializeListHead(&gMyList);
KeInitializeSpinLock(&gMyListLock);
Your list entry struct would look like
typedef struct my_list_entry{
int var1;
int var2;
LIST ENTRY listentry;
}my_list_entry
So to add an item to the list
Acquire the spinlock
KLOCK_QUEUE_HANDLE myListLockHandle;
KeAcquireInStackQueuedSpinLock( &gMyListLock, &myListLockHandle );
Allocate memory to your list entry
My_List_Entry \* yourNewListEntry;
yourNewListEntry = ExAllocatePoolWithTag(My_List_Entry,'sgaT');
InsertTailList(gList,yourNewListEntry-\>listEntry);
yourNewListEntry=NULL; //Change ownership
Release the spinlock
KeAcquireInStackQueuedSpinLock( &gMyListLock, &myListLockHandle );
“2 In addition to the above do I have to free the memory for an Entry when I remove one?”
Yes, otherwise you will leak memory.
Once again
Acquire the spinlock
Remove the entry from the list
Release the spinlock
Release the memory … ExFreeTagWithPool
KeAcquireInStackQueuedSpinLock( &gMyListLock, &myListLockHandle );
myListEntry = gMyList.Flink;//This gets a pointer to the first item in the list
myListEntry = CONTAINING_RECORD(MY_LIST_ENTRY,,MY_LIST_ENTRY);
RemoveEntryList(myListEntry);
KeReleaseInStackQueuedSpinLock( &gMyListLock, &myListLockHandle );
ExFreePoolWithTag(myListEntry,'sgaT');
“3 When I want to remove 1 Entry what is the easiest way to find the correct Entry? do have to store an pointer when I insert an Entry? Can i find this pointer back in the list?”
You can iterate through the list.
Acquire spinlock
Check list is not empty
For loop
Check each entry using CONTAINING_RECORD as above
If you find the entry you want to remove then use RemoveEntryList.
HTH, any questions just holler.