Debian Codegraph @

tests/mlock.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #define _GNU_SOURCE
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <sys/mman.h>
28 #include <sys/resource.h>
29
30 #include <hugetlbfs.h>
31 #include "hugetests.h"
32
33 static void test_simple_mlock(int flags)
34 {
35 int fd = hugetlbfs_unlinked_fd();
36 void *p;
37 int ret;
38 long hpage_size = check_hugepagesize();
39
40 p = mmap(0, hpage_size, PROT_READ|PROT_WRITE, flags, fd, 0);
41 if (p == MAP_FAILED)
42 FAIL("mmap() failed (flags=%x): %s", flags, strerror(errno));
43
44 ret = mlock(p, hpage_size);
45 if (ret)
46 FAIL("mlock() failed (flags=%x): %s", flags, strerror(errno));
47
48 ret = munlock(p, hpage_size);
49 if (ret)
50 FAIL("munlock() failed (flags=%x): %s", flags, strerror(errno));
51
52 ret = munmap(p, hpage_size);
53 if (ret)
54 FAIL("munmap() failed (flags=%x): %s", flags, strerror(errno));
55
56 close(fd);
57 }
58
59 int main(int argc, char *argv[])
60 {
61 struct rlimit limit_info;
62 if(getrlimit(RLIMIT_MEMLOCK, &limit_info))
63 ERROR("Unable to read locked memory rlimit: %s", strerror(errno));
64 if(limit_info.rlim_cur < check_hugepagesize())
65 CONFIG("Locked memory ulimit set below huge page size");
66
67 test_simple_mlock(MAP_PRIVATE);
68 test_simple_mlock(MAP_SHARED);
69 test_simple_mlock(MAP_PRIVATE|MAP_LOCKED);
70 test_simple_mlock(MAP_SHARED|MAP_LOCKED);
71 PASS();
72 }