Debian Codegraph @

tests/get_huge_pages.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 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <sys/mman.h>
23
24 #include <hugetlbfs.h>
25
26 #include "hugetests.h"
27
28 long hpage_size;
29 long oc_hugepages = -1;
30
31 /* Restore nr_overcommit_hugepages */
32 void cleanup(void)
33 {
34 if (oc_hugepages != -1)
35 set_nr_overcommit_hugepages(hpage_size, oc_hugepages);
36 }
37
38 /* Confirm a region really frees, only really important for GHP_FALLBACK */
39 void free_and_confirm_region_free(void *p, int line) {
40 unsigned char vec = 0;
41 free_huge_pages(p);
42 if (mincore(p, 4, &vec) == 0 || vec)
43 FAIL("free_huge_pages did not free region at line %d", line);
44 }
45
46 void test_get_huge_pages(int num_hugepages)
47 {
48 unsigned long long mapping_size;
49 void *p = get_huge_pages(num_hugepages * hpage_size, GHP_DEFAULT);
50 if (p == NULL)
51 FAIL("get_huge_pages() for %d hugepages", num_hugepages);
52
53 memset(p, 1, hpage_size);
54
55 mapping_size = get_mapping_page_size(
56 (void *)p + (num_hugepages -1) * hpage_size);
57 if (mapping_size != hpage_size)
58 FAIL("Returned page is not hugepage");
59
60 free_and_confirm_region_free(p, __LINE__);
61 mapping_size = get_mapping_page_size(
62 (void *)p + (num_hugepages -1) * hpage_size);
63 if (mapping_size)
64 FAIL("hugepage was not correctly freed");
65 }
66
67 int main(int argc, char *argv[])
68 {
69 test_init(argc, argv);
70 hpage_size = gethugepagesize();
71 check_free_huge_pages(4);
72 test_get_huge_pages(1);
73 test_get_huge_pages(4);
74
75 PASS();
76 }