From: Paul Cercueil Date: Sat, 11 Feb 2023 13:26:15 +0000 (+0000) Subject: psxmem: Use POSIX mmap functions as default hooks X-Git-Tag: r24l~308 X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=08621fe59becb20dc0b653fc98b797eea4d9f244;p=pcsx_rearmed.git psxmem: Use POSIX mmap functions as default hooks Simplify the code by using the POSIX mmap functions as the default psxMapHook / psxUnmapHook functions. Signed-off-by: Paul Cercueil --- diff --git a/libpcsxcore/psxmem.c b/libpcsxcore/psxmem.c index fb48f77b..54219ae0 100644 --- a/libpcsxcore/psxmem.c +++ b/libpcsxcore/psxmem.c @@ -41,34 +41,36 @@ #define MAP_ANONYMOUS MAP_ANON #endif +static void * psxMapDefault(unsigned long addr, size_t size, + int is_fixed, enum psxMapTag tag) +{ + int flags = MAP_PRIVATE | MAP_ANONYMOUS; + + return mmap((void *)(uintptr_t)addr, size, + PROT_READ | PROT_WRITE, flags, -1, 0); +} + +static void psxUnmapDefault(void *ptr, size_t size, enum psxMapTag tag) +{ + munmap(ptr, size); +} + void *(*psxMapHook)(unsigned long addr, size_t size, int is_fixed, - enum psxMapTag tag); -void (*psxUnmapHook)(void *ptr, size_t size, enum psxMapTag tag); + enum psxMapTag tag) = psxMapDefault; +void (*psxUnmapHook)(void *ptr, size_t size, + enum psxMapTag tag) = psxUnmapDefault; void *psxMap(unsigned long addr, size_t size, int is_fixed, enum psxMapTag tag) { - int flags = MAP_PRIVATE | MAP_ANONYMOUS; int try_ = 0; unsigned long mask; - void *req, *ret; + void *ret; retry: - if (psxMapHook != NULL) { - ret = psxMapHook(addr, size, 0, tag); - if (ret == NULL) - return MAP_FAILED; - } - else { - /* avoid MAP_FIXED, it overrides existing mappings.. */ - /* if (is_fixed) - flags |= MAP_FIXED; */ - - req = (void *)(uintptr_t)addr; - ret = mmap(req, size, PROT_READ | PROT_WRITE, flags, -1, 0); - if (ret == MAP_FAILED) - return ret; - } + ret = psxMapHook(addr, size, 0, tag); + if (ret == NULL) + return MAP_FAILED; if (addr != 0 && ret != (void *)(uintptr_t)addr) { SysMessage("psxMap: warning: wanted to map @%08x, got %p\n", @@ -97,13 +99,7 @@ retry: void psxUnmap(void *ptr, size_t size, enum psxMapTag tag) { - if (psxUnmapHook != NULL) { - psxUnmapHook(ptr, size, tag); - return; - } - - if (ptr) - munmap(ptr, size); + psxUnmapHook(ptr, size, tag); } s8 *psxM = NULL; // Kernel & User Memory (2 Meg)