Contents of /trunk/mkinitrd-magellan/klibc/usr/klibc/tests/mmaptest.c
Parent Directory | Revision Log
Revision 532 -
(show annotations)
(download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 1461 byte(s)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 1461 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd
1 | /* |
2 | * mmaptest.c |
3 | * |
4 | * Test some simple cases of mmap() |
5 | */ |
6 | |
7 | #include <sys/mman.h> |
8 | #include <unistd.h> |
9 | #include <stdio.h> |
10 | #include <errno.h> |
11 | #include <stdint.h> |
12 | #include <fcntl.h> |
13 | |
14 | static void make_test_file(int fd) |
15 | { |
16 | unsigned long v; |
17 | FILE *f = fdopen(fd, "wb"); |
18 | |
19 | for (v = 0; v < 262144; v += sizeof(v)) |
20 | _fwrite(&v, sizeof(v), f); |
21 | } |
22 | |
23 | int main(int argc, char *argv[]) |
24 | { |
25 | void *foo; |
26 | char *test_file = (argc > 1) ? argv[1] : "/tmp/mmaptest.tmp"; |
27 | int rv, fd; |
28 | |
29 | /* Important case, this is how we get memory for malloc() */ |
30 | errno = 0; |
31 | foo = mmap(NULL, 65536, PROT_READ | PROT_WRITE, |
32 | MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); |
33 | |
34 | printf("mmap() returned %p, errno = %d\n", foo, errno); |
35 | if (foo == MAP_FAILED) |
36 | return 1; |
37 | |
38 | rv = munmap(foo, 65536); |
39 | printf("munmap() returned %d, errno = %d\n", rv, errno); |
40 | if (rv) |
41 | return 1; |
42 | |
43 | /* Create test file */ |
44 | fd = open(test_file, O_RDWR | O_CREAT | O_TRUNC, 0666); |
45 | if (fd < 0) { |
46 | perror(test_file); |
47 | return 1; |
48 | } |
49 | |
50 | make_test_file(fd); |
51 | |
52 | /* Map test file */ |
53 | foo = mmap(NULL, 65536, PROT_READ, MAP_SHARED, fd, 131072); |
54 | printf("mmap() returned %p, errno = %d\n", foo, errno); |
55 | if (foo == MAP_FAILED) |
56 | return 1; |
57 | |
58 | if (*(unsigned long *)foo != 131072) { |
59 | printf("mmap() with offset returned the wrong offset %ld!\n", |
60 | *(unsigned long *)foo); |
61 | return 1; |
62 | } |
63 | |
64 | if (munmap(foo, 65536)) { |
65 | printf("munmap() returned nonzero, errno = %d\n", errno); |
66 | return 1; |
67 | } |
68 | |
69 | close(fd); |
70 | unlink(test_file); |
71 | |
72 | return 0; |
73 | } |