138b870ccSIlya Leoshkevich #include <assert.h>
238b870ccSIlya Leoshkevich #include <stdlib.h>
338b870ccSIlya Leoshkevich #include <sys/mman.h>
438b870ccSIlya Leoshkevich #include <unistd.h>
538b870ccSIlya Leoshkevich
test_anonymous(void)638b870ccSIlya Leoshkevich static void test_anonymous(void)
738b870ccSIlya Leoshkevich {
838b870ccSIlya Leoshkevich int pagesize = getpagesize();
938b870ccSIlya Leoshkevich char *page;
1038b870ccSIlya Leoshkevich int ret;
1138b870ccSIlya Leoshkevich
1238b870ccSIlya Leoshkevich page = mmap(NULL, pagesize, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
1338b870ccSIlya Leoshkevich assert(page != MAP_FAILED);
1438b870ccSIlya Leoshkevich
1538b870ccSIlya Leoshkevich /* Check that mprotect() does not interfere with MADV_DONTNEED. */
1638b870ccSIlya Leoshkevich ret = mprotect(page, pagesize, PROT_READ | PROT_WRITE);
1738b870ccSIlya Leoshkevich assert(ret == 0);
1838b870ccSIlya Leoshkevich
1938b870ccSIlya Leoshkevich /* Check that MADV_DONTNEED clears the page. */
2038b870ccSIlya Leoshkevich *page = 42;
2138b870ccSIlya Leoshkevich ret = madvise(page, pagesize, MADV_DONTNEED);
2238b870ccSIlya Leoshkevich assert(ret == 0);
2338b870ccSIlya Leoshkevich assert(*page == 0);
2438b870ccSIlya Leoshkevich
2538b870ccSIlya Leoshkevich ret = munmap(page, pagesize);
2638b870ccSIlya Leoshkevich assert(ret == 0);
2738b870ccSIlya Leoshkevich }
2838b870ccSIlya Leoshkevich
test_file(void)2938b870ccSIlya Leoshkevich static void test_file(void)
3038b870ccSIlya Leoshkevich {
3138b870ccSIlya Leoshkevich char tempname[] = "/tmp/.cmadviseXXXXXX";
3238b870ccSIlya Leoshkevich int pagesize = getpagesize();
3338b870ccSIlya Leoshkevich ssize_t written;
3438b870ccSIlya Leoshkevich char c = 42;
3538b870ccSIlya Leoshkevich char *page;
3638b870ccSIlya Leoshkevich int ret;
3738b870ccSIlya Leoshkevich int fd;
3838b870ccSIlya Leoshkevich
3938b870ccSIlya Leoshkevich fd = mkstemp(tempname);
4038b870ccSIlya Leoshkevich assert(fd != -1);
4138b870ccSIlya Leoshkevich ret = unlink(tempname);
4238b870ccSIlya Leoshkevich assert(ret == 0);
4338b870ccSIlya Leoshkevich written = write(fd, &c, sizeof(c));
4438b870ccSIlya Leoshkevich assert(written == sizeof(c));
45*6ada8619SRichard Henderson ret = ftruncate(fd, pagesize);
46*6ada8619SRichard Henderson assert(ret == 0);
4738b870ccSIlya Leoshkevich page = mmap(NULL, pagesize, PROT_READ, MAP_PRIVATE, fd, 0);
4838b870ccSIlya Leoshkevich assert(page != MAP_FAILED);
4938b870ccSIlya Leoshkevich
5038b870ccSIlya Leoshkevich /* Check that mprotect() does not interfere with MADV_DONTNEED. */
5138b870ccSIlya Leoshkevich ret = mprotect(page, pagesize, PROT_READ | PROT_WRITE);
5238b870ccSIlya Leoshkevich assert(ret == 0);
5338b870ccSIlya Leoshkevich
5438b870ccSIlya Leoshkevich /* Check that MADV_DONTNEED resets the page. */
5538b870ccSIlya Leoshkevich *page = 0;
5638b870ccSIlya Leoshkevich ret = madvise(page, pagesize, MADV_DONTNEED);
5738b870ccSIlya Leoshkevich assert(ret == 0);
5838b870ccSIlya Leoshkevich assert(*page == c);
5938b870ccSIlya Leoshkevich
6038b870ccSIlya Leoshkevich ret = munmap(page, pagesize);
6138b870ccSIlya Leoshkevich assert(ret == 0);
6238b870ccSIlya Leoshkevich ret = close(fd);
6338b870ccSIlya Leoshkevich assert(ret == 0);
6438b870ccSIlya Leoshkevich }
6538b870ccSIlya Leoshkevich
main(void)6638b870ccSIlya Leoshkevich int main(void)
6738b870ccSIlya Leoshkevich {
6838b870ccSIlya Leoshkevich test_anonymous();
6938b870ccSIlya Leoshkevich test_file();
7038b870ccSIlya Leoshkevich
7138b870ccSIlya Leoshkevich return EXIT_SUCCESS;
7238b870ccSIlya Leoshkevich }
73