代码拉取完成,页面将自动刷新
已使用mutex对读写指针进行保护,适用于多线程通信 若用于串口等硬件类数据收发,mutex可用中断屏蔽相关代码代替
/**
*
*------------------------------------------------------------------------
* eg.1 有1个数据
* 0 1 2 pM=Max-1=m_mask
* [*|_|_|_|_|______________|_]Max
* R W
*
*
* eg.2 W=R, 空情况
* 0 1 2 pM=Max-1=m_mask
* [_|_|_|_|_|______________|_]Max
* R
* W
*
*
* eg.3 W+1=R, 满情况(为区分空情况,最满状态,需要空余一个单位(字节)
* 0 1 2 pM=Max-1=m_mask
* [*|*|*****|_|*|*****|*|*]Max
* W R
*
*
* eg.3.1 (W+1)%M=R, 满情况(为区分空情况,最满状态,需要空余一个单位(字节))
* 0 1 2 pM=Max-1=m_mask
* [*|*|*****|*|*|*****|*|_]Max
* R W
*
*
*
* eg.4 W>R的情况
* 0 1 pM=Max-1=m_mask
* [_|_|_|*|*|*****|*|*|_|_|_|_|_]Max
* R W
*
* eg.5 W<R的情况
* 0 1 2 pM=Max-1=m_mask
* [*|*|*|*|*|_|_____|_|*|*|*|*]Max
* W R
*------------------------------------------------------------------------
*
*剩余空间:
*1) R=W, L=M
*2) R>W, L=R-W
*3) R<W, L=R-W+M
*综合3种情况:
* L=(R-W+M)%M
*考滤满情况,必需要空余一个存储单位(Byte):
* L=(R-W+M-1)%M
*
* ----------
*
* 缓存区中数据大小:N
* L + N = M-1
* N = M-1 - L
* N = M-1 - (R-W+M-1)%M
* N = M-1 - (R-W+M-1)%M
*----------------------------------------------------------------------------
*
*取余操作“%”,简化流程
* M = m_maxSize
* X % M = X & m_mask;
*
**/
参考test()代码
void RingBufferBytes::test()
{
RingBufferBytes buffer;
buffer.setBufferSizePower(5);
char wb[] = "0123456789abcdef0";
printf("buffer size:%d\n", buffer.getBufferSize());
printf("will write 17 byte:%s \n", wb);
int ret = buffer.writeBytes(wb, 17);
printf("write success bytes:%d\n", ret);
printf("now buffer data:%d, free:%d\n", buffer.getDataSize(), buffer.getFreeSize());
char rb[64] = {0};
printf("want read 20 bytes.\n");
ret = buffer.readBytes(rb, 20);
printf("read %d bytes ok.\n", ret);
printf("buffer left data:%d, free:%d\n", buffer.getDataSize(), buffer.getFreeSize());
printf("buffer now:\n");
buffer.printhex(0, 32);
printf("will write 17 byte:%s \n", wb);
ret = buffer.writeBytes(wb, 17);
printf("write success bytes:%d\n", ret);
printf("now buffer data:%d, free:%d\n", buffer.getDataSize(), buffer.getFreeSize());
printf("will write 17 byte:%s \n", wb);
ret = buffer.writeBytes(wb, 17);
printf("write success bytes:%d\n", ret);
printf("now buffer data:%d, free:%d\n", buffer.getDataSize(), buffer.getFreeSize());
printf("want read 20 bytes.\n");
ret = buffer.readBytes(rb, 20);
printf("read %d bytes ok.\n", ret);
printf("buffer left data:%d, free:%d\n", buffer.getDataSize(), buffer.getFreeSize());
printf("buffer now:\n");
buffer.printhex(0, 32);
printf("want read 20 bytes.\n");
ret = buffer.readBytes(rb, 20);
printf("read %d bytes ok.\n", ret);
printf("buffer left data:%d, free:%d\n", buffer.getDataSize(), buffer.getFreeSize());
printf("buffer now:\n");
buffer.printhex(0, 32);
}
buffer size:31
will write 17 byte:0123456789abcdef0
write success bytes:17
now buffer data:17, free:14
want read 20 bytes.
read 17 bytes ok.
buffer left data:0, free:31
buffer now:
0 1 2 3 4 5 6 7 8 9 a b c d e f
-------------------------------------------------------
30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66
30 53 74 72 69 6E 67 00 00 00 6F 00 00 00 00 00
will write 17 byte:0123456789abcdef0
write success bytes:17
now buffer data:17, free:14
will write 17 byte:0123456789abcdef0
write success bytes:14
now buffer data:31, free:0
want read 20 bytes.
read 20 bytes ok.
buffer left data:11, free:20
buffer now:
0 1 2 3 4 5 6 7 8 9 a b c d e f
-------------------------------------------------------
66 30 30 31 32 33 34 35 36 37 38 39 61 62 63 64
30 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65
want read 20 bytes.
read 11 bytes ok.
buffer left data:0, free:31
buffer now:
0 1 2 3 4 5 6 7 8 9 a b c d e f
-------------------------------------------------------
66 30 30 31 32 33 34 35 36 37 38 39 61 62 63 64
30 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。