Added alloc to ringbuf module.
This commit is contained in:
@@ -64,6 +64,8 @@ extern "C" {
|
|||||||
bool Ringbuf_Put(
|
bool Ringbuf_Put(
|
||||||
RING_BUFFER * b, /* ring buffer structure */
|
RING_BUFFER * b, /* ring buffer structure */
|
||||||
char *data_element); /* one element to add to the ring */
|
char *data_element); /* one element to add to the ring */
|
||||||
|
char * Ringbuf_Alloc(
|
||||||
|
RING_BUFFER * b);
|
||||||
void Ringbuf_Init(
|
void Ringbuf_Init(
|
||||||
RING_BUFFER * b, /* ring buffer structure */
|
RING_BUFFER * b, /* ring buffer structure */
|
||||||
char *data, /* data block or array of data */
|
char *data, /* data block or array of data */
|
||||||
|
|||||||
@@ -61,6 +61,15 @@ typedef bool(
|
|||||||
*object_valid_instance_function) (
|
*object_valid_instance_function) (
|
||||||
uint32_t object_instance);
|
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
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif /* __cplusplus */
|
#endif /* __cplusplus */
|
||||||
|
|||||||
@@ -76,17 +76,17 @@ char *Ringbuf_Get_Front(
|
|||||||
char *Ringbuf_Pop_Front(
|
char *Ringbuf_Pop_Front(
|
||||||
RING_BUFFER * b)
|
RING_BUFFER * b)
|
||||||
{
|
{
|
||||||
char *data = NULL; /* return value */
|
char *ring_data = NULL; /* used to help point ring data */
|
||||||
|
|
||||||
if (b && b->count) {
|
if (b && b->count) {
|
||||||
data = &(b->data[b->head * b->element_size]);
|
ring_data = &(b->data[b->head * b->element_size]);
|
||||||
b->head++;
|
b->head++;
|
||||||
if (b->head >= b->element_count)
|
if (b->head >= b->element_count)
|
||||||
b->head = 0;
|
b->head = 0;
|
||||||
b->count--;
|
b->count--;
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
return ring_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@@ -122,6 +122,33 @@ bool Ringbuf_Put(
|
|||||||
return status;
|
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
|
* DESCRIPTION: Configures the ring buffer
|
||||||
* RETURN: none
|
* RETURN: none
|
||||||
@@ -251,7 +278,19 @@ void testRingBuf(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ct_test(pTest, Ringbuf_Empty(&test_buffer));
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user