forked from teawater/libhermit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallocator.c
More file actions
74 lines (67 loc) · 1.38 KB
/
allocator.c
File metadata and controls
74 lines (67 loc) · 1.38 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <hermit/syscall.h>
static
uint64_t memparse(const char *ptr)
{
// local pointer to end of parsed string
char *endptr;
// parse number
uint64_t size = strtoull(ptr, &endptr, 0);
// parse size extension, intentional fall-through
switch (*endptr) {
case 'E':
case 'e':
size <<= 10;
case 'P':
case 'p':
size <<= 10;
case 'T':
case 't':
size <<= 10;
case 'G':
case 'g':
size <<= 10;
case 'M':
case 'm':
size <<= 10;
case 'K':
case 'k':
size <<= 10;
endptr++;
default:
break;
}
return size;
}
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "ERROR: You have to specify the size of the memory region to be allocated!\n");
exit(-1);
}
uint64_t alloc_size = memparse(argv[1]);
printf("Allocating %llu bytes ...\n", alloc_size);
char *mem_reg = (char*)malloc(alloc_size);
if (mem_reg == NULL) {
fprintf(stderr, "ERROR: Could not allocate %llu bytes of memory!\n", alloc_size);
exit(-1);
}
memset(mem_reg, 0x42, alloc_size);
printf("Allocated and mapped!\n");
uint32_t cnt = 0;
while(1) {
uint64_t i;
for (i=0; i<alloc_size; ++i) {
if (mem_reg[i] != 0x42) {
fprintf(stderr, "ERROR: mem_reg[%llu] = 0x%x != 0x42!\n", i, mem_reg[i]);
}
}
printf("Sleeping ...");
fflush(stdout);
sys_msleep(500);
printf("%d!\n", cnt++);
}
return 0;
}