large-mmap.c 1.1 KB

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