-
Notifications
You must be signed in to change notification settings - Fork 0
/
memlib.c
151 lines (126 loc) · 2.92 KB
/
memlib.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* memlib.c - bridge to mmap
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>
#include <inttypes.h>
#include <errno.h>
#include "memlib.h"
#include "pagemap.h"
/* private variables */
static int activity_counter = 0; /* to simulate other processes */
static int page_count;
/*
* mem_init - initialize the memory system model
*/
void mem_init(void)
{
size_t page_size = (size_t)getpagesize();
if (APAGE_SIZE != page_size) {
fprintf(stderr, "configuration error: APAGE_SIZE does not match %ld\n",
page_size);
abort();
}
}
static void unmap(void *p)
{
if (munmap(p, APAGE_SIZE) < 0) {
fprintf(stderr, "unexpected error in munmap: %s (%d)\n",
strerror(errno), errno);
abort();
}
}
/*
* mem_deinit - free the storage used by the memory system model
*/
void mem_reset(void)
{
pagemap_for_each(unmap, 1);
page_count = 0;
activity_counter = 0;
}
/*
* mem_pagesize() - returns the page size of the system
*/
size_t mem_pagesize()
{
return APAGE_SIZE;
}
size_t mem_heapsize(void)
{
return APAGE_SIZE * page_count;
}
void *mem_map(size_t sz)
{
void *p;
size_t i;
if (sz & (APAGE_SIZE - 1)) {
fprintf(stderr, "mem_map: requested size is not a multiple of %d: %ld\n",
APAGE_SIZE, sz);
abort();
}
activity_counter++;
if ((activity_counter & (activity_counter - 1)) == 0) {
/* allocate a page to ensure that mem_map results are not
always sequential */
mmap(0, APAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
}
p = mmap(0, sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
if (p == MAP_FAILED) {
fprintf(stderr, "mmap failed: %s (%d)\n",
strerror(errno), errno);
abort();
}
for (i = 0; i < sz; i += APAGE_SIZE) {
pagemap_modify(p + i, 1);
page_count++;
}
return p;
}
int check_mapped(void *p, size_t sz, int fail_with_error)
{
size_t i;
if (((uintptr_t)p) & (APAGE_SIZE - 1)) {
fprintf(stderr, "mem_unmap: given address is not page-aligned: %p\n",
p);
abort();
}
if (sz & (APAGE_SIZE - 1)) {
fprintf(stderr, "mem_unmap: given size is not a multiple of %d: %ld\n",
APAGE_SIZE, sz);
abort();
}
for (i = 0; i < sz; i += APAGE_SIZE) {
if (!pagemap_is_mapped(p+i)) {
if (fail_with_error) {
fprintf(stderr, "mem_unmap: given page is not mapped: %p (in %p:%p)\n",
p + i, p, p + sz);
abort();
}
return 0;
}
}
return 1;
}
void mem_unmap(void *p, size_t sz)
{
size_t i;
(void)check_mapped(p, sz, 1);
for (i = 0; i < sz; i += APAGE_SIZE) {
pagemap_modify(p + i, 0);
--page_count;
}
if (munmap(p, sz) < 0) {
fprintf(stderr, "munmap failed: %s (%d)\n",
strerror(errno), errno);
abort();
}
}
int mem_is_mapped(void *p, size_t sz)
{
return check_mapped(p, sz, 0);
}