I definitely agree on the fragmentation problem that I hear for the first time.
So yes, I'd like to replace malloc with a static array queue management but I don't know how to implement static arrays queues management with different sizes.
Before trying implementing queues with malloc, I had coded a circular buffer routine which works fine. In fact this is this circular buffer routine that I've upgraded to add the malloc because I don't know how to to create several different circular buffers with different sizes, by using this unique circular buffer routine. I don't want to duplicate the circular buffer routine each time I need a buffer. As long as the buffers were with the same size, no problem, I could create many buffers. But how to manage then different sizes ?
Here ishow I've implemented my circular buffer routine before implementing the malloc :
#define CIRCULAR_BUFFER_SIZE 100
/* buffer structure */
typedef struct{
U8 u8tab[CIRCULAR_BUFFER_SIZE];
U8 *u8p_in, *u8p_out;
U32 u32Count;
}stBUFFER;
/* Circular buffer prototypes */
void CircularBuffer_Reset(stBUFFER *stBuffer);
char CircularBuffer_Push(stBUFFER *stBuffer, U8 u8byte);
U8 CircularBuffer_Pull(stBUFFER *stBuffer);
The circular buffer routines are just playing with the pointers. The limitation is that my buffer size is the same for any buffer.... So what would be the method to make it flexible ?
Then to manage several buffer sizes I've added the following function which allocate the memory
the typdef became :
typedef struct
{
U8 *u8p_in, *u8p_out;
volatile U32 u32Count;
U32 u32Size;
U8 *u8tab;
}stBUFFER;
and I've added the allocation routine :
/*--------------------------------------------------------------------------------
Description : Initialize a circular buffer
Call : CircularBuffer_Init (stBUFFER *stBuffer, U32 u32TableSize)
Input(s) : *stBuffer = Pointer to the buffer to initialize
u32TableSize = size to allocate
Output(s) : none
Return : none
--------------------------------------------------------------------------------*/
void CircularBuffer_Init (stBUFFER *stBuffer, U32 u32TableSize)
{
stBuffer->u8tab = malloc (u32TableSize * sizeof(char));
stBuffer->u32Size = u32TableSize;
}
The full code is attached (Buffer rouines renamed in queues routines)
any advice on the method I could use ?
Thanks
Stephane