I use the following type:
/* double_buffer.h */
typedef struct
{
uint8_t * active_buffer_p; //< Address of active buffer
uint8_t current_writing_index; //< Current writing index in active buffer
uint8_t buffer_1[BUFFER_SIZE]; //< First buffer
uint8_t buffer_2[BUFFER_SIZE]; //< Second buffer
} double_buffer_t;
#define DoubleBuffer_init(buffers) do { \
(buffers).active_buffer_p = (buffers).buffer_1; \
(buffers).current_writing_index = 0; \
} while(0)
In my code, I declare an array of double buffer, using the volatile keywoard (because the buffers can be updated/read asynchronously in interrupts and in functions):
static volatile double_buffer_t m_double_buffers[NB_DOUBLE_BUFFERS];
I then initialize those buffers individually:
DoubleBuffer_init(m_double_buffers[id]);
When I compile the software (gcc), I got the following warning:
error: assignment discards 'volatile' qualifier from pointer target type [-Werror=discarded-qualifiers]
28 | (buffers).active_buffer_p = (buffers).buffer_1; \
The reason why I have this warning is quite unclear to me, and I am not sure how to fix it.
Any help would be appreciated (I can update the question if something is not clear).
You get this warning because you have a volatile object, and you create a non-volatile pointer to it.
This is bad as the compiler could access the volatile object without knowing that it is volatile. E.g. it could transform two reads into a single, it could change the order etc.
One way to fix it is to define active_buffer_p
to uint8_t volatile * active_buffer_p
.