-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnano_malloc.c
249 lines (213 loc) · 7.83 KB
/
nano_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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Copyright (c) 2012, 2013 ARM Ltd
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the company may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include "nano_malloc.h"
#include "machine.h"
#ifndef MAX
#define MAX(a,b) ((a) >= (b) ? (a) : (b))
#endif
/* Define free_list as internal name to avoid conflict with user names */
#define free_list __malloc_free_list
#define ALIGN_TO(size, align) ((size+(align-1)) & ~(align-1))
typedef struct malloc_chunk {
/* ------------------
* chunk->| size (4 bytes) |
* ------------------
* | Padding for |
* | alignment |
* | holding neg |
* | offset to size |
* ------------------
* mem_ptr->| point to next |
* | free when freed|
* | or data load |
* | when allocated |
* ------------------
*/
/* size of the allocated payload area, including size before
CHUNK_OFFSET */
int size;
/* since here, the memory is either the next free block, or data load */
struct malloc_chunk * next;
} chunk;
#define CHUNK_OFFSET ((size_t)(&(((struct malloc_chunk *)0)->next)))
/* as well as the minimal allocation size
* to hold a free pointer */
#define MALLOC_MINSIZE (sizeof(void *))
/* Alignment of allocated block */
#ifdef KIT68K
// Alignment to even address is sufficient for 68008
// Simplify size calculations here since IDE68K compiler generates redundant code
#define MALLOC_ALIGN 2
#define CHUNK_ALIGN 2
#define MALLOC_PADDING 0
#define MALLOC_MINCHUNK 8
#else
#define MALLOC_ALIGN sizeof(double)
#define CHUNK_ALIGN sizeof(void*)
#define MALLOC_PADDING ((MAX(MALLOC_ALIGN, CHUNK_ALIGN)) - CHUNK_ALIGN)
/* size of smallest possible chunk. A memory piece smaller than this size
* won't be able to create a chunk */
#define MALLOC_MINCHUNK (CHUNK_OFFSET + MALLOC_PADDING + MALLOC_MINSIZE)
#endif
static chunk * get_chunk_from_ptr(void * ptr) {
chunk * c = (chunk *)((char *)ptr - CHUNK_OFFSET);
/* Skip the padding area */
if (c->size < 0) c = (chunk *)((char *)c + c->size);
return c;
}
static chunk* free_list;
static char myHeap[HEAP_SIZE];
void init_freelist(void) {
free_list = (chunk *)(myHeap);
free_list->size = HEAP_SIZE;
free_list->next = NULL;
}
/** Algorithm:
* Walk through the free list to find the first match. If fails to find
* one, call sbrk to allocate a new chunk.
*/
void* nano_malloc(size_t s) {
chunk *p, *r;
char *ptr, *align_ptr;
int offset;
size_t alloc_size;
alloc_size = ALIGN_TO(s, CHUNK_ALIGN); /* size of aligned data load */
alloc_size += MALLOC_PADDING; /* padding */
alloc_size += CHUNK_OFFSET; /* size of chunk head */
// alloc_size = MAX(alloc_size, MALLOC_MINCHUNK);
// alloc_size >= MALLOC_MINCHUNK holds always in Lox szenario
if (alloc_size < s) return NULL;
p = free_list;
r = p;
while (r) {
int rem = r->size - alloc_size;
if (rem >= 0) {
if (rem >= MALLOC_MINCHUNK) {
/* Find a chunk that much larger than required size, break
* it into two chunks and return the second one */
r->size = rem;
r = (chunk *)((char *)r + rem);
r->size = alloc_size;
}
/* Find a chunk that is exactly the size or slightly bigger
* than requested size, just return this chunk */
else if (p == r) {
/* Now it implies p==r==free_list. Move the free_list
* to next chunk */
free_list = r->next;
} else {
/* Normal case. Remove it from free_list */
p->next = r->next;
}
break;
}
p=r;
r=r->next;
}
/* Failed to find a appropriate chunk. Give up */
if (r == NULL) return NULL;
ptr = (char *)r + CHUNK_OFFSET;
align_ptr = (char *)ALIGN_TO((unsigned long)ptr, MALLOC_ALIGN);
offset = align_ptr - ptr;
if (offset)
*(int *)((char *)r + offset) = -offset;
return align_ptr;
}
#define MALLOC_CHECK_DOUBLE_FREE
/** Algorithm:
* Maintain a global free chunk single link list, headed by global
* variable free_list.
* When free, insert the to-be-freed chunk into free list. The place to
* insert should make sure all chunks are sorted by address from low to
* high. Then merge with neighbor chunks if adjacent.
*/
void nano_free (void* free_p) {
chunk * p_to_free;
chunk * p, * q;
if (free_p == NULL) return;
p_to_free = get_chunk_from_ptr(free_p);
if (free_list == NULL) {
/* Set first free list element */
p_to_free->next = free_list;
free_list = p_to_free;
return;
}
if (p_to_free < free_list) {
if ((char *)p_to_free + p_to_free->size == (char *)free_list) {
/* Chunk to free is just before the first element of
* free list */
p_to_free->size += free_list->size;
p_to_free->next = free_list->next;
} else {
/* Insert before current free_list */
p_to_free->next = free_list;
}
free_list = p_to_free;
return;
}
q = free_list;
/* Walk through the free list to find the place for insert. */
do {
p = q;
q = q->next;
} while (q && q <= p_to_free);
/* Now p <= p_to_free and either q == NULL or q > p_to_free
* Try to merge with chunks immediately before/after it. */
if ((char *)p + p->size == (char *)p_to_free) {
/* Chunk to be freed is adjacent
* to a free chunk before it */
p->size += p_to_free->size;
/* If the merged chunk is also adjacent
* to the chunk after it, merge again */
if ((char *)p + p->size == (char *) q) {
p->size += q->size;
p->next = q->next;
}
}
#ifdef MALLOC_CHECK_DOUBLE_FREE
else if ((char *)p + p->size > (char *)p_to_free) {
/* Report double free fault */
putstr("Double free\n");
return;
}
#endif
else if ((char *)p_to_free + p_to_free->size == (char *) q) {
/* Chunk to be freed is adjacent
* to a free chunk after it */
p_to_free->size += q->size;
p_to_free->next = q->next;
p->next = p_to_free;
} else {
/* Not adjacent to any chunk. Just insert it. Resulting
* a fragment. */
p_to_free->next = q;
p->next = p_to_free;
}
}