2

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
    }
Jeremy
  • 103
  • 12

2 Answers2

2

You could create a structure for a buffer with a zero/one length array for Q and then union it with the real structure for the buffer you want to use. You'd then also need to have some way of knowing the length that you are meant to use, which you could either get from your mask type or an additional structure member.

struct BuffType
{
    int Head;
    int Tail;
    int Mask;
    char Full;
    char Empty;
    int Q[1];
};

struct BuffType1_storage
{
    int Head;
    int Tail;
    int Mask;
    char Full;
    char Empty;
    int Q[BUFF_TYPE_1_SIZE];
};

union BuffType1
{
    struct BuffType b;
    struct BuffType1_storage b1;
}

union BuffType1 MyBuff1[4];

You can then pass a pointer to the union BuffType1 or struct BuffType b member and you know that the memory behind it is there for a particular type.

tinman
  • 6,348
  • 1
  • 30
  • 43
  • I really like this idea but I have a question about it. When using a data member, for instance BuffType1.BuffType1_storage.Head would I have to worry about the extra time it takes to deference the union or will it take the same number of instructions as BuffType1_storage.Head (if there was no union of course in that case) – Jeremy Aug 05 '11 at 16:46
  • Cool, I don't use unions a lot so I never though of that. Q: For unions, does the overall memory footprint need to be the same for the internal components (ByffType and BuffType1_storage) or will the allocation err toward the bigger footprint and shove the smaller footprint inside? – spade78 Aug 05 '11 at 16:47
  • @Jeremy: There should not be any difference between using your BuffType1.Head and a union BuffType1.BuffType_storage.Head. It is still just accessing a variable from memory at some offset from the variable. The union does not create any extra dereferencing at runtime, it is just different types overlaid onto the same memory. – tinman Aug 05 '11 at 16:54
  • @spade78: The compiler will make your union at least as big as the biggest member contained within it. – tinman Aug 05 '11 at 16:54
  • @spade78: It shouldn't matter, you can make a typedef for both at 2048 but when you allocate that into the union variable name under a union the total for both is still 2048 as they are just overlaid bitwise – Jeremy Aug 05 '11 at 16:56
  • @tinman: I was wondering if it dereferences the same as a structure. for example the structure member g in "a.b.c.d.e.f.g" takes a lot longer to reference than "a.g" I was wondering if that would be the same with unions – Jeremy Aug 05 '11 at 16:59
  • @Jeremy: It's probably compiler dependant and yes it will probably output the same code for a union as it does for a structure, but I'd be disappointed if my compiler had to sequentially add all the offsets instead of calculating them once at compile time. These are fixed size structures, all members are at a fixed offset, and they're not pointers (so the base address of the offset into memory never changes). Get your compiler to output the assembly and check it to make sure. – tinman Aug 05 '11 at 17:07
  • @tinman: I think I will give this a go. in theory it seems to solve all my current issues and allows for much better organization – Jeremy Aug 05 '11 at 17:17
0

Putting aside performance considerations, memory size implications for the moment and focusing on the multi-structure-type input parameter, have you considered creating a header struct to describe each individual buffer with a pointer to the buffer in question? That way you'd be passing a single structure type (the buffer meta-data) and then having the processing function figure out for itself how to proceed.

EDIT -- So as an illustration, you could take BuffTypeX types and condense the buffer info into a meta struct like so:

typedef struct buffMetaData
  {
  int Head, Tail, Mask, Full, Empty...
  int *PtrToBuff
  };

You're buffers would be defined somewhere appropriate as straight arrays and during initialization you'd have to init your buffMetaData objects and point them to the appropriate storage location.

Then you'd implement your processing function to take in a buffMetaData type, read the contents and proceed with each individual buffer as necessary (probably based on Mask or whatever unique ID label a buffMetaData field could hold).

Admittedly this approach could be a memory hog as all buffers need to be allocated in full before-hand and you also need to save RAM for stack, heap, other variables.

spade78
  • 1,939
  • 1
  • 20
  • 29