exec: remove code duplication in qemu_ram_alloc() and qemu_ram_alloc_from_ptr()
Since most of the code in qemu_ram_alloc() and qemu_ram_alloc_from_ptr() are duplicated, let qemu_ram_alloc_from_ptr() to switch by checking void *host, and change qemu_ram_alloc() to a wrapper. Signed-off-by: Yoshiaki Tamura <tamura.yoshiaki@lab.ntt.co.jp> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
		
							parent
							
								
									9742bf26b1
								
							
						
					
					
						commit
						6977dfe6af
					
				
							
								
								
									
										50
									
								
								exec.c
									
									
									
									
									
								
							
							
						
						
									
										50
									
								
								exec.c
									
									
									
									
									
								
							@ -2833,48 +2833,9 @@ ram_addr_t qemu_ram_alloc_from_ptr(DeviceState *dev, const char *name,
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (host) {
 | 
				
			||||||
        new_block->host = host;
 | 
					        new_block->host = host;
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
    new_block->offset = find_ram_offset(size);
 | 
					 | 
				
			||||||
    new_block->length = size;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    QLIST_INSERT_HEAD(&ram_list.blocks, new_block, next);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    ram_list.phys_dirty = qemu_realloc(ram_list.phys_dirty,
 | 
					 | 
				
			||||||
                                       last_ram_offset() >> TARGET_PAGE_BITS);
 | 
					 | 
				
			||||||
    memset(ram_list.phys_dirty + (new_block->offset >> TARGET_PAGE_BITS),
 | 
					 | 
				
			||||||
           0xff, size >> TARGET_PAGE_BITS);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (kvm_enabled())
 | 
					 | 
				
			||||||
        kvm_setup_guest_memory(new_block->host, size);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return new_block->offset;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size)
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    RAMBlock *new_block, *block;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    size = TARGET_PAGE_ALIGN(size);
 | 
					 | 
				
			||||||
    new_block = qemu_mallocz(sizeof(*new_block));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
 | 
					 | 
				
			||||||
        char *id = dev->parent_bus->info->get_dev_path(dev);
 | 
					 | 
				
			||||||
        if (id) {
 | 
					 | 
				
			||||||
            snprintf(new_block->idstr, sizeof(new_block->idstr), "%s/", id);
 | 
					 | 
				
			||||||
            qemu_free(id);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    pstrcat(new_block->idstr, sizeof(new_block->idstr), name);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    QLIST_FOREACH(block, &ram_list.blocks, next) {
 | 
					 | 
				
			||||||
        if (!strcmp(block->idstr, new_block->idstr)) {
 | 
					 | 
				
			||||||
            fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n",
 | 
					 | 
				
			||||||
                    new_block->idstr);
 | 
					 | 
				
			||||||
            abort();
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (mem_path) {
 | 
					        if (mem_path) {
 | 
				
			||||||
#if defined (__linux__) && !defined(TARGET_S390X)
 | 
					#if defined (__linux__) && !defined(TARGET_S390X)
 | 
				
			||||||
            new_block->host = file_ram_alloc(new_block, size, mem_path);
 | 
					            new_block->host = file_ram_alloc(new_block, size, mem_path);
 | 
				
			||||||
@ -2901,6 +2862,8 @@ ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size)
 | 
				
			|||||||
            madvise(new_block->host, size, MADV_MERGEABLE);
 | 
					            madvise(new_block->host, size, MADV_MERGEABLE);
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    new_block->offset = find_ram_offset(size);
 | 
					    new_block->offset = find_ram_offset(size);
 | 
				
			||||||
    new_block->length = size;
 | 
					    new_block->length = size;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -2917,6 +2880,11 @@ ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size)
 | 
				
			|||||||
    return new_block->offset;
 | 
					    return new_block->offset;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    return qemu_ram_alloc_from_ptr(dev, name, size, NULL);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void qemu_ram_free(ram_addr_t addr)
 | 
					void qemu_ram_free(ram_addr_t addr)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    RAMBlock *block;
 | 
					    RAMBlock *block;
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user