From f852ae26d4a60334bc24bbf5d72a33922b2a938a Mon Sep 17 00:00:00 2001 From: Lucy Date: Wed, 9 Mar 2022 16:19:57 +1100 Subject: [PATCH] Updated according to feedback Signed-off-by: Lucy --- .../shared_ringbuffer/shared_ringbuffer.h | 58 +++++++++++-------- libsharedringbuffer/src/shared_ringbuffer.c | 2 +- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/libsharedringbuffer/include/shared_ringbuffer/shared_ringbuffer.h b/libsharedringbuffer/include/shared_ringbuffer/shared_ringbuffer.h index b8c87e8..46a2d13 100644 --- a/libsharedringbuffer/include/shared_ringbuffer/shared_ringbuffer.h +++ b/libsharedringbuffer/include/shared_ringbuffer/shared_ringbuffer.h @@ -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; @@ -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); } /** @@ -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); } /** @@ -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(); + } } /** @@ -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; @@ -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++; @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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; -} \ No newline at end of file +} diff --git a/libsharedringbuffer/src/shared_ringbuffer.c b/libsharedringbuffer/src/shared_ringbuffer.c index 5e1f526..779b2cd 100644 --- a/libsharedringbuffer/src/shared_ringbuffer.c +++ b/libsharedringbuffer/src/shared_ringbuffer.c @@ -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; } -} \ No newline at end of file +}