Opaque pointer to an unstable structure

I have a simple FIFO ring buffer that I use in my C firmware (using the TI C28x C / C ++ compiler, which is very similar to GCC for C89 without extensions). Data is pushed and knocked out of the queue from interruptions, so queues must be unstable.

I implemented the queue code itself without using volatile, so the queue user has the choice of whether the queue is volatile or not (I want to use this for several projects with different applications), declaring that the handle to the volatile queue object is in use, instead of defining the queue object itself as mutable in the implementation.

i.e. in que.c:

struct QUE_Obj { /* Object & members are not defined as volatile. */
    void * data;
    uint16_t capacity;
    uint16_t head;
    uint16_t tail;
    uint16_t size;
    bool full;
    bool empty;
}

/* Implementation uses all non-volatile types. */
QUE_Handle QUE_init(void * data, uint_least8_t size, uint16_t capacity) { 
    /* ... */ 
    QUE_Handle q = (QUE_Handle)malloc(sizeof(struct QUE_Obj));
    /* ... */
    return q;
}

/* ... */

in queue.h:

typedef QUE_Obj * QUE_Handle;
QUE_Handle QUE_init(void * data, uint_least8_t size, uint16_t capacity)

in main.c:

/* Data buffer and queue handle declared to be for volatile data. */
static volatile uint16_t buffer[BUFFER_LENGTH] = {0};
volatile QUE_Handle que = QUE_init((void *)buffer); /* Buffer passed without volatile. */

, C, void *, ?

QUE_Obj , , ?

, push() pop() , "" , ?

+4
1

. :

volatile int volatileVar;
int* nonVolatilePtr = &volatileVar;

, volatileVar nonVolatilePtr, , , , ( ). .

, ( ), .

+5

All Articles