如何将文件映射到 OSX 中的虚拟内存管理器?

use*_*597 5 macos file objective-c virtual-machine

我正在尝试将文件映射到 OS X 的虚拟内存管理器。如何使用 Objective C 在 Mac OS X 上执行此操作?

Gra*_*rks 2

使用映射。例如

FILE* f = fopen(...);

// Map the file into memory.

// Need the file size.
fseek(f, 0, SEEK_END); // seek to end of file
off_t fileSize = ftello(f); // get current file pointer
fseek(f, 0, SEEK_SET); // seek back to beginning of file
mappedSize = fileSize;

mappedAddress = mmap(0, _mappedSize, PROT_READ, MAP_PRIVATE, f->_file, 0);

... use mappedAddress as a pointer to your data

// Finally free up
munmap(_mappedAddress, _mappedSize);
fclose(f);
Run Code Online (Sandbox Code Playgroud)