psxmem: Use POSIX mmap functions as default hooks
authorPaul Cercueil <paul@crapouillou.net>
Sat, 11 Feb 2023 13:26:15 +0000 (13:26 +0000)
committernotaz <notasas@gmail.com>
Wed, 12 Jul 2023 22:25:45 +0000 (01:25 +0300)
Simplify the code by using the POSIX mmap functions as the default
psxMapHook / psxUnmapHook functions.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
libpcsxcore/psxmem.c

index fb48f77..54219ae 100644 (file)
 #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)