Skip to content

Commit

Permalink
Updated according to feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Lucy <[email protected]>
  • Loading branch information
lucypa committed Mar 9, 2022
1 parent 3fea94c commit f852ae2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
58 changes: 33 additions & 25 deletions libsharedringbuffer/include/shared_ringbuffer/shared_ringbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ typedef void (*notify_fn)(void);
/* Buffer descriptor */
typedef struct buff_desc {
uintptr_t encoded_addr; /* encoded dma addresses */
unsigned int len; /* associated memory lengths */
size_t len; /* associated memory lengths */
void *cookie; /* index into client side metadata */
} buff_desc_t;

Expand Down Expand Up @@ -57,9 +57,10 @@ void ring_init(ring_handle_t *ring, ring_buffer_t *avail, ring_buffer_t *used, n
*
* @return true indicates the buffer is empty, false otherwise.
*/
static inline int ring_empty(ring_buffer_t *ring)
static inline bool ring_empty(ring_buffer_t *ring)
{
return !((ring->write_idx - ring->read_idx) % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT);
//return !((ring->write_idx - ring->read_idx) % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT);
return (ring->write_idx == ring->read_idx);
}

/**
Expand All @@ -69,9 +70,10 @@ static inline int ring_empty(ring_buffer_t *ring)
*
* @return true indicates the buffer is full, false otherwise.
*/
static inline int ring_full(ring_buffer_t *ring)
static inline bool ring_full(ring_buffer_t *ring)
{
return !((ring->write_idx - ring->read_idx + 1) % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT);
//return !((ring->write_idx - ring->read_idx + 1) % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT);
return CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT == (ring->write_idx - ring->read_idx);
}

/**
Expand All @@ -82,7 +84,9 @@ static inline int ring_full(ring_buffer_t *ring)
*/
static inline void notify(ring_handle_t *ring)
{
return ring->notify();
if (ring->notify != NULL) {
return ring->notify();
}
}

/**
Expand All @@ -95,18 +99,20 @@ static inline void notify(ring_handle_t *ring)
*
* @return -1 when ring is empty, 0 on success.
*/
static inline int enqueue(ring_buffer_t *ring, uintptr_t buffer, unsigned int len, void *cookie)
static inline int enqueue(ring_buffer_t *ring, uintptr_t buffer, size_t len, void *cookie)
{
if (ring_full(ring)) {
ZF_LOGE("Ring full");
return -1;
}

ring->buffers[ring->write_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT].encoded_addr = buffer;
ring->buffers[ring->write_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT].len = len;
ring->buffers[ring->write_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT].cookie = cookie;
buff_desc_t *buff_desc = &(ring->buffers[ring->write_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT]);
buff_desc->encoded_addr = buffer;
buff_desc->len = len;
buff_desc->cookie = cookie;
ring->write_idx++;

/* Ensure data is written back to memory */
THREAD_MEMORY_RELEASE();

return 0;
Expand All @@ -122,17 +128,19 @@ static inline int enqueue(ring_buffer_t *ring, uintptr_t buffer, unsigned int le
*
* @return -1 when ring is empty, 0 on success.
*/
static inline int dequeue(ring_buffer_t *ring, uintptr_t *addr, unsigned int *len, void **cookie)
static inline int dequeue(ring_buffer_t *ring, uintptr_t *addr, size_t *len, void **cookie)
{
if (ring_empty(ring)) {
ZF_LOGF("Ring is empty");
return -1;
}

*addr = ring->buffers[ring->read_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT].encoded_addr;
*len = ring->buffers[ring->read_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT].len;
*cookie = ring->buffers[ring->read_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT].cookie;
buff_desc_t *buff_desc = &(ring->buffers[ring->read_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT]);
*addr = buff_desc->encoded_addr;
*len = buff_desc->len;
*cookie = buff_desc->cookie;

/* Ensure data is written back to memory */
THREAD_MEMORY_RELEASE();
ring->read_idx++;

Expand All @@ -150,7 +158,7 @@ static inline int dequeue(ring_buffer_t *ring, uintptr_t *addr, unsigned int *le
*
* @return -1 when ring is empty, 0 on success.
*/
static inline int enqueue_avail(ring_handle_t *ring, uintptr_t addr, unsigned int len, void *cookie)
static inline int enqueue_avail(ring_handle_t *ring, uintptr_t addr, size_t len, void *cookie)
{
return enqueue(ring->avail_ring, addr, len, cookie);
}
Expand All @@ -166,7 +174,7 @@ static inline int enqueue_avail(ring_handle_t *ring, uintptr_t addr, unsigned in
*
* @return -1 when ring is empty, 0 on success.
*/
static inline int enqueue_used(ring_handle_t *ring, uintptr_t addr, unsigned int len, void *cookie)
static inline int enqueue_used(ring_handle_t *ring, uintptr_t addr, size_t len, void *cookie)
{
return enqueue(ring->used_ring, addr, len, cookie);
}
Expand All @@ -181,7 +189,7 @@ static inline int enqueue_used(ring_handle_t *ring, uintptr_t addr, unsigned int
*
* @return -1 when ring is empty, 0 on success.
*/
static inline int dequeue_avail(ring_handle_t *ring, uintptr_t *addr, unsigned int *len, void **cookie)
static inline int dequeue_avail(ring_handle_t *ring, uintptr_t *addr, size_t *len, void **cookie)
{
return dequeue(ring->avail_ring, addr, len, cookie);
}
Expand All @@ -196,7 +204,7 @@ static inline int dequeue_avail(ring_handle_t *ring, uintptr_t *addr, unsigned i
*
* @return -1 when ring is empty, 0 on success.
*/
static inline int dequeue_used(ring_handle_t *ring, uintptr_t *addr, unsigned int *len, void **cookie)
static inline int dequeue_used(ring_handle_t *ring, uintptr_t *addr, size_t *len, void **cookie)
{
return dequeue(ring->used_ring, addr, len, cookie);
}
Expand All @@ -209,25 +217,25 @@ static inline int dequeue_used(ring_handle_t *ring, uintptr_t *addr, unsigned in
* @param ring Ring buffer to dequeue from.
* @param addr pointer to the address of where to store buffer address.
* @param len pointer to variable to store length of data dequeueing.
* @param cookie pointer to store a pointer to this particular entry.
* @param pointer pointer to store the address to this particular entry.
*
* @return -1 when ring is empty, 0 on success.
*/

static int driver_dequeue(ring_buffer_t *ring, uintptr_t *addr, unsigned int *len, void **cookie)
static int driver_dequeue(ring_buffer_t *ring, uintptr_t *addr, size_t *len, void **pointer)
{
if (!((ring->write_idx - ring->read_idx) % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT)) {
ZF_LOGW("write idx = %d, read idx = %d", ring->write_idx, ring->read_idx);
ZF_LOGW("Ring is empty");
return -1;
}

*addr = ring->buffers[ring->read_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT].encoded_addr;
*len = ring->buffers[ring->read_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT].len;
*cookie = &ring->buffers[ring->read_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT];
buff_desc_t *buff_desc = &(ring->buffers[ring->read_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT]);
*addr = buff_desc->encoded_addr;
*len = buff_desc->len;
*pointer = buff_desc;

THREAD_MEMORY_RELEASE();
ring->read_idx++;

return 0;
}
}
2 changes: 1 addition & 1 deletion libsharedringbuffer/src/shared_ringbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ void ring_init(ring_handle_t *ring, ring_buffer_t *avail, ring_buffer_t *used, n
ring->avail_ring->write_idx = 0;
ring->avail_ring->read_idx = 0;
}
}
}

0 comments on commit f852ae2

Please sign in to comment.