Debian Codegraph @

tests/malloc.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 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/mman.h>
25
26 #include "hugetests.h"
27
28 /*
29 * We cannot test mapping size against huge page size because we are not linked
30 * against libhugetlbfs so gethugepagesize() won't work. So instead we define
31 * our MIN_PAGE_SIZE as 64 kB (the largest base page available) and make sure
32 * the mapping page size is larger than this.
33 */
34 #define MIN_PAGE_SIZE 65536
35
36 static int block_sizes[] = {
37 sizeof(int), 1024, 128*1024, 1024*1024, 16*1024*1024,
38 32*1024*1024,
39 };
40 #define NUM_SIZES (sizeof(block_sizes) / sizeof(block_sizes[0]))
41
42 int main(int argc, char *argv[])
43 {
44 int i;
45 char *env1, *env2, *exe;
46 int expect_hugepage = 0;
47 char *p;
48
49 test_init(argc, argv);
50 exe = strrchr(test_name, '/');
51 if (exe)
52 exe++; /* skip over "/" */
53 else
54 exe = test_name;
55
56 env1 = getenv("HUGETLB_MORECORE");
57 verbose_printf("HUGETLB_MORECORE=%s\n", env1);
58 env2 = getenv("HUGETLB_RESTRICT_EXE");
59 verbose_printf("HUGETLB_RESTRICT_EXE=%s\n", env2);
60 if (env1 && (!env2 || strstr(env2, exe)))
61 expect_hugepage = 1;
62 verbose_printf("expect_hugepage=%d\n", expect_hugepage);
63
64 for (i = 0; i < NUM_SIZES; i++) {
65 int size = block_sizes[i];
66 unsigned long long mapping_size;
67
68 p = malloc(size);
69 if (! p)
70 FAIL("malloc()");
71
72 verbose_printf("malloc(%d) = %p\n", size, p);
73
74 memset(p, 0, size);
75
76 mapping_size = get_mapping_page_size(p);
77
78 if (expect_hugepage && (mapping_size <= MIN_PAGE_SIZE))
79 FAIL("Address is not hugepage");
80 if (!expect_hugepage && (mapping_size > MIN_PAGE_SIZE))
81 FAIL("Address is unexpectedly huge");
82
83 free(p);
84 }
85
86 PASS();
87 }