-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemlib.c
More file actions
163 lines (141 loc) · 4.52 KB
/
Copy pathmemlib.c
File metadata and controls
163 lines (141 loc) · 4.52 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
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
152
153
154
155
156
157
158
159
160
161
162
163
/*
* memlib.c - a module that simulates the memory system. Needed
* because it allows us to interleave calls from the student's malloc
* package with the system's malloc package in libc.
*
* This version has been updated to enable sparse emulation of very large heaps
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include "memlib.h"
#include "config.h"
/* private global variables */
static unsigned char *heap; /* Starting address of heap */
static unsigned char *mem_brk; /* Current position of break */
static unsigned char *mem_max_addr; /* Maximum allowable heap address */
static size_t mmap_length = MAX_DENSE_HEAP; /* Number of bytes allocated by mmap */
static bool show_stats = false; /* Should program print allocation information? */
static bool stats_printed = false; /* Has information been printed about allocation */
static void print_stats();
/*
* mem_init - initialize the memory system model
*/
void mem_init(){
/* Dense allocation */
mmap_length = MAX_DENSE_HEAP;
int dev_zero = open("/dev/zero", O_RDWR);
void *start = TRY_DENSE_HEAP_START;
void *addr = mmap(start, /* suggested start*/
mmap_length, /* length */
PROT_WRITE, /* permissions */
MAP_PRIVATE, /* private or shared? */
dev_zero, /* fd */
0); /* offset */
if (addr == MAP_FAILED) {
fprintf(stderr, "FAILURE. mmap couldn't allocate space for heap\n");
exit(1);
}
heap = addr;
mem_max_addr = heap + MAX_DENSE_HEAP;
stats_printed = false;
mem_brk = heap;
mem_reset_brk();
}
/*
* mem_deinit - free the storage used by the memory system model
*/
void mem_deinit(void){
print_stats();
munmap(heap, mmap_length);
}
/*
* mem_reset_brk - reset the simulated brk pointer to make an empty heap
*/
void mem_reset_brk(){
print_stats();
mem_brk = heap;
}
/*
* mem_sbrk - simple model of the sbrk function. Extends the heap
* by incr bytes and returns the start address of the new area. In
* this model, the heap cannot be shrunk.
*/
void *mem_sbrk(intptr_t incr) {
unsigned char *old_brk = mem_brk;
bool ok = true;
if (incr < 0) {
ok = false;
fprintf(stderr, "ERROR: mem_sbrk failed. Attempt to expand heap by negative value %ld\n", (long) incr);
} else if (mem_brk + incr > mem_max_addr) {
ok = false;
size_t alloc = mem_brk - heap + incr;
fprintf(stderr, "ERROR: mem_sbrk failed. Ran out of memory. Would require heap size of %zd (0x%zx) bytes\n", alloc, alloc);
} else if (sbrk(incr) == (void*) -1) {
ok = false;
fprintf(stderr, "ERROR: mem_sbrk failed. Could not allocate more heap space\n");
}
if (ok) {
mem_brk += incr;
return (void *) old_brk;
} else {
errno = ENOMEM;
return (void *) -1;
}
}
/*
* mem_heap_lo - return address of the first heap byte
*/
void *mem_heap_lo(){
return (void *) heap;
}
/*
* mem_heap_hi - return address of last heap byte
*/
void *mem_heap_hi(){
return (void *)(mem_brk - 1);
}
/*
* mem_heapsize() - returns the heap size in bytes
*/
size_t mem_heapsize() {
return (size_t)(mem_brk - heap);
}
/*
* mem_pagesize() - returns the page size of the system
*/
size_t mem_pagesize(){
return (size_t) sysconf(_SC_PAGESIZE);
}
/*************** Private Functions *******************/
static void print_stats() {
size_t vbytes = mem_heapsize();
if (!show_stats || vbytes == 0 || stats_printed)
return;
printf("Allocated %zu heap bytes. Max address = %p\n",
vbytes, mem_brk);
stats_printed = true;
}
uint64_t mem_read(const void *addr, size_t len) {
uint64_t rdata;
rdata = *(uint64_t *) addr;
if (len < sizeof(uint64_t)) {
uint64_t mask = ((uint64_t) 1 << (8 * len)) - 1;
rdata &= mask;
}
return rdata;
}
/* Write lower order len bytes of val to address */
void mem_write(void *addr, uint64_t val, size_t len) {
if (len == sizeof(uint64_t))
*(uint64_t *) addr = val;
else
memcpy(addr, (void *) &val, len);
}