diff --git a/core/hw/mem/_vmem.cpp b/core/hw/mem/_vmem.cpp index 00d0c04db..5febf743d 100644 --- a/core/hw/mem/_vmem.cpp +++ b/core/hw/mem/_vmem.cpp @@ -481,9 +481,10 @@ void _vmem_init_mappings() { _vmem_term_mappings(); // Fallback to statically allocated buffers, this results in slow-ops being generated. - if (vmemstatus == MemTypeError) { + if (vmemstatus == MemTypeError) + { WARN_LOG(VMEM, "Warning! nvmem is DISABLED (due to failure or not being built-in"); - virt_ram_base = 0; + virt_ram_base = nullptr; // Allocate it all and initialize it. p_sh4rcb = (Sh4RCB*)malloc_pages(sizeof(Sh4RCB)); @@ -595,10 +596,15 @@ void _vmem_init_mappings() #define freedefptr(x) \ if (x) { free(x); x = NULL; } -void _vmem_release() { +void _vmem_release() +{ if (virt_ram_base) + { vmem_platform_destroy(); - else { + virt_ram_base = nullptr; + } + else + { _vmem_unprotect_vram(0, VRAM_SIZE); freedefptr(p_sh4rcb); freedefptr(vram.data); diff --git a/core/linux/posix_vmem.cpp b/core/linux/posix_vmem.cpp index e05344fe1..8f122494d 100644 --- a/core/linux/posix_vmem.cpp +++ b/core/linux/posix_vmem.cpp @@ -17,19 +17,11 @@ #include "stdclass.h" #ifndef MAP_NOSYNC -#define MAP_NOSYNC 0 //missing from linux :/ -- could be the cause of android slowness ? +#define MAP_NOSYNC 0 #endif #ifdef __ANDROID__ - #include - #ifndef ASHMEM_DEVICE - #define ASHMEM_DEVICE "/dev/ashmem" - #undef PAGE_MASK - #define PAGE_MASK (PAGE_SIZE-1) - #else - #define PAGE_SIZE 4096 - #define PAGE_MASK (PAGE_SIZE-1) - #endif +#include // Only available in SDK 26+. Required in SDK 29+ (android 10) extern "C" int __attribute__((weak)) ASharedMemory_create(const char*, size_t); @@ -37,16 +29,22 @@ extern "C" int __attribute__((weak)) ASharedMemory_create(const char*, size_t); // Android specific ashmem-device stuff for creating shared memory regions int ashmem_create_region(const char *name, size_t size) { + int fd = -1; if (ASharedMemory_create != nullptr) - return ASharedMemory_create(name, size); + { + fd = ASharedMemory_create(name, size); + if (fd < 0) + WARN_LOG(VMEM, "ASharedMemory_create failed: errno %d", errno); + } - int fd = open(ASHMEM_DEVICE, O_RDWR); if (fd < 0) - return -1; - - if (ioctl(fd, ASHMEM_SET_SIZE, size) < 0) { - close(fd); - return -1; + { + fd = open("/dev/ashmem", O_RDWR); + if (fd >= 0 && ioctl(fd, ASHMEM_SET_SIZE, size) < 0) + { + close(fd); + fd = -1; + } } return fd; @@ -133,17 +131,18 @@ static int allocate_shared_filemem(unsigned size) { fd = open(path.c_str(), O_CREAT|O_RDWR|O_TRUNC, S_IRWXU|S_IRWXG|S_IRWXO); unlink(path.c_str()); } - // If we can't open the file, fallback to slow mem. - if (fd < 0) - return -1; - - // Finally make the file as big as we need! - if (ftruncate(fd, size)) { - // Can't get as much memory as needed, fallback. - close(fd); - return -1; + if (fd >= 0) + { + // Finally make the file as big as we need! + if (ftruncate(fd, size)) { + // Can't get as much memory as needed, fallback. + close(fd); + fd = -1; + } } #endif + if (fd < 0) + WARN_LOG(VMEM, "Virtual memory file allocation failed: errno %d", errno); return fd; }