大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
概述
ring buffer,或者说循环队列,是嵌入式开发中的一个基本模型,常用于命令队列,资源循环分配场合。
示例
ring_buffer.h ring buffer 封装API
testringbuffer.c 测试ring buffer api.
//ring_buffer.h
#include <stdlib.h>
typedef struct s_ring_buffer{
int tail;
int head;
int size;
int item_size;
void *buf;
} ring_buffer;
static ring_buffer* ring_buffer_create(int item_size, int size)
{
size = size + 1;//alloc one more
void* buf = malloc(item_size * size);
if (NULL == buf)
return buf;
ring_buffer *p_ring_buf = (ring_buffer*)malloc(sizeof(ring_buffer));
if (NULL == p_ring_buf)
return p_ring_buf;
p_ring_buf->head = 0;
p_ring_buf->tail = 0;
p_ring_buf->size = size;
p_ring_buf->item_size = item_size;
p_ring_buf->buf = buf;
return p_ring_buf;
}
static void ring_buffer_free(ring_buffer *p_ring_buf)
{
if (NULL != p_ring_buf->buf){
free(p_ring_buf->buf);
p_ring_buf->buf = NULL;
free(p_ring_buf);
}
}
static void* ring_buffer_get_tail(ring_buffer *p_ring_buf)
{
void *item = NULL;
int tail = p_ring_buf->tail;
int size = p_ring_buf->size;
int item_size = p_ring_buf->item_size;
int n = (tail + 1) % size;
if (n != p_ring_buf->head){
item = p_ring_buf->buf + (item_size * tail);
p_ring_buf->tail = n;
}
return item;
}
static void* ring_buffer_get_head(ring_buffer *p_ring_buf)
{
if (p_ring_buf->head == p_ring_buf->tail)
return NULL;
int head = p_ring_buf->head;
int size = p_ring_buf->size;
int item_size = p_ring_buf->item_size;
void *item = p_ring_buf->buf + (item_size * head);
p_ring_buf->head = (head + 1) % size;
return item;
}
static int ring_buffer_size(ring_buffer *p_ring_buf)
{
int head = p_ring_buf->head;
int tail = p_ring_buf->tail;
int size = p_ring_buf->size;
return (tail >= head) ? (size - 1 - tail + head) : (size - 1 - head + tail);
}
static int ring_buffer_capacity(ring_buffer *p_ring_buf)
{
return p_ring_buf->size - 1;
}
//testringbuffer.c
#include "ring_buffer.h"
#include <stdio.h>
void test_get_tail(ring_buffer *ring_buf){
void *item = ring_buffer_get_tail(ring_buf);
printf("item %p\n", item);
printf("buffer size %d, capacity %d\n", ring_buffer_size(ring_buf), ring_buffer_capacity(ring_buf));
item = ring_buffer_get_tail(ring_buf);
printf("item %p\n", item);
printf("buffer size %d, capacity %d\n", ring_buffer_size(ring_buf), ring_buffer_capacity(ring_buf));
item = ring_buffer_get_tail(ring_buf);
printf("item %p\n", item);
printf("buffer size %d, capacity %d\n", ring_buffer_size(ring_buf), ring_buffer_capacity(ring_buf));
}
void test_get_head(ring_buffer *ring_buf){
void *item = ring_buffer_get_head(ring_buf);
printf("item %p\n", item);
printf("buffer size %d, capacity %d\n", ring_buffer_size(ring_buf), ring_buffer_capacity(ring_buf));
item = ring_buffer_get_head(ring_buf);
printf("item %p\n", item);
printf("buffer size %d, capacity %d\n", ring_buffer_size(ring_buf), ring_buffer_capacity(ring_buf));
item = ring_buffer_get_head(ring_buf);
printf("item %p\n", item);
printf("buffer size %d, capacity %d\n", ring_buffer_size(ring_buf), ring_buffer_capacity(ring_buf));
}
int main(){
ring_buffer *ring_buf = ring_buffer_create(8, 2);
printf("buffer size %d, capacity %d\n", ring_buffer_size(ring_buf), ring_buffer_capacity(ring_buf));
test_get_tail(ring_buf);
test_get_head(ring_buf);
return 0;
}
测试结果
$ ./test.exe
buffer size 2, capacity 2
item 0x8000003c0
buffer size 1, capacity 2
item 0x8000003c8
buffer size 0, capacity 2
item 0x0
buffer size 0, capacity 2
item 0x8000003c0
buffer size 1, capacity 2
item 0x8000003c8
buffer size 2, capacity 2
item 0x0
buffer size 2, capacity 2
引用
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/195317.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...