{"id":2298,"date":"2018-11-27T12:05:41","date_gmt":"2018-11-27T04:05:41","guid":{"rendered":"https:\/\/damogame.cn:15443\/wordpress\/?p=2298"},"modified":"2018-11-27T12:07:46","modified_gmt":"2018-11-27T04:07:46","slug":"%e5%b7%a7%e5%a4%ba%e5%a4%a9%e5%b7%a5%e7%9a%84kfifo%e4%bf%ae%e8%ae%a2%e7%89%88%ef%bc%89","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/2298","title":{"rendered":"\u5de7\u593a\u5929\u5de5\u7684kfifo(\u4fee\u8ba2\u7248\uff09"},"content":{"rendered":"<div class=\"article-header-box\">\n<div class=\"article-header\">\n<div class=\"article-title-box\"><\/div>\n<div class=\"article-info-box\">\n<div class=\"operating\">\u7248\u6743\u58f0\u660e\uff1a\u672c\u6587\u4e3a\u535a\u4e3b\u539f\u521b\u6587\u7ae0\uff0c\u627f\u8499\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u4f5c\u8005\u548c\u51fa\u5904 https:\/\/blog.csdn.net\/linyt\/article\/details\/53355355<\/div>\n<\/div>\n<\/div>\n<\/div>\n<article class=\"baidu_pl\">\n<div id=\"article_content\" class=\"article_content clearfix csdn-tracking-statistics\" data-pid=\"blog\" data-mod=\"popu_307\" data-dsm=\"post\">\n<div id=\"content_views\" class=\"markdown_views\">\n<p>\u591a\u5e74\u4ee5\u524d\u5199\u4e86\u7bc7\u300a<a href=\"http:\/\/blog.csdn.net\/linyt\/article\/details\/5764312\" target=\"_blank\" rel=\"nofollow noopener\">\u5de7\u593a\u5929\u5de5\u7684kfifo<\/a>\u300b\u6587\u7ae0\uff0c\u6700\u8fd1\u6709\u597d\u4f4d\u7f51\u53cb\u53d1\u6d88\u606f\u8ba8\u8bbakfifo\u5b9e\u73b0\u7ec6\u8282\u3002\u53d1\u73b0\u539f\u6587\u6709\u4e9b\u7ec6\u8282\u6ca1\u6709\u8bf4\u5f97\u5f88\u6e05\u695a\uff0c\u751a\u81f3\u6709\u70b9\u5570\u55e6\uff0c\u4e8e\u662f\u8d81\u4eca\u665a\u6709\u7a7a\u4fee\u8ba2\u4e00\u4e0b\u3002<\/p>\n<p>Linux kernel\u91cc\u9762\u4ece\u6765\u5c31\u4e0d\u7f3a\u5c11\u7b80\u6d01\uff0c\u4f18\u96c5\u548c\u9ad8\u6548\u7684\u4ee3\u7801\uff0c\u53ea\u662f\u6211\u4eec\u7f3a\u5c11\u53d1\u73b0\u548c\u54c1\u5473\u7684\u773c\u5149\u3002\u5728Linux kernel\u91cc\u9762\uff0c\u7b80\u6d01\u5e76\u4e0d\u8868\u793a\u4ee3\u7801\u4f7f\u7528\u795e\u51fa\u9b3c\u6ca1\u7684\u8d85\u7136\u6280\u5de7\uff0c\u76f8\u53cd\uff0c\u5b83\u4f7f\u7528\u7684\u4e0d\u8fc7\u662f\u5927\u5bb6\u975e\u5e38\u719f\u6089\u7684\u57fa\u7840\u6570\u636e\u7ed3\u6784\uff0c\u4f46\u662fkernel\u5f00\u53d1\u8005\u80fd\u4ece\u57fa\u7840\u7684\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u63d0\u70bc\u51fa\u4f18\u7f8e\u7684\u7279\u6027\u3002<br \/>\nkfifo\u5c31\u662f\u8fd9\u6837\u7684\u4e00\u7c7b\u4f18\u7f8e\u4ee3\u7801\uff0c\u5b83\u5341\u5206\u7b80\u6d01\uff0c\u7edd\u65e0\u591a\u4f59\u7684\u4e00\u884c\u4ee3\u7801\uff0c\u5374\u975e\u5e38\u9ad8\u6548\u3002<br \/>\n\u5173\u4e8ekfifo\u4fe1\u606f\u5982\u4e0b\uff1a<\/p>\n<p>\u672c\u6587\u5206\u6790\u7684\u539f\u4ee3\u7801\u7248\u672c\uff1a 2.6.24.4<\/p>\n<p>kfifo\u7684\u5b9a\u4e49\u6587\u4ef6\uff1a kernel\/kfifo.c<\/p>\n<p>kfifo\u7684\u5934\u6587\u4ef6\uff1a include\/linux\/kfifo.h<\/p>\n<h1 id=\"kfifo\u6982\u8ff0\"><a name=\"t0\"><\/a>kfifo\u6982\u8ff0<\/h1>\n<p>kfifo\u662f\u5185\u6838\u91cc\u9762\u7684\u4e00\u4e2aFirst In First Out\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u91c7\u7528\u73af\u5f62\u5faa\u73af\u961f\u5217\u7684\u6570\u636e\u7ed3\u6784\u6765\u5b9e\u73b0\uff1b\u5b83\u63d0\u4f9b\u4e00\u4e2a\u65e0\u8fb9\u754c\u7684\u5b57\u8282\u6d41\u670d\u52a1\uff0c\u6700\u91cd\u8981\u7684\u4e00\u70b9\u662f\uff0c\u5b83\u4f7f\u7528\u5e76\u884c\u65e0\u9501\u7f16\u7a0b\u6280\u672f\uff0c\u5373\u5f53\u5b83\u7528\u4e8e\u53ea\u6709\u4e00\u4e2a\u5165\u961f\u7ebf\u7a0b\u548c\u4e00\u4e2a\u51fa\u961f\u7ebf\u7a0b\u7684\u573a\u60c5\u65f6\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u5e76\u53d1\u64cd\u4f5c\uff0c\u800c\u4e0d\u9700\u8981\u4efb\u4f55\u52a0\u9501\u884c\u4e3a\uff0c\u5c31\u53ef\u4ee5\u4fdd\u8bc1kfifo\u7684\u7ebf\u7a0b\u5b89\u5168\u3002<br \/>\nkfifo\u4ee3\u7801\u65e2\u7136\u80a9\u8d1f\u7740\u8fd9\u4e48\u591a\u7279\u6027\uff0c\u90a3\u6211\u4eec\u5148\u4e00\u655d\u5b83\u7684\u4ee3\u7801\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">struct kfifo {\r\n    unsigned char *buffer;    \/* the buffer holding the data *\/\r\n    unsigned int size;    \/* the size of the allocated buffer *\/\r\n    unsigned int in;    \/* data is added at offset (in % size) *\/\r\n    unsigned int out;    \/* data is extracted from off. (out % size) *\/\r\n    spinlock_t *lock;    \/* protects concurrent modifications *\/\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>\u8fd9\u662fkfifo\u7684\u6570\u636e\u7ed3\u6784\uff0ckfifo\u4e3b\u8981\u63d0\u4f9b\u4e86\u4e24\u4e2a\u64cd\u4f5c\uff0c__kfifo_put(\u5165\u961f\u64cd\u4f5c)\u548c__kfifo_get(\u51fa\u961f\u64cd\u4f5c)\u3002 \u5b83\u7684\u5404\u4e2a\u6570\u636e\u6210\u5458\u5982\u4e0b\uff1a<\/p>\n<p>buffer: \u7528\u4e8e\u5b58\u653e\u6570\u636e\u7684\u7f13\u5b58<\/p>\n<p>size: buffer\u7a7a\u95f4\u7684\u5927\u5c0f\uff0c\u5728\u521d\u5316\u65f6\uff0c\u5c06\u5b83\u5411\u4e0a\u6269\u5c55\u62102\u7684\u5e42<\/p>\n<p>lock: \u5982\u679c\u4f7f\u7528\u4e0d\u80fd\u4fdd\u8bc1\u4efb\u4f55\u65f6\u95f4\u6700\u591a\u53ea\u6709\u4e00\u4e2a\u8bfb\u7ebf\u7a0b\u548c\u5199\u7ebf\u7a0b\uff0c\u9700\u8981\u4f7f\u7528\u8be5lock\u5b9e\u65bd\u540c\u6b65\u3002<\/p>\n<p>in, out: \u548cbuffer\u4e00\u8d77\u6784\u6210\u4e00\u4e2a\u5faa\u73af\u961f\u5217\u3002 in\u6307\u5411buffer\u4e2d\u961f\u5934\uff0c\u800c\u4e14out\u6307\u5411buffer\u4e2d\u7684\u961f\u5c3e\uff0c\u5b83\u7684\u7ed3\u6784\u5982\u793a\u56fe\u5982\u4e0b\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">+--------------------------------------------------------------+\r\n|            |&lt;----------data----------&gt;|                      |\r\n+--------------------------------------------------------------+\r\n             ^                          ^                      ^\r\n             |                          |                      |\r\n            out                        in                     size<\/pre>\n<p>&nbsp;<\/p>\n<p>\u5f53\u7136\uff0c\u5185\u6838\u5f00\u53d1\u8005\u4f7f\u7528\u4e86\u4e00\u79cd\u66f4\u597d\u7684\u6280\u672f\u5904\u7406\u4e86in, out\u548cbuffer\u7684\u5173\u7cfb\uff0c\u6211\u4eec\u5c06\u5728\u4e0b\u9762\u8fdb\u884c\u8be6\u7ec6\u5206\u6790\u3002<\/p>\n<h1 id=\"kfifo\u529f\u80fd\u63cf\u8ff0\"><a name=\"t1\"><\/a>kfifo\u529f\u80fd\u63cf\u8ff0<\/h1>\n<p>kfifo\u63d0\u4f9b\u5982\u4e0b\u5bf9\u5916\u529f\u80fd\u89c4\u683c<\/p>\n<ol>\n<li>\u53ea\u652f\u6301\u4e00\u4e2a\u8bfb\u8005\u548c\u4e00\u4e2a\u8bfb\u8005\u5e76\u53d1\u64cd\u4f5c<\/li>\n<li>\u65e0\u963b\u585e\u7684\u8bfb\u5199\u64cd\u4f5c\uff0c\u5982\u679c\u7a7a\u95f4\u4e0d\u591f\uff0c\u5219\u8fd4\u56de\u5b9e\u9645\u8bbf\u95ee\u7a7a\u95f4<\/li>\n<\/ol>\n<h1 id=\"kfifoalloc-\u5206\u914dkfifo\u5185\u5b58\u548c\u521d\u59cb\u5316\u5de5\u4f5c\"><a name=\"t2\"><\/a>kfifo_alloc \u5206\u914dkfifo\u5185\u5b58\u548c\u521d\u59cb\u5316\u5de5\u4f5c<\/h1>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">struct kfifo *kfifo_alloc(unsigned int size, gfp_t gfp_mask, spinlock_t *lock)\r\n{\r\n    unsigned char *buffer;\r\n    struct kfifo *ret;\r\n\r\n    \/*\r\n     * round up to the next power of 2, since our 'let the indices\r\n     * wrap' tachnique works only in this case.\r\n     *\/\r\n    if (size &amp; (size - 1)) {\r\n        BUG_ON(size &gt; 0x80000000);\r\n        size = roundup_pow_of_two(size);\r\n    }\r\n\r\n    buffer = kmalloc(size, gfp_mask);\r\n    if (!buffer)\r\n        return ERR_PTR(-ENOMEM);\r\n\r\n    ret = kfifo_init(buffer, size, gfp_mask, lock);\r\n\r\n    if (IS_ERR(ret))\r\n        kfree(buffer);\r\n\r\n    return ret;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u8fd9\u91cc\u503c\u5f97\u4e00\u63d0\u7684\u662f\uff0ckfifo-&gt;size\u7684\u503c\u603b\u662f\u5728\u8c03\u7528\u8005\u4f20\u8fdb\u6765\u7684size\u53c2\u6570\u7684\u57fa\u7840\u4e0a\u54112\u7684\u5e42\u6269\u5c55\uff0c\u8fd9\u662f\u5185\u6838\u4e00\u8d2f\u7684\u505a\u6cd5\u3002\u8fd9\u6837\u7684\u597d\u5904\u4e0d\u8a00\u800c\u55bb\u2014\u2014\u5bf9kfifo-&gt;size\u53d6\u6a21\u8fd0\u7b97\u53ef\u4ee5\u8f6c\u5316\u4e3a\u4e0e\u8fd0\u7b97\uff0c\u5982\u4e0b\uff1a<\/p>\n<p>kfifo-&gt;in % kfifo-&gt;size \u53ef\u4ee5\u8f6c\u5316\u4e3a\u00a0<strong>kfifo-&gt;in &amp; (kfifo-&gt;size \u2013 1)<\/strong><\/p>\n<p>\u5728kfifo_alloc\u51fd\u6570\u4e2d\uff0c\u4f7f\u7528size &amp; (size \u2013 1)\u6765\u5224\u65adsize \u662f\u5426\u4e3a2\u5e42\uff0c\u5982\u679c\u6761\u4ef6\u4e3a\u771f\uff0c\u5219\u8868\u793asize\u4e0d\u662f2\u7684\u5e42\uff0c\u7136\u540e\u8c03\u7528roundup_pow_of_two\u5c06\u4e4b\u5411\u4e0a\u6269\u5c55\u4e3a2\u7684\u5e42\u3002<\/p>\n<p>\u8fd9\u90fd\u662f\u5e38\u7528\u7684\u6280\u5de7\uff0c\u53ea\u4e0d\u8fc7\u5927\u5bb6\u6ca1\u6709\u5c06\u5b83\u4eec\u7ed3\u5408\u8d77\u6765\u4f7f\u7528\u800c\u5df2\uff0c\u4e0b\u9762\u8981\u5206\u6790\u7684__kfifo_put\u548c__kfifo_get\u5219\u662f\u5c06kfifo-&gt;size\u7684\u7279\u70b9\u53d1\u6325\u5230\u4e86\u6781\u81f4\u3002<\/p>\n<h1 id=\"kfifoput\u548ckfifoget\u5de7\u5999\u7684\u5165\u961f\u548c\u51fa\u961f\"><a name=\"t3\"><\/a>__kfifo_put\u548c__kfifo_get\u5de7\u5999\u7684\u5165\u961f\u548c\u51fa\u961f<\/h1>\n<p>__kfifo_put\u662f\u5165\u961f\u64cd\u4f5c\uff0c\u5b83\u5148\u5c06\u6570\u636e\u653e\u5165buffer\u91cc\u9762\uff0c\u6700\u540e\u624d\u4fee\u6539in\u53c2\u6570\uff1b__kfifo_get\u662f\u51fa\u961f\u64cd\u4f5c\uff0c\u5b83\u5148\u5c06\u6570\u636e\u4ecebuffer\u4e2d\u79fb\u8d70\uff0c\u6700\u540e\u624d\u4fee\u6539out\u3002\u4f60\u4f1a\u53d1\u73b0in\u548cout\u4e24\u8005\u5404\u53f8\u5176\u804c\u3002<\/p>\n<p>\u4e0b\u9762\u662f__kfifo_put\u548c__kfifo_get\u7684\u4ee3\u7801<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">unsigned int __kfifo_put(struct kfifo *fifo,\r\n             unsigned char *buffer, unsigned int len)\r\n{\r\n    unsigned int l;\r\n\r\n    len = min(len, fifo-&gt;size - fifo-&gt;in + fifo-&gt;out);\r\n\r\n    \/*\r\n     * Ensure that we sample the fifo-&gt;out index -before- we\r\n     * start putting bytes into the kfifo.\r\n     *\/\r\n\r\n    smp_mb();\r\n\r\n    \/* first put the data starting from fifo-&gt;in to buffer end *\/\r\n    l = min(len, fifo-&gt;size - (fifo-&gt;in &amp; (fifo-&gt;size - 1)));\r\n    memcpy(fifo-&gt;buffer + (fifo-&gt;in &amp; (fifo-&gt;size - 1)), buffer, l);\r\n\r\n    \/* then put the rest (if any) at the beginning of the buffer *\/\r\n    memcpy(fifo-&gt;buffer, buffer + l, len - l);\r\n\r\n    \/*\r\n     * Ensure that we add the bytes to the kfifo -before-\r\n     * we update the fifo-&gt;in index.\r\n     *\/\r\n\r\n    smp_wmb();\r\n\r\n    fifo-&gt;in += len;\r\n\r\n    return len;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u5947\u602a\u5417\uff1f\u4ee3\u7801\u5b8c\u5168\u662f\u7ebf\u6027\u7ed3\u6784\uff0c\u6ca1\u6709\u4efb\u4f55if-else\u5206\u652f\u6765\u5224\u65ad\u662f\u5426\u6709\u8db3\u591f\u7684\u7a7a\u95f4\u5b58\u653e\u6570\u636e\u3002\u5185\u6838\u5728\u8fd9\u91cc\u7684\u4ee3\u7801\u975e\u5e38\u7b80\u6d01\uff0c\u6ca1\u6709\u4e00\u884c\u591a\u4f59\u7684\u4ee3\u7801\u3002<\/p>\n<p>l = min(len, fifo-&gt;size &#8211; (fifo-&gt;in &amp; (fifo-&gt;size &#8211; 1)));<\/p>\n<p>\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u8ba1\u7b97\u5f53\u524d\u5199\u5165\u7684\u7a7a\u95f4\uff0c\u6362\u6210\u4eba\u53ef\u7406\u89e3\u7684\u8bed\u8a00\u5c31\u662f\uff1a<\/p>\n<p>l = kfifo\u53ef\u5199\u7a7a\u95f4\u548c\u9884\u671f\u5199\u5165\u7a7a\u95f4\u7684\u6700\u5c0f\u503c<\/p>\n<p><strong>\u4f7f\u7528min\u5b8f\u6765\u4ee3if-else\u5206\u652f<\/strong><\/p>\n<p>__kfifo_get\u4e5f\u5e94\u7528\u4e86\u540c\u6837\u6280\u5de7\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">unsigned int __kfifo_get(struct kfifo *fifo,\r\n             unsigned char *buffer, unsigned int len)\r\n{\r\n    unsigned int l;\r\n\r\n    len = min(len, fifo-&gt;in - fifo-&gt;out);\r\n\r\n    \/*\r\n     * Ensure that we sample the fifo-&gt;in index -before- we\r\n     * start removing bytes from the kfifo.\r\n     *\/\r\n\r\n    smp_rmb();\r\n\r\n    \/* first get the data from fifo-&gt;out until the end of the buffer *\/\r\n    l = min(len, fifo-&gt;size - (fifo-&gt;out &amp; (fifo-&gt;size - 1)));\r\n    memcpy(buffer, fifo-&gt;buffer + (fifo-&gt;out &amp; (fifo-&gt;size - 1)), l);\r\n\r\n    \/* then get the rest (if any) from the beginning of the buffer *\/\r\n    memcpy(buffer + l, fifo-&gt;buffer, len - l);\r\n\r\n    \/*\r\n     * Ensure that we remove the bytes from the kfifo -before-\r\n     * we update the fifo-&gt;out index.\r\n     *\/\r\n\r\n    smp_mb();\r\n\r\n    fifo-&gt;out += len;\r\n\r\n    return len;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u8ba4\u771f\u8bfb\u4e24\u904d\u5427\uff0c\u6211\u4e5f\u8bfb\u4e86\u591a\u6b21\uff0c\u6bcf\u6b21\u603b\u662f\u6709\u65b0\u53d1\u73b0\uff0c\u56e0\u4e3ain, out\u548csize\u7684\u5173\u7cfb\u592a\u5de7\u5999\u4e86\uff0c\u7adf\u7136\u80fd\u5229\u7528\u4e0aunsigned int\u56de\u7ed5\u7684\u7279\u6027\u3002<\/p>\n<p>\u539f\u6765\uff0ckfifo\u6bcf\u6b21\u5165\u961f\u6216\u51fa\u961f\uff0ckfifo-&gt;in\u6216kfifo-&gt;out\u53ea\u662f\u7b80\u5355\u5730kfifo-&gt;in\/kfifo-&gt;out += len\uff0c\u5e76\u6ca1\u6709\u5bf9kfifo-&gt;size \u8fdb\u884c\u53d6\u6a21\u8fd0\u7b97\u3002\u56e0\u6b64kfifo-&gt;in\u548ckfifo-&gt;out\u603b\u662f\u4e00\u76f4\u589e\u5927\uff0c\u76f4\u5230unsigned in\u6700\u5927\u503c\u65f6\uff0c\u53c8\u4f1a\u7ed5\u56de\u52300\u8fd9\u4e00\u8d77\u59cb\u7aef\u3002\u4f46\u59cb\u7ec8\u6ee1\u8db3\uff1a<\/p>\n<p><strong>kfifo-&gt;in &#8211; kfifo-&gt;out &lt;= kfifo-&gt;size<\/strong><\/p>\n<p>\u5373\u4f7fkfifo-&gt;in\u56de\u7ed5\u5230\u4e860\u7684\u90a3\u4e00\u7aef\uff0c\u8fd9\u4e2a\u6027\u8d28\u4ecd\u7136\u662f\u4fdd\u6301\u7684\u3002<\/p>\n<p>\u5bf9\u4e8e\u7ed9\u5b9a\u7684kfifo:<\/p>\n<p>\u6570\u636e\u7a7a\u95f4\u957f\u5ea6\u4e3a\uff1a<strong>kfifo-&gt;in &#8211; kfifo-&gt;out<\/strong><\/p>\n<p>\u800c\u5269\u4f59\u7a7a\u95f4\uff08\u53ef\u5199\u5165\u7a7a\u95f4\uff09\u957f\u5ea6\u4e3a\uff1a<strong>kfifo-&gt;size &#8211; (kfifo-&gt;in &#8211; kfifo-&gt;out)<\/strong><\/p>\n<p>\u5c3d\u7ba1kfifo-&gt;in\u548ckfofo-&gt;out\u4e00\u76f4\u8d85\u8fc7kfifo-&gt;size\u8fdb\u884c\u589e\u957f\uff0c\u4f46\u5b83\u5bf9\u5e94\u5728kfifo-&gt;buffer\u7a7a\u95f4\u7684\u4e0b\u6807\u5374\u662f\u5982\u4e0b\uff1a<\/p>\n<p><strong>kfifo-&gt;in % kfifo-&gt;size<\/strong>\u00a0(i.e. kfifo-&gt;in &amp; (kfifo-&gt;size &#8211; 1))<\/p>\n<p><strong>kfifo-&gt;out % kfifo-&gt;size<\/strong>\u00a0(i.e. kfifo-&gt;out &amp; (kfifo-&gt;size &#8211; 1))<\/p>\n<p>\u5f80kfifo\u91cc\u9762\u5199\u4e00\u5757\u6570\u636e\u65f6\uff0c\u6570\u636e\u7a7a\u95f4\u3001\u5199\u5165\u7a7a\u95f4\u548ckfifo-&gt;size\u7684\u5173\u7cfb\u5982\u679c\u6ee1\u8db3\uff1a<\/p>\n<p><strong>kfifo-&gt;in % size + len &gt; size<\/strong><\/p>\n<p>\u90a3\u5c31\u8981\u505a\u5199\u62c6\u5206\u4e86\uff0c\u89c1\u4e0b\u56fe\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">                                                    kfifo_put\uff08\u5199\uff09\u7a7a\u95f4\u5f00\u59cb\u5730\u5740\r\n                                                    |\r\n                                                   \\_\/\r\n                                                    |XXXXXXXXXX\r\nXXXXXXXX|                                                    \r\n+--------------------------------------------------------------+\r\n|                        |&lt;----------data----------&gt;|          |\r\n+--------------------------------------------------------------+\r\n                         ^                          ^          ^\r\n                         |                          |          |\r\n                       out%size                   in%size     size\r\n        ^\r\n        |\r\n      \u5199\u7a7a\u95f4\u7ed3\u675f\u5730\u5740<\/pre>\n<p>&nbsp;<\/p>\n<p>\u7b2c\u4e00\u5757\u5f53\u7136\u662f: [kfifo-&gt;in % kfifo-&gt;size, kfifo-&gt;size]<br \/>\n\u7b2c\u4e8c\u5757\u5f53\u7136\u662f\uff1a[0, len &#8211; (kfifo-&gt;size &#8211; kfifo-&gt;in % kfifo-&gt;size)]<\/p>\n<p>\u4e0b\u9762\u662f\u4ee3\u7801\uff0c\u7ec6\u7ec6\u4f53\u5473\u5427\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/* first put the data starting from fifo-&gt;in to buffer end *\/   \r\nl = min(len, fifo-&gt;size - (fifo-&gt;in &amp; (fifo-&gt;size - 1)));   \r\nmemcpy(fifo-&gt;buffer + (fifo-&gt;in &amp; (fifo-&gt;size - 1)), buffer, l);   \r\n\r\n\/* then put the rest (if any) at the beginning of the buffer *\/   \r\nmemcpy(fifo-&gt;buffer, buffer + l, len - l);<\/pre>\n<p>&nbsp;<\/p>\n<p>\u5bf9\u4e8ekfifo_get\u8fc7\u7a0b\uff0c\u4e5f\u662f\u7c7b\u4f3c\u7684\uff0c\u8bf7\u5404\u4f4d\u81ea\u884c\u5206\u6790\u3002<\/p>\n<h1 id=\"kfifoget\u548ckfifoput\u65e0\u9501\u5e76\u53d1\u64cd\u4f5c\"><a name=\"t4\"><\/a>kfifo_get\u548ckfifo_put\u65e0\u9501\u5e76\u53d1\u64cd\u4f5c<\/h1>\n<p>\u8ba1\u7b97\u673a\u79d1\u5b66\u5bb6\u5df2\u7ecf\u8bc1\u660e\uff0c\u5f53\u53ea\u6709\u4e00\u4e2a\u8bfb\u7ecf\u7a0b\u548c\u4e00\u4e2a\u5199\u7ebf\u7a0b\u5e76\u53d1\u64cd\u4f5c\u65f6\uff0c\u4e0d\u9700\u8981\u4efb\u4f55\u989d\u5916\u7684\u9501\uff0c\u5c31\u53ef\u4ee5\u786e\u4fdd\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u4e5f\u5373kfifo\u4f7f\u7528\u4e86\u65e0\u9501\u7f16\u7a0b\u6280\u672f\uff0c\u4ee5\u63d0\u9ad8kernel\u7684\u5e76\u53d1\u3002<\/p>\n<p>kfifo\u4f7f\u7528in\u548cout\u4e24\u4e2a\u6307\u9488\u6765\u63cf\u8ff0\u5199\u5165\u548c\u8bfb\u53d6\u6e38\u6807\uff0c\u5bf9\u4e8e\u5199\u5165\u64cd\u4f5c\uff0c\u53ea\u66f4\u65b0in\u6307\u9488\uff0c\u800c\u8bfb\u53d6\u64cd\u4f5c\uff0c\u53ea\u66f4\u65b0out\u6307\u9488\uff0c\u53ef\u8c13<strong>\u4e95\u6c34\u4e0d\u72af\u6cb3\u6c34<\/strong>\uff0c\u793a\u610f\u56fe\u5982\u4e0b\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">                                               |&lt;--\u5199\u5165--&gt;|\r\n+--------------------------------------------------------------+\r\n|                        |&lt;----------data-----&gt;|               |\r\n+--------------------------------------------------------------+\r\n                         |&lt;--\u8bfb\u53d6--&gt;|\r\n                         ^                     ^               ^\r\n                         |                     |               |\r\n                        out                   in              size<\/pre>\n<p>&nbsp;<\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u8bfb\u8005\u770b\u5230<strong>\u5199\u8005\u9884\u8ba1\u5199\u5165\uff0c\u4f46\u5b9e\u9645\u6ca1\u6709\u5199\u5165\u6570\u636e<\/strong>\u7684\u7a7a\u95f4\uff0c\u5199\u8005\u5fc5\u987b\u4fdd\u8bc1\u4ee5\u4e0b\u7684\u5199\u5165\u987a\u5e8f\uff1a<\/p>\n<ol>\n<li>\u5f80[kfifo-&gt;in, kfifo-&gt;in + len]\u7a7a\u95f4\u5199\u5165\u6570\u636e<\/li>\n<\/ol>\n<ol>\n<li>\u66f4\u65b0kfifo-&gt;in\u6307\u9488\u4e3a kfifo-&gt;in + len<\/li>\n<\/ol>\n<p>\u5728\u64cd\u4f5c1\u5b8c\u6210\u65f6\uff0c\u8bfb\u8005\u662f\u8fd8\u6ca1\u6709\u770b\u5230\u5199\u5165\u7684\u4fe1\u606f\u7684\uff0c\u56e0\u4e3akfifo-&gt;in\u6ca1\u6709\u53d8\u5316\uff0c\u8ba4\u4e3a\u8bfb\u8005\u8fd8\u6ca1\u6709\u5f00\u59cb\u5199\u64cd\u4f5c\uff0c\u53ea\u6709\u66f4\u65b0kfifo-&gt;in\u4e4b\u540e\uff0c\u8bfb\u8005\u624d\u80fd\u770b\u5230\u3002<\/p>\n<p>\u90a3\u4e48\u5982\u4f55\u4fdd\u8bc11\u5fc5\u987b\u57282\u4e4b\u524d\u5b8c\u6210\uff0c\u79d8\u5bc6\u5c31\u662f\u4f7f\u7528\u5185\u5b58\u5c4f\u969c\uff1a<strong>smp_mb()\uff0csmp_rmb(), smp_wmb()<\/strong>\uff0c\u6765\u4fdd\u8bc1\u5bf9\u65b9\u89c2\u5bdf\u5230\u7684\u5185\u5b58\u64cd\u4f5c\u987a\u5e8f\u3002<\/p>\n<h1 id=\"\u603b\u7ed3\"><a name=\"t5\"><\/a>\u603b\u7ed3<\/h1>\n<p>\u8bfb\u5b8ckfifo\u4ee3\u7801\uff0c\u4ee4\u6211\u60f3\u8d77\u90a3\u9996\u8bd7\u201c\u4f17\u91cc\u5bfb\u4ed6\u5343\u767e\u5ea6\uff0c\u9ed8\u7136\u56de\u9996\uff0c\u90a3\u4eba\u6b63\u5728\u706f\u706b\u9611\u73ca\u5904\u201d\u3002\u4e0d\u77e5\u4f60\u662f\u5426\u548c\u6211\u4e00\u6837\uff0c\u603b\u60f3\u8ffd\u6c42\u7b80\u6d01\uff0c\u9ad8\u8d28\u91cf\u548c\u53ef\u8bfb\u6027\u7684\u4ee3\u7801\uff0c\u5f53\u7528\u5c3d\u5404\u79cd\u65b9\u6cd5\uff0c\u6c5f\u90de\u624d\u5c3d\u4e4b\u65f6\uff0c\u624d\u53d1\u73b0Linux kernel\u91cc\u9762\u7684\u4ee3\u7801\u5c31\u662f\u6211\u4eec\u5bfb\u627e\u548c\u5b66\u4e60\u7684\u5bf9\u8c61\u3002<\/p>\n<\/div>\n<\/div>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>\u7248\u6743\u58f0\u660e\uff1a\u672c\u6587\u4e3a\u535a\u4e3b\u539f\u521b\u6587\u7ae0\uff0c\u627f\u8499\u8f6c\u8f7d\u8bf7<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[119],"class_list":["post-2298","post","type-post","status-publish","format-standard","hentry","tag-03-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/2298","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=2298"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/2298\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=2298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=2298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=2298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}