large-mmap.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include <stdio.h>
  2. #include <sys/mman.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #define TEST_LENGTH 0x10000f000
  6. #define TEST_LENGTH2 0x8000f000
  7. int main() {
  8. FILE* fp = fopen("testfile", "a+");
  9. if (!fp) {
  10. perror("fopen");
  11. return 1;
  12. }
  13. int rv = ftruncate(fileno(fp), TEST_LENGTH);
  14. if (rv) {
  15. perror("ftruncate");
  16. return 1;
  17. } else {
  18. printf("large-mmap: ftruncate OK\n");
  19. }
  20. void* a = mmap(NULL, TEST_LENGTH2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fileno(fp), 0);
  21. if (a == MAP_FAILED) {
  22. perror("mmap 1");
  23. return 1;
  24. }
  25. ((char*)a)[0x80000000] = 0xff;
  26. printf("large-mmap: mmap 1 completed OK\n");
  27. rv = munmap(a, TEST_LENGTH2);
  28. if (rv) {
  29. perror("mumap");
  30. return 1;
  31. }
  32. a = mmap(NULL, TEST_LENGTH, PROT_READ | PROT_WRITE, MAP_PRIVATE, fileno(fp), 0);
  33. if (a == MAP_FAILED) {
  34. perror("mmap 2");
  35. return 1;
  36. }
  37. ((char*)a)[0x100000000] = 0xff;
  38. printf("large-mmap: mmap 2 completed OK\n");
  39. return 0;
  40. }