From ca914eb17bfdb2e56a39951a96a9fb54e1a5f607 Mon Sep 17 00:00:00 2001 From: Sander Vrijders Date: Mon, 16 May 2016 17:05:36 +0200 Subject: lib: Add missing modulo calculation Adds a missing modulo calculation that would result in index -1 being returned instead of zero when the ringbuffer skipped back to the beginning. --- src/lib/shm_du_map.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/lib/shm_du_map.c') diff --git a/src/lib/shm_du_map.c b/src/lib/shm_du_map.c index 7099b265..a7ec5ac9 100644 --- a/src/lib/shm_du_map.c +++ b/src/lib/shm_du_map.c @@ -227,11 +227,11 @@ void shm_du_map_destroy(struct shm_du_map * dum) free(dum); } -int shm_create_du_buff(struct shm_du_map * dum, - size_t size, - size_t headspace, - uint8_t * data, - size_t len) +ssize_t shm_create_du_buff(struct shm_du_map * dum, + size_t size, + size_t headspace, + uint8_t * data, + size_t len) { struct shm_du_buff * sdb; long blocks = 0; @@ -239,7 +239,7 @@ int shm_create_du_buff(struct shm_du_map * dum, int sz2 = headspace + len + sizeof *sdb; uint8_t * write_pos; size_t copy_len; - size_t index; + ssize_t index; if (dum == NULL || data == NULL) { LOG_DBGF("Bogus input, bugging out."); @@ -294,7 +294,7 @@ int shm_create_du_buff(struct shm_du_map * dum, --blocks; } - index = *dum->ptr_head - 1; + index = (*dum->ptr_head - 1) & (SHM_BLOCKS_IN_MAP - 1); pthread_mutex_unlock(dum->shm_mutex); @@ -304,9 +304,9 @@ int shm_create_du_buff(struct shm_du_map * dum, /* FIXME: this cannot handle packets stretching beyond the ringbuffer border */ int shm_du_map_read_sdu(uint8_t ** dst, struct shm_du_map * dum, - size_t idx) + ssize_t idx) { - size_t len = 0; + size_t len = 0; if (idx > SHM_BLOCKS_IN_MAP) return -1; @@ -328,7 +328,7 @@ int shm_du_map_read_sdu(uint8_t ** dst, return len; } -int shm_release_du_buff(struct shm_du_map * dum, size_t idx) +int shm_release_du_buff(struct shm_du_map * dum, ssize_t idx) { long sz; long blocks = 0; -- cgit v1.2.3