2018-09-11 19:56:38 +08:00
|
|
|
/*
|
2021-03-08 18:19:04 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2018-09-11 19:56:38 +08:00
|
|
|
*
|
2018-10-14 19:37:18 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2018-09-11 19:56:38 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2018-08-25 armink the first version
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rthw.h>
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <rtdevice.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ring block buffer object initialization
|
|
|
|
*
|
|
|
|
* @param rbb ring block buffer object
|
|
|
|
* @param buf buffer
|
|
|
|
* @param buf_size buffer size
|
2018-11-22 18:08:12 +08:00
|
|
|
* @param block_set block set
|
|
|
|
* @param blk_max_num max block number
|
|
|
|
*
|
|
|
|
* @note When your application need align access, please make the buffer address is aligned.
|
2018-09-11 19:56:38 +08:00
|
|
|
*/
|
|
|
|
void rt_rbb_init(rt_rbb_t rbb, rt_uint8_t *buf, rt_size_t buf_size, rt_rbb_blk_t block_set, rt_size_t blk_max_num)
|
|
|
|
{
|
|
|
|
rt_size_t i;
|
|
|
|
|
|
|
|
RT_ASSERT(rbb);
|
|
|
|
RT_ASSERT(buf);
|
|
|
|
RT_ASSERT(block_set);
|
|
|
|
|
|
|
|
rbb->buf = buf;
|
|
|
|
rbb->buf_size = buf_size;
|
|
|
|
rbb->blk_set = block_set;
|
|
|
|
rbb->blk_max_num = blk_max_num;
|
2021-12-01 14:19:25 +08:00
|
|
|
rbb->tail = &rbb->blk_list;
|
2018-09-11 19:56:38 +08:00
|
|
|
rt_slist_init(&rbb->blk_list);
|
2021-12-01 14:19:25 +08:00
|
|
|
rt_slist_init(&rbb->free_list);
|
2018-09-11 19:56:38 +08:00
|
|
|
/* initialize block status */
|
|
|
|
for (i = 0; i < blk_max_num; i++)
|
|
|
|
{
|
|
|
|
block_set[i].status = RT_RBB_BLK_UNUSED;
|
2021-12-01 14:19:25 +08:00
|
|
|
rt_slist_init(&block_set[i].list);
|
|
|
|
rt_slist_insert(&rbb->free_list, &block_set[i].list);
|
2018-09-11 19:56:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_rbb_init);
|
|
|
|
|
2021-09-01 19:58:23 +08:00
|
|
|
#ifdef RT_USING_HEAP
|
|
|
|
|
2018-09-11 19:56:38 +08:00
|
|
|
/**
|
|
|
|
* ring block buffer object create
|
|
|
|
*
|
|
|
|
* @param buf_size buffer size
|
|
|
|
* @param blk_max_num max block number
|
|
|
|
*
|
2021-04-01 23:04:53 +08:00
|
|
|
* @return != RT_NULL: ring block buffer object
|
|
|
|
* RT_NULL: create failed
|
2018-09-11 19:56:38 +08:00
|
|
|
*/
|
|
|
|
rt_rbb_t rt_rbb_create(rt_size_t buf_size, rt_size_t blk_max_num)
|
|
|
|
{
|
2021-04-01 23:04:53 +08:00
|
|
|
rt_rbb_t rbb = RT_NULL;
|
2018-09-11 19:56:38 +08:00
|
|
|
rt_uint8_t *buf;
|
|
|
|
rt_rbb_blk_t blk_set;
|
|
|
|
|
|
|
|
rbb = (rt_rbb_t)rt_malloc(sizeof(struct rt_rbb));
|
|
|
|
if (!rbb)
|
|
|
|
{
|
2021-04-01 23:04:53 +08:00
|
|
|
return RT_NULL;
|
2018-09-11 19:56:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
buf = (rt_uint8_t *)rt_malloc(buf_size);
|
|
|
|
if (!buf)
|
|
|
|
{
|
|
|
|
rt_free(rbb);
|
2021-04-01 23:04:53 +08:00
|
|
|
return RT_NULL;
|
2018-09-11 19:56:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
blk_set = (rt_rbb_blk_t)rt_malloc(sizeof(struct rt_rbb_blk) * blk_max_num);
|
|
|
|
if (!blk_set)
|
|
|
|
{
|
|
|
|
rt_free(buf);
|
|
|
|
rt_free(rbb);
|
2021-04-01 23:04:53 +08:00
|
|
|
return RT_NULL;
|
2018-09-11 19:56:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rt_rbb_init(rbb, buf, buf_size, blk_set, blk_max_num);
|
|
|
|
|
|
|
|
return rbb;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_rbb_create);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ring block buffer object destroy
|
|
|
|
*
|
|
|
|
* @param rbb ring block buffer object
|
|
|
|
*/
|
|
|
|
void rt_rbb_destroy(rt_rbb_t rbb)
|
|
|
|
{
|
|
|
|
RT_ASSERT(rbb);
|
|
|
|
|
|
|
|
rt_free(rbb->buf);
|
|
|
|
rt_free(rbb->blk_set);
|
2019-12-19 10:39:55 +08:00
|
|
|
rt_free(rbb);
|
2018-09-11 19:56:38 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_rbb_destroy);
|
|
|
|
|
2021-09-01 19:58:23 +08:00
|
|
|
#endif
|
|
|
|
|
2018-09-11 19:56:38 +08:00
|
|
|
static rt_rbb_blk_t find_empty_blk_in_set(rt_rbb_t rbb)
|
|
|
|
{
|
2021-12-01 14:19:25 +08:00
|
|
|
struct rt_rbb_blk *blk;
|
2018-09-11 19:56:38 +08:00
|
|
|
|
|
|
|
RT_ASSERT(rbb);
|
|
|
|
|
2021-12-01 14:19:25 +08:00
|
|
|
if (rt_slist_isempty(&rbb->free_list))
|
2018-09-11 19:56:38 +08:00
|
|
|
{
|
2021-12-01 14:19:25 +08:00
|
|
|
return RT_NULL;
|
2018-09-11 19:56:38 +08:00
|
|
|
}
|
2021-12-01 14:19:25 +08:00
|
|
|
blk = rt_slist_first_entry(&rbb->free_list, struct rt_rbb_blk, list);
|
|
|
|
rt_slist_remove(&rbb->free_list, &blk->list);
|
|
|
|
RT_ASSERT(blk->status == RT_RBB_BLK_UNUSED);
|
|
|
|
return blk;
|
|
|
|
}
|
2018-09-11 19:56:38 +08:00
|
|
|
|
2021-12-01 14:19:25 +08:00
|
|
|
rt_inline void list_append(rt_rbb_t rbb, rt_slist_t* n)
|
|
|
|
{
|
|
|
|
/* append the node to the tail */
|
|
|
|
rbb->tail->next = n;
|
|
|
|
n->next = RT_NULL;
|
|
|
|
/* save tail node */
|
|
|
|
rbb->tail = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_inline rt_slist_t *list_remove(rt_rbb_t rbb, rt_slist_t* n)
|
|
|
|
{
|
|
|
|
rt_slist_t* l = &rbb->blk_list;
|
|
|
|
struct rt_slist_node* node = l;
|
|
|
|
|
|
|
|
/* remove slist head */
|
|
|
|
while (node->next && node->next != n) node = node->next;
|
|
|
|
/* remove node */
|
|
|
|
if (node->next != (rt_slist_t*)0)
|
|
|
|
{
|
|
|
|
node->next = node->next->next;
|
|
|
|
n->next = RT_NULL;
|
|
|
|
/* update tail node */
|
|
|
|
if (rbb->tail == n)
|
|
|
|
rbb->tail = node;
|
|
|
|
}
|
|
|
|
return l;
|
2018-09-11 19:56:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate a block by given size. The block will add to blk_list when allocate success.
|
|
|
|
*
|
|
|
|
* @param rbb ring block buffer object
|
|
|
|
* @param blk_size block size
|
|
|
|
*
|
2018-11-22 18:08:12 +08:00
|
|
|
* @note When your application need align access, please make the blk_szie is aligned.
|
|
|
|
*
|
2021-04-01 23:04:53 +08:00
|
|
|
* @return != RT_NULL: allocated block
|
|
|
|
* RT_NULL: allocate failed
|
2018-09-11 19:56:38 +08:00
|
|
|
*/
|
|
|
|
rt_rbb_blk_t rt_rbb_blk_alloc(rt_rbb_t rbb, rt_size_t blk_size)
|
|
|
|
{
|
|
|
|
rt_base_t level;
|
|
|
|
rt_size_t empty1 = 0, empty2 = 0;
|
2021-04-01 23:04:53 +08:00
|
|
|
rt_rbb_blk_t head, tail, new_rbb = RT_NULL;
|
2018-09-11 19:56:38 +08:00
|
|
|
|
|
|
|
RT_ASSERT(rbb);
|
2018-11-22 18:08:12 +08:00
|
|
|
RT_ASSERT(blk_size < (1L << 24));
|
2018-09-11 19:56:38 +08:00
|
|
|
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
|
2019-06-18 20:09:19 +08:00
|
|
|
new_rbb = find_empty_blk_in_set(rbb);
|
2018-09-11 19:56:38 +08:00
|
|
|
|
2021-12-01 14:19:25 +08:00
|
|
|
if (new_rbb)
|
2018-09-11 19:56:38 +08:00
|
|
|
{
|
2021-12-01 14:19:25 +08:00
|
|
|
if (rt_slist_isempty(&rbb->blk_list) == 0)
|
2018-09-11 19:56:38 +08:00
|
|
|
{
|
|
|
|
head = rt_slist_first_entry(&rbb->blk_list, struct rt_rbb_blk, list);
|
2021-12-01 14:19:25 +08:00
|
|
|
/* get tail rbb blk object */
|
|
|
|
tail = rt_slist_entry(rbb->tail, struct rt_rbb_blk, list);
|
2018-09-11 19:56:38 +08:00
|
|
|
if (head->buf <= tail->buf)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* head tail
|
|
|
|
* +--------------------------------------+-----------------+------------------+
|
|
|
|
* | empty2 | block1 | block2 | block3 | empty1 |
|
|
|
|
* +--------------------------------------+-----------------+------------------+
|
|
|
|
* rbb->buf
|
|
|
|
*/
|
|
|
|
empty1 = (rbb->buf + rbb->buf_size) - (tail->buf + tail->size);
|
|
|
|
empty2 = head->buf - rbb->buf;
|
|
|
|
|
|
|
|
if (empty1 >= blk_size)
|
|
|
|
{
|
2021-12-01 14:19:25 +08:00
|
|
|
list_append(rbb, &new_rbb->list);
|
2019-06-18 20:09:19 +08:00
|
|
|
new_rbb->status = RT_RBB_BLK_INITED;
|
|
|
|
new_rbb->buf = tail->buf + tail->size;
|
|
|
|
new_rbb->size = blk_size;
|
2018-09-11 19:56:38 +08:00
|
|
|
}
|
|
|
|
else if (empty2 >= blk_size)
|
|
|
|
{
|
2021-12-01 14:19:25 +08:00
|
|
|
list_append(rbb, &new_rbb->list);
|
2019-06-18 20:09:19 +08:00
|
|
|
new_rbb->status = RT_RBB_BLK_INITED;
|
|
|
|
new_rbb->buf = rbb->buf;
|
|
|
|
new_rbb->size = blk_size;
|
2018-09-11 19:56:38 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* no space */
|
2021-04-01 23:04:53 +08:00
|
|
|
new_rbb = RT_NULL;
|
2018-09-11 19:56:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* tail head
|
|
|
|
* +----------------+-------------------------------------+--------+-----------+
|
|
|
|
* | block3 | empty1 | block1 | block2 |
|
|
|
|
* +----------------+-------------------------------------+--------+-----------+
|
|
|
|
* rbb->buf
|
|
|
|
*/
|
|
|
|
empty1 = head->buf - (tail->buf + tail->size);
|
|
|
|
|
|
|
|
if (empty1 >= blk_size)
|
|
|
|
{
|
2021-12-01 14:19:25 +08:00
|
|
|
list_append(rbb, &new_rbb->list);
|
2019-06-18 20:09:19 +08:00
|
|
|
new_rbb->status = RT_RBB_BLK_INITED;
|
|
|
|
new_rbb->buf = tail->buf + tail->size;
|
|
|
|
new_rbb->size = blk_size;
|
2018-09-11 19:56:38 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* no space */
|
2021-04-01 23:04:53 +08:00
|
|
|
new_rbb = RT_NULL;
|
2018-09-11 19:56:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* the list is empty */
|
2021-12-01 14:19:25 +08:00
|
|
|
list_append(rbb, &new_rbb->list);
|
2019-06-18 20:09:19 +08:00
|
|
|
new_rbb->status = RT_RBB_BLK_INITED;
|
|
|
|
new_rbb->buf = rbb->buf;
|
|
|
|
new_rbb->size = blk_size;
|
2018-09-11 19:56:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-04-01 23:04:53 +08:00
|
|
|
new_rbb = RT_NULL;
|
2018-09-11 19:56:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
2019-06-18 20:09:19 +08:00
|
|
|
return new_rbb;
|
2018-09-11 19:56:38 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_rbb_blk_alloc);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* put a block to ring block buffer object
|
|
|
|
*
|
|
|
|
* @param block the block
|
|
|
|
*/
|
|
|
|
void rt_rbb_blk_put(rt_rbb_blk_t block)
|
|
|
|
{
|
|
|
|
RT_ASSERT(block);
|
|
|
|
RT_ASSERT(block->status == RT_RBB_BLK_INITED);
|
|
|
|
|
|
|
|
block->status = RT_RBB_BLK_PUT;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_rbb_blk_put);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get a block from the ring block buffer object
|
|
|
|
*
|
|
|
|
* @param rbb ring block buffer object
|
|
|
|
*
|
2021-04-01 23:04:53 +08:00
|
|
|
* @return != RT_NULL: block
|
|
|
|
* RT_NULL: get failed
|
2018-09-11 19:56:38 +08:00
|
|
|
*/
|
|
|
|
rt_rbb_blk_t rt_rbb_blk_get(rt_rbb_t rbb)
|
|
|
|
{
|
|
|
|
rt_base_t level;
|
2021-04-01 23:04:53 +08:00
|
|
|
rt_rbb_blk_t block = RT_NULL;
|
2018-09-11 19:56:38 +08:00
|
|
|
rt_slist_t *node;
|
|
|
|
|
|
|
|
RT_ASSERT(rbb);
|
|
|
|
|
|
|
|
if (rt_slist_isempty(&rbb->blk_list))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
|
|
|
|
for (node = rt_slist_first(&rbb->blk_list); node; node = rt_slist_next(node))
|
|
|
|
{
|
|
|
|
block = rt_slist_entry(node, struct rt_rbb_blk, list);
|
|
|
|
if (block->status == RT_RBB_BLK_PUT)
|
|
|
|
{
|
|
|
|
block->status = RT_RBB_BLK_GET;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* not found */
|
2021-04-01 23:04:53 +08:00
|
|
|
block = RT_NULL;
|
2018-09-11 19:56:38 +08:00
|
|
|
|
|
|
|
__exit:
|
|
|
|
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_rbb_blk_get);
|
|
|
|
|
2018-11-22 18:08:12 +08:00
|
|
|
/**
|
|
|
|
* return the block size
|
|
|
|
*
|
|
|
|
* @param block the block
|
|
|
|
*
|
|
|
|
* @return block size
|
|
|
|
*/
|
|
|
|
rt_size_t rt_rbb_blk_size(rt_rbb_blk_t block)
|
|
|
|
{
|
|
|
|
RT_ASSERT(block);
|
|
|
|
|
|
|
|
return block->size;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_rbb_blk_size);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return the block buffer
|
|
|
|
*
|
|
|
|
* @param block the block
|
|
|
|
*
|
|
|
|
* @return block buffer
|
|
|
|
*/
|
|
|
|
rt_uint8_t *rt_rbb_blk_buf(rt_rbb_blk_t block)
|
|
|
|
{
|
|
|
|
RT_ASSERT(block);
|
|
|
|
|
|
|
|
return block->buf;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_rbb_blk_buf);
|
|
|
|
|
2018-09-11 19:56:38 +08:00
|
|
|
/**
|
|
|
|
* free the block
|
|
|
|
*
|
|
|
|
* @param rbb ring block buffer object
|
|
|
|
* @param block the block
|
|
|
|
*/
|
|
|
|
void rt_rbb_blk_free(rt_rbb_t rbb, rt_rbb_blk_t block)
|
|
|
|
{
|
|
|
|
rt_base_t level;
|
|
|
|
|
|
|
|
RT_ASSERT(rbb);
|
|
|
|
RT_ASSERT(block);
|
|
|
|
RT_ASSERT(block->status != RT_RBB_BLK_UNUSED);
|
|
|
|
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
/* remove it on rbb block list */
|
2021-12-01 14:19:25 +08:00
|
|
|
list_remove(rbb, &block->list);
|
2018-09-11 19:56:38 +08:00
|
|
|
block->status = RT_RBB_BLK_UNUSED;
|
2021-12-01 14:19:25 +08:00
|
|
|
rt_slist_insert(&rbb->free_list, &block->list);
|
2018-09-11 19:56:38 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_rbb_blk_free);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get a continuous block to queue by given size
|
|
|
|
*
|
|
|
|
* tail head
|
|
|
|
* +------------------+---------------+--------+----------+--------+
|
|
|
|
* | block3 | empty1 | block1 | block2 |fragment|
|
|
|
|
* +------------------+------------------------+----------+--------+
|
|
|
|
* |<-- return_size -->| |
|
|
|
|
* |<--- queue_data_len --->|
|
|
|
|
*
|
|
|
|
* tail head
|
|
|
|
* +------------------+---------------+--------+----------+--------+
|
|
|
|
* | block3 | empty1 | block1 | block2 |fragment|
|
|
|
|
* +------------------+------------------------+----------+--------+
|
|
|
|
* |<-- return_size -->| out of len(b1+b2+b3) |
|
|
|
|
* |<-------------------- queue_data_len -------------------->|
|
|
|
|
*
|
|
|
|
* @param rbb ring block buffer object
|
|
|
|
* @param queue_data_len The max queue data size, and the return size must less then it.
|
|
|
|
* @param queue continuous block queue
|
|
|
|
*
|
|
|
|
* @return the block queue data total size
|
|
|
|
*/
|
|
|
|
rt_size_t rt_rbb_blk_queue_get(rt_rbb_t rbb, rt_size_t queue_data_len, rt_rbb_blk_queue_t blk_queue)
|
|
|
|
{
|
|
|
|
rt_base_t level;
|
|
|
|
rt_size_t data_total_size = 0;
|
2021-12-01 14:19:25 +08:00
|
|
|
rt_slist_t *node, *tmp = RT_NULL;
|
2021-04-01 23:04:53 +08:00
|
|
|
rt_rbb_blk_t last_block = RT_NULL, block;
|
2018-09-11 19:56:38 +08:00
|
|
|
|
|
|
|
RT_ASSERT(rbb);
|
|
|
|
RT_ASSERT(blk_queue);
|
|
|
|
|
|
|
|
if (rt_slist_isempty(&rbb->blk_list))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
|
2021-12-01 14:19:25 +08:00
|
|
|
node = rt_slist_first(&rbb->blk_list);
|
|
|
|
if (node != RT_NULL)
|
|
|
|
{
|
|
|
|
tmp = rt_slist_next(node);
|
|
|
|
}
|
|
|
|
for (; node; node = tmp, tmp = rt_slist_next(node))
|
2018-09-11 19:56:38 +08:00
|
|
|
{
|
|
|
|
if (!last_block)
|
|
|
|
{
|
|
|
|
last_block = rt_slist_entry(node, struct rt_rbb_blk, list);
|
|
|
|
if (last_block->status == RT_RBB_BLK_PUT)
|
|
|
|
{
|
|
|
|
/* save the first put status block to queue */
|
|
|
|
blk_queue->blocks = last_block;
|
|
|
|
blk_queue->blk_num = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* the first block must be put status */
|
2021-04-01 23:04:53 +08:00
|
|
|
last_block = RT_NULL;
|
2018-09-11 19:56:38 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
block = rt_slist_entry(node, struct rt_rbb_blk, list);
|
|
|
|
/*
|
|
|
|
* these following conditions will break the loop:
|
|
|
|
* 1. the current block is not put status
|
|
|
|
* 2. the last block and current block is not continuous
|
|
|
|
* 3. the data_total_size will out of range
|
|
|
|
*/
|
|
|
|
if (block->status != RT_RBB_BLK_PUT ||
|
|
|
|
last_block->buf > block->buf ||
|
|
|
|
data_total_size + block->size > queue_data_len)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* backup last block */
|
|
|
|
last_block = block;
|
|
|
|
}
|
|
|
|
/* remove current block */
|
|
|
|
data_total_size += last_block->size;
|
|
|
|
last_block->status = RT_RBB_BLK_GET;
|
|
|
|
blk_queue->blk_num++;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
|
|
return data_total_size;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_rbb_blk_queue_get);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get all block length on block queue
|
|
|
|
*
|
|
|
|
* @param blk_queue the block queue
|
|
|
|
*
|
|
|
|
* @return total length
|
|
|
|
*/
|
|
|
|
rt_size_t rt_rbb_blk_queue_len(rt_rbb_blk_queue_t blk_queue)
|
|
|
|
{
|
2021-12-01 14:19:25 +08:00
|
|
|
rt_size_t i = 0, data_total_size = 0;
|
|
|
|
rt_rbb_blk_t blk;
|
2018-09-11 19:56:38 +08:00
|
|
|
|
|
|
|
RT_ASSERT(blk_queue);
|
|
|
|
|
2021-12-01 14:19:25 +08:00
|
|
|
for (blk = blk_queue->blocks; i < blk_queue->blk_num; i++)
|
2018-09-11 19:56:38 +08:00
|
|
|
{
|
2021-12-01 14:19:25 +08:00
|
|
|
data_total_size += blk->size;
|
|
|
|
blk = rt_slist_entry(blk->list.next, struct rt_rbb_blk, list);
|
2018-09-11 19:56:38 +08:00
|
|
|
}
|
|
|
|
return data_total_size;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_rbb_blk_queue_len);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return the block queue buffer
|
|
|
|
*
|
|
|
|
* @param blk_queue the block queue
|
|
|
|
*
|
|
|
|
* @return block queue buffer
|
|
|
|
*/
|
|
|
|
rt_uint8_t *rt_rbb_blk_queue_buf(rt_rbb_blk_queue_t blk_queue)
|
|
|
|
{
|
|
|
|
RT_ASSERT(blk_queue);
|
|
|
|
|
|
|
|
return blk_queue->blocks[0].buf;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_rbb_blk_queue_buf);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* free the block queue
|
|
|
|
*
|
|
|
|
* @param rbb ring block buffer object
|
|
|
|
* @param blk_queue the block queue
|
|
|
|
*/
|
|
|
|
void rt_rbb_blk_queue_free(rt_rbb_t rbb, rt_rbb_blk_queue_t blk_queue)
|
|
|
|
{
|
2021-12-01 14:19:25 +08:00
|
|
|
rt_size_t i = 0;
|
|
|
|
rt_rbb_blk_t blk, next_blk;
|
2018-09-11 19:56:38 +08:00
|
|
|
|
|
|
|
RT_ASSERT(rbb);
|
|
|
|
RT_ASSERT(blk_queue);
|
|
|
|
|
2021-12-01 14:19:25 +08:00
|
|
|
for (blk = blk_queue->blocks; i < blk_queue->blk_num; i++)
|
2018-09-11 19:56:38 +08:00
|
|
|
{
|
2021-12-01 14:19:25 +08:00
|
|
|
next_blk = rt_slist_entry(blk->list.next, struct rt_rbb_blk, list);
|
|
|
|
rt_rbb_blk_free(rbb, blk);
|
|
|
|
blk = next_blk;
|
2018-09-11 19:56:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_rbb_blk_queue_free);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The put status and buffer continuous blocks can be make a block queue.
|
|
|
|
* This function will return the length which from next can be make block queue.
|
|
|
|
*
|
|
|
|
* @param rbb ring block buffer object
|
|
|
|
*
|
|
|
|
* @return the next can be make block queue's length
|
|
|
|
*/
|
|
|
|
rt_size_t rt_rbb_next_blk_queue_len(rt_rbb_t rbb)
|
|
|
|
{
|
|
|
|
rt_base_t level;
|
|
|
|
rt_size_t data_len = 0;
|
|
|
|
rt_slist_t *node;
|
2021-04-01 23:04:53 +08:00
|
|
|
rt_rbb_blk_t last_block = RT_NULL, block;
|
2018-09-11 19:56:38 +08:00
|
|
|
|
|
|
|
RT_ASSERT(rbb);
|
|
|
|
|
|
|
|
if (rt_slist_isempty(&rbb->blk_list))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
|
|
|
|
for (node = rt_slist_first(&rbb->blk_list); node; node = rt_slist_next(node))
|
|
|
|
{
|
|
|
|
if (!last_block)
|
|
|
|
{
|
|
|
|
last_block = rt_slist_entry(node, struct rt_rbb_blk, list);
|
|
|
|
if (last_block->status != RT_RBB_BLK_PUT)
|
|
|
|
{
|
|
|
|
/* the first block must be put status */
|
2021-04-01 23:04:53 +08:00
|
|
|
last_block = RT_NULL;
|
2018-09-11 19:56:38 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
block = rt_slist_entry(node, struct rt_rbb_blk, list);
|
|
|
|
/*
|
|
|
|
* these following conditions will break the loop:
|
|
|
|
* 1. the current block is not put status
|
|
|
|
* 2. the last block and current block is not continuous
|
|
|
|
*/
|
|
|
|
if (block->status != RT_RBB_BLK_PUT || last_block->buf > block->buf)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* backup last block */
|
|
|
|
last_block = block;
|
|
|
|
}
|
|
|
|
data_len += last_block->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
|
|
return data_len;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_rbb_next_blk_queue_len);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the ring block buffer object buffer size
|
|
|
|
*
|
|
|
|
* @param rbb ring block buffer object
|
|
|
|
*
|
|
|
|
* @return buffer size
|
|
|
|
*/
|
|
|
|
rt_size_t rt_rbb_get_buf_size(rt_rbb_t rbb)
|
|
|
|
{
|
|
|
|
RT_ASSERT(rbb);
|
|
|
|
|
|
|
|
return rbb->buf_size;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_rbb_get_buf_size);
|