android: fallback to /dev/ashmem if ASharedMemory fails. More logs

Issue #439
This commit is contained in:
Flyinghead 2021-12-15 23:09:59 +01:00
parent 5a6197cfc6
commit 04be40767b
2 changed files with 35 additions and 30 deletions

View File

@ -481,9 +481,10 @@ void _vmem_init_mappings()
{ {
_vmem_term_mappings(); _vmem_term_mappings();
// Fallback to statically allocated buffers, this results in slow-ops being generated. // 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"); 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. // Allocate it all and initialize it.
p_sh4rcb = (Sh4RCB*)malloc_pages(sizeof(Sh4RCB)); p_sh4rcb = (Sh4RCB*)malloc_pages(sizeof(Sh4RCB));
@ -595,10 +596,15 @@ void _vmem_init_mappings()
#define freedefptr(x) \ #define freedefptr(x) \
if (x) { free(x); x = NULL; } if (x) { free(x); x = NULL; }
void _vmem_release() { void _vmem_release()
{
if (virt_ram_base) if (virt_ram_base)
{
vmem_platform_destroy(); vmem_platform_destroy();
else { virt_ram_base = nullptr;
}
else
{
_vmem_unprotect_vram(0, VRAM_SIZE); _vmem_unprotect_vram(0, VRAM_SIZE);
freedefptr(p_sh4rcb); freedefptr(p_sh4rcb);
freedefptr(vram.data); freedefptr(vram.data);

View File

@ -17,19 +17,11 @@
#include "stdclass.h" #include "stdclass.h"
#ifndef MAP_NOSYNC #ifndef MAP_NOSYNC
#define MAP_NOSYNC 0 //missing from linux :/ -- could be the cause of android slowness ? #define MAP_NOSYNC 0
#endif #endif
#ifdef __ANDROID__ #ifdef __ANDROID__
#include <linux/ashmem.h> #include <linux/ashmem.h>
#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
// Only available in SDK 26+. Required in SDK 29+ (android 10) // Only available in SDK 26+. Required in SDK 29+ (android 10)
extern "C" int __attribute__((weak)) ASharedMemory_create(const char*, size_t); 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 // Android specific ashmem-device stuff for creating shared memory regions
int ashmem_create_region(const char *name, size_t size) int ashmem_create_region(const char *name, size_t size)
{ {
int fd = -1;
if (ASharedMemory_create != nullptr) 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) if (fd < 0)
return -1; {
fd = open("/dev/ashmem", O_RDWR);
if (ioctl(fd, ASHMEM_SET_SIZE, size) < 0) { if (fd >= 0 && ioctl(fd, ASHMEM_SET_SIZE, size) < 0)
close(fd); {
return -1; close(fd);
fd = -1;
}
} }
return fd; 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); fd = open(path.c_str(), O_CREAT|O_RDWR|O_TRUNC, S_IRWXU|S_IRWXG|S_IRWXO);
unlink(path.c_str()); unlink(path.c_str());
} }
// If we can't open the file, fallback to slow mem. if (fd >= 0)
if (fd < 0) {
return -1; // Finally make the file as big as we need!
if (ftruncate(fd, size)) {
// Finally make the file as big as we need! // Can't get as much memory as needed, fallback.
if (ftruncate(fd, size)) { close(fd);
// Can't get as much memory as needed, fallback. fd = -1;
close(fd); }
return -1;
} }
#endif #endif
if (fd < 0)
WARN_LOG(VMEM, "Virtual memory file allocation failed: errno %d", errno);
return fd; return fd;
} }