-
Notifications
You must be signed in to change notification settings - Fork 2
/
mmf-mmap.c
58 lines (49 loc) · 1.08 KB
/
mmf-mmap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include "mmf.h"
#include "mmf-mmap.h"
#include "mmf-shm.h"
int mmap_init(test_t *test){
int fd = -1;
/* Open file, create and truncate it */
fd = open("mmap-datafile", O_RDWR | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR |
S_IRGRP | S_IWGRP |
S_IROTH | S_IWOTH);
if (fd == -1) {
perror("open");
return EXIT_FAILURE;
}
/* Set the file size */
if (ftruncate(fd, map_size) < 0) {
perror("ftruncate");
return EXIT_FAILURE;
}
test->fd = fd;
return 0;
}
int mmap_create(map_t *map, int order, test_t *test){
void *addr = (void*)((unsigned long long) 1<<order);
void *ptr;
ptr= mmap(addr, map_size, PROT_READ | PROT_WRITE,
test->flag, test->fd, 0);
printf ("order=%d,addr=%p,ptr=%p \n",order,addr,ptr);
if (ptr == MAP_FAILED){
perror ("mmap");
return -1;
}
map->addr = ptr;
return 0;
}
void mmap_destroy(map_t *map){
munmap(map->addr, map_size);
}
void mmap_cleanup(test_t *test){
if (test->fd != -1)
close(test->fd);
}