-
Notifications
You must be signed in to change notification settings - Fork 0
/
hmalloc.c
174 lines (142 loc) · 3.73 KB
/
hmalloc.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
167
168
169
170
171
172
173
// CS3650 CH02 starter code
// Spring 2019
//
// Author: Nat Tuck
//
// Once you've read this, you're done with the simple allocator homework.
#include <stdint.h>
#include <sys/mman.h>
#include <assert.h>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include "hmalloc.h"
typedef struct nu_free_cell {
int64_t size;
struct nu_free_cell* next;
} nu_free_cell;
static const int64_t CHUNK_SIZE = 65536;
static const int64_t CELL_SIZE = (int64_t)sizeof(nu_free_cell);
static nu_free_cell* nu_free_list = 0;
static pthread_mutex_t freelist_lock = PTHREAD_MUTEX_INITIALIZER;
int64_t
nu_free_list_length()
{
int len = 0;
for (nu_free_cell* pp = nu_free_list; pp != 0; pp = pp->next) {
len++;
}
return len;
}
void
nu_print_free_list()
{
nu_free_cell* pp = nu_free_list;
printf("= Free list: =\n");
for (; pp != 0; pp = pp->next) {
printf("%lx: (cell %ld %lx)\n", (int64_t) pp, pp->size, (int64_t) pp->next);
}
}
static
void
nu_free_list_coalesce()
{
nu_free_cell* pp = nu_free_list;
int free_chunk = 0;
while (pp != 0 && pp->next != 0) {
if (((int64_t)pp) + pp->size == ((int64_t) pp->next)) {
pp->size += pp->next->size;
pp->next = pp->next->next;
}
pp = pp->next;
}
}
static
void
nu_free_list_insert(nu_free_cell* cell)
{
if (nu_free_list == 0 || ((uint64_t) nu_free_list) > ((uint64_t) cell)) {
cell->next = nu_free_list;
nu_free_list = cell;
return;
}
nu_free_cell* pp = nu_free_list;
while (pp->next != 0 && ((uint64_t)pp->next) < ((uint64_t) cell)) {
pp = pp->next;
}
cell->next = pp->next;
pp->next = cell;
nu_free_list_coalesce();
}
static
nu_free_cell*
free_list_get_cell(int64_t size)
{
nu_free_cell** prev = &nu_free_list;
for (nu_free_cell* pp = nu_free_list; pp != 0; pp = pp->next) {
if (pp->size >= size) {
*prev = pp->next;
return pp;
}
prev = &(pp->next);
}
return 0;
}
static
nu_free_cell*
make_cell()
{
void* addr = mmap(0, CHUNK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
nu_free_cell* cell = (nu_free_cell*) addr;
cell->size = CHUNK_SIZE;
return cell;
}
void*
hmalloc(size_t usize)
{
pthread_mutex_lock(&freelist_lock);
int64_t size = (int64_t) usize;
// space for size
int64_t alloc_size = size + sizeof(int64_t);
// space for free cell when returned to list
if (alloc_size < CELL_SIZE) {
alloc_size = CELL_SIZE;
}
// TODO: Handle large allocations.
if (alloc_size > CHUNK_SIZE) {
void* addr = mmap(0, alloc_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
*((int64_t*)addr) = alloc_size;
pthread_mutex_unlock(&freelist_lock);
return addr + sizeof(int64_t);
}
nu_free_cell* cell = free_list_get_cell(alloc_size);
if (!cell) {
cell = make_cell();
}
// Return unused portion to free list.
int64_t rest_size = cell->size - alloc_size;
if (rest_size >= CELL_SIZE) {
void* addr = (void*) cell;
nu_free_cell* rest = (nu_free_cell*) (addr + alloc_size);
rest->size = rest_size;
nu_free_list_insert(rest);
}
*((int64_t*)cell) = alloc_size;
pthread_mutex_unlock(&freelist_lock);
return ((void*)cell) + sizeof(int64_t);
}
void
hfree(void* addr)
{
pthread_mutex_lock(&freelist_lock);
nu_free_cell* cell = (nu_free_cell*)(addr - sizeof(int64_t));
int64_t size = *((int64_t*) cell);
if (size > CHUNK_SIZE) {
munmap((void*) cell, size);
}
else {
cell->size = size;
nu_free_list_insert(cell);
}
pthread_mutex_unlock(&freelist_lock);
}