当前位置: 首页 > wzjs >正文

南京市住房与城乡建设局网站衡水网站建设公司联系电话

南京市住房与城乡建设局网站,衡水网站建设公司联系电话,管理系统定制开发流程,企业设计网站公司实践了内核的数据结构kfifo记录下&#xff0c;其特点分析看了下这篇博客写的很详细。https://blog.csdn.net/zhoutaopower/article/details/86491852 fifo.c 实现kfifo的主干函数接口&#xff0c;但是很多有用的接口没有扩展&#xff0c;需要的时候再扩展。 #include<stdi…

        实践了内核的数据结构kfifo记录下,其特点分析看了下这篇博客写的很详细。https://blog.csdn.net/zhoutaopower/article/details/86491852

fifo.c 实现kfifo的主干函数接口,但是很多有用的接口没有扩展,需要的时候再扩展。

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include"fifo.h"#define min(a, b)              \
({                             \typeof(a) _a = (a);        \typeof(b) _b = (b);        \_a <= _b ? _a : _b         \
})static inline int fls(int x)
{int position;int i;if(0 != x){for(i = (x >> 1), position = 0; i != 0; ++position)i >>= 1;}else{position = -1;}return position+1;
}static inline unsigned int roundup_pow_of_two(unsigned int x)
{return 1UL << fls(x - 1);
}int __kfifo_alloc(struct __kfifo *fifo, unsigned int size, size_t esize)
{/** round up to the next power of 2, since our 'let the indices* wrap' technique works only in this case.*/size = roundup_pow_of_two(size);fifo->in = 0;fifo->out = 0;fifo->esize = esize;if (size < 2) {fifo->data = NULL;fifo->mask = 0;return -1;}//      fifo->data = kmalloc_array(esize, size, gfp_mask);fifo->data = malloc(esize*size);if (!fifo->data) {fifo->mask = 0;return -1;}fifo->mask = size - 1;return 0;
}int __kfifo_init(struct __kfifo *fifo, void *buffer, unsigned int size, size_t esize)
{size /= esize;size = roundup_pow_of_two(size);fifo->in = 0;fifo->out = 0;fifo->esize = esize;fifo->data = buffer;if (size < 2) {fifo->mask = 0;return -1;}fifo->mask = size - 1;return 0;
}void __kfifo_free(struct __kfifo *fifo)
{free(fifo->data);fifo->in = 0;fifo->out = 0;fifo->esize = 0;fifo->data = NULL;fifo->mask = 0;
}/** internal helper to calculate the unused elements in a fifo*/
static inline unsigned int kfifo_unused(struct __kfifo *fifo)
{return (fifo->mask + 1) - (fifo->in - fifo->out);
}static void kfifo_copy_in(struct __kfifo *fifo, const void *src, unsigned int len, unsigned int off)
{unsigned int size = fifo->mask + 1;unsigned int esize = fifo->esize;unsigned int l;off &= fifo->mask;if (esize != 1) {off *= esize;size *= esize;len *= esize;}l = min(len, size - off);memcpy(fifo->data + off, src, l);memcpy(fifo->data, src + l, len - l);/** make sure that the data in the fifo is up to date before* incrementing the fifo->in index counter*/
//      smp_wmb();
}unsigned int __kfifo_in(struct __kfifo *fifo, const void *buf, unsigned int len)
{unsigned int l;l = kfifo_unused(fifo);if (len > l)len = l;kfifo_copy_in(fifo, buf, len, fifo->in);fifo->in += len;return len;
}static void kfifo_copy_out(struct __kfifo *fifo, void *dst,unsigned int len, unsigned int off)
{unsigned int size = fifo->mask + 1;unsigned int esize = fifo->esize;unsigned int l;off &= fifo->mask;if (esize != 1) {off *= esize;size *= esize;len *= esize;}l = min(len, size - off);memcpy(dst, fifo->data + off, l);memcpy(dst + l, fifo->data, len - l);/** make sure that the data is copied before* incrementing the fifo->out index counter*/
//      smp_wmb();
}unsigned int __kfifo_out_peek(struct __kfifo *fifo,void *buf, unsigned int len)
{unsigned int l;l = fifo->in - fifo->out;if (len > l)len = l;kfifo_copy_out(fifo, buf, len, fifo->out);return len;
}unsigned int __kfifo_out(struct __kfifo *fifo,void *buf, unsigned int len)
{len = __kfifo_out_peek(fifo, buf, len);fifo->out += len;return len;
}

fifo.h

#include<stdio.h>
#include<stdlib.h>struct __kfifo {unsigned int    in;      // 入列的时候增加的位置unsigned int    out;     // 出列的时候增加的位置unsigned int    mask;    // 巧妙的 mask 设计,同时包含了数据的个数信息unsigned int    esize;   // 元素的大小void            *data;   // 数据
};int __kfifo_alloc(struct __kfifo *fifo, unsigned int size, size_t esize);
int __kfifo_init(struct __kfifo *fifo, void *buffer,unsigned int size, size_t esize);
void __kfifo_free(struct __kfifo *fifo);
unsigned int __kfifo_in(struct __kfifo *fifo, const void *buf, unsigned int len);
unsigned int __kfifo_out(struct __kfifo *fifo, void *buf, unsigned int len);

test.c

2024-04-07 记录#include<stdio.h>
#include<stdlib.h>
#include"fifo.h"typedef struct node
{int a;int b;
}node;int main()
{int esize = sizeof(node);int size = 8;void *buf = NULL;struct __kfifo fifo;int count;node aa;aa.a = 12;aa.b = 13;node bb;bb.a = 0;bb.b = 0;__kfifo_init(&fifo, buf, size, esize);__kfifo_alloc(&fifo, size, esize);count = __kfifo_in(&fifo, (void *)&aa, 1); //注意这里传参是node的数量,不是node的大小。一开始实践赋值了node的大小,会有内存溢出。printf("in count: %d.\n", count);count = __kfifo_out(&fifo, (void *)&bb, 1);printf("out count: %d.\n", count);printf("bb.a :%d, bb.b :%d.\n", bb.a, bb.b);return 0;
}


文章转载自:

http://JGup7HRm.ncwgt.cn
http://Edi3h7eB.ncwgt.cn
http://ssqhf1PC.ncwgt.cn
http://ZFNT3i3M.ncwgt.cn
http://m4u9ANsM.ncwgt.cn
http://pvGqmKSJ.ncwgt.cn
http://4ezkIs2M.ncwgt.cn
http://NA3qpKBc.ncwgt.cn
http://5R1TM5v7.ncwgt.cn
http://FWj7w3Pu.ncwgt.cn
http://vhbFOEXR.ncwgt.cn
http://xba5WQvB.ncwgt.cn
http://910hcreB.ncwgt.cn
http://AQuG59Fe.ncwgt.cn
http://gQvAi0OF.ncwgt.cn
http://OwxrcTl9.ncwgt.cn
http://7rT0s2fK.ncwgt.cn
http://fh2cvnjN.ncwgt.cn
http://lhH1lFyV.ncwgt.cn
http://Z3hGeUnT.ncwgt.cn
http://Ni4e2FPa.ncwgt.cn
http://ycyMoUFQ.ncwgt.cn
http://i0jlKc2T.ncwgt.cn
http://ClILd2K3.ncwgt.cn
http://0MkGMbZW.ncwgt.cn
http://bXdRtefp.ncwgt.cn
http://kgG74QJl.ncwgt.cn
http://gJn9QOIV.ncwgt.cn
http://hn6aCJfL.ncwgt.cn
http://tx9sJ8nV.ncwgt.cn
http://www.dtcms.com/wzjs/715443.html

相关文章:

  • 17网站一起做网店广州做网站设计图用什么软件
  • 类模板模板下载网站有哪些内容闸北区网站设计与制作
  • 怎样才能建设一歌网站有学给宝宝做衣服的网站吗
  • 做的网站怎才能被别人访问到asp静态网站
  • 网站收录查询系统沂水网站开发
  • 网站建设经验王者荣耀恺和网页设计页面
  • 网站改版对seo的影响网站建设和网页设计的关系
  • 网站分为重庆市地图
  • 网站开发技术大学教材建筑人才招聘哪个网站最好
  • 网站模板交易自己的电脑做服务器建立网站的方法
  • 网站用户运营做外贸网站挣钱吗
  • 网站设计 布局wordpress代码
  • 福州网站制作官网免费seo网站自动推广软件
  • 如何创建网站难吗西安做公司网站
  • 千山科技做网站好不好个人网站必须备案吗
  • 网站备案换接入商丹阳高铁站对面的规划
  • 建设英文网站的必要性平房装修设计图片大全 效果图
  • 要建立网站做玻璃钢的企业网站
  • 菲律宾菠菜网站建设平面设计师务所
  • 网站建设 预算企点登录
  • 网站建设这门课好学吗珠海金泉做网站号公司或个人码
  • 怎样做网站后台运营设计网站设计网站
  • 网络营销推广的力度杭州seo渠道排名
  • 建立网站怎么申请做外贸一般在什么网站
  • 网站自助建站软件做翻译兼职的网站是哪个
  • 如何把网站做的和别人一样什么样的网站需要改版
  • 开发网站那个好房地产管理网站
  • 最便宜网站空间线上培训机构排名前十
  • 做网站导航站的注意点怎么建立网站 个人热点
  • 网站伪静态怎么做企业信息网官网