内存管理fifo实现 C语言利用指针数组存储不定长度数据

内存管理fifo实现 C语言利用指针数组存储不定长度数据,第1张

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录
fifo实现
C语言利用指针数组存储不定长度数据
头文件~~~~~~~~~~~~~~~~~~~~~~~~~
#define MAX_SIZE_FIFO 50
extern int fifo_put(uint8_t *tmp,uint32_t prame_size);
extern void fifo_init(void);
extern int fifo_get(uint8_t **tmp);
typedef struct
{
uint8_t ptr_put;
uint8_t ptr_get;
//uint8_t ptr_fifo[MAX_SIZE_FIFO][PER_ARRAY_SIZE];
uint8_t *(ptr[MAX_SIZE_FIFO]);
uint32_t prame_size[MAX_SIZE_FIFO];
uint16_t remain_size;
uint16_t totle_size;
QMutex mutex_q;
}RAM_FIFO;
extern RAM_FIFO ram_fifo;

c文件~~~~~~~~~~~~~~~~~~~~~~~~~
RAM_FIFO ram_fifo;
void fifo_init(void)
{
ram_fifo.ptr_put = 0;
ram_fifo.ptr_get = 0;
memset(ram_fifo.prame_size,0,MAX_SIZE_FIFO*sizeof(ram_fifo.prame_size));
ram_fifo.remain_size = MAX_SIZE_FIFO;
ram_fifo.totle_size = MAX_SIZE_FIFO;
}

int fifo_put(uint8_t *tmp,uint32_t prame_size)
{
int ret = 0;
static int sscount = 0;
ram_fifo.mutex_q.lock();
if(ram_fifo.remain_size == 0)
{
printf(“FIFO FULL…:%d\r\n”,sscount++);
return -1;
}
ram_fifo.ptr[ram_fifo.ptr_put] = (uint8_t )malloc( prame_size sizeof(uint8_t));
memcpy(ram_fifo.ptr[ram_fifo.ptr_put],tmp,prame_size);
ram_fifo.prame_size[ram_fifo.ptr_put] = prame_size;
ram_fifo.remain_size–;
ram_fifo.ptr_put++;
if(ram_fifo.ptr_put == ram_fifo.totle_size)ram_fifo.ptr_put = 0;
ram_fifo.mutex_q.unlock();
return ret;
}

int fifo_get_frame_size (uint32_t *prame_size)
{
ram_fifo.mutex_q.lock();
int ret = 0;
if(ram_fifo.remain_size == ram_fifo.totle_size)
{
printf(“FIFO EMPTY…\r\n”);
return -1;
}
*prame_size = ram_fifo.prame_size[ram_fifo.ptr_get];
ram_fifo.mutex_q.unlock();
return ret;
}

int fifo_get(uint8_t **tmp)
{
ram_fifo.mutex_q.lock();
int ret = 0;
if(ram_fifo.remain_size == ram_fifo.totle_size)
{
//printf(“FIFO EMPTY…\r\n”);
return -1;
}
memcpy(*tmp,ram_fifo.ptr[ram_fifo.ptr_get],ram_fifo.prame_size[ram_fifo.ptr_get]);
free(ram_fifo.ptr[ram_fifo.ptr_get]);
ram_fifo.remain_size++;
ram_fifo.ptr_get++;
if(ram_fifo.ptr_get == ram_fifo.totle_size)ram_fifo.ptr_get = 0;
ram_fifo.mutex_q.unlock();
return ret;
}

int fifo_get_count(void)
{
return (ram_fifo.totle_size-ram_fifo.remain_size);
}

int fifo_get_remain_count(void)
{
return ram_fifo.remain_size;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/673448.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-19
下一篇 2022-04-19

发表评论

登录后才能评论

评论列表(0条)

保存