|
@@ -220,6 +220,7 @@ static void discard_all_cache ( void ) {
|
220
|
220
|
*
|
221
|
221
|
* @v size Requested size
|
222
|
222
|
* @v align Physical alignment
|
|
223
|
+ * @v offset Offset from physical alignment
|
223
|
224
|
* @ret ptr Memory block, or NULL
|
224
|
225
|
*
|
225
|
226
|
* Allocates a memory block @b physically aligned as requested. No
|
|
@@ -227,7 +228,7 @@ static void discard_all_cache ( void ) {
|
227
|
228
|
*
|
228
|
229
|
* @c align must be a power of two. @c size may not be zero.
|
229
|
230
|
*/
|
230
|
|
-void * alloc_memblock ( size_t size, size_t align ) {
|
|
231
|
+void * alloc_memblock ( size_t size, size_t align, size_t offset ) {
|
231
|
232
|
struct memory_block *block;
|
232
|
233
|
size_t align_mask;
|
233
|
234
|
size_t pre_size;
|
|
@@ -244,12 +245,13 @@ void * alloc_memblock ( size_t size, size_t align ) {
|
244
|
245
|
size = ( size + MIN_MEMBLOCK_SIZE - 1 ) & ~( MIN_MEMBLOCK_SIZE - 1 );
|
245
|
246
|
align_mask = ( align - 1 ) | ( MIN_MEMBLOCK_SIZE - 1 );
|
246
|
247
|
|
247
|
|
- DBG ( "Allocating %#zx (aligned %#zx)\n", size, align );
|
|
248
|
+ DBG ( "Allocating %#zx (aligned %#zx+%zx)\n", size, align, offset );
|
248
|
249
|
while ( 1 ) {
|
249
|
250
|
/* Search through blocks for the first one with enough space */
|
250
|
251
|
list_for_each_entry ( block, &free_blocks, list ) {
|
251
|
|
- pre_size = ( - virt_to_phys ( block ) ) & align_mask;
|
252
|
|
- post_size = block->size - pre_size - size;
|
|
252
|
+ pre_size = ( ( offset - virt_to_phys ( block ) )
|
|
253
|
+ & align_mask );
|
|
254
|
+ post_size = ( block->size - pre_size - size );
|
253
|
255
|
if ( post_size >= 0 ) {
|
254
|
256
|
/* Split block into pre-block, block, and
|
255
|
257
|
* post-block. After this split, the "pre"
|
|
@@ -418,7 +420,7 @@ void * realloc ( void *old_ptr, size_t new_size ) {
|
418
|
420
|
if ( new_size ) {
|
419
|
421
|
new_total_size = ( new_size +
|
420
|
422
|
offsetof ( struct autosized_block, data ) );
|
421
|
|
- new_block = alloc_memblock ( new_total_size, 1 );
|
|
423
|
+ new_block = alloc_memblock ( new_total_size, 1, 0 );
|
422
|
424
|
if ( ! new_block )
|
423
|
425
|
return NULL;
|
424
|
426
|
VALGRIND_MAKE_MEM_UNDEFINED ( new_block, offsetof ( struct autosized_block, data ) );
|