-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaligned_memory.c
More file actions
78 lines (48 loc) · 1.6 KB
/
aligned_memory.c
File metadata and controls
78 lines (48 loc) · 1.6 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/fcntl.h>
#include <sys/mman.h>
static char * progname;
#define PAGE_SIZE (4096)
void usage(void) {
printf("Usage: %s [filename]\n", progname);
return;
}
int main(int argc, char * argv[]) {
const char * filename;
int fd, ret;
char *buffer;
progname = argv[0];
if (argc != 2) {
usage();
exit(0);
}
filename = argv[1];
ret = posix_memalign(&buffer, 512, PAGE_SIZE);
if(ret) {
printf("%s: %s", progname, strerror(ret));
exit(-5);
}
printf("%s: Got aligned buffer %p\n", progname, buffer);
fd = open(filename, O_RDWR|O_CREAT|O_DIRECT, S_IRWXU);
if(-1 == fd) {
perror(progname);
exit(-1);
}
strcpy(buffer, "testing testing 1 2 3!");
ret = write(fd, buffer, PAGE_SIZE);
if(-1 == ret) {
perror(progname);
exit(-2);
}
printf("%s: Written: %s\n", progname, buffer);
lseek(fd, SEEK_SET, 0);
ret = read(fd, buffer, PAGE_SIZE);
if(-1 == ret) {
perror(progname);
exit(-2);
}
printf("%s: Got %s\n", progname, buffer);
return 0;
}