From 40f7aaba7476503e5c7c9b8ce8ecba0603a35f17 Mon Sep 17 00:00:00 2001 From: Steve Karg Date: Mon, 13 Apr 2020 11:45:57 -0500 Subject: [PATCH] Added macro to calculate the next power of two for FIFO data store (#69) Co-authored-by: Steve Karg --- src/bacnet/basic/sys/fifo.c | 6 +++--- src/bacnet/basic/sys/fifo.h | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/bacnet/basic/sys/fifo.c b/src/bacnet/basic/sys/fifo.c index 8a62d58b..0a9b540a 100644 --- a/src/bacnet/basic/sys/fifo.c +++ b/src/bacnet/basic/sys/fifo.c @@ -45,7 +45,7 @@ * * To use this library, first declare a data store, sized for a power of 2: * {@code - * static volatile uint8_t data_store[64]; + * static volatile FIFO_DATA_STORE(data_store, 64); * } * * Then declare the FIFO tracking structure: @@ -343,8 +343,7 @@ void testFIFOBuffer(Test *pTest) { /* FIFO data structure */ FIFO_BUFFER test_buffer = { 0 }; - /* FIFO data store. Note: size must be a power of two! */ - volatile uint8_t data_store[64] = { 0 }; + volatile FIFO_DATA_STORE(data_store, 60) = { 0 }; uint8_t add_data[40] = { "RoseSteveLouPatRachelJessicaDaniAmyHerb" }; uint8_t test_add_data[40] = { 0 }; uint8_t test_data = 0; @@ -352,6 +351,7 @@ void testFIFOBuffer(Test *pTest) unsigned count = 0; bool status = 0; + ct_test(pTest, sizeof(data_store) == 64); FIFO_Init(&test_buffer, data_store, sizeof(data_store)); ct_test(pTest, FIFO_Empty(&test_buffer)); diff --git a/src/bacnet/basic/sys/fifo.h b/src/bacnet/basic/sys/fifo.h index b2009908..41f3dfee 100644 --- a/src/bacnet/basic/sys/fifo.h +++ b/src/bacnet/basic/sys/fifo.h @@ -10,6 +10,29 @@ #include #include "bacnet/bacnet_stack_exports.h" +/** +* FIFO buffer power of two alignment macro +* +* @{ +*/ +#ifndef NEXT_POWER_OF_2 +#define B2(x) ( (x) | ( (x) >> 1) ) +#define B4(x) ( B2(x) | ( B2(x) >> 2) ) +#define B8(x) ( B4(x) | ( B4(x) >> 4) ) +#define B16(x) ( B8(x) | ( B8(x) >> 8) ) +#define B32(x) (B16(x) | (B16(x) >>16) ) +#define NEXT_POWER_OF_2(x) (B32((x)-1) + 1) +#endif +/** @} */ + +/** +* FIFO data store structure +* +* @{ +*/ +#define FIFO_DATA_STORE(b,c) uint8_t b[NEXT_POWER_OF_2(c)] +/** @} */ + /** * FIFO data structure *