I implemented a queue in C using an array of structures.
typedef struct{
req_t buffer[BUFFER_SIZE];
uint16_t size;
uint16_t count;
req_t *p_head;
req_t *p_tail;
}circular_buffer_t;
void init_cb(circular_buffer_t *p_cb){
p_cb->p_head = p_cb->buffer;
p_cb->p_tail = p_cb->buffer;
p_cb->count = 0;
p_cb->size = BUFFER_SIZE;
}
The problem is that the above implementation is only applicable for storing instances of req_t structures. Now I need to store instances of another and I don’t know how to define a queue in a more general way, so that I can use the same queue for instances of different structures. The problem is that I need to know the type of structure before defining the buffer. Does anyone have any ideas how to solve this?
\
typedef struct{ \
TYPE buffer[BUFFER_SIZE]; \
uint16_t size; \
uint16_t count; \
TYPE *p_head; \
TYPE *p_tail; \
}circular_buffer_
\
\
void init_cb_
p_cb->p_head = p_cb->buffer; \
p_cb->p_tail = p_cb->buffer; \
p_cb->count = 0; \
p_cb->size = BUFFER_SIZE; \
} \
\
BOOL enqueue_cb_
\
if(p_cb->count < p_cb->size){ \
\
taskENTER_CRITICAL(); \
\
*(p_cb->p_tail) = *p_enq_elem; \
p_cb->p_tail = ((++(p_cb->p_tail) == (p_cb->buffer + p_cb->size)) ? \
(p_cb->buffer) : (p_cb->p_tail)); \
p_cb->count++; \
\
taskEXIT_CRITICAL(); \
\
return TRUE; \
\
}else{ \
\
return FALSE; \
\
} \
\
} \
\
BOOL dequeue_cb_
\
if((p_cb->count) != 0){ \
\
taskENTER_CRITICAL(); \
\
*p_deq_elem = *(p_cb->p_head); \
p_cb->p_head = ((++(p_cb->p_head) == (p_cb->buffer + p_cb->size)) ? \
(p_cb->buffer) : (p_cb->p_head)); \
p_cb->count--; \
\
taskEXIT_CRITICAL(); \
\
return TRUE; \
\
}else{ \
\
return FALSE; \
\
} \
\
} \
Structures I'm going to use with a queue
typedef struct{
uint32_t addr;
BOOL critical;
BOOL set;
BOOL cleared;
BOOL communicated;
uint8_t code;
uint8_t no;
uint8_t no_flashes;
}alarm_t;
and
typedef struct{
msg_e req_type;
uint8_t blk_no;
uint8_t no_records;
uint8_t data_id[MAX_NO_RECORDS];
uint16_t value[MAX_NO_RECORDS];
uint8_t cleared_alarm_no;
uint8_t flash_load;
uint8_t mode[6];
uint8_t data_block[BLOCK_SIZE];
uint8_t flash_page_number;
uint8_t flash_block_number;
}req_t;
Steve source
share