memcpy和volatile [英] memcpy and volatile

查看:694
本文介绍了memcpy和volatile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi List,


我想编写一个函数来从硬件缓冲区复制一些数据。

硬件可以改变这个内容没有它的缓冲区

由我的函数写入。我想使用memcpy来卸载数据。


在这种情况下我是否需要将源数据指定为volatile?


什么是指定源指针所指向的数据的正确语法是指针源指针是易失性而不是指针本身?


如何确保所有N源字节被视为易失性和

不仅仅是第一个字节?


感谢您的帮助。


Mark

Hi List,

I want to write a function to copy some data out of a hardware buffer.
The hardware can change the contents of this buffer without it being
written to by my function. I want to use memcpy to unload the data.

Do I need to specify the source data as volatile in this case?

What is the correct syntax for specifying that the data pointed to by
the source pointer is volatile and not the pointer itself?

How do I ensure that all N source bytes are regarded as volatile and
not just the first byte?

Thanks for your help.

Mark

推荐答案



2006年12月11日星期一,Mark写道:

On Mon, 11 Dec 2006, Mark wrote:

>

我想编写一个函数来复制硬件缓冲区中的一些数据。

硬件可以改变内容这个缓冲区没有它我的函数写的
。我想使用memcpy来卸载数据。
>
I want to write a function to copy some data out of a hardware buffer.
The hardware can change the contents of this buffer without it being
written to by my function. I want to use memcpy to unload the data.



为什么?如果您自己编写了memcpy,那么只需将代码

剪切并粘贴到一个新函数中,例如


size_t volatile_memcpy(volatile void * d,volatile void * s,size_t n)

{

[您的代码在这里]

}


一切都会奏效。如果你自己没有写memcpy,那么

你怎么知道它会做你想要的?例如,memcpy可能会多次向目标缓冲区写入
;它可能以N字节块而不是字节大小的块读取或写入
;它可能以相反的顺序读取和写入
字节(从s [n-1]开始并向下到s [0])。

有各种各样的memcpy允许在许多平台上执行---和/ do / do,

--这会对大多数数据造成严重破坏

归类为volatile(内存) - 映射寄存器,也许是视频内存,

等等,handwavey handwavey)。

Why? If you wrote memcpy yourself, then just cut-and-paste that code
into a new function, like

size_t volatile_memcpy(volatile void *d, volatile void *s, size_t n)
{
[your code here]
}

and everything will work. And if you didn''t write memcpy yourself, then
how do you know that it will do what you want? For example, memcpy might
write to the destination buffer multiple times; it might read or write
in N-byte chunks instead of byte-sized chunks; it might read and write
the bytes in reverse order (starting with s[n-1] and going down to s[0]).
There are all sorts of things memcpy is allowed to do --- and /does/ do,
on many platforms --- that would play havoc with most data worth
classifying as volatile (memory-mapped registers, perhaps video memory,
and so on, handwavey handwavey).


我是否需要将源数据指定为volatile在这种情况下?
Do I need to specify the source data as volatile in this case?



如果你最终只使用memcpy,那就不一算了。 memcpy'的

参数没有'volatile'限定词;在调用memcpy之前,你必须将它们从两个参数中抛出




volatile char * myd = ...;

char mys [100] = {...};

memcpy((void *)myd,mys,100);

If you end up just using memcpy, it won''t matter one whit. memcpy''s
parameters don''t have the ''volatile'' qualifier; you''ll have to cast it
away from both arguments before calling memcpy.

volatile char *myd = ...;
char mys[100] = { ... };
memcpy((void*)myd, mys, 100);


指定源指针指向源数指针的数据是否是易失性而不是指针本身的正确语法是什么?
What is the correct syntax for specifying that the data pointed to by
the source pointer is volatile and not the pointer itself?



volatile T * pt;

T volatile * pt;两者都意味着* pt不稳定


T * volatile pt;意味着pt是易失性的(但是* pt isn''t)

volatile T *pt;
T volatile *pt; both mean that *pt is volatile

T * volatile pt; means that pt is volatile (but *pt isn''t)


如何确保所有N个源字节都被视为易失性和

不只是第一个字节?
How do I ensure that all N source bytes are regarded as volatile and
not just the first byte?



''volatile''限定符适用于通过pt的/ all / accessses。那个

,如果你写volatile T * pt;然后* pt,pt [0],pt [1],*(pt + 42)

所有易变性访问。


-Arthur


Mark写道:
Mark wrote:

嗨列表,


我想写一个从硬件缓冲区复制一些数据的函数。

硬件可以改变这个缓冲区的内容,而不是我的函数写入的
。我想使用memcpy来卸载数据。

[...]
Hi List,

I want to write a function to copy some data out of a hardware buffer.
The hardware can change the contents of this buffer without it being
written to by my function. I want to use memcpy to unload the data.
[...]



你运气不好:memcpy()不接受指向

挥发性物品的指示。


不,这不是任意的限制,因为它可能

出现。考虑一下:memcpy()复制其数据,就好像通过一个接一个地复制

个别字节一样。如果当你的硬件决定打嗝新的一批数据时,如果memcpy()是
,那么会发生什么?你得到(充其量)目的地区域中旧的和

新字节的混合物;也许你得到更糟糕的东西(记忆界面综合症:迟到的知识事件)

并且有机会重启系统。


memcpy()是用来驱动这个螺丝的错误锤子。


-

Eric Sosman
es ***** @ acm-dot-org.inva 盖子


感谢所有帮助人员,不过我想问一下发生了什么

与我描述的情况略有不同...


假设我在地址BUFFER_BASE上有一个硬件缓冲区。这个缓冲区的内容

将保持稳定,除非我访问硬件以改变缓冲区页面,因此缓冲区的内容会发生变化。


如果我写...


char * ptr = BUFFER_BASE;

char a;

for(int i = 0; i< 10; i ++)

{

SetHardwarePage(i);

a = * ptr;

printf(" Page%d包含字符%c \ n",i,a);

}


我不知道期望这个工作,除非ptr被定义为volatile char *

ptr;


但是,如果我替换a = * ptr会发生什么?带函数调用,例如

memcpy?


char * ptr = BUFFER_BASE;

char a [N];

for(int i = 0; i< 10; i ++)

{

SetHardwarePage(i);

memcpy (ptr,a,N);

printf(Page%d包含位置0 \ n的字符%c,i,a [0]);

}


在这种情况下,在memcpy调用中,数据不易变为

不调用SetHardwarePage。但是,

memcpy调用之间的数据是不稳定的。它会起作用吗?如果编译器决定使用
内联memcpy会发生什么(这不太可能,但会帮助我理解

点)?


再次感谢,

Mark

Thanks for all your help guys, however I would like to ask what happens
in a slightly different situation to the one that I described...

Lets say I have a hardware buffer at address BUFFER_BASE. The contents
of this buffer will remain stable, unless I access the hardware to
change the buffer page, whereby the contents of the buffer will change.

if I write...

char* ptr = BUFFER_BASE;
char a;
for(int i=0; i<10; i++)
{
SetHardwarePage(i);
a = *ptr;
printf("Page %d contains character %c\n", i, a);
}

I do not expect this to work unless ptr is defined as volatile char*
ptr;

However, what happens if I replace a=*ptr; with a function call, e.g.
memcpy?

char* ptr = BUFFER_BASE;
char a[N];
for(int i=0; i<10; i++)
{
SetHardwarePage(i);
memcpy(ptr, a, N);
printf("Page %d contains character %c at position 0\n", i, a[0]);
}

In this case, within the memcpy call, the data is not volatile as
SetHardwarePage is not called. However the data is volatile between
memcpy calls. Will it work? What happens if the compiler decides to
inline memcpy (which is unlikely, but will help me understand the
point)?

Thanks again,

Mark


这篇关于memcpy和volatile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆