|
@@ -74,6 +74,7 @@ void * alloc_memblock ( size_t size, size_t align ) {
|
74
|
74
|
/* Round up alignment and size to multiples of MIN_MEMBLOCK_SIZE */
|
75
|
75
|
align = ( align + MIN_MEMBLOCK_SIZE - 1 ) & ~( MIN_MEMBLOCK_SIZE - 1 );
|
76
|
76
|
size = ( size + MIN_MEMBLOCK_SIZE - 1 ) & ~( MIN_MEMBLOCK_SIZE - 1 );
|
|
77
|
+ DBG ( "Allocating %zx (aligned %zx)\n", size, align );
|
77
|
78
|
|
78
|
79
|
/* Search through blocks for the first one with enough space */
|
79
|
80
|
list_for_each_entry ( block, &free_blocks, list ) {
|
|
@@ -88,6 +89,9 @@ void * alloc_memblock ( size_t size, size_t align ) {
|
88
|
89
|
pre = block;
|
89
|
90
|
block = ( ( ( void * ) pre ) + pre_size );
|
90
|
91
|
post = ( ( ( void * ) block ) + size );
|
|
92
|
+ DBG ( "[%p,%p) -> [%p,%p) + [%p,%p)\n", pre,
|
|
93
|
+ ( ( ( void * ) pre ) + pre->size ), pre, block,
|
|
94
|
+ post, ( ( ( void * ) pre ) + pre->size ) );
|
91
|
95
|
/* If there is a "post" block, add it in to
|
92
|
96
|
* the free list. Leak it if it is too small
|
93
|
97
|
* (which can happen only at the very end of
|
|
@@ -111,6 +115,8 @@ void * alloc_memblock ( size_t size, size_t align ) {
|
111
|
115
|
list_del ( &pre->list );
|
112
|
116
|
/* Zero allocated memory, for calloc() */
|
113
|
117
|
memset ( block, 0, size );
|
|
118
|
+ DBG ( "Allocated [%p,%p)\n", block,
|
|
119
|
+ ( ( ( void * ) block ) + size ) );
|
114
|
120
|
return block;
|
115
|
121
|
}
|
116
|
122
|
}
|
|
@@ -129,7 +135,7 @@ void free_memblock ( void *ptr, size_t size ) {
|
129
|
135
|
struct memory_block *freeing;
|
130
|
136
|
struct memory_block *block;
|
131
|
137
|
ssize_t gap_before;
|
132
|
|
- ssize_t gap_after;
|
|
138
|
+ ssize_t gap_after = -1;
|
133
|
139
|
|
134
|
140
|
/* Allow for ptr==NULL */
|
135
|
141
|
if ( ! ptr )
|
|
@@ -141,6 +147,7 @@ void free_memblock ( void *ptr, size_t size ) {
|
141
|
147
|
size = ( size + MIN_MEMBLOCK_SIZE - 1 ) & ~( MIN_MEMBLOCK_SIZE - 1 );
|
142
|
148
|
freeing = ptr;
|
143
|
149
|
freeing->size = size;
|
|
150
|
+ DBG ( "Freeing [%p,%p)\n", freeing, ( ( ( void * ) freeing ) + size ));
|
144
|
151
|
|
145
|
152
|
/* Insert/merge into free list */
|
146
|
153
|
list_for_each_entry ( block, &free_blocks, list ) {
|
|
@@ -151,22 +158,32 @@ void free_memblock ( void *ptr, size_t size ) {
|
151
|
158
|
( ( ( void * ) freeing ) + freeing->size ) );
|
152
|
159
|
/* Merge with immediately preceding block, if possible */
|
153
|
160
|
if ( gap_before == 0 ) {
|
|
161
|
+ DBG ( "[%p,%p) + [%p,%p) -> [%p,%p)\n", block,
|
|
162
|
+ ( ( ( void * ) block ) + block->size ), freeing,
|
|
163
|
+ ( ( ( void * ) freeing ) + freeing->size ),block,
|
|
164
|
+ ( ( ( void * ) freeing ) + freeing->size ) );
|
154
|
165
|
block->size += size;
|
155
|
166
|
list_del ( &block->list );
|
156
|
167
|
freeing = block;
|
157
|
168
|
}
|
158
|
|
- /* Insert before the immediately following block. If
|
159
|
|
- * possible, merge the following block into the
|
160
|
|
- * "freeing" block.
|
161
|
|
- */
|
162
|
|
- if ( gap_after >= 0 ) {
|
163
|
|
- list_add_tail ( &freeing->list, &block->list );
|
164
|
|
- if ( gap_after == 0 ) {
|
165
|
|
- freeing->size += block->size;
|
166
|
|
- list_del ( &block->list );
|
167
|
|
- }
|
|
169
|
+ /* Stop processing as soon as we reach a following block */
|
|
170
|
+ if ( gap_after >= 0 )
|
168
|
171
|
break;
|
169
|
|
- }
|
|
172
|
+ }
|
|
173
|
+
|
|
174
|
+ /* Insert before the immediately following block. If
|
|
175
|
+ * possible, merge the following block into the "freeing"
|
|
176
|
+ * block.
|
|
177
|
+ */
|
|
178
|
+ DBG ( "[%p,%p)\n", freeing, ( ( ( void * ) freeing ) + freeing->size));
|
|
179
|
+ list_add_tail ( &freeing->list, &block->list );
|
|
180
|
+ if ( gap_after == 0 ) {
|
|
181
|
+ DBG ( "[%p,%p) + [%p,%p) -> [%p,%p)\n", freeing,
|
|
182
|
+ ( ( ( void * ) freeing ) + freeing->size ), block,
|
|
183
|
+ ( ( ( void * ) block ) + block->size ), freeing,
|
|
184
|
+ ( ( ( void * ) block ) + block->size ) );
|
|
185
|
+ freeing->size += block->size;
|
|
186
|
+ list_del ( &block->list );
|
170
|
187
|
}
|
171
|
188
|
}
|
172
|
189
|
|
|
@@ -192,7 +209,7 @@ void * malloc ( size_t size ) {
|
192
|
209
|
block = alloc_memblock ( total_size, 1 );
|
193
|
210
|
if ( ! block )
|
194
|
211
|
return NULL;
|
195
|
|
- block->size = size;
|
|
212
|
+ block->size = total_size;
|
196
|
213
|
return &block->data;
|
197
|
214
|
}
|
198
|
215
|
|
|
@@ -227,10 +244,13 @@ void free ( void *ptr ) {
|
227
|
244
|
* @c start must be aligned to at least a multiple of sizeof(void*).
|
228
|
245
|
*/
|
229
|
246
|
void mpopulate ( void *start, size_t len ) {
|
|
247
|
+ /* Prevent free_memblock() from rounding up len beyond the end
|
|
248
|
+ * of what we were actually given...
|
|
249
|
+ */
|
230
|
250
|
free_memblock ( start, ( len & ~( MIN_MEMBLOCK_SIZE - 1 ) ) );
|
231
|
251
|
}
|
232
|
252
|
|
233
|
|
-#if 1
|
|
253
|
+#if 0
|
234
|
254
|
#include <vsprintf.h>
|
235
|
255
|
/**
|
236
|
256
|
* Dump free block list
|
|
@@ -246,4 +266,3 @@ void mdumpfree ( void ) {
|
246
|
266
|
}
|
247
|
267
|
}
|
248
|
268
|
#endif
|
249
|
|
-
|