diff --git a/src/hal/components/histobinstream.comp b/src/hal/components/histobinstream.comp index e65230e69fd..845e088c1f4 100644 --- a/src/hal/components/histobinstream.comp +++ b/src/hal/components/histobinstream.comp @@ -122,9 +122,8 @@ EXTRA_SETUP() { int err; // One complete set is MAXBINNUMBER+2 records: the negative tail, every - // bin and the positive tail. The ring buffer's usable capacity is depth-1 - // (one slot reserved), so add 1 to fit it. - if (0 != (err = hal_stream_create(&fifo, comp_id, keys[extra_arg], MAXBINNUMBER + 2 + 1, "u"))) + // bin and the positive tail. + if (0 != (err = hal_stream_create(&fifo, comp_id, keys[extra_arg], MAXBINNUMBER + 2, "u"))) return err; // The other side should run hal_stream_attach(&fifo, comp_id, key, "u") return 0; diff --git a/src/hal/components/latencybinstream.comp b/src/hal/components/latencybinstream.comp index 1dbf725c565..7e655c4a13d 100644 --- a/src/hal/components/latencybinstream.comp +++ b/src/hal/components/latencybinstream.comp @@ -101,9 +101,8 @@ EXTRA_SETUP() { if (extra_arg < 0 || extra_arg >= MAX_INST || !keys[extra_arg]) return -EINVAL; int err; - // One complete set is 2*MAXBINNUMBER+1+2 records. The ring buffer's - // usable capacity is depth-1 (one slot reserved), so add 1 to fit it. - if (0 != (err = hal_stream_create(&fifo, comp_id, keys[extra_arg], 2*MAXBINNUMBER + 1 + 2 + 1, "u"))) + // One complete set is 2*MAXBINNUMBER+1+2 records. + if (0 != (err = hal_stream_create(&fifo, comp_id, keys[extra_arg], 2*MAXBINNUMBER + 1 + 2, "u"))) return err; // The other side should run hal_stream_attach(&fifo, comp_id, LBSKEY, "u") return 0; diff --git a/src/hal/hal_lib.c b/src/hal/hal_lib.c index c982589e915..a574f32f660 100644 --- a/src/hal/hal_lib.c +++ b/src/hal/hal_lib.c @@ -4358,7 +4358,7 @@ typedef struct __hal_stream_shm_t { atomic_uint in; atomic_uint out; unsigned this_sample; - unsigned depth; + unsigned depth; // capacity in samples; the ring has depth+1 slots int num_pins; atomic_uint num_overruns; atomic_uint num_underruns; @@ -4427,7 +4427,7 @@ int hal_stream_create(hal_stream_t *stream, int comp, int key, unsigned depth, c if(!result) return -EINVAL; int pin_count = result; - size_t size = sizeof(hal_stream_shm_t) + sizeof(hal_stream_data_u) * depth * (1+pin_count); + size_t size = sizeof(hal_stream_shm_t) + sizeof(hal_stream_data_u) * (depth + 1) * (1+pin_count); result = rtapi_shmem_new(key, comp, size); if(result < 0) return result; stream->shmem_id = result; @@ -4455,8 +4455,9 @@ void hal_stream_destroy(hal_stream_t *stream) static inline unsigned hal_stream_advance(hal_stream_t *stream, unsigned n) { + // one slot more than depth, so a full ring is not mistaken for empty n += 1; - if(n >= stream->fifo->depth) return 0; + if(n > stream->fifo->depth) return 0; return n; } @@ -4546,7 +4547,7 @@ int hal_stream_depth(hal_stream_t *stream) unsigned out = hal_stream_atomic_load_out(stream); unsigned in = hal_stream_atomic_load_in(stream); if(out > in) - return stream->fifo->depth + in - out; + return stream->fifo->depth + 1 + in - out; return in - out; } @@ -4633,7 +4634,7 @@ int hal_stream_attach(hal_stream_t *stream, int comp_id, int key, const char *ty /* now use data in fifo structure to calculate proper shmem size */ unsigned depth = fifo->depth; int pin_count = fifo->num_pins; - size_t size = sizeof(hal_stream_shm_t) + sizeof(hal_stream_data_u) * depth * (1+pin_count); + size_t size = sizeof(hal_stream_shm_t) + sizeof(hal_stream_data_u) * (depth + 1) * (1+pin_count); /* close shmem, re-open with proper size */ rtapi_shmem_delete(shmem_id, comp_id); shmem_id = result = rtapi_shmem_new(key, comp_id, size); diff --git a/tests/halmodule/stream/stream_reader.py b/tests/halmodule/stream/stream_reader.py index 0aaa20965e8..e6af4a38199 100755 --- a/tests/halmodule/stream/stream_reader.py +++ b/tests/halmodule/stream/stream_reader.py @@ -3,7 +3,7 @@ import time c = hal.component("stream_reader") reader = hal.stream(c, hal.streamer_base, "bfsu") -for i in range(9): +for i in range(10): assert reader.readable assert reader.read() == ((i % 2, i, i, i)) assert reader.num_underruns == 0 diff --git a/tests/halmodule/stream/stream_writer.py b/tests/halmodule/stream/stream_writer.py index a4161a0ad55..edc78c5a206 100755 --- a/tests/halmodule/stream/stream_writer.py +++ b/tests/halmodule/stream/stream_writer.py @@ -4,7 +4,7 @@ c = hal.component("stream_writer") writer = hal.stream(c, hal.streamer_base, 10, "bfsu") -for i in range(9): +for i in range(10): assert writer.writable writer.write((i % 2, i, i, i)) assert not writer.writable