From da58c029a0079db62c2eeb40ac1dfdf1959b4e0a Mon Sep 17 00:00:00 2001 From: EmergReanimator Date: Wed, 5 Jan 2022 20:48:44 +0100 Subject: [PATCH 1/5] Introduced ringbuf_init to perform static buffer initialisation --- py/ringbuf.c | 14 +++++++++++++- py/ringbuf.h | 7 +++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/py/ringbuf.c b/py/ringbuf.c index a904a37413..a720abc7d7 100644 --- a/py/ringbuf.c +++ b/py/ringbuf.c @@ -27,11 +27,20 @@ #include "ringbuf.h" +bool ringbuf_init(ringbuf_t *r, uint8_t *buf, size_t capacity) { + r->heap = false; + r->buf = buf; + r->size = capacity + 1; + r->iget = r->iput = 0; + return r->buf != NULL; +} + // Dynamic initialization. This should be accessible from a root pointer. // capacity is the number of bytes the ring buffer can hold. The actual // size of the buffer is one greater than that, due to how the buffer // handles empty and full statuses. bool ringbuf_alloc(ringbuf_t *r, size_t capacity, bool long_lived) { + r->heap = true; r->buf = gc_alloc(capacity + 1, false, long_lived); r->size = capacity + 1; r->iget = r->iput = 0; @@ -39,7 +48,10 @@ bool ringbuf_alloc(ringbuf_t *r, size_t capacity, bool long_lived) { } void ringbuf_free(ringbuf_t *r) { - gc_free(r->buf); + if (r->heap) { + gc_free(r->buf); + } + r->buf = (uint8_t *)NULL; r->size = 0; ringbuf_clear(r); } diff --git a/py/ringbuf.h b/py/ringbuf.h index 35550d7750..6ed72dcf1a 100644 --- a/py/ringbuf.h +++ b/py/ringbuf.h @@ -37,14 +37,13 @@ typedef struct _ringbuf_t { uint32_t size; uint32_t iget; uint32_t iput; + bool heap; } ringbuf_t; // Note that the capacity of the buffer is N-1! -// Static initialization: -// byte buf_array[N]; -// ringbuf_t buf = {buf_array, sizeof(buf_array)}; - +// For static initialization use ringbuf_init() +bool ringbuf_init(ringbuf_t *r, uint8_t *buf, size_t capacity); bool ringbuf_alloc(ringbuf_t *r, size_t capacity, bool long_lived); void ringbuf_free(ringbuf_t *r); size_t ringbuf_capacity(ringbuf_t *r); From 12046323a2f3953d48f1676933116db4ea5724ba Mon Sep 17 00:00:00 2001 From: EmergReanimator Date: Thu, 6 Jan 2022 09:27:44 +0100 Subject: [PATCH 2/5] Fixed unit port build issue --- ports/unix/coverage.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ports/unix/coverage.c b/ports/unix/coverage.c index cdab9b8ef4..16287fdc01 100644 --- a/ports/unix/coverage.c +++ b/ports/unix/coverage.c @@ -527,7 +527,8 @@ STATIC mp_obj_t extra_coverage(void) { // ringbuf { byte buf[100]; - ringbuf_t ringbuf = {buf, sizeof(buf), 0, 0}; + ringbuf_t ringbuf; + ringbuf_init(&ringbuf, &buf[0], sizeof(buf)); mp_printf(&mp_plat_print, "# ringbuf\n"); From ee8000050de841272d2be6ca1037c1ef742c20ff Mon Sep 17 00:00:00 2001 From: EmergReanimator Date: Thu, 6 Jan 2022 13:00:20 +0100 Subject: [PATCH 3/5] Fixed unix port extra_coverage test failure --- tests/unix/extra_coverage.py.exp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/unix/extra_coverage.py.exp b/tests/unix/extra_coverage.py.exp index 4cce80a412..eca4fdc193 100644 --- a/tests/unix/extra_coverage.py.exp +++ b/tests/unix/extra_coverage.py.exp @@ -99,21 +99,21 @@ KeyboardInterrupt: KeyboardInterrupt: 10 # ringbuf -99 0 -98 1 +100 0 +99 1 22 -99 0 -97 2 +100 0 +98 2 aa55 -99 0 -0 99 +100 0 +1 99 -1 -1 98 --1 -2 97 +2 98 0 -cc99 -99 0 +1 99 +-1 +3377 +100 0 0 11bb 0 From 6b872acab7b74069d6465ee7bec897b8adfe2c6f Mon Sep 17 00:00:00 2001 From: EmergReanimator Date: Fri, 7 Jan 2022 21:18:51 +0100 Subject: [PATCH 4/5] Update py/ringbuf.c Co-authored-by: Scott Shawcroft --- py/ringbuf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/ringbuf.c b/py/ringbuf.c index a720abc7d7..2003cc97d5 100644 --- a/py/ringbuf.c +++ b/py/ringbuf.c @@ -30,7 +30,7 @@ bool ringbuf_init(ringbuf_t *r, uint8_t *buf, size_t capacity) { r->heap = false; r->buf = buf; - r->size = capacity + 1; + r->size = capacity; r->iget = r->iput = 0; return r->buf != NULL; } From e712e50aed214f9b6c4d902137e26b75f1a3fb0d Mon Sep 17 00:00:00 2001 From: EmergReanimator Date: Sat, 8 Jan 2022 11:05:09 +0100 Subject: [PATCH 5/5] Updated extra coverage test corresponding to change from 6b872aca --- tests/unix/extra_coverage.py.exp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/unix/extra_coverage.py.exp b/tests/unix/extra_coverage.py.exp index eca4fdc193..4cce80a412 100644 --- a/tests/unix/extra_coverage.py.exp +++ b/tests/unix/extra_coverage.py.exp @@ -99,21 +99,21 @@ KeyboardInterrupt: KeyboardInterrupt: 10 # ringbuf -100 0 -99 1 +99 0 +98 1 22 -100 0 -98 2 +99 0 +97 2 aa55 -100 0 -1 99 +99 0 +0 99 -1 -2 98 +1 98 +-1 +2 97 0 -1 99 --1 -3377 -100 0 +cc99 +99 0 0 11bb 0