不透明的结构的不透明指针(Opaque pointer to volatile struct)

系统教程 行业动态 更新时间:2024-06-14 16:59:47
不透明的结构的不透明指针(Opaque pointer to volatile struct)

我有一个简单的FIFO环缓冲队列,我在我的嵌入式C程序中使用(使用TI C28x C / C ++编译器,它非常类似于没有扩展的C89的GCC)。 数据被中断队列推送和弹出,因此队列需要是易失性的。

我已经实现了队列代码本身而不使用volatile,因此队列用户可以选择队列是否是易失性的(我希望在具有不同用途的几个项目中使用它),通过将句柄声明为volatile使用中的队列对象,而不是在实现中将队列对象本身定义为volatile。

即在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; } /* ... */

在queue.h中:

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

在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. */

我的问题是,当我将缓冲区转换为void *时,在C的最后一行,这是否会消除波动率的任何有用性?

我应该将QUE_Obj的成员定义为始终是volatile,并将实现中使用的类型调整为volatile,而不管队列的使用情况如何?

换句话说, push()和pop()函数是从中断服务程序调用的,但它们的实现并不“知道”波动性,它们会被优化掉吗?

I have a simple FIFO ring buffer queue that I am using in my embedded C program (using TI C28x C/C++ compiler which is quite similar to GCC for C89 without the extensions). Data is pushed and popped with the queue from interrupts, so the queues need to be volatile.

I've implemented the queue code itself without using volatile so that the queue user has the choice of whether a queue is volatile or not (I want to use this across several projects with differing uses), by declaring the handle to be to a volatile queue object in the usage, instead of defining the queue object itself as volatile 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. */

My question then is, on that last line in C when I cast the buffer to void * does this remove any usefulness of the volatility?

Should I instead define the members of the QUE_Obj to always be volatile and adjust the types used in the implementation to be volatile regardless of the queue's use?

Asked another way, the push() and pop() functions are called from interrupt service routines but their implementation does not "know" about the volatility, will they be optimized away?

最满意答案

您不应该通过非易失性指针访问volatile数据。 请考虑以下简单示例:

volatile int volatileVar; int* nonVolatilePtr = &volatileVar;

每当您的代码通过nonVolatilePtr访问volatileVar ,编译器都不知道您访问的数据是易失性的,并且可能会优化对它的访问(这显然是一种不受欢迎的行为)。 我发现很少使用非易失性指针来表示易失性数据。

如果您希望您的队列可能被中断服务程序(或类似程序)访问,我会使整个数据结构变得不稳定。

You should not access volatile data through a non volatile pointer. Consider the following simple example:

volatile int volatileVar; int* nonVolatilePtr = &volatileVar;

Whenever your code accesses volatileVar through nonVolatilePtr the compiler does not know that the data you are accessing is volatile and might go for optimizing accesses to it (which is clearly an undesired behaviour). I see very little uses of non-volatile pointers to volatile data.

If you expect your queue to be possibly accessed by interrupt service routines (or similar) I would make the whole data structure volatile.

更多推荐

本文发布于:2023-04-17 08:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/6b3fb3ad5e1c9b95f53d7a00541448b5.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不透明   指针   结构   Opaque   volatile

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!