| 1 |
#include <stdio.h> |
| 2 |
#include <assert.h> |
| 3 |
#include <sys/mman.h> |
| 4 |
#include <sys/types.h> |
| 5 |
#include <sys/stat.h> |
| 6 |
#include <fcntl.h> |
| 7 |
|
| 8 |
void generate_test_file(int count) |
| 9 |
{ |
| 10 |
FILE *f; |
| 11 |
int i; |
| 12 |
|
| 13 |
f = fopen("testfile", "w+"); |
| 14 |
for (i=0;i<count;i++) { |
| 15 |
fwrite(&i, 1, sizeof(i), f); |
| 16 |
} |
| 17 |
fclose(f); |
| 18 |
} |
| 19 |
|
| 20 |
void test_mmap_directly() |
| 21 |
{ |
| 22 |
int fd; |
| 23 |
void *area_short, *area_long; |
| 24 |
|
| 25 |
generate_test_file(1024); |
| 26 |
fd = open("testfile", O_RDONLY); |
| 27 |
assert(fd>=0); |
| 28 |
|
| 29 |
area_short = mmap(0, 4096, PROT_READ, MAP_PRIVATE, fd, 0); |
| 30 |
assert(area_short != MAP_FAILED); |
| 31 |
|
| 32 |
close(fd); |
| 33 |
generate_test_file(1024*1024); |
| 34 |
fd = open("testfile", O_RDONLY); |
| 35 |
assert(fd>=0); |
| 36 |
|
| 37 |
area_long = mmap(area_short, 4096*1024, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0); |
| 38 |
assert(area_long != MAP_FAILED); |
| 39 |
assert(area_short == area_long); |
| 40 |
} |
| 41 |
|
| 42 |
int main(int argc, char ** argv) |
| 43 |
{ |
| 44 |
test_mmap_directly(); |
| 45 |
|
| 46 |
} |