/
malloc.c
195 lines (180 loc) · 4.34 KB
/
malloc.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//
// malloc.c
// Malloc
//
// Created by Zoltán Sasvári on 05.06.15.
// Copyright (c) 2015 BinaryTrasher. All rights reserved.
//
#include "malloc.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#ifdef __GNUC__
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#else
#define likely(x) (x)
#define unlikely(x) (x)
#endif
typedef struct metadata {
int magic1;
size_t size;
struct metadata *next;
struct metadata *prev;
int free;
int magic2;
} metadata_t;
void *base = 0;
int alloc_count = 0;
pthread_mutex_t list_lock;
#define METADATA_SIZE sizeof(struct metadata)
#define MAGIC_FREE 0xDEADDA7A
#define MAGIC_USED 0xB16B00B5
#define SEGFAULT "Segmentation fault!\n"
metadata_t *get_free_block(metadata_t **last, size_t size){
metadata_t *current = base;
while (current && !(current->free && current->size >= size)) {
*last = current;
current = current->next;
if (!current) continue;
if (unlikely((current->magic1 != MAGIC_USED && current->magic1 != MAGIC_FREE) || current->magic1 != current->magic2)) {
printf(SEGFAULT);
exit(-1);
}
}
return current;
}
metadata_t *get_space(metadata_t* last, size_t size) {
metadata_t *block;
block = sbrk(0);
void *request = sbrk((int)(size + METADATA_SIZE));
if (unlikely((void*)block != request)) {
printf(SEGFAULT);
exit(-1);
}
if (unlikely(request == (void*) -1)) {
return 0; // shit
}
if (last) {
last->next = block;
block->prev = last;
} else {
block->prev = NULL;
}
block->size = size;
block->next = 0;
block->free = 0;
block->magic1 = MAGIC_USED;
block->magic2 = MAGIC_USED;
return block;
}
void *malloc(size_t size) {
metadata_t *block;
// TODO: align size?
if (unlikely(size <= 0)) {
return 0;
}
if (unlikely(!base)) { // first
pthread_mutex_init(&list_lock, NULL);
pthread_mutex_lock(&list_lock);
block = get_space(0, size);
if (unlikely(!block)) {
pthread_mutex_unlock(&list_lock);
return 0;
}
base = block;
// pthread_mutex_unlock(&list_lock);
} else {
pthread_mutex_lock(&list_lock);
metadata_t *last = base;
block = get_free_block(&last, size);
if (!block) {
block = get_space(last, size);
if (unlikely(!block)) {
pthread_mutex_unlock(&list_lock);
return 0;
}
} else {
// TODO: split
block->free = 0;
block->magic1 = MAGIC_USED;
block->magic2 = MAGIC_USED;
}
}
alloc_count++;
pthread_mutex_unlock(&list_lock);
return (block+1);
}
metadata_t *get_block(void *ptr){
metadata_t* ret_val = ((metadata_t*)ptr)-1;
if (unlikely((ret_val->magic1 != MAGIC_USED && ret_val->magic1 != MAGIC_FREE) || ret_val->magic1 != ret_val->magic2))
{
printf(SEGFAULT);
exit(-1);
}
return ret_val;
}
void merge_block(metadata_t*block){
if (block->next && block->next->free){
block->size += METADATA_SIZE + block->next->size;
block->next = block->next->next;
}
}
void free(void *ptr) {
if (unlikely(!ptr)) {
return;
}
pthread_mutex_lock(&list_lock);
metadata_t* block_ptr = get_block(ptr);
merge_block(block_ptr);
if (unlikely(block_ptr->free != 0 || block_ptr->magic1 != MAGIC_USED || block_ptr->magic1 != block_ptr->magic2)) {
printf(SEGFAULT);
exit(-1);
}
alloc_count--;
if (unlikely(alloc_count == 0)){
brk(base);
base = 0;
} else if (block_ptr->next == NULL){
block_ptr->prev->next = NULL;
brk(block_ptr);
} else {
block_ptr->free = 1;
block_ptr->magic1 = MAGIC_FREE;
block_ptr->magic2 = MAGIC_FREE;
}
pthread_mutex_unlock(&list_lock);
}
void *realloc(void *ptr, size_t size) {
if (unlikely(!ptr)) {
return malloc(size);
}
pthread_mutex_lock(&list_lock);
metadata_t *block_ptr = get_block(ptr);
if (block_ptr->size >= size) {
pthread_mutex_unlock(&list_lock);
return ptr;
}
void *new_ptr;
pthread_mutex_unlock(&list_lock);
new_ptr = malloc(size);
if (unlikely(!new_ptr)) {
return 0;
}
memcpy(new_ptr, ptr, block_ptr->size);
free(ptr);
return new_ptr;
}
void *calloc(size_t nelem, size_t elsize) {
if (unlikely(nelem == 0 || elsize == 0)) {
return 0;
}
void *ptr = malloc(nelem*elsize);
if (unlikely(!ptr)) {
return 0;
}
memset(ptr, 0, nelem*elsize);
return ptr;
}