From dc2a432a655bb68157def7de929f289a658cafaa Mon Sep 17 00:00:00 2001 From: skarg Date: Fri, 18 Dec 2009 04:20:44 +0000 Subject: [PATCH] Added alloc to ringbuf module. --- bacnet-stack/include/ringbuf.h | 2 ++ bacnet-stack/include/rp.h | 9 +++++++ bacnet-stack/src/ringbuf.c | 47 +++++++++++++++++++++++++++++++--- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/bacnet-stack/include/ringbuf.h b/bacnet-stack/include/ringbuf.h index 02b0fe92..5e96d59f 100644 --- a/bacnet-stack/include/ringbuf.h +++ b/bacnet-stack/include/ringbuf.h @@ -64,6 +64,8 @@ extern "C" { bool Ringbuf_Put( RING_BUFFER * b, /* ring buffer structure */ char *data_element); /* one element to add to the ring */ + char * Ringbuf_Alloc( + RING_BUFFER * b); void Ringbuf_Init( RING_BUFFER * b, /* ring buffer structure */ char *data, /* data block or array of data */ diff --git a/bacnet-stack/include/rp.h b/bacnet-stack/include/rp.h index 8adfa4cf..1396b467 100644 --- a/bacnet-stack/include/rp.h +++ b/bacnet-stack/include/rp.h @@ -61,6 +61,15 @@ typedef bool( *object_valid_instance_function) ( uint32_t object_instance); +/* structure for linked list of object functions */ +struct BACnet_Read_Property_Functions; +typedef struct BACnet_Read_Property_Functions { + BACNET_OBJECT_TYPE object_type; + read_property_function rp_object_property_value; + object_valid_instance_function rp_object_valid_instance; + struct BACnet_Read_Property_Functions *next; +} BACNET_RP_FUNCTIONS; + #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ diff --git a/bacnet-stack/src/ringbuf.c b/bacnet-stack/src/ringbuf.c index 1d28d90a..bf690814 100644 --- a/bacnet-stack/src/ringbuf.c +++ b/bacnet-stack/src/ringbuf.c @@ -76,17 +76,17 @@ char *Ringbuf_Get_Front( char *Ringbuf_Pop_Front( RING_BUFFER * b) { - char *data = NULL; /* return value */ + char *ring_data = NULL; /* used to help point ring data */ if (b && b->count) { - data = &(b->data[b->head * b->element_size]); + ring_data = &(b->data[b->head * b->element_size]); b->head++; if (b->head >= b->element_count) b->head = 0; b->count--; } - return data; + return ring_data; } /**************************************************************************** @@ -122,6 +122,33 @@ bool Ringbuf_Put( return status; } +/**************************************************************************** +* DESCRIPTION: Allocates and adds an element of data to the ring buffer +* RETURN: pointer to the data, or NULL if nothing in the list +* ALGORITHM: none +* NOTES: none +*****************************************************************************/ +char * Ringbuf_Alloc( + RING_BUFFER * b) +{ + unsigned offset = 0; /* offset into array of data */ + char *ring_data = NULL; /* used to help point ring data */ + + if (b) { + /* limit the amount of data that we accept */ + if (b->count < b->element_count) { + offset = b->head + b->count; + if (offset >= b->element_count) + offset -= b->element_count; + ring_data = b->data + offset * b->element_size; + b->count++; + } + } + + return ring_data; +} + + /**************************************************************************** * DESCRIPTION: Configures the ring buffer * RETURN: none @@ -251,7 +278,19 @@ void testRingBuf( } } ct_test(pTest, Ringbuf_Empty(&test_buffer)); - + /* test the alloc feature */ + test_data = Ringbuf_Alloc(&test_buffer); + ct_test(pTest, test_data != NULL); + ct_test(pTest, !Ringbuf_Empty(&test_buffer)); + for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { + test_data[data_index] = data_index; + } + test_data = Ringbuf_Pop_Front(&test_buffer); + ct_test(pTest, test_data != NULL); + ct_test(pTest, Ringbuf_Empty(&test_buffer)); + for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { + ct_test(pTest, test_data[data_index] == data_index); + } return; }