Linux Audio

Check our new training course

Loading...
v3.15
 
 1/*
 2 * Example of using hugepage memory in a user application using the mmap
 3 * system call with MAP_HUGETLB flag.  Before running this program make
 4 * sure the administrator has allocated enough default sized huge pages
 5 * to cover the 256 MB allocation.
 6 *
 7 * For ia64 architecture, Linux kernel reserves Region number 4 for hugepages.
 8 * That means the addresses starting with 0x800000... will need to be
 9 * specified.  Specifying a fixed address is not required on ppc64, i386
10 * or x86_64.
11 */
12#include <stdlib.h>
13#include <stdio.h>
14#include <unistd.h>
15#include <sys/mman.h>
16#include <fcntl.h>
17
18#define LENGTH (256UL*1024*1024)
19#define PROTECTION (PROT_READ | PROT_WRITE)
20
21#ifndef MAP_HUGETLB
22#define MAP_HUGETLB 0x40000 /* arch specific */
23#endif
24
25/* Only ia64 requires this */
26#ifdef __ia64__
27#define ADDR (void *)(0x8000000000000000UL)
28#define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_FIXED)
29#else
30#define ADDR (void *)(0x0UL)
31#define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB)
32#endif
33
34static void check_bytes(char *addr)
35{
36	printf("First hex is %x\n", *((unsigned int *)addr));
37}
38
39static void write_bytes(char *addr)
40{
41	unsigned long i;
42
43	for (i = 0; i < LENGTH; i++)
44		*(addr + i) = (char)i;
45}
46
47static int read_bytes(char *addr)
48{
49	unsigned long i;
50
51	check_bytes(addr);
52	for (i = 0; i < LENGTH; i++)
53		if (*(addr + i) != (char)i) {
54			printf("Mismatch at %lu\n", i);
55			return 1;
56		}
57	return 0;
58}
59
60int main(void)
61{
62	void *addr;
63	int ret;
64
65	addr = mmap(ADDR, LENGTH, PROTECTION, FLAGS, 0, 0);
66	if (addr == MAP_FAILED) {
67		perror("mmap");
68		exit(1);
69	}
70
71	printf("Returned address is %p\n", addr);
72	check_bytes(addr);
73	write_bytes(addr);
74	ret = read_bytes(addr);
75
76	munmap(addr, LENGTH);
 
 
 
 
77
78	return ret;
79}
v4.17
 1// SPDX-License-Identifier: GPL-2.0
 2/*
 3 * Example of using hugepage memory in a user application using the mmap
 4 * system call with MAP_HUGETLB flag.  Before running this program make
 5 * sure the administrator has allocated enough default sized huge pages
 6 * to cover the 256 MB allocation.
 7 *
 8 * For ia64 architecture, Linux kernel reserves Region number 4 for hugepages.
 9 * That means the addresses starting with 0x800000... will need to be
10 * specified.  Specifying a fixed address is not required on ppc64, i386
11 * or x86_64.
12 */
13#include <stdlib.h>
14#include <stdio.h>
15#include <unistd.h>
16#include <sys/mman.h>
17#include <fcntl.h>
18
19#define LENGTH (256UL*1024*1024)
20#define PROTECTION (PROT_READ | PROT_WRITE)
21
22#ifndef MAP_HUGETLB
23#define MAP_HUGETLB 0x40000 /* arch specific */
24#endif
25
26/* Only ia64 requires this */
27#ifdef __ia64__
28#define ADDR (void *)(0x8000000000000000UL)
29#define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_FIXED)
30#else
31#define ADDR (void *)(0x0UL)
32#define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB)
33#endif
34
35static void check_bytes(char *addr)
36{
37	printf("First hex is %x\n", *((unsigned int *)addr));
38}
39
40static void write_bytes(char *addr)
41{
42	unsigned long i;
43
44	for (i = 0; i < LENGTH; i++)
45		*(addr + i) = (char)i;
46}
47
48static int read_bytes(char *addr)
49{
50	unsigned long i;
51
52	check_bytes(addr);
53	for (i = 0; i < LENGTH; i++)
54		if (*(addr + i) != (char)i) {
55			printf("Mismatch at %lu\n", i);
56			return 1;
57		}
58	return 0;
59}
60
61int main(void)
62{
63	void *addr;
64	int ret;
65
66	addr = mmap(ADDR, LENGTH, PROTECTION, FLAGS, -1, 0);
67	if (addr == MAP_FAILED) {
68		perror("mmap");
69		exit(1);
70	}
71
72	printf("Returned address is %p\n", addr);
73	check_bytes(addr);
74	write_bytes(addr);
75	ret = read_bytes(addr);
76
77	/* munmap() length of MAP_HUGETLB memory must be hugepage aligned */
78	if (munmap(addr, LENGTH)) {
79		perror("munmap");
80		exit(1);
81	}
82
83	return ret;
84}