内核中的list相关

论坛 期权论坛     
选择匿名的用户   2021-5-23 05:21   42   0
<p>linux内核有很多优秀的代码...</p>
<p>比如list</p>
<p> </p>
<p>这是一个双向链表。</p>
<p>先贴个好文章...</p>
<p><a href="https://myao0730.blogspot.com/2016/12/linux.html">https://myao0730.blogspot.com/2016/12/linux.html</a></p>
<p> </p>
<hr>
<p>先看下list_head的结构体,</p>
<pre class="blockcode"><code class="language-cpp">/*                                                                                                                                                                                 
* Simple doubly linked list implementation.                                                                                                                                       
*                                                                                                                                                                                 
* Some of the internal functions (&#34;__xxx&#34;) are useful when                                                                                                                        
* manipulating whole lists rather than single entries, as                                                                                                                        
* sometimes we already know the next/prev entries and we can                                                                                                                     
* generate better code by using them directly rather than                                                                                                                        
* using the generic single-entry routines.                                                                                                                                       
*/                                                                                                                                                                                
                                                                                                                                                                                   
struct list_head {                                                                                                                                                                 
   struct list_head *next, *prev;                                                                                                                                                
}; </code></pre>
<p>非常简单, 就记录了两个指针, 一个指向前面节点, 一个指向后面节点。</p>
<hr>
<p>以dwc3驱动为例, 看下list如何使用。</p>
<pre class="blockcode"><code class="language-cpp">static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
{
struct dwc3_ep   *dep;
u8    epnum;

INIT_LIST_HEAD(&amp;dwc-&gt;gadget.ep_list);</code></pre>
<p>初始化端点时, 初始化了一个list_head, 叫ep_list</p>
<p> </p>
<p>看下INIT_LIST_HEAD宏,</p>
<pre class="blockcode"><code class="language-cpp"> 25 static inline void INIT_LIST_HEAD(struct list_head *list)                                                                           
26 {                                                                                                                                   
27    list-&gt;next &#61; list;                                                                                                              
28    list-&gt;prev &#61; list;                                                                                                              
29 }   </code></pre>
<p>即该list_head的prev, next指针都指向自己。</p>
<p>也就是说这个list目前是空的。</p>
<p> </p>
<p>端点初始化函数中, 继续往下翻,</p>
<pre class="blockcode"><code class="language-cpp">  if (num &#61;&#61; 0) {
   usb_ep_set_maxpacket_limit(&amp;dep-&gt;endpoint, 512);
   dep-&gt;endpoint.maxburst &#61; 1;
   dep-&gt;endpoint.ops &#61; &amp;dwc3_gadget_ep0_ops;
   if (!direction)
    dwc-&gt;gadget.ep0 &#61; &amp;dep-&gt;endpoint;
  } else if (direction) {
   int mdwidth;
   int kbytes;
   int size;
   int ret;

   mdwidth &#61; DWC3_MDWIDTH(dwc-&gt;hwparams.hwparams0);
   /* MDWIDTH is represented in bits, we need it in bytes */
   mdwidth /&#61; 8;

   size &#61; dwc3_readl(dwc-&gt;regs, DWC3_GTXFIFOSIZ(num));
   size &#61; DWC3_GTXFIFOSIZ_TXFDEF(size);

   /* FIFO Depth is in MDWDITH bytes. Multiply */
   size *&#61; mdwidth;

   kbytes &#61; size / 1024;
   if (kbytes &#61;&#61; 0)
    kbytes &#61; 1;

   /*
    * FIFO sizes account an extra MDWIDTH * (kbytes &#43; 1) bytes for
    * internal overhead. We don&#39;t really know how these are used,
    * but documentation say it exists.
    */
   size -&#61; mdwidth * (kbytes &#43; 1);
   size /&#61; kbytes;

   usb_ep_set_maxpacket_limit(&amp;dep-&gt;endpoint, size);

   dep-&gt;endpoint.max_streams &#61; 15;
   dep-&gt;endpoint.ops &#61; &amp;dwc3_gadget_ep_ops;
   list_add_tail(&amp;dep-&gt;endpoint.ep_list,
     &amp;dwc-&gt;gadget.ep_list);

   ret &#61; dwc3_alloc_trb_pool(dep);
   if (ret)
    return ret;
  } else {
   int  ret;

   usb_ep_set_maxpacket_limit(&amp;dep-&gt;endpoint, 1024);
   dep-&gt;endpoint.max_streams &#61; 15;
   dep-&gt;endpoint.ops &#61; &amp;dwc3_gadget_ep_ops;
   list_add_tail(&amp;dep-&gt;endpoint.ep_list,
     &amp;dwc-&gt;gadget.ep_list);

   ret &#61; dwc3_alloc_trb_pool(dep);
   if (ret)
    return ret;
  }</code></pre>
<p>这里有些逻辑,
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP