开发者

How to pass multiple structures to one function (ANSI C)

开发者 https://www.devze.com 2023-03-26 09:14 出处:网络
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 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)
    els开发者_如何学JAVAe
        {
        (*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
    }


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号