@@ -464,4 +464,84 @@ void rt_ringbuffer_destroy(struct rt_ringbuffer *rb)
|
||||
}
|
||||
RTM_EXPORT(rt_ringbuffer_destroy);
|
||||
|
||||
/**
|
||||
* copy data from ring buffer
|
||||
*/
|
||||
rt_size_t rt_ringbuffer_cpy(struct rt_ringbuffer *rb,
|
||||
rt_uint8_t *ptr,
|
||||
rt_uint16_t length)
|
||||
{
|
||||
rt_size_t size;
|
||||
|
||||
RT_ASSERT(rb != RT_NULL);
|
||||
|
||||
/* whether has enough data */
|
||||
size = rt_ringbuffer_data_len(rb);
|
||||
|
||||
/* no data */
|
||||
if (size == 0)
|
||||
return 0;
|
||||
|
||||
/* less data */
|
||||
if (size < length)
|
||||
length = size;
|
||||
|
||||
if (rb->buffer_size - rb->read_index > length)
|
||||
{
|
||||
/* copy all of data */
|
||||
memcpy(ptr, &rb->buffer_ptr[rb->read_index], length);
|
||||
/* this should not cause overflow because there is enough space for
|
||||
* length of data in current mirror */
|
||||
//rb->read_index += length;
|
||||
return length;
|
||||
}
|
||||
|
||||
memcpy(&ptr[0],
|
||||
&rb->buffer_ptr[rb->read_index],
|
||||
rb->buffer_size - rb->read_index);
|
||||
memcpy(&ptr[rb->buffer_size - rb->read_index],
|
||||
&rb->buffer_ptr[0],
|
||||
length - (rb->buffer_size - rb->read_index));
|
||||
|
||||
/* we are going into the other side of the mirror */
|
||||
//rb->read_mirror = ~rb->read_mirror;
|
||||
//rb->read_index = length - (rb->buffer_size - rb->read_index);
|
||||
|
||||
return length;
|
||||
}
|
||||
RTM_EXPORT(rt_ringbuffer_copy);
|
||||
|
||||
/**
|
||||
* del data from ring buffer
|
||||
*/
|
||||
rt_size_t rt_ringbuffer_del(struct rt_ringbuffer *rb,
|
||||
rt_uint16_t length)
|
||||
{
|
||||
rt_size_t size;
|
||||
|
||||
RT_ASSERT(rb != RT_NULL);
|
||||
|
||||
/* whether has enough data */
|
||||
size = rt_ringbuffer_data_len(rb);
|
||||
|
||||
/* no data */
|
||||
if (size == 0)
|
||||
return 0;
|
||||
|
||||
/* less data */
|
||||
if (size < length)
|
||||
length = size;
|
||||
|
||||
if (rb->buffer_size - rb->read_index > length)
|
||||
{
|
||||
rb->read_index += length;
|
||||
return length;
|
||||
}
|
||||
rb->read_mirror = ~rb->read_mirror;
|
||||
rb->read_index = length - (rb->buffer_size - rb->read_index);
|
||||
|
||||
return length;
|
||||
}
|
||||
RTM_EXPORT(rt_ringbuffer_del);
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user