Browse Source

【主线待合】尝试解决ringbuffer的memcpy传参为负问题

master
Lizongdi 24 hours ago
parent
commit
7619d5fda5
  1. 26
      library/ringbuffer/ringbuffer.c

26
library/ringbuffer/ringbuffer.c

@ -120,23 +120,31 @@ int rd_RingbufferPut(rd_ringbuf_t *rb, const char *ptr, int length)
{
if (!rb || !ptr || length <= 0) return RD_INVALUE;
int space = rd_RingbufferSpaceLen(rb);
if (space == 0) return 0;
if (space < length) length = space;
unsigned short wi = ATOMIC_LOAD(&rb->m_sWriteIndex, unsigned short, ATOMIC_ORDER_RELAXED);
unsigned short ri = ATOMIC_LOAD(&rb->m_sReadIndex, unsigned short, ATOMIC_ORDER_RELAXED);
unsigned char wb = ATOMIC_LOAD(&rb->m_bWriteMirror, unsigned char, ATOMIC_ORDER_RELAXED);
unsigned char rb_m = ATOMIC_LOAD(&rb->m_bReadMirror, unsigned char, ATOMIC_ORDER_RELAXED);
int space;
if (wb == rb_m) {
space = rb->m_iBufsize - (int)(unsigned short)(wi - ri);
} else {
space = (int)(unsigned short)(ri - wi);
}
if (space <= 0) return 0;
if (space < length) length = space;
/* 写入不跨越尾部 */
if (rb->m_iBufsize - wi > length) {
int tail = rb->m_iBufsize - wi;
if (tail > length) {
RD_MEMCPY(&rb->m_pcBufPtr[wi], ptr, length);
wi += length;
} else {
int first = rb->m_iBufsize - wi;
RD_MEMCPY(&rb->m_pcBufPtr[wi], ptr, first);
RD_MEMCPY(&rb->m_pcBufPtr[0], ptr + first, length - first);
RD_MEMCPY(&rb->m_pcBufPtr[wi], ptr, tail);
RD_MEMCPY(&rb->m_pcBufPtr[0], ptr + tail, length - tail);
wb ^= 1; /* 翻转镜像位 */
wi = length - first;
wi = length - tail;
}
ATOMIC_STORE(&rb->m_sWriteIndex, wi, unsigned short, ATOMIC_ORDER_RELEASE);

Loading…
Cancel
Save