在linux上的luajit中,所有vm magaged ram必须低于2gb进程内存边界,因为内部指针总是32位的。因此,我想自己管理更大的alloc(使用ffi和malloc
等),例如用于大纹理、音频、缓冲区等。
我现在想确保它们被映射到2GB Boudary之上,因为这样它们就不会带走任何虚拟机可管理的内存。
是否有任何方法可以malloc
或mmap
(不带映射文件,或在shm中)在该地址上方分配指针?不需要占用2GIG,只需将指针映射到更高的(=非32位)地址
最佳答案
分配2 GB块。如果它位于限制之下,请再分配2 GB(此块必须大于2 GB,因为只有一个大小可以小于2 GB的块)。
/* Allocates size bytes at an address higher than address */
void *HighMalloc(size_t size, void *address) {
size_t mysize = (size_t)address;
void *y, *x;
if (mysize < size) {
mysize = size;
}
y = x = malloc(mysize);
if (x < address) {
/* The memory starts at a low address.
* mysize is so big that another block this size cannot fit
* at a low address. So let's allocate another block and
* then free the block that is using low memory. */
x = malloc(mysize);
free(y);
}
return x;
}
注:
如果
size
小于address
,则第二个malloc的低地址可能有足够的空间。这就是为什么我会在这些情况下增加分配的大小。所以不要用这个来分配小内存块。分配一大块,然后手动将其分成小块。关于c - Linux上的Malloc特定地址或页面(指定“最小偏移量”),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32601363/