Commit abb26d63 authored by Paolo Bonzini's avatar Paolo Bonzini Committed by Juan Quintela

exec: sort the memory from biggest to smallest

Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
Signed-off-by: default avatarJuan Quintela <quintela@redhat.com>
parent a3161038
......@@ -505,35 +505,6 @@ uint64_t ram_bytes_total(void)
return total;
}
static int block_compar(const void *a, const void *b)
{
RAMBlock * const *ablock = a;
RAMBlock * const *bblock = b;
return strcmp((*ablock)->idstr, (*bblock)->idstr);
}
static void sort_ram_list(void)
{
RAMBlock *block, *nblock, **blocks;
int n;
n = 0;
QTAILQ_FOREACH(block, &ram_list.blocks, next) {
++n;
}
blocks = g_malloc(n * sizeof *blocks);
n = 0;
QTAILQ_FOREACH_SAFE(block, &ram_list.blocks, next, nblock) {
blocks[n++] = block;
QTAILQ_REMOVE(&ram_list.blocks, block, next);
}
qsort(blocks, n, sizeof *blocks, block_compar);
while (--n >= 0) {
QTAILQ_INSERT_HEAD(&ram_list.blocks, blocks[n], next);
}
g_free(blocks);
}
static void migration_end(void)
{
if (migration_bitmap) {
......@@ -562,7 +533,6 @@ static void reset_ram_globals(void)
{
last_block = NULL;
last_offset = 0;
sort_ram_list();
}
#define MAX_WAIT 50 /* ms, half buffered_file limit */
......
......@@ -1007,7 +1007,7 @@ static int memory_try_enable_merging(void *addr, size_t len)
ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
MemoryRegion *mr)
{
RAMBlock *new_block;
RAMBlock *block, *new_block;
size = TARGET_PAGE_ALIGN(size);
new_block = g_malloc0(sizeof(*new_block));
......@@ -1043,7 +1043,17 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
}
new_block->length = size;
QTAILQ_INSERT_HEAD(&ram_list.blocks, new_block, next);
/* Keep the list sorted from biggest to smallest block. */
QTAILQ_FOREACH(block, &ram_list.blocks, next) {
if (block->length < new_block->length) {
break;
}
}
if (block) {
QTAILQ_INSERT_BEFORE(block, new_block, next);
} else {
QTAILQ_INSERT_TAIL(&ram_list.blocks, new_block, next);
}
ram_list.mru_block = NULL;
ram_list.phys_dirty = g_realloc(ram_list.phys_dirty,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment