-
Notifications
You must be signed in to change notification settings - Fork 1
/
memory_utils.c
94 lines (76 loc) · 2.26 KB
/
memory_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <sys/mman.h>
#include "memory_utils.h"
int mem_map_private(uint64_t** addr, int len, int withhuge) {
if (withhuge)
*addr = (uint64_t*) mmap (NULL, len, PROT_READ|PROT_WRITE, PFLAGS_HG, 0, 0);
else
*addr = (uint64_t*) mmap (NULL, len, PROT_READ|PROT_WRITE, PFLAGS_4K, 0, 0);
if (*addr == MAP_FAILED) {
if (errno == ENOMEM) {
if (len > 2 * MB)
printf("Could not allocate buffer (no free 1 GiB huge pages)\n");
if (len > 4 * KB)
printf("Could not allocate buffer (no free 2 MiB huge pages)\n");
else
printf("Could not allocate buffer (out of memory)\n");
}
printf("mmap failed: %s\n", strerror(errno));
return -1;
}
return 0;
}
int mem_map_shared(uint64_t** addr, int len, int withhuge) {
// map shared memory to process address space
if (withhuge)
*addr = (uint64_t*) mmap (NULL, len, SPROTECTION, SFLAGS_HG, 0, 0);
else
*addr = (uint64_t*) mmap (NULL, len, SPROTECTION, SFLAGS_4K, 0, 0);
if (*addr == MAP_FAILED) {
if (errno == ENOMEM) {
if (len > 2 * MB)
printf("Could not allocate buffer (no free 1 GiB huge pages)\n");
if (len > 4 * KB)
printf("Could not allocate buffer (no free 2 MiB huge pages)\n");
else
printf("Could not allocate buffer (out of memory)\n");
}
printf("mmap failed: %s\n", strerror(errno));
return -1;
}
return 0;
}
int mem_unmap(uint64_t *addr, int len) {
if (len > 2 * MB)
len = (len + (1 * GB - 1)) & (~(1 * GB - 1));
else if (len > 4 * KB)
len = 2 * MB;
// un-map
if (munmap((void*)addr, len) == -1) {
printf("munmap failed\n");
return -1;
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
int var_map_shared(volatile uint64_t** addr) {
int len = sizeof *addr;
*addr = (volatile uint64_t*)mmap(NULL, len, SPROTECTION, SFLAGS_4K, 0, 0);
if (*addr == MAP_FAILED) {
printf("mmap failed: %s\n", strerror(errno));
return -1;
}
return 0;
}
int var_unmap(volatile uint64_t *addr) {
int len = sizeof *addr;
// un-map
if (munmap((void*)addr, len) == -1) {
printf("munmap failed\n");
return -1;
}
return 0;
}