0% found this document useful (0 votes)
2 views

Swap_data_function

The document defines a LIFO buffer structure and provides a function to check if the buffer is full. It highlights a correction needed in the logic of adding items to the buffer to ensure the head pointer tracks the last added item correctly. An example of a corrected circular buffer item addition function is also provided.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Swap_data_function

The document defines a LIFO buffer structure and provides a function to check if the buffer is full. It highlights a correction needed in the logic of adding items to the buffer to ensure the head pointer tracks the last added item correctly. An example of a corrected circular buffer item addition function is also provided.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

LIFO Struct :

typedef struct LIFO_Buf {

uint32_t length;

uint8_t * base;

uint8_t * head;

} LIFO_Buf_t;

LB_Status LIFO_Is_Buf_Full(LiIFO_Buf_t * lbuf) {

/* Checks if buffer is initialized */

if ( ! lbuf || ! lbuf->head || ! lbuf->base) {

return LB_NULL;

/* Checks if in the ragen of buf[0] < index < buf[length] */

if ( lbuf->head == lbuf->base + (lbuf->length - 1) ){


return LB_FULL:

else {

return LB_NOT_FULL;

Reflect

Correction:

This buffer shows that it will add the item in before it adjusts the pointer. Instead to be consistent with
our definition of CB_Is_Buf_Full(), the line that adds the item in should come after the check for the
head pointer. In this corrected definition, the head always tracks the last added item, where in the slides
it tracks the next available spot.

An example definition is below:

CB_Status CB_Add_Item(CB_Buf_t * cbuf, uint8_t item) {

/* Checks if buffer is initialized */

if ( ! cbuf || ! cbuf->head || ! cbuf->base) {

return CB_NULL;

}
/* Checks if the buffer is Full */

if ( CB_Is_Buf_Full(lbuf) == CB_FULL ){

return CB_FULL;

/* Checks if buffer is at the end of the allocated region, moves to beginning if so */

if ( cbuf->head == cbuf->head + (cbuf->length -1 ) ) {

cbuf->head = cbuf->base;

else {

cbuf->head++;

*cbuf->head = item;

return CB_NO_ERROR;

}
Play

Volume

6:51

7:22

Autoplay is on

Subtitles

Settings

Exit full Screen

You might also like