{"id":108,"date":"2007-02-24T13:50:19","date_gmt":"2007-02-24T13:50:19","guid":{"rendered":"https:\/\/driverentry.com.br\/en\/2007\/02\/24\/linked-lists-on-ddk\/"},"modified":"2026-07-25T13:35:54","modified_gmt":"2026-07-25T13:35:54","slug":"linked-lists-on-ddk","status":"publish","type":"post","link":"https:\/\/driverentry.com.br\/en\/2007\/02\/24\/linked-lists-on-ddk\/","title":{"rendered":"Linked lists on DDK"},"content":{"rendered":"<div style=\"text-align: right;\"><a href=\"https:\/\/driverentry.com.br\/2007\/02\/24\/listas-ligadas-no-ddk\/\"><img loading=\"lazy\" decoding=\"async\" title=\"Em Portugu\u00eas\" src=\"https:\/\/driverentry.com.br\/en\/wp-content\/uploads\/2026\/07\/br.gif\" alt=\"\" width=\"21\" height=\"19\" \/><\/a><\/div>\n<p>Since the beginning of my studies, I have always chosen to study something that would unite computer science with electronics, and this led me to choose the Computing Industrial\u00a0course at\u00a0<a href=\"http:\/\/www.jorgestreet.com.br\/\">ETE Jorge Street<\/a>.\u00a0An excellent school and I learned a lot there.\u00a0However I have only learned what a linked list is in a professional environment during my internship.\u00a0Years later, the university introduced me to these lists in form of classes written in <a href=\"http:\/\/en.wikipedia.org\/wiki\/Java_2\">Java<\/a>, where we dealt with references and <a href=\"http:\/\/en.wikipedia.org\/wiki\/Garbage_collection_%28computer_science%29\">garbage collectors<\/a>. Even those who work with Visual C\/C++, handles, templates and classes offered by <a href=\"http:\/\/en.wikipedia.org\/wiki\/Microsoft_Foundation_Classes\">MFC<\/a>, <a href=\"http:\/\/en.wikipedia.org\/wiki\/Active_Template_Library\">ATL<\/a>, <a href=\"http:\/\/en.wikipedia.org\/wiki\/Windows_Template_Library\">WTL<\/a> and <a href=\"http:\/\/en.wikipedia.org\/wiki\/C%2B%2B_STL\">STL<\/a>, which end up abstracting real linked list implementation.\u00a0In this post, I will talk a little about resources offered by <strong>DDK<\/strong> regarding to this subject.<\/p>\n<h3>But I am already a big boy and I already know how to build linked lists in C\/C++.\u00a0Why should I use DDK resources?<\/h3>\n<p>If you only store private data, such as a list of buffers to be sent to the device, that&#8217;s fine, but to deal with <strong>DDK<\/strong> structures, it would be, at least, interesting to know how they are stored in lists.\u00a0Some situations require you to know how to use <strong>DDK<\/strong> lists.\u00a0An example is: If you implement the custom control queue for <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/aa491631.aspx\">IRPs<\/a> in your driver, the <em>pIrp-&gt;Tail.Overlay.ListEntry<\/em> field will be free to be used while such <strong>IRP<\/strong> is held by your driver.<\/p>\n<p>The simplest of these resources is <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/aa491561.aspx\">SINGLE_LIST_ENTRY<\/a> structure that is defined at <em>ntdef.h<\/em>, as shown below:<\/p>\n<div style=\"font-family: Courier New; font-size: 9pt; color: black; background: #A4F64C; border: 1px outset; padding: 0 0 0 5px;\">\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"><span style=\"color: blue;\">typedef<\/span> <span style=\"color: blue;\">struct<\/span> _SINGLE_LIST_ENTRY {<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">  <span style=\"color: blue;\">struct<\/span> _SINGLE_LIST_ENTRY *Next;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">} SINGLE_LIST_ENTRY, *PSINGLE_LIST_ENTRY;<\/pre>\n<\/div>\n<p>A\u00a0<strong>SINGLE_LIST_ENTRY<\/strong> variable must be set to be the head of our list.\u00a0This variable should have its <em>Next <\/em>member initialized with NULL before being used.\u00a0The\u00a0<a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/ms802971.aspx\">PushEntryList()<\/a> and\u00a0<a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/ms804288.aspx\">PopEntryList()<\/a> routines\u00a0are used respectively to add and remove elements from the list head.\u00a0Like most linked lists, nodes are referenced by the <em>Next <\/em>member until it is NULL, thus indicating the node chain\u00a0ending.<\/p>\n<div style=\"font-family: Courier New; font-size: 9pt; color: black; background: #A4F64C; border: 1px outset; padding: 0 0 0 5px;\">\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">VOID <\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">  PushEntryList(<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    IN PSINGLE_LIST_ENTRY  ListHead,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    IN PSINGLE_LIST_ENTRY  Entry<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    );<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\"> <\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">PSINGLE_LIST_ENTRY <\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">  PopEntryList(<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    IN PSINGLE_LIST_ENTRY  ListHead<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    );<\/pre>\n<\/div>\n<p><strong>But wait a minute.\u00a0All this is beautiful, but isn&#8217;t anything missing?<\/strong> Like any linked list structure, among the members of each node, there must be one which points to a similar structure, which is the link to the list next node.\u00a0In the example below, the <em>pNext<\/em> member is the one responsible for it.<\/p>\n<div style=\"font-family: Courier New; font-size: 9pt; color: black; background: #A4F64C; border: 1px outset; padding: 0 0 0 5px;\">\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"><span style=\"color: green;\">\/\/-f--&gt; Structure that defines the node list<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\"><span style=\"color: green;\">\/\/      which will be used as an example.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"><span style=\"color: blue;\">typedef<\/span> <span style=\"color: blue;\">struct<\/span> _MY_NODE<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">{<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/-f--&gt; Your private data that will be defined<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/      by yourself.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    UNICODE_STRING      usSomeData;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    ULONG               ulAnotherData;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: blue;\">struct<\/span> _MY_NODE     *pNext;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    PVOID               pMoreData;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"> <\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">} MY_NODE, *PMY_NODE;<\/pre>\n<\/div>\n<p>But from what we could see at the <strong>DDK <\/strong>structure, there are no useful members on it, those ones that contain information that will be kept in the list, like fields as <em>usSomeData<\/em> or <em>pMoreData<\/em>.\u00a0At the structure offered by the <strong>DDK<\/strong>, there is only the next node address.<\/p>\n<h3>What is the point on saving only nodes?<\/h3>\n<p>Actually, the <strong>SINGLE_LIST_ENTRY<\/strong> structure as well as other DDK lists\u00a0structures should be used in conjunction with the <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/ms802001.aspx\">CONTAINING_RECORD<\/a> macro.\u00a0This macro returns the structure\u00a0base address that contains a known\u00a0field in a known address.\u00a0Well, I have tried to rewrite this sentence three times, but I think you will only understand when you see the example below. Suppose we are using the <strong>SINGLE_LIST_ENTRY<\/strong> in our previous example.\u00a0Thus, we would have the following structure:<\/p>\n<div style=\"font-family: Courier New; font-size: 9pt; color: black; background: #A4F64C; border: 1px outset; padding: 0 0 0 5px;\">\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"><span style=\"color: green;\">\/\/-f--&gt; Structure that defines a register on our linked list<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\"><span style=\"color: green;\">\/\/      and will be used in this example<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"><span style=\"color: blue;\">typedef<\/span> <span style=\"color: blue;\">struct<\/span> _MY_NODE<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">{<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/-f--&gt; Register's private data on the list node.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/      These fields are not fixed and defined by you.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    UNICODE_STRING      usSomeData;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    ULONG               ulAnotherData;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"> <\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/-f--&gt; That member doesn't necessarily need<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/      to be the first or the last one at this structure;<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/      it can be at any position here inside.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    SINGLE_LIST_ENTRY   Entry;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\"> <\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/-f--&gt; More data<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    PVOID               pMoreData;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"> <\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">} MY_NODE, *PMY_NODE;<\/pre>\n<\/div>\n<p>Our structure is basically the same, except the member pNext, which has now been changed to use the structure <strong>SINGLE_LIST_ENTRY<\/strong>. Notice that the field doesn&#8217;t need to be either at the beginning or the end of our structure. The example below demonstrates how to include nodes in lists composed by <strong>SINGLE_LIST_ENTRY<\/strong> nodes.<\/p>\n<div style=\"font-family: Courier New; font-size: 9pt; color: black; background: #A4F64C; border: 1px outset; padding: 0 0 0 5px;\">\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">NTSTATUS PrepareDataAndPush(VOID)<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">{<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    PMY_NODE            pMyNode;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\"> <\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/-f--&gt; Here we get the already allocated node<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/      with its fields correctly initialized.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    pMyNode = AllocateAndFillNode();<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\"> <\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/-f--&gt; Testing is never too much.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    ASSERT(pMyNode != NULL);<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"> <\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/-f--&gt; To put the node at the list, it is necessary to use<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/      the SINGLE_LIST_ENTRY member address.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    PushEntryList(&amp;m_ListHead, &amp;pMyNode-&gt;Entry);<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"> <\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: blue;\">return<\/span> STATUS_SUCCESS;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">}<\/pre>\n<\/div>\n<p>At the time  to include the node in the list, we use the <strong>SINGLE_LIST_ENTRY<\/strong> member address, as suggested by the <strong>PushEntryList <\/strong>prototype function.  Follow the example below that demonstrates how to get this node using <strong>PopEntryList<\/strong> function. This function returns the address passed to <strong>PushEntryList<\/strong> function, which is a <strong>PSINGLE_LIST_ENTRY<\/strong>. From this address, as I had commented before, we can get the base address of our structure using <strong>CONTAINING_RECORD<\/strong> macro. See the example below.<\/p>\n<div style=\"font-family: Courier New; font-size: 9pt; color: black; background: #A4F64C; border: 1px outset; padding: 0 0 0 5px;\">\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">NTSTATUS PopAndProcessNode(VOID)<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">{<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    PSINGLE_LIST_ENTRY  pEntry;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    PMY_NODE            pMyNode;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"> <\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/-f--&gt; Here we get the node from the list.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    pEntry = PopEntryList(&amp;m_ListHead);<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\"> <\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/-f--&gt; Now we can check if a register really was retrieved<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/      from the list.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: blue;\">if<\/span> (!pEntry)<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">        <span style=\"color: blue;\">return<\/span> STATUS_NO_MORE_ENTRIES;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"> <\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/-f--&gt; Now we use the CONTAINING_RECORD macro<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/      to get the base address we need.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    pMyNode = (PMY_NODE) CONTAINING_RECORD(pEntry, MY_NODE, Entry);<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"> <\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/-f--&gt; Now we can access the whole structure.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    ProcessAndFreeNode(pMyNode);<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\"> <\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: blue;\">return<\/span> STATUS_SUCCESS;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">}<\/pre>\n<\/div>\n<p>This same style is also used with the <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/aa491571.aspx\">LIST_ENTRY<\/a> structure, which is the one used in most drivers. This  structure allows you to create doubly linked lists, and like <strong> SINGLE_LIST_ENTRY<\/strong>, a <strong>LIST_ENTRY<\/strong> variable is set to be our list head. Its initialization is done using <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/ms802982.aspx\">InitializeListHead()<\/a> function, which initializes the <em>Flink<\/em> and <em>Blink<\/em> members with the list head address.<\/p>\n<div style=\"font-family: Courier New; font-size: 9pt; color: black; background: #A4F64C; border: 1px outset; padding: 0 0 0 5px;\">\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"><span style=\"color: blue;\">typedef<\/span> <span style=\"color: blue;\">struct<\/span> _LIST_ENTRY {<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">  <span style=\"color: blue;\">struct<\/span> _LIST_ENTRY *Flink;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">  <span style=\"color: blue;\">struct<\/span> _LIST_ENTRY *Blink;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">} LIST_ENTRY, *PLIST_ENTRY;<\/pre>\n<\/div>\n<p>The <em>Flink<\/em> member points to the next node, whereas the <em>Blink<\/em> member points to the previous node. Unlike most linked lists we know, its endings are not marked by NULL at <em>Flink<\/em> and <em>Blink<\/em> members. Instead, their ending points to the address of the node designated as our list head. The example below demonstrates how we could sweep this kind of list, searching a certain node. Before performing any operation with the lists composed by <strong>LIST_ENTRY<\/strong> structures, use <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/ms802978.aspx\">IsListEmpty()<\/a> function.<\/p>\n<div style=\"font-family: Courier New; font-size: 9pt; color: black; background: #A4F64C; border: 1px outset; padding: 0 0 0 5px;\">\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"><span style=\"color: green;\">\/\/-f--&gt; Looks for a node through a list<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\"><span style=\"color: green;\">\/\/      composed by nodes using LIST_ENTRY structures.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">BOOLEAN SearchEntry(PUNICODE_STRING  pusLookFor)<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">{<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    PLIST_ENTRY pEntry;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    PMY_NODE    pMyNode;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"> <\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/-f--&gt; Before any operation, it is necessary<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/      to check for an empty list condition.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: blue;\">if<\/span> (IsListEmpty(&amp;m_ListHead))<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">        <span style=\"color: blue;\">return<\/span> FALSE;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\"> <\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/-f--&gt; Get the first node address.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    pEntry = m_ListHead.Flink;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"> <\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/-f--&gt; While pEntry is not the head list node address,<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/      it means we have a valid node address.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: blue;\">while<\/span>(pEntry != &amp;m_ListHead)<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    {<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">        <span style=\"color: green;\">\/\/-f--&gt; Get the node structure base address.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">        pMyNode = (PMY_NODE) CONTAINING_RECORD(pEntry, MY_NODE, Entry);<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\"> <\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">        <span style=\"color: green;\">\/\/-f--&gt; Check if this is the node we're looking for.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">        <span style=\"color: blue;\">if<\/span> (RtlEqualUnicodeString(pusLookFor,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">                                  &amp;pMyNode-&gt;usSomeData,<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">                                  TRUE))<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">        {<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">            <span style=\"color: green;\">\/\/-f--&gt; Returning TRUE to indicate the node was found.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">            <span style=\"color: blue;\">return<\/span> TRUE;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">        }<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"> <\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">        <span style=\"color: green;\">\/\/-f--&gt; Get the next node address.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">        pEntry = pEntry-&gt;Flink;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    }<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"> <\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: green;\">\/\/-f--&gt; If we got here, it means that the node we were looking for<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">    <span style=\"color: green;\">\/\/      was not found. So, returning FALSE to indicate that.<\/span><\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">    <span style=\"color: blue;\">return<\/span> FALSE;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">}<\/pre>\n<\/div>\n<p><strong>NOTE: The above example is only used to illustrate the search for a node. To  implement a search like this in a production environment, you must keep  in mind issues about multiple thread access synchronism control and reference counters.<\/strong><\/p>\n<p>To manipulate lists formed by <strong>LIST_ENTRY<\/strong>, you can use routines like <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/ms804320.aspx\">InsertHeadList()<\/a>, <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/ms804277.aspx\">InsertTailList()<\/a>, <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/ms804330.aspx\">RemoveHeadList()<\/a> and so on. I will not list all the functions here, I&#8217;m sure <strong>DDK <\/strong>reference do it better than me.<\/p>\n<p>For both types of lists, there are functions like <strong>ExInterlockedXxxList<\/strong> that take a <em>Spin Lock<\/em> to synchronize changes in the list. Remember that if you synchronize access to lists using <em>Spin Locks<\/em>, all nodes must reside in non-paged memory. As some of you may know, when acquiring a <em>Spin Lock<\/em>, the thread <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/ms796706.aspx\">IRQL<\/a> goes to <strong>DISPATCH_LEVEL<\/strong>. In this IRQL, <em>Memory Manager<\/em> is unable to retrieve data page that was paged out to disk, resulting in a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Bugcheck\">BugCheck<\/a>.<\/p>\n<p>It is also important to remember that you should not mix the use of <strong>ExInterlockedXxxList<\/strong> functions with unsynchronized ones. If a list is accessed by multiple threads, all access to it should be controlled.<\/p>\n<div style=\"font-family: Courier New; font-size: 9pt; color: black; background: #A4F64C; border: 1px outset; padding: 0 0 0 5px;\">\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\"><span style=\"color: blue;\">typedef<\/span> <span style=\"color: blue;\">struct<\/span> _SLIST_ENTRY {<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FFFFFF;\">  <span style=\"color: blue;\">struct<\/span> _SLIST_ENTRY *Next;<\/pre>\n<pre style=\"margin: 0px; padding: 0 0 0 5px; background: #FAFAFA;\">} SLIST_ENTRY, *PSLIST_ENTRY;<\/pre>\n<\/div>\n<p>The <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/aa491605.aspx\">SLIST_ENTRY<\/a> structure is an alternative to <strong>SINGLE_LIST_ENTRY<\/strong> lists with synchronized access. This version aims at being more efficient using the functions like <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/ms797099.aspx\">ExInterlockedPopEntryList()<\/a> and <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/aa490127.aspx\">ExInterlockedPushEntryList()<\/a>. Unlike  other structures presented here, the list ending is formed by a  different structure of that used on its nodes; this structure is the <strong> SLIST_HEADER<\/strong>, which is an opaque structure that should be initialized by <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/ms797110.aspx\">ExInitializeSListHead()<\/a> function.<\/p>\n<p>Other  advantage on using this list type is that <strong>DDK<\/strong> offers the <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/ms797006.aspx\">ExQueryDepthSList()<\/a> function, which returns the number of nodes stored the list.<\/p>\n<p>If your main problem is performance, then the ideal situation would be to use the group of functions that work with <em>Generic Tables<\/em>. The list head is defined by an opaque structure called <strong>RTL_GENERIC_TABLE<\/strong>. This  structure, along with its manipulation functions, such as <a href=\"http:\/\/msdn2.microsoft.com\/en-gb\/library\/ms796883.aspx\"> RtlGetElementGenericTable()<\/a>, creates binary trees with self balancing. What a chic thing, huh?<\/p>\n<p>But <em>Generic Tables<\/em> is an issue that deserves a dedicated post. Until next time!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since the beginning of my studies, I have always chosen to study something that would unite computer science with electronics, and this led me to choose the Computing Industrial\u00a0course at\u00a0ETE Jorge Street.\u00a0An excellent school and I learned a lot there.\u00a0However I have only learned what a linked list is in a professional environment during my [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-108","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/driverentry.com.br\/en\/wp-json\/wp\/v2\/posts\/108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/driverentry.com.br\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/driverentry.com.br\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/driverentry.com.br\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/driverentry.com.br\/en\/wp-json\/wp\/v2\/comments?post=108"}],"version-history":[{"count":1,"href":"https:\/\/driverentry.com.br\/en\/wp-json\/wp\/v2\/posts\/108\/revisions"}],"predecessor-version":[{"id":109,"href":"https:\/\/driverentry.com.br\/en\/wp-json\/wp\/v2\/posts\/108\/revisions\/109"}],"wp:attachment":[{"href":"https:\/\/driverentry.com.br\/en\/wp-json\/wp\/v2\/media?parent=108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/driverentry.com.br\/en\/wp-json\/wp\/v2\/categories?post=108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/driverentry.com.br\/en\/wp-json\/wp\/v2\/tags?post=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}