I am writing an embedded application designed to run at about 3-7 MHz so speed is CRITICAL. also the device only has 32K of RAM. Dynamic memory allocation is NOT and option. That said...
I am writing a buffering program which requires circular buffers with different queue lengths
- 4x 1024+ int (+ meaning more than this is wanted if available)
- 4x 256 int
- 1x 256 int (with a different application than the one above)
- 1x 2048+ int (+ meaning more than this is wanted if avaliable)
I have implemented it in the following code. You will note that I have created 2 of the buffers as an array of size 1, this is because I want to access all buffers using the same ReadBuff and WriteBuff functions. My problem is that when you pass a structure pointer to a function, the compiler expects that the pointers datatype is the same. Passing a pointer to a BuffType2 datatype to a function that is expecting a BuffType1, however, is somewhat illegal in C and may or may not work properly even though the structure of the structures is exactly the same with the exception of the queue size. The only way I can see to solve this problem is to dynamically allocate the size of my queue after I create the various structure arrays that I need.
It would also be good for you to know that I have designed the buffers to read from the tail and write to the head and it is designed such that when the head and tail indexes pass the buffer size it overflows to 0.
Here is a bit of code
/***************************************************************\
Macro Definitions
\***************************************************************/
// The following sizes MUST be 2^n
#define BUFF_TYPE_1_SIZE (0h400) /* 1024 */
#define BUFF_TYPE_2_SIZE (0h100) /* 256 */
#define BUFF_TYPE_3_SIZE (0h100) /* 256 */
#define BUFF_TYPE_4_SIZE (0h800) /* 2048 */
#define BUFF_TYPE_1_Q_MASK (BUFF_TYPE_1_SIZE-0h1) /* all ones */
#define BUFF_TYPE_2_Q_MASK (BUFF_TYPE_2_SIZE-0h1) /* all ones */
#define BUFF_TYPE_3_Q_MASK (BUFF_TYPE_3_SIZE-0h1) /* all ones */
#define BUFF_TYPE_4_Q_MASK (BUFF_TYPE_4_SIZE-0h1) /* all ones */
// Error Codes
#define ERROR_BUFF_EMPTY (-1) /* The buffer is empty */
#define ERROR_BUFF_DNE (-2) /* The buffer does not exist */
// Test for Buffer Empty
#define BUFF_EMPTY (Buffer.Head == Buffer.Tail)
// Test for data in buffer
#define BUFF_NOT_EMPTY (Buffer.Head != Buffer.Tail)
// Test for Buffer Full
#define BUFF_FULL (((Buffer.Head + 1) & Buffer.Mask) == Buffer.Tail)
/***************************************************************\
Structure Definitions
\***************************************************************/
// Buffers(queues) - These need to be global to allow use in interrupts
typedef struct BuffType1
{
int Head = 0;
int Tail = 0;
int Mask = BUFF_TYPE_1_Q_MASK;
char Full = false;
char Empty = true;
int Q[BUFF_TYPE_1_SIZE];
};
typedef struct BuffType2
{
int Head = 0;
int Tail = 0;
int Mask = BUFF_TYPE_2_Q_MASK;
char Full = false;
char Empty = true;
int Q[BUFF_TYPE_2_SIZE];
};
typedef struct BuffType3
{
int Head = 0;
int Tail = 0;
int Mask = BUFF_TYPE_3_Q_MASK;
char Full = false;
char Empty = true;
int Q[BUFF_TYPE_3_SIZE];
};
typedef struct BuffType4
{
int Head = 0;
int Tail = 0;
int Mask = BUFF_TYPE_4_Q_MASK;
char Full = false;
char Empty = true;
int Q[BUFF_TYPE_4_SIZE];
};
/***************************************************************\
Global Variables
\***************************************************************/
// FIFO Ring buffers - These need to be global to allow use in interrupts
struct BuffType1 MyBuff1[4];
struct BuffType2 MyBuff2[4];
struct BuffType3 MyBuff3[1];
struct BuffType4 MyBuff4[1];
/***************************************************************\
Functions
\***************************************************************/
/*---------------------------------------------------------------
int ReadBuff(struct BuffType1* BufferPtr)
Parameters : struct* BufferPtr
this is a pointer to the buffer you wish to read
Returns : int
if empty - error code
if not empty - The value that was popped off the buffer
Description : This function returns the value at the tail of the
buffer or an error if the buffer is empty. The
tail is incremented after the read and overflows
automatically
---------------------------------------------------------------*/
int ReadBuff(struct BuffType1* BufferPtr)
{
int Value = ERROR_BUFF_EMPTY; // error
if(BUFF_EMPTY)
(*BufferPtr).Empty = true; // set the empty flag
else
{
(*BufferPtr).Empty = false; // reset the empty flag
Value = (*BufferPtr).Q[(*BufferPtr).Tail]; // Read value is at the tail of the buffer
(*BufferPtr).Tail = ((*BufferPtr).Tail + 1)&((*BufferPtr).Mask) /* increment the tail,
making sure that if it rolls over its queue size, it rolls over to 0 */
}
return Value;
}
/*---------------------------------------------------------------
int WriteBuff(struct* BufferPtr, int Data)
Parameters : struct* BufferPtr
The pointer to the buffer you wish to write to
int Data
The Data you wish to write to the buffer
Returns : true - write was successful
false - the buffer is full and did not write
Description : This function writes the data to the head of the
buffer and returns an error if the buffer is full.
if the buffer is full, no data is written. The
head is incremented after the write and overflows
automatically
---------------------------------------------------------------*/
char WriteBuff(struct BuffType1* BufferPtr, int Data)
{
int Success = false; // there was an error writing to the buffer
if (BUFF_FULL)
(*BufferPtr).Full = true; // Indicate buffer is full (next avaliable spot to write is the tail)
else
{
(*BufferPtr).Full = false;
(*BufferPtr).Q[(*BufferPtr).Head] = Data;
(*BufferPtr).Head = ((*BufferPtr).Head + 1)&((*BufferPtr).Mask)
}
return !((*BufferPtr).Full;); // Return false if buffer was full and write could not happen
}
/*---------------------------------------------------------------
void ResetBuff(struct* BufferPtr)
Parameters : struct* BufferPtr
The pointer to the buffer you wish to write to
Returns : nothing
Description : This function resets the buffer but does not clear
anything
---------------------------------------------------------------*/
void ResetBuff(struct BuffType1* BufferPtr)
{
(*BufferPtr).Head = (*BufferPtr).Tail = 0;
(*BufferPtr).Full = false;
(*BufferPtr).Empty = true;
(*BufferPtr).Q[0] = 0; //or null if it is defined
}
/*---------------------------------------------------------------
void NullBuff(struct* BufferPtr)
Parameters : struct* BufferPtr
The pointer to the buffer you wish to write to
Returns : nothing
Description : This function resets all values in the queue to 0
---------------------------------------------------------------*/
void NullBuff(struct BuffType1* BufferPtr)
{
int i;
for(i=0; i=((*BufferPtr).Mask); i++) // for all values in the buffer
(*BufferPtr).Q = 0; // clear the value
}