-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmm-implicit.c
166 lines (130 loc) · 3.88 KB
/
mm-implicit.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stddef.h>
#include <unistd.h>
#include "mm.h"
#include "memlib.h"
/* If you want debugging output, use the following macro.
* When you hand in, remove the #define DEBUG line. */
// #define DEBUG
#ifdef DEBUG
#define debug(fmt, ...) printf("%s: " fmt "\n", __func__, __VA_ARGS__)
#define msg(...) printf(__VA_ARGS__)
#else
#define debug(fmt, ...)
#define msg(...)
#endif
#define __unused __attribute__((unused))
/* do not change the following! */
#ifdef DRIVER
/* create aliases for driver tests */
#define malloc mm_malloc
#define free mm_free
#define realloc mm_realloc
#define calloc mm_calloc
#endif /* !DRIVER */
typedef int32_t word_t; /* Heap is bascially an array of 4-byte words. */
typedef enum {
FREE = 0, /* Block is free */
USED = 1, /* Block is used */
PREVFREE = 2, /* Previous block is free (optimized boundary tags) */
} bt_flags;
static word_t *heap_start; /* Address of the first block */
static word_t *heap_end; /* Address past last byte of last block */
static word_t *last; /* Points at last block */
/* --=[ boundary tag handling ]=-------------------------------------------- */
static inline word_t bt_size(word_t *bt) {
return *bt & ~(USED | PREVFREE);
}
static inline int bt_used(word_t *bt) {
return *bt & USED;
}
static inline int bt_free(word_t *bt) {
return !(*bt & USED);
}
/* Given boundary tag address calculate it's buddy address. */
static inline word_t *bt_footer(word_t *bt) {
return (void *)bt + bt_size(bt) - sizeof(word_t);
}
/* Given payload pointer returns an address of boundary tag. */
static inline word_t *bt_fromptr(void *ptr) {
return (word_t *)ptr - 1;
}
/* Creates boundary tag(s) for given block. */
static inline void bt_make(word_t *bt, size_t size, bt_flags flags) {
}
/* Previous block free flag handling for optimized boundary tags. */
static inline bt_flags bt_get_prevfree(word_t *bt) {
return *bt & PREVFREE;
}
static inline void bt_clr_prevfree(word_t *bt) {
if (bt)
*bt &= ~PREVFREE;
}
static inline void bt_set_prevfree(word_t *bt) {
*bt |= PREVFREE;
}
/* Returns address of payload. */
static inline void *bt_payload(word_t *bt) {
return bt + 1;
}
/* Returns address of next block or NULL. */
static inline word_t *bt_next(word_t *bt) {
}
/* Returns address of previous block or NULL. */
static inline word_t *bt_prev(word_t *bt) {
}
/* --=[ miscellanous procedures ]=------------------------------------------ */
/* Calculates block size incl. header, footer & payload,
* and aligns it to block boundary (ALIGNMENT). */
static inline size_t blksz(size_t size) {
}
static void *morecore(size_t size) {
void *ptr = mem_sbrk(size);
if (ptr == (void *)-1)
return NULL;
return ptr;
}
/* --=[ mm_init ]=---------------------------------------------------------- */
int mm_init(void) {
void *ptr = morecore(ALIGNMENT - sizeof(word_t));
if (!ptr)
return -1;
heap_start = NULL;
heap_end = NULL;
last = NULL;
return 0;
}
/* --=[ malloc ]=----------------------------------------------------------- */
#if 0
/* First fit startegy. */
static word_t *find_fit(size_t reqsz) {
}
#else
/* Best fit startegy. */
static word_t *find_fit(size_t reqsz) {
}
#endif
void *malloc(size_t size) {
}
/* --=[ free ]=------------------------------------------------------------- */
void free(void *ptr) {
}
/* --=[ realloc ]=---------------------------------------------------------- */
void *realloc(void *old_ptr, size_t size) {
}
/* --=[ calloc ]=----------------------------------------------------------- */
void *calloc(size_t nmemb, size_t size) {
size_t bytes = nmemb * size;
void *new_ptr = malloc(bytes);
if (new_ptr)
memset(new_ptr, 0, bytes);
return new_ptr;
}
/* --=[ mm_checkheap ]=----------------------------------------------------- */
void mm_checkheap(int verbose) {
}