Skip to content

Instantly share code, notes, and snippets.

@piaoger
Last active July 8, 2020 18:34
Show Gist options
  • Save piaoger/4ba83f6a954bca113505 to your computer and use it in GitHub Desktop.
Save piaoger/4ba83f6a954bca113505 to your computer and use it in GitHub Desktop.
mmap on ios
// It's tested on ios 8.2 ..
// Apple document about virtual memory:
// Both OS X and iOS include a fully-integrated virtual memory system that you cannot turn off; it is always on.
// https://developer.apple.com/library/mac/documentation/Performance/Conceptual/ManagingMemory/Articles/AboutMemory.html
// Discussing mmap on ios:
// http://stackoverflow.com/questions/13425558/why-does-mmap-fail-on-ios
// http://stackoverflow.com/questions/9184773/is-there-a-practical-limit-on-the-number-of-memory-mapped-files-in-ios
#include <sys/mman.h>
void test_mmap_ios(NSURL* url) {
FILE *fd = fopen([[url path] UTF8String], "r");
fseek(fd, 0, SEEK_END);
int len = (int)ftell(fd);
fseek( fd, 0, SEEK_SET );
void *map = mmap(0, len, PROT_READ, MAP_SHARED, fileno( fd), 0);
if (map == MAP_FAILED) {
fclose(fd);
printf( "MAP_FAILED. errno=%d", errno );
return;
} else {
printf( "MAP SUCCEEDED.");
}
munmap(map, len);
fclose(fd);
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment