memlib.c 2.15 KB
Newer Older
Adam Blank's avatar
Adam Blank committed
1
2
3
4
5
/*
 * 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.
 */
Caleb C. Sander's avatar
Fix #4    
Caleb C. Sander committed
6
7
8
#include "memlib.h"

#include <errno.h>
Adam Blank's avatar
Adam Blank committed
9
10
11
#include <stdint.h>
#include <stdio.h>
#include <string.h>
Caleb C. Sander's avatar
Fix #4    
Caleb C. Sander committed
12
#include <sys/mman.h>
Adam Blank's avatar
Adam Blank committed
13

Caleb C. Sander's avatar
Fix #4    
Caleb C. Sander committed
14
#define MAX_HEAP (100 * (1 << 20)) /* 100 MB */
Adam Blank's avatar
Adam Blank committed
15
16
17
18
19
20
21
22
23

/* private variables */
static uint8_t *heap;
static uint8_t *mem_brk;

/*
 * mem_init - initialize the memory system model
 */
void mem_init() {
Caleb C. Sander's avatar
Fix #4    
Caleb C. Sander committed
24
25
26
27
28
29
    heap = mmap((void *) 0x800000000,        /* suggested start*/
                MAX_HEAP,                    /* length */
                PROT_READ | PROT_WRITE,      /* heap can be read or written */
                MAP_PRIVATE | MAP_ANONYMOUS, /* initialize region with zeros */
                -1,                          /* fd (unused) */
                0                            /* offset (unused) */
Adam Blank's avatar
Adam Blank committed
30
31
32
    );

    /* Heap is initially empty. */
33
    mem_reset_brk(true);
Adam Blank's avatar
Adam Blank committed
34
35
36
37
38
39
40
41
42
43
44
45
}

/*
 * mem_deinit - free the storage used by the memory system model
 */
void mem_deinit() {
    munmap(heap, MAX_HEAP);
}

/*
 * mem_reset_brk - reset the simulated brk pointer to make an empty heap
 */
46
void mem_reset_brk(bool clear) {
Adam Blank's avatar
Adam Blank committed
47
    mem_brk = heap;
Caleb C. Sander's avatar
Fix #4    
Caleb C. Sander committed
48
49

    /* Fill heap with garbage since it is uninitialized. */
50
51
52
    if (clear) {
        memset(heap, 0xCC, MAX_HEAP);
    }
Adam Blank's avatar
Adam Blank committed
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
}

/*
 * 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(ssize_t incr) {
    void *old_brk = mem_brk;

    if (incr < 0 || mem_brk + incr > heap + MAX_HEAP) {
        errno = ENOMEM;
        fprintf(stderr, "ERROR: mem_sbrk failed. Ran out of memory...\n");
        return (void *) -1;
    }

    mem_brk += incr;
    return old_brk;
}

/*
 * mem_heap_lo - return address of the first heap byte
 */
void *mem_heap_lo() {
    return heap;
}

/*
 * mem_heap_hi - return address of last heap byte
 */
Caleb C. Sander's avatar
Fix #4    
Caleb C. Sander committed
83
void *mem_heap_hi() {
Adam Blank's avatar
Adam Blank committed
84
85
86
87
88
89
    return mem_brk - 1;
}

/*
 * mem_heapsize() - returns the heap size in bytes
 */
Caleb C. Sander's avatar
Fix #4    
Caleb C. Sander committed
90
size_t mem_heapsize() {
Adam Blank's avatar
Adam Blank committed
91
92
    return mem_brk - heap;
}