This source file includes following definitions.
- check_bytes
- write_bytes
- read_bytes
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <sys/mman.h>
24 #include <fcntl.h>
25
26 #define FILE_NAME "huge/hugepagefile"
27 #define LENGTH (256UL*1024*1024)
28 #define PROTECTION (PROT_READ | PROT_WRITE)
29
30
31 #ifdef __ia64__
32 #define ADDR (void *)(0x8000000000000000UL)
33 #define FLAGS (MAP_SHARED | MAP_FIXED)
34 #else
35 #define ADDR (void *)(0x0UL)
36 #define FLAGS (MAP_SHARED)
37 #endif
38
39 static void check_bytes(char *addr)
40 {
41 printf("First hex is %x\n", *((unsigned int *)addr));
42 }
43
44 static void write_bytes(char *addr)
45 {
46 unsigned long i;
47
48 for (i = 0; i < LENGTH; i++)
49 *(addr + i) = (char)i;
50 }
51
52 static int read_bytes(char *addr)
53 {
54 unsigned long i;
55
56 check_bytes(addr);
57 for (i = 0; i < LENGTH; i++)
58 if (*(addr + i) != (char)i) {
59 printf("Mismatch at %lu\n", i);
60 return 1;
61 }
62 return 0;
63 }
64
65 int main(void)
66 {
67 void *addr;
68 int fd, ret;
69
70 fd = open(FILE_NAME, O_CREAT | O_RDWR, 0755);
71 if (fd < 0) {
72 perror("Open failed");
73 exit(1);
74 }
75
76 addr = mmap(ADDR, LENGTH, PROTECTION, FLAGS, fd, 0);
77 if (addr == MAP_FAILED) {
78 perror("mmap");
79 unlink(FILE_NAME);
80 exit(1);
81 }
82
83 printf("Returned address is %p\n", addr);
84 check_bytes(addr);
85 write_bytes(addr);
86 ret = read_bytes(addr);
87
88 munmap(addr, LENGTH);
89 close(fd);
90 unlink(FILE_NAME);
91
92 return ret;
93 }