Code: Select all
static s32 find_smallest_hole(u32 size, u8 page_align, heap_t *heap)
{
// Find the smallest hole that will fit.
u32 iterator = 0;
while (iterator < heap->index.size)
{
header_t *header = (header_t *)lookup_ordered_array(iterator, &heap->index);
// If the user has requested the memory be page-aligned
if (page_align > 0)
{
// Page-align the starting point of this header.
u32 location = (u32)header;
u32 offset = 0;
if ((location+sizeof(header_t)) & 0xFFFFF000 != 0)
offset = 0x1000 /* page size */ - (location+sizeof(header_t))%0x1000;
s32 hole_size = (u32)header->size - offset;
// Can we fit now?
if (hole_size >= (u32)size)
break;
}
else if (header->size >= size)
break;
iterator++;
}
// Why did the loop exit?
if (iterator == heap->index.size)
return -1; // We got to the end and didn't find anything.
else
return iterator;
}
Code: Select all
type_t lookup_ordered_array(u32 i, ordered_array_t *array)
{
ASSERT(i < array->size);
return array->array[i];
}
After returning lookup_ordered_array, %eax is assigned to 0xe94aaa55 at 0x00003a13 <+34> line(see assembler code)
Dump of assembler code for function lookup_ordered_array:
Code: Select all
0x000039f1 <+0>: push %ebp
0x000039f2 <+1>: mov %esp,%ebp
=> 0x000039f4 <+3>: mov 0xc(%ebp),%eax
0x000039f7 <+6>: mov 0x4(%eax),%eax
0x000039fa <+9>: cmp 0x8(%ebp),%eax
0x000039fd <+12>: ja 0x3a06 <lookup_ordered_array+21>
0x000039ff <+14>: mov $0xffffffff,%eax
0x00003a04 <+19>: jmp 0x3a15 <lookup_ordered_array+36>
0x00003a06 <+21>: mov 0xc(%ebp),%eax
0x00003a09 <+24>: mov (%eax),%eax
0x00003a0b <+26>: mov 0x8(%ebp),%edx
0x00003a0e <+29>: shl $0x2,%edx
0x00003a11 <+32>: add %edx,%eax
0x00003a13 <+34>: mov (%eax),%eax
0x00003a15 <+36>: pop %ebp
0x00003a16 <+37>: ret
Dump of assembler code for function find_smallest_hole:
Code: Select all
0x00002b21 <+0>: push %ebp
0x00002b22 <+1>: mov %esp,%ebp
0x00002b24 <+3>: sub $0x38,%esp
0x00002b27 <+6>: mov 0xc(%ebp),%eax
0x00002b2a <+9>: mov %al,-0x2c(%ebp)
0x00002b2d <+12>: movl $0x0,-0xc(%ebp)
0x00002b34 <+19>: jmp 0x2ba8 <find_smallest_hole+135>
0x00002b36 <+21>: mov 0x10(%ebp),%eax
0x00002b39 <+24>: mov %eax,0x4(%esp)
0x00002b3d <+28>: mov -0xc(%ebp),%eax
0x00002b40 <+31>: mov %eax,(%esp)
0x00002b43 <+34>: call 0x39f1 <lookup_ordered_array>
0x00002b48 <+39>: mov %eax,-0x14(%ebp)
=> 0x00002b4b <+42>: cmpb $0x0,-0x2c(%ebp)
0x00002b4f <+46>: je 0x2b97 <find_smallest_hole+118>
0x00002b51 <+48>: mov -0x14(%ebp),%eax
0x00002b54 <+51>: mov %eax,-0x18(%ebp)
0x00002b57 <+54>: movl $0x0,-0x10(%ebp)
0x00002b5e <+61>: mov -0x18(%ebp),%eax
0x00002b61 <+64>: and $0x1,%eax
0x00002b64 <+67>: test %eax,%eax
0x00002b66 <+69>: je 0x2b7f <find_smallest_hole+94>
0x00002b68 <+71>: mov -0x18(%ebp),%eax
0x00002b6b <+74>: add $0xc,%eax
0x00002b6e <+77>: and $0xfff,%eax
0x00002b73 <+82>: mov %eax,%edx
0x00002b75 <+84>: mov $0x1000,%eax
0x00002b7a <+89>: sub %edx,%eax
0x00002b7c <+91>: mov %eax,-0x10(%ebp)
0x00002b7f <+94>: mov -0x14(%ebp),%eax
0x00002b82 <+97>: mov 0x8(%eax),%eax
0x00002b85 <+100>: sub -0x10(%ebp),%eax
0x00002b88 <+103>: mov %eax,-0x1c(%ebp)
0x00002b8b <+106>: mov -0x1c(%ebp),%eax
0x00002b8e <+109>: cmp 0x8(%ebp),%eax
0x00002b91 <+112>: jb 0x2b95 <find_smallest_hole+116>
0x00002b93 <+114>: jmp 0x2bb3 <find_smallest_hole+146>
0x00002b95 <+116>: jmp 0x2ba4 <find_smallest_hole+131>
0x00002b97 <+118>: mov -0x14(%ebp),%eax
0x00002b9a <+121>: mov 0x8(%eax),%eax
0x00002b9d <+124>: cmp 0x8(%ebp),%eax
0x00002ba0 <+127>: jb 0x2ba4 <find_smallest_hole+131>
0x00002ba2 <+129>: jmp 0x2bb3 <find_smallest_hole+146>
0x00002ba4 <+131>: addl $0x1,-0xc(%ebp)
0x00002ba8 <+135>: mov 0x10(%ebp),%eax
0x00002bab <+138>: mov 0x4(%eax),%eax
0x00002bae <+141>: cmp -0xc(%ebp),%eax
0x00002bb1 <+144>: ja 0x2b36 <find_smallest_hole+21>
0x00002bb3 <+146>: mov 0x10(%ebp),%eax
0x00002bb6 <+149>: mov 0x4(%eax),%eax
0x00002bb9 <+152>: cmp -0xc(%ebp),%eax
0x00002bbc <+155>: jne 0x2bc5 <find_smallest_hole+164>
0x00002bbe <+157>: mov $0xffffffff,%eax
0x00002bc3 <+162>: jmp 0x2bc8 <find_smallest_hole+167>
0x00002bc5 <+164>: mov -0xc(%ebp),%eax
0x00002bc8 <+167>: leave
0x00002bc9 <+168>: ret