- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
考虑一个符合 POSIX.1-2008 标准的操作系统,让 fd 成为一个有效的文件描述符(对于打开的文件、读取模式、足够的数据...)。以下代码符合 C++11 标准*(忽略错误检查):
void* map = mmap(NULL, sizeof(int)*10, PROT_READ, MAP_PRIVATE, fd, 0);
int* foo = static_cast<int*>(map);
现在,下面的指令是否违反了严格的别名规则?
int bar = *foo;
根据标准:
If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:
- the dynamic type of the object,
- a cv-qualified version of the dynamic type of the object,
- a type similar (as defined in 4.4) to the dynamic type of the object,
- a type that is the signed or unsigned type corresponding to the dynamic type of the object,
- a type that is the signed or unsigned type corresponding to a cv-qualified version of the dynamic type of the object,
- an aggregate or union type that includes one of the aforementioned types among its elements or non-static data members (including, recursively, an element or non-static data member of a subaggregate or contained union),
- a type that is a (possibly cv-qualified) base class type of the dynamic type of the object,
- a char or unsigned char type.
map/foo 指向的对象的动态类型是什么?那甚至是一个对象吗?标准说:
The lifetime of an object of type T begins when: storage with the proper alignment and size for type T is obtained, and if the object has non-trivial initialization, its initialization is complete.
这是否意味着映射内存包含10个int对象(假设初始地址对齐)?但如果它是真的,这是否也适用于这段代码(这显然打破了严格的别名)?
char baz[sizeof(int)];
int* p=reinterpret_cast<int*>(&baz);
*p=5;
奇怪的是,这是否意味着声明 baz 会启动任何(正确对齐的)大小为 4 的对象的生命周期?
一些上下文:我正在映射一个文件,其中包含我希望直接访问的一大块数据。由于这个 block 很大,我想避免 memcpy-ing 到临时对象。
* nullptr 可以在这里代替 NULL 吗,它是否隐式转换为 NULL?是否引用了标准?
最佳答案
我相信简单的转换确实违反了严格的别名。令人信服地争论高于我的薪水等级,所以这是一种解决方法的尝试:
template<class T>
T* launder_raw_pod_at( void* ptr ) {
static_assert( std::is_pod<T>::value, "this only works with plain old data" );
char buff[sizeof(T)];
std::memcpy( buff, ptr, sizeof(T) );
T* r = ::new(ptr) T;
std::memcpy( ptr, buff, sizeof(T) );
return r;
}
我相信上面的代码对内存有零可观察到的副作用,并返回一个指向位置 ptr
的合法 T*
的指针。
检查您的编译器是否将上述代码优化为 noop。为此,它必须在真正基础的层面上理解 memcpy
,并且构造一个T
不必对那里的内存做任何事情。
At least clang 4.0.0 can optimize this operation away .
我们所做的是首先将字节复制。然后我们使用 placement new 来在那里创建一个 T
。最后,我们将字节复制回来。
我们有一个合法创建的 T
,其中包含我们想要的字节。
但是复制和返回都是到本地缓冲区,所以它没有可观察到的效果。
对象的构造,如果是一个pod,也不必接触字节;从技术上讲,字节是未定义的。但是聪明的编译器会说“什么都不做”。
因此编译器可以计算出所有这些操作都可以在运行时跳过。同时,我们在抽象机中正确地创建了一个在该位置具有正确字节的对象。 (假设它有有效的对齐方式!但这不是这段代码的问题。)
关于c++ - mmap 和 C++ 严格的别名规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45310822/
我想了解 mmap 的工作原理。mmap 的用户级调用如下所示。 void *mmap(void *addr, size_t len, int prot, int flags, int
我正在做一个Bottle驱动程序,我使用 yield 关键字和 mmap.mmap 对象在输出流中发送多个映射文件,如以下代码所示: for mapping in mappings: yield
我来自 C++/RAII 世界。 所以我对何时以及如何调用 mmap.close() 感到困惑[不是 file.close()]。 或者,根本不调用它?会不会漏气? 至于来自 document 的示例
我正在我的大学上操作系统类(class),我们的任务之一是使用 mmap 实现简单的 malloc。现在我开始工作了,我尝试使用 valgrind 来检测遗留的任何错误。不管是否释放内存,valgri
有谁知道 mmap(2) 和 mmap(3) 的区别是什么?手册第 3 节被描述为“本章描述了除第 2 章中描述的实现系统调用的库函数之外的所有库函数。” mmap(3) 不执行系统调用吗? 阅读这两
我不知道我在理解mmap时错过了哪些知识。我就是想不通。但让我这样问我的问题: 我有很多(例如 3 个)文件 block ,其大小分别为 s1、s2、s3。 s1、s2 和 s3 均小于 Mmap (
在 Linux 下: #free -m total used free shared buffers cachedMem:
我正在尝试将 C 库(beaglebone PRU 驱动程序 prussdrv.c)与 Python 连接。我想要访问的特定函数返回一个 mmap 指针,如下所示: int __prussdrv_me
当我调用mmap时: ptr = mmap(NULL, ...); 并要求系统提供一个缓冲区并将文件映射到其中,然后使用 再次调用 mmap ptr2 = mmap(ptr, ...); 尝试
在 Android 上用 Java 内存映射一个大文件效果很好。但是当映射总数超过 ~1.5GB 时,即使有多个映射调用,它也会失败: mmap failed: ENOMEM (Out of memo
我在具有 64G 内存和大量磁盘空间的 debian-64 上运行一个专门的数据库守护进程。它使用磁盘上的哈希表(mmaped)并通过定期 write() 调用将实际数据写入文件。当进行大量更新时,m
C++代码: #include #include #include #include #include using namespace std; #define FILE_MODE (S_I
我想处理一个由 4Kb block 组成的文件。 随着事情的发生,我将编写更多数据并映射新部分,取消映射我不再需要的部分。 当要映射的文件数据总量约为 4Gb 时,仅 4Kb 的 map() 是否太小
大家好,我正在尝试将下面的代码转换为 python(访问树莓派 1Mhz 计时器),我不知道什么时候要映射对象,我们需要 + TIMER_OFFSET (timer = (long long int
我所做的是一个垃圾收集器,使用mmap(2)为用户空间分配空间,这就要求最初分配时可以从任何地方开始,但是后面的分配地址应该是与之前的分配连续,如下所示: page_size = getpagesiz
众所周知,最重要的 mmap() 功能是在许多进程之间共享文件映射。但众所周知,每个进程都有自己的地址空间。 问题是内存映射文件(更具体地说,它的数据)真正保存在哪里,以及进程如何访问这些内存? 我的
什么限制了内存映射文件的大小?我知道它不能大于未分配地址空间的最大连续块,并且应该有足够的可用磁盘空间。但是还有其他限制吗? 最佳答案 您太保守了:内存映射文件可能大于地址空间。 查看 内存映射文件的
如果我使用 mmap 来编写 uint32_t,我会遇到大端/小端约定的问题吗?特别是,如果我在 big-endian 机器上写入一些数据 mmap,当我尝试在 little-endian 机器上读取
所以,对于我最后一年的项目,我使用 Video4Linux2 从相机中提取 YUV420 图像,将它们解析为 x264(本地使用这些图像),然后通过 Live555 将编码流发送到 RTP/RTCP通
是 mmap在它们的效果中调用原子? 也就是说,是否由 mmap 进行了映射更改以原子方式出现在访问受影响区域的其他线程中? 作为试金石,请考虑您执行 mmap 的情况。在一个全为零的文件中(来自线程
我是一名优秀的程序员,十分优秀!