add a thp-based huge page alloc fallback
authornotaz <notasas@gmail.com>
Sun, 1 Oct 2023 22:24:50 +0000 (01:24 +0300)
committernotaz <notasas@gmail.com>
Sun, 1 Oct 2023 22:33:47 +0000 (01:33 +0300)
frontend/libpicofe
libpcsxcore/psxmem.c

index 5dd225e..b0ce6fa 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 5dd225ecd6d5a04fd8e6f16c8f8ee65ee88c6fed
+Subproject commit b0ce6fa8bd3c171debd5589f3ee8a95e26b1d61b
index 42755e5..389bdba 100644 (file)
 static void * psxMapDefault(unsigned long addr, size_t size,
                            int is_fixed, enum psxMapTag tag)
 {
-#if !P_HAVE_MMAP
        void *ptr;
-
-       ptr = malloc(size);
+#if !P_HAVE_MMAP
+       ptr = calloc(1, size);
        return ptr ? ptr : MAP_FAILED;
 #else
        int flags = MAP_PRIVATE | MAP_ANONYMOUS;
 
-       return mmap((void *)(uintptr_t)addr, size,
+       ptr = mmap((void *)(uintptr_t)addr, size,
                    PROT_READ | PROT_WRITE, flags, -1, 0);
+#ifdef MADV_HUGEPAGE
+       if (size >= 2*1024*1024) {
+               if (ptr != MAP_FAILED && ((uintptr_t)ptr & (2*1024*1024 - 1))) {
+                       // try to manually realign assuming bottom-to-top alloc
+                       munmap(ptr, size);
+                       addr = (uintptr_t)ptr & ~(2*1024*1024 - 1);
+                       ptr = mmap((void *)(uintptr_t)addr, size,
+                               PROT_READ | PROT_WRITE, flags, -1, 0);
+               }
+               if (ptr != MAP_FAILED)
+                       madvise(ptr, size, MADV_HUGEPAGE);
+       }
+#endif
+       return ptr;
 #endif
 }