提交 981d929b 编写于 作者: G Grissiom

ringbuffer: fix the ambiguous name

RT_RINGBUFFER_SIZE could mean "the size of the whole buffer", "the size
of the empty space" or "the size of the data". Moreover, it's never a
micro anymore. Change it to rt_ringbuffer_data_len before it's too late.
Also, RT_RINGBUFFER_EMPTY is changed to rt_ringbuffer_space_len.
上级 6f75988c
...@@ -45,9 +45,9 @@ struct rt_ringbuffer ...@@ -45,9 +45,9 @@ struct rt_ringbuffer
/* use the msb of the {read,write}_index as mirror bit. You can see this as /* use the msb of the {read,write}_index as mirror bit. You can see this as
* if the buffer adds a virtual mirror and the pointers point either to the * if the buffer adds a virtual mirror and the pointers point either to the
* normal or to the mirrored buffer. If the write_index has the same value * normal or to the mirrored buffer. If the write_index has the same value
* with the read_index, but in differenct mirro, the buffer is full. While * with the read_index, but in a different mirror, the buffer is full.
* if the write_index and the read_index are the same and within the same * While if the write_index and the read_index are the same and within the
* mirror, the buffer is empty. The ASCII art of the ringbuffer is: * same mirror, the buffer is empty. The ASCII art of the ringbuffer is:
* *
* mirror = 0 mirror = 1 * mirror = 0 mirror = 1
* +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+ * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
...@@ -73,29 +73,6 @@ struct rt_ringbuffer ...@@ -73,29 +73,6 @@ struct rt_ringbuffer
rt_int16_t buffer_size; rt_int16_t buffer_size;
}; };
/** return the size of data in rb */
rt_inline rt_uint16_t RT_RINGBUFFER_SIZE(struct rt_ringbuffer *rb)
{
if (rb->read_index == rb->write_index)
{
if (rb->read_mirror == rb->write_mirror)
/* we are in the same side, the ringbuffer is empty. */
return 0;
else
return rb->buffer_size;
}
else
{
if (rb->write_index > rb->read_index)
return rb->write_index - rb->read_index;
else
return rb->buffer_size - (rb->read_index - rb->write_index);
}
}
/** return the size of empty space in rb */
#define RT_RINGBUFFER_EMPTY(rb) ((rb)->buffer_size - RT_RINGBUFFER_SIZE(rb))
/* pipe device */ /* pipe device */
#define PIPE_DEVICE(device) ((struct rt_pipe_device*)(device)) #define PIPE_DEVICE(device) ((struct rt_pipe_device*)(device))
struct rt_pipe_device struct rt_pipe_device
...@@ -163,12 +140,36 @@ rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb, ...@@ -163,12 +140,36 @@ rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb,
rt_uint8_t *ptr, rt_uint8_t *ptr,
rt_uint16_t length); rt_uint16_t length);
rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch); rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch);
rt_inline rt_uint16_t rt_ringbuffer_get_size(struct rt_ringbuffer *rb) rt_inline rt_uint16_t rt_ringbuffer_get_size(struct rt_ringbuffer *rb)
{ {
RT_ASSERT(rb != RT_NULL); RT_ASSERT(rb != RT_NULL);
return rb->buffer_size; return rb->buffer_size;
} }
/** return the size of data in rb */
rt_inline rt_uint16_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb)
{
if (rb->read_index == rb->write_index)
{
if (rb->read_mirror == rb->write_mirror)
/* we are in the same side, the ringbuffer is empty. */
return 0;
else
return rb->buffer_size;
}
else
{
if (rb->write_index > rb->read_index)
return rb->write_index - rb->read_index;
else
return rb->buffer_size - (rb->read_index - rb->write_index);
}
}
/** return the size of empty space in rb */
#define rt_ringbuffer_space_len(rb) ((rb)->buffer_size - rt_ringbuffer_data_len(rb))
/** /**
* Pipe Device * Pipe Device
*/ */
......
...@@ -53,7 +53,7 @@ rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb, ...@@ -53,7 +53,7 @@ rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb,
RT_ASSERT(rb != RT_NULL); RT_ASSERT(rb != RT_NULL);
/* whether has enough space */ /* whether has enough space */
size = RT_RINGBUFFER_EMPTY(rb); size = rt_ringbuffer_space_len(rb);
/* no space */ /* no space */
if (size == 0) if (size == 0)
...@@ -100,7 +100,7 @@ rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb, ...@@ -100,7 +100,7 @@ rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb,
RT_ASSERT(rb != RT_NULL); RT_ASSERT(rb != RT_NULL);
/* whether has enough data */ /* whether has enough data */
size = RT_RINGBUFFER_SIZE(rb); size = rt_ringbuffer_data_len(rb);
/* no data */ /* no data */
if (size == 0) if (size == 0)
...@@ -143,7 +143,7 @@ rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch) ...@@ -143,7 +143,7 @@ rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch)
RT_ASSERT(rb != RT_NULL); RT_ASSERT(rb != RT_NULL);
/* whether has enough space */ /* whether has enough space */
if (!RT_RINGBUFFER_EMPTY(rb)) if (!rt_ringbuffer_space_len(rb))
return 0; return 0;
rb->buffer_ptr[rb->write_index] = ch; rb->buffer_ptr[rb->write_index] = ch;
...@@ -171,7 +171,7 @@ rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch) ...@@ -171,7 +171,7 @@ rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch)
RT_ASSERT(rb != RT_NULL); RT_ASSERT(rb != RT_NULL);
/* ringbuffer is empty */ /* ringbuffer is empty */
if (!RT_RINGBUFFER_SIZE(rb)) if (!rt_ringbuffer_data_len(rb))
return 0; return 0;
/* put character */ /* put character */
......
...@@ -188,7 +188,7 @@ static rt_err_t _ep_in_handler(udevice_t device, uclass_t cls, rt_size_t size) ...@@ -188,7 +188,7 @@ static rt_err_t _ep_in_handler(udevice_t device, uclass_t cls, rt_size_t size)
eps = (cdc_eps_t)cls->eps; eps = (cdc_eps_t)cls->eps;
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
remain = RT_RINGBUFFER_SIZE(&tx_ringbuffer); remain = rt_ringbuffer_data_len(&tx_ringbuffer);
if (remain != 0) if (remain != 0)
{ {
/* although vcom_in_sending is set in SOF handler in the very /* although vcom_in_sending is set in SOF handler in the very
...@@ -453,7 +453,7 @@ static rt_err_t _class_sof_handler(udevice_t device, uclass_t cls) ...@@ -453,7 +453,7 @@ static rt_err_t _class_sof_handler(udevice_t device, uclass_t cls)
eps = (cdc_eps_t)cls->eps; eps = (cdc_eps_t)cls->eps;
size = RT_RINGBUFFER_SIZE(&tx_ringbuffer); size = rt_ringbuffer_data_len(&tx_ringbuffer);
if (size == 0) if (size == 0)
return -RT_EFULL; return -RT_EFULL;
...@@ -611,7 +611,7 @@ static int _vcom_putc(struct rt_serial_device *serial, char c) ...@@ -611,7 +611,7 @@ static int _vcom_putc(struct rt_serial_device *serial, char c)
* data out soon. But we cannot rely on that and if we wait to long, just * data out soon. But we cannot rely on that and if we wait to long, just
* return. */ * return. */
for (cnt = 500; for (cnt = 500;
RT_RINGBUFFER_EMPTY(&tx_ringbuffer) == 0 && cnt; rt_ringbuffer_space_len(&tx_ringbuffer) == 0 && cnt;
cnt--) cnt--)
{ {
/*rt_kprintf("wait for %d\n", cnt);*/ /*rt_kprintf("wait for %d\n", cnt);*/
...@@ -628,7 +628,7 @@ static int _vcom_putc(struct rt_serial_device *serial, char c) ...@@ -628,7 +628,7 @@ static int _vcom_putc(struct rt_serial_device *serial, char c)
} }
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
if (RT_RINGBUFFER_EMPTY(&tx_ringbuffer)) if (rt_ringbuffer_space_len(&tx_ringbuffer))
{ {
rt_ringbuffer_putchar(&tx_ringbuffer, c); rt_ringbuffer_putchar(&tx_ringbuffer, c);
} }
...@@ -646,7 +646,7 @@ static int _vcom_getc(struct rt_serial_device *serial) ...@@ -646,7 +646,7 @@ static int _vcom_getc(struct rt_serial_device *serial)
result = -1; result = -1;
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
if (RT_RINGBUFFER_SIZE(&rx_ringbuffer)) if (rt_ringbuffer_data_len(&rx_ringbuffer))
{ {
rt_ringbuffer_getchar(&rx_ringbuffer, &ch); rt_ringbuffer_getchar(&rx_ringbuffer, &ch);
result = ch; result = ch;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册