> > Subject: [Qemu-devel] [PATCH 1/6] exec: Return RAMBlock pointer from > allocating functions > > Previously we return RAMBlock.offset; now return the pointer to the > whole structure. > > ram_block_add returns void now, error is completely passed with errp. > > Signed-off-by: Fam Zheng <f...@redhat.com> > --- > exec.c | 51 > +++++++++++++++++++++---------------------------- > include/exec/ram_addr.h | 22 ++++++++++----------- > memory.c | 25 +++++++++++++++++++----- > 3 files changed, 53 insertions(+), 45 deletions(-) >
Reviewed-by: Gonglei <arei.gong...@huawei.com> > diff --git a/exec.c b/exec.c > index c62c439..2b14b79 100644 > --- a/exec.c > +++ b/exec.c > @@ -1554,7 +1554,7 @@ static void dirty_memory_extend(ram_addr_t > old_ram_size, > } > } > > -static ram_addr_t ram_block_add(RAMBlock *new_block, Error **errp) > +static void ram_block_add(RAMBlock *new_block, Error **errp) > { > RAMBlock *block; > RAMBlock *last_block = NULL; > @@ -1573,7 +1573,6 @@ static ram_addr_t ram_block_add(RAMBlock > *new_block, Error **errp) > if (err) { > error_propagate(errp, err); > qemu_mutex_unlock_ramlist(); > - return -1; > } > } else { > new_block->host = phys_mem_alloc(new_block->max_length, > @@ -1583,7 +1582,6 @@ static ram_addr_t ram_block_add(RAMBlock > *new_block, Error **errp) > "cannot set up guest memory '%s'", > > memory_region_name(new_block->mr)); > qemu_mutex_unlock_ramlist(); > - return -1; > } > memory_try_enable_merging(new_block->host, > new_block->max_length); > } > @@ -1631,22 +1629,19 @@ static ram_addr_t ram_block_add(RAMBlock > *new_block, Error **errp) > kvm_setup_guest_memory(new_block->host, > new_block->max_length); > } > } > - > - return new_block->offset; > } > > #ifdef __linux__ > -ram_addr_t qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, > - bool share, const char > *mem_path, > - Error **errp) > +RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, > + bool share, const char > *mem_path, > + Error **errp) > { > RAMBlock *new_block; > - ram_addr_t addr; > Error *local_err = NULL; > > if (xen_enabled()) { > error_setg(errp, "-mem-path not supported with Xen"); > - return -1; > + return NULL; > } > > if (phys_mem_alloc != qemu_anon_ram_alloc) { > @@ -1657,7 +1652,7 @@ ram_addr_t qemu_ram_alloc_from_file(ram_addr_t > size, MemoryRegion *mr, > */ > error_setg(errp, > "-mem-path not supported with this accelerator"); > - return -1; > + return NULL; > } > > size = HOST_PAGE_ALIGN(size); > @@ -1670,29 +1665,28 @@ ram_addr_t > qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, > mem_path, errp); > if (!new_block->host) { > g_free(new_block); > - return -1; > + return NULL; > } > > - addr = ram_block_add(new_block, &local_err); > + ram_block_add(new_block, &local_err); > if (local_err) { > g_free(new_block); > error_propagate(errp, local_err); > - return -1; > + return NULL; > } > - return addr; > + return new_block; > } > #endif > > static > -ram_addr_t qemu_ram_alloc_internal(ram_addr_t size, ram_addr_t > max_size, > - void (*resized)(const char*, > - uint64_t > length, > - void *host), > - void *host, bool resizeable, > - MemoryRegion *mr, Error **errp) > +RAMBlock *qemu_ram_alloc_internal(ram_addr_t size, ram_addr_t > max_size, > + void (*resized)(const char*, > + uint64_t length, > + void *host), > + void *host, bool resizeable, > + MemoryRegion *mr, Error **errp) > { > RAMBlock *new_block; > - ram_addr_t addr; > Error *local_err = NULL; > > size = HOST_PAGE_ALIGN(size); > @@ -1711,29 +1705,28 @@ ram_addr_t > qemu_ram_alloc_internal(ram_addr_t size, ram_addr_t max_size, > if (resizeable) { > new_block->flags |= RAM_RESIZEABLE; > } > - addr = ram_block_add(new_block, &local_err); > + ram_block_add(new_block, &local_err); > if (local_err) { > g_free(new_block); > error_propagate(errp, local_err); > - return -1; > + return NULL; > } > - > mr->ram_block = new_block; > - return addr; > + return new_block; > } > > -ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host, > +RAMBlock *qemu_ram_alloc_from_ptr(ram_addr_t size, void *host, > MemoryRegion *mr, Error **errp) > { > return qemu_ram_alloc_internal(size, size, NULL, host, false, mr, errp); > } > > -ram_addr_t qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr, Error > **errp) > +RAMBlock *qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr, Error > **errp) > { > return qemu_ram_alloc_internal(size, size, NULL, NULL, false, mr, errp); > } > > -ram_addr_t qemu_ram_alloc_resizeable(ram_addr_t size, ram_addr_t > maxsz, > +RAMBlock *qemu_ram_alloc_resizeable(ram_addr_t size, ram_addr_t > maxsz, > void (*resized)(const char*, > uint64_t > length, > void *host), > diff --git a/include/exec/ram_addr.h b/include/exec/ram_addr.h > index 5d33def..865e19b 100644 > --- a/include/exec/ram_addr.h > +++ b/include/exec/ram_addr.h > @@ -94,17 +94,17 @@ ram_addr_t last_ram_offset(void); > void qemu_mutex_lock_ramlist(void); > void qemu_mutex_unlock_ramlist(void); > > -ram_addr_t qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, > - bool share, const char > *mem_path, > - Error **errp); > -ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host, > - MemoryRegion *mr, Error **errp); > -ram_addr_t qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr, Error > **errp); > -ram_addr_t qemu_ram_alloc_resizeable(ram_addr_t size, ram_addr_t > max_size, > - void (*resized)(const char*, > - uint64_t > length, > - void *host), > - MemoryRegion *mr, Error > **errp); > +RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, > + bool share, const char > *mem_path, > + Error **errp); > +RAMBlock *qemu_ram_alloc_from_ptr(ram_addr_t size, void *host, > + MemoryRegion *mr, Error **errp); > +RAMBlock *qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr, Error > **errp); > +RAMBlock *qemu_ram_alloc_resizeable(ram_addr_t size, ram_addr_t > max_size, > + void (*resized)(const char*, > + uint64_t > length, > + void *host), > + MemoryRegion *mr, Error > **errp); > int qemu_get_ram_fd(ram_addr_t addr); > void qemu_set_ram_fd(ram_addr_t addr, int fd); > void *qemu_get_ram_block_host_ptr(ram_addr_t addr); > diff --git a/memory.c b/memory.c > index 0dd9695..ae13ba9 100644 > --- a/memory.c > +++ b/memory.c > @@ -1226,11 +1226,14 @@ void memory_region_init_ram(MemoryRegion > *mr, > uint64_t size, > Error **errp) > { > + RAMBlock *ram_block; > + > memory_region_init(mr, owner, name, size); > mr->ram = true; > mr->terminates = true; > mr->destructor = memory_region_destructor_ram; > - mr->ram_addr = qemu_ram_alloc(size, mr, errp); > + ram_block = qemu_ram_alloc(size, mr, errp); > + mr->ram_addr = ram_block->offset; > mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0; > } > > @@ -1244,11 +1247,14 @@ void > memory_region_init_resizeable_ram(MemoryRegion *mr, > void > *host), > Error **errp) > { > + RAMBlock *ram_block; > + > memory_region_init(mr, owner, name, size); > mr->ram = true; > mr->terminates = true; > mr->destructor = memory_region_destructor_ram; > - mr->ram_addr = qemu_ram_alloc_resizeable(size, max_size, resized, mr, > errp); > + ram_block = qemu_ram_alloc_resizeable(size, max_size, resized, mr, > errp); > + mr->ram_addr = ram_block->offset; > mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0; > } > > @@ -1261,11 +1267,14 @@ void > memory_region_init_ram_from_file(MemoryRegion *mr, > const char *path, > Error **errp) > { > + RAMBlock *ram_block; > + > memory_region_init(mr, owner, name, size); > mr->ram = true; > mr->terminates = true; > mr->destructor = memory_region_destructor_ram; > - mr->ram_addr = qemu_ram_alloc_from_file(size, mr, share, path, errp); > + ram_block = qemu_ram_alloc_from_file(size, mr, share, path, errp); > + mr->ram_addr = ram_block->offset; > mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0; > } > #endif > @@ -1276,6 +1285,8 @@ void memory_region_init_ram_ptr(MemoryRegion > *mr, > uint64_t size, > void *ptr) > { > + RAMBlock *ram_block; > + > memory_region_init(mr, owner, name, size); > mr->ram = true; > mr->terminates = true; > @@ -1284,7 +1295,8 @@ void memory_region_init_ram_ptr(MemoryRegion > *mr, > > /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */ > assert(ptr != NULL); > - mr->ram_addr = qemu_ram_alloc_from_ptr(size, ptr, mr, &error_fatal); > + ram_block = qemu_ram_alloc_from_ptr(size, ptr, mr, &error_fatal); > + mr->ram_addr = ram_block->offset; > } > > void memory_region_set_skip_dump(MemoryRegion *mr) > @@ -1312,13 +1324,16 @@ void > memory_region_init_rom_device(MemoryRegion *mr, > uint64_t size, > Error **errp) > { > + RAMBlock *ram_block; > + > memory_region_init(mr, owner, name, size); > mr->ops = ops; > mr->opaque = opaque; > mr->terminates = true; > mr->rom_device = true; > mr->destructor = memory_region_destructor_rom_device; > - mr->ram_addr = qemu_ram_alloc(size, mr, errp); > + ram_block = qemu_ram_alloc(size, mr, errp); > + mr->ram_addr = ram_block->offset; > } > > void memory_region_init_iommu(MemoryRegion *mr, > -- > 2.4.3 >