-
Notifications
You must be signed in to change notification settings - Fork 0
/
malloc.c
167 lines (136 loc) · 3.84 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
/***********************************************************************
**
** This file is part of the CXCOMM project.
**
** Copyright (C) 2015 Michael Maydaniuk. All rights reserved.
**
**
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file gpl-2.0.txt included in the
** packaging of this file.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
** 02110-1301 USA
**
**
**
**********************************************************************/
#include "malloc.h"
#include "debug.h"
char *heap_start = NULL;
struct malloc_chunk *
find_free_block(struct malloc_chunk **last, size_t size) {
struct malloc_chunk *p = (struct malloc_chunk*) heap_start;
// traverse the list
while (p && !(p->is_free && p->size >= size)) {
*last = p;
p = p->fd;
}
return p;
}
struct malloc_chunk *
request_space(struct malloc_chunk* last, size_t size) {
struct malloc_chunk *block;
block = (struct malloc_chunk*) sbrk(0);
char* request = (char*) sbrk(size + MALLOC_CHUNK_SIZE);
if (request == (char*) -1) {
// if sbrk fails return -1
return NULL;
}
if (last) { // NULL on first request.
last->fd = block;
}
block->size = size;
block->fd = NULL;
block->is_free = 0;
return block;
}
char *
malloc(size_t size) {
struct malloc_chunk *block;
if (size <= 0) {
return NULL;
}
if (!heap_start) { // First call.
block = request_space(NULL, size);
if (!block) {
return NULL;
}
heap_start = (char*) block;
} else {
struct malloc_chunk *last = (struct malloc_chunk*) heap_start;
block = find_free_block(&last, size);
if (!block) { // Failed to find free block.
block = request_space(last, size);
if (!block) {
return NULL;
}
} else {
// Found a block we can re-use
/*
if ((block->size - size) > (31+MALLOC_CHUNK_SIZE)) {
// if the block we are re-using is considerably larger (32 bytes)
// than what we request, then split it.
int old_size = block->size;
block->size = size;
// save next ptr
struct malloc_chunk *next = (struct malloc_chunk*) block->fd;
char* p = (char*) block;
p = p + (MALLOC_CHUNK_SIZE + size);
block->fd = (struct malloc_chunk*) p;
// point to next free byte
//(char*) block->fd = (char*) block + size + MALLOC_CHUNK_SIZE;
// create a new block header for the remainder of the block
struct malloc_chunk *split = (struct malloc_chunk*) block->fd;
split->size = old_size - MALLOC_CHUNK_SIZE - size;
split->is_free = 1;
split->fd = next;
}
*/
block->is_free = 0;
}
}
return(block+1);
}
struct malloc_chunk *
get_block_ptr(char* ptr) {
return (struct malloc_chunk*) ptr - 1;
}
void
free(char *ptr) {
if (!ptr) {
return;
}
// TODO: merge blocks
struct malloc_chunk* block_ptr = get_block_ptr(ptr);
block_ptr->is_free = 1;
}
void
malloc_print_blocks() {
struct malloc_chunk *p = (struct malloc_chunk*) heap_start;
// traverse the list
while (p) {
if (p->is_free == 0) {
printf("Used Block address = %x size = %d\n",p, p->size);
} else {
printf("Free Block address = %x size = %d\n",p, p->size);
}
p = p->fd;
}
}
char *
calloc(size_t nelem, size_t elsize) {
size_t size = nelem * elsize;
char *ptr = malloc(size);
memset(ptr, 0x72, size);
return ptr;
}