diff --git a/include/uc_priv.h b/include/uc_priv.h index dc4fcfd6..c196291a 100644 --- a/include/uc_priv.h +++ b/include/uc_priv.h @@ -175,6 +175,7 @@ struct uc_struct { bool block_full; MemoryRegion **mapped_blocks; uint32_t mapped_block_count; + uint32_t mapped_block_cache_index; void *qemu_thread_data; // to support cross compile to Windows (qemu-thread-win32.c) uint32_t target_page_size; uint32_t target_page_align; diff --git a/uc.c b/uc.c index 14fcabb1..732e6d04 100644 --- a/uc.c +++ b/uc.c @@ -798,9 +798,18 @@ MemoryRegion *memory_mapping(struct uc_struct* uc, uint64_t address) { unsigned int i; + // try with the cache index first + i = uc->mapped_block_cache_index; + + if (address >= uc->mapped_blocks[i]->addr && address < uc->mapped_blocks[i]->end) + return uc->mapped_blocks[i]; + for(i = 0; i < uc->mapped_block_count; i++) { - if (address >= uc->mapped_blocks[i]->addr && address < uc->mapped_blocks[i]->end) + if (address >= uc->mapped_blocks[i]->addr && address < uc->mapped_blocks[i]->end) { + // cache this index for the next query + uc->mapped_block_cache_index = i; return uc->mapped_blocks[i]; + } } // not found