-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmadv_dontneed.c
More file actions
41 lines (36 loc) · 1.01 KB
/
madv_dontneed.c
File metadata and controls
41 lines (36 loc) · 1.01 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
/*
* Verifying the behavior or MADV_DONTNEED on private memory mappings.
* Create a private anonymous memory mapping, mark it with MADV_DONTNEED
* Then check it's value. Linux should have special behavior where it's value
* reinitialized to 0/the file's value.
*/
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <stdio.h>
#include <string.h>
static void exit_err(char *reason) {
perror(reason);
exit(EXIT_FAILURE);
}
static void pbuf(char *buf, int len) {
puts("Contents of buf: ");
write(STDOUT_FILENO, buf, len);
puts("");
}
int main(int argc, char **argv) {
char *buf;
int psz;
setbuf(stdout, NULL);
psz = sysconf(_SC_PAGESIZE);
if ((buf = mmap(NULL, 2 * psz, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)) == NULL)
exit_err("mmmap");
memset(buf, 'a', psz);
memset(buf + psz, 'b', psz);
pbuf(buf, 2 * psz);
puts("madvise MADV_DONTNEED on second half of buf");
if (madvise(buf + psz, psz, MADV_DONTNEED) == -1)
exit_err("madvise");
pbuf(buf, 2 * psz);
exit(EXIT_SUCCESS);
}