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

循环队列KFIFO

根据linux的kfifo改写而来,无锁,用于MCU,比如异步打印调试信息,MCU串口通信等。

#ifndef _KFIFO_H_
#define _KFIFO_H_

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <stdint.h>

struct KFIFO{   
	unsigned char *buffer;    /* the buffer holding the data */   
	unsigned int size;        /* the size of the allocated buffer */   
	unsigned int in;          /* data is added at offset (in % size) */   
	unsigned int out;         /* data is extracted from off. (out % size) */   
};

unsigned int kfifo_init(struct KFIFO *fifo, unsigned char *buffer, unsigned int len);
unsigned int kfifo_put(struct KFIFO *fifo, unsigned char *buffer, unsigned int len);
unsigned int kfifo_get(struct KFIFO *fifo, unsigned char *buffer, unsigned int len);
uint32_t kfifo_len(struct KFIFO fifo);
uint8_t kfifo_is_empty(struct KFIFO fifo);
uint8_t kfifo_is_full(struct KFIFO fifo);
uint32_t kfifo_avail(struct KFIFO fifo);

#endif
#include "kfifo.h"

#define min(a, b)                (((a) < (b)) ? (a) : (b))



unsigned int is_power_of_2(unsigned int n){
	return (n != 0 && (n & (n-1) == 0));   
}  

unsigned int kfifo_init(struct KFIFO *fifo, unsigned char *buffer, unsigned int len){
	if(!is_power_of_2(len))
		return 0;
	fifo->in = 0;
	fifo->out = 0;
	fifo->size = len;
	fifo->buffer = buffer;
	return len;   
}  

/**
 * @description: 注意len超过剩余空间的部分会丢失
 * @param {KFIFO} *fifo
 * @param {unsigned char} *buffer
 * @param {unsigned int} len
 * @return {*}
 */
unsigned int kfifo_put(struct KFIFO *fifo, unsigned char *buffer, unsigned int len){
	unsigned int L;
	len = min(len , fifo->size - fifo->in + fifo->out );
	L = min(len, fifo->size - (fifo->in & (fifo->size - 1)));
	memcpy(fifo->buffer + (fifo->in & (fifo->size - 1)), buffer, L);   
	memcpy(fifo->buffer, buffer + L, len - L);
	fifo->in += len; 
	return len;   
}  

unsigned int kfifo_get(struct KFIFO *fifo, unsigned char *buffer, unsigned int len){
	unsigned int L;
	len = min(len, fifo->in - fifo->out);
	L = min(len, fifo->size - (fifo->out & (fifo->size - 1)));
	memcpy(buffer, fifo->buffer + (fifo->out & (fifo->size - 1)), L);
	memcpy(buffer + L, fifo->buffer, len - L);
	fifo->out += len; 
	return len;
}

// kfifo_len - returns the number of used elements in the fifo
uint32_t kfifo_len(struct KFIFO fifo)
{
    return (fifo->in - fifo->out);
}

// kfifo_is_empty - returns true if the fifo is empty
uint8_t kfifo_is_empty(struct KFIFO fifo)
{
    return (fifo->in == fifo->out);
}

// kfifo_is_full - returns true if the fifo is full
uint8_t kfifo_is_full(struct KFIFO fifo)
{
    return ((fifo->in - fifo->out) > (fifo->size - 1));
}

// kfifo_avail - returns the number of unused elements in the fifo
uint32_t kfifo_avail(struct KFIFO fifo)
{
    return (fifo->size - (fifo->in - fifo->out));
}


http://www.kler.cn/a/396107.html

相关文章:

  • Scala的Array
  • 单片机智能家居火灾环境安全检测
  • 恒流数显驱动数显LED驱动芯片VK16D32
  • maven的optional选项说明以及具体应用
  • 学习rust语言宏之macro_rules!
  • 报错 No available slot found for the embedding model
  • 【Linux篇】面试——用户和组、文件类型、权限、进程
  • shell脚本(1)
  • 4. Spring Cloud Ribbon 实现“负载均衡”的详细配置说明
  • TMMI(测试成熟度模型集成)认证是什么?
  • uniapp微信登录的流程
  • 同三维T610UDP-4K60 4K60 DP或HDMI或手机信号采集卡
  • paddle表格识别数据制作
  • 【3D Slicer】的小白入门使用指南八
  • Redis五大基本类型——String字符串命令详解(命令用法详解+思维导图详解)
  • 自动化运维(k8s):一键获取指定命名空间镜像包脚本
  • 衡石科技BI如何助力企业实现数字化转型
  • Spring Boot编程训练系统:敏捷开发与持续集成
  • My_SQL day3
  • 如何在 untitled 软件中安装 Scala插件
  • 微服务学习重点:底层的实现逻辑
  • java8之Stream流
  • 方法论-WPS模型(高效沟通和决策分析的框架)
  • 【软件工程】具体的设计方法(等价类、边界值、场景法)
  • 【JAVA】使用IDEA创建maven聚合项目
  • 2024React面试精选——持续更新