mmap罢!因为64位地址极大丰富
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/mman.h> #include <asm-generic/mman-common.h> int a; int main () { int b; char* c = new char[1]; char* d = (char*)malloc(1); printf("%p %p %p %p\n", &a, c, d, &b); unsigned long addr = 0x7efffff1e000; size_t n = 0x40000000; char* m = (char*)mmap((void*)addr, n, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_UNINITIALIZED, -1, 0); printf("%p\n", m); // This flag, MAP_UNINITIALIZED, is honored only if the kernel was configured with the // CONFIG_MMAP_ALLOW_UNINITIALIZED option. Because of the security implications, that // option is normally enabled only on embedded devices (i.e., devices where one has complete // control of the contents of user memory). for (int i = 0; i < n; i++) { if (m[i]) { printf("i=%d\n", i); break; } } memset(m, 0x5a, n); puts("ok"); }
$ gcc mmap.cpp; a.out 0x55d602f26044 0x55d63cc79eb0 0x55d63cc79ed0 0x7fff841e85cc 0x7efffff1e000 ok $ clang++ mmap.cpp; a.out 0x557518029044 0x55751b620eb0 0x55751b620ed0 0x7ffcdf084828 0x7efffff1e000 ok
(0x7ffdfdf100ac - 0x7efffff1e000) ≈ 1T
比方说有个set<UserDefinedStruct>,如何save/load,或者说序列化/反序列化?load时如何避免再建一遍树?
以前我用自定义STL容器的allocator和placement new做过个程序,mmap到固定地址。但32位地址空间太小了,不实用——说得好像不实用的原因是地址空间小似的:-)
如何知道0x7efffff1e000这样的地址?一开始我用0x1{11个0},mmap返回NULL。然后用0,mmap返回个和0x7efffff1e000很接近的地址。
12*4才48位。1 << 48 = 2 ** 48 = 65536G = 64T
1 << 1 = 2 ** 1;上面的算法没错。
2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536应该背住。
2 ** 20 = 1048576也常用。1E6,1E8, 1E9也方便。
头条上有人问为啥没有128位电脑。首先问之前赖好动下脖子上的那玩意:-),其次就数据宽度而言,avx2算512位的吧。

浙公网安备 33010602011771号