#define _GNU_SOURCE 1 // strcasestr
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdint.h>
 #include <string.h>
 #include <strings.h>
 #ifdef __MACH__
 #include "3ds/3ds_utils.h"
 #endif
 
+#ifndef MAP_FAILED
+#define MAP_FAILED      ((void *)(intptr_t)-1)
+#endif
+
 #define PORTS_NUMBER 8
 
 #ifndef MIN
 static u32 mapped_ram, mapped_ram_src;
 
 // http://3dbrew.org/wiki/Memory_layout#ARM11_User-land_memory_regions
-void *pl_3ds_mmap(unsigned long addr, size_t size,
+static void *pl_3ds_mmap(unsigned long addr, size_t size,
     enum psxMapTag tag, int *can_retry_addr)
 {
-   (void)addr;
+   void *ret = MAP_FAILED;
    *can_retry_addr = 0;
+   (void)addr;
 
    if (__ctr_svchax) do
    {
       u32 found_addr = 0;
       MemInfo mem_info;
       PageInfo page_info;
-      void *ret = NULL;
       size_t i;
       int r;
 
    }
    while (0);
 
-   return calloc(size, 1);
+   ret = calloc(size, 1);
+   return ret ? ret : MAP_FAILED;
 }
 
-void pl_3ds_munmap(void *ptr, size_t size, enum psxMapTag tag)
+static void pl_3ds_munmap(void *ptr, size_t size, enum psxMapTag tag)
 {
    (void)tag;
 
 
 static void *addr = NULL;
 
-psx_map_t custom_psx_maps[] = {
+static psx_map_t custom_psx_maps[] = {
    { NULL, 0x800000, MAP_TAG_LUTS },
    { NULL, 0x080000, MAP_TAG_OTHER },
    { NULL, 0x010000, MAP_TAG_OTHER },
    { NULL, 0x210000, MAP_TAG_RAM },
 };
 
-int init_vita_mmap()
+static int init_vita_mmap()
 {
    int n;
    void *tmpaddr;
    return 0;
 }
 
-void deinit_vita_mmap()
+static void deinit_vita_mmap()
 {
    size_t i;
    for (i = 0; i < sizeof(custom_psx_maps) / sizeof(custom_psx_maps[0]); i++) {
    free(addr);
 }
 
-void *pl_vita_mmap(unsigned long addr, size_t size,
+static void *pl_vita_mmap(unsigned long addr, size_t size,
     enum psxMapTag tag, int *can_retry_addr)
 {
+   void *ret;
    (void)addr;
    *can_retry_addr = 0;
 
       }
    }
 
-   return calloc(size, 1);
+   ret = calloc(size, 1);
+   return ret ? ret : MAP_FAILED;
 }
 
-void pl_vita_munmap(void *ptr, size_t size, enum psxMapTag tag)
+static void pl_vita_munmap(void *ptr, size_t size, enum psxMapTag tag)
 {
    (void)tag;
 
    if (vout_buf == NULL)
    {
       LogErr("OOM for vout_buf.\n");
-      exit(1);
+      // may be able to continue if we get retro_framebuffer access
    }
 
    vout_buf_ptr = vout_buf;
 
        }
 
 basic_map:
-       retval = plat_mmap(addr, size, 0, is_fixed);
+       retval = plat_mmap(addr, size, 0, 0);
 
 out:
-       if (tag == MAP_TAG_VRAM)
+       if (tag == MAP_TAG_VRAM && retval)
                psx_vram = retval;
-       return retval;
+       return retval ? retval : MAP_FAILED;
 }
 
 static void pl_emu_munmap(void *ptr, size_t size, enum psxMapTag tag)
 
                if (ret != MAP_FAILED)
                        psxUnmap(ret, size, tag);
                ret = psxMapHook(addr, size, tag, &can_retry_addr);
-               if (ret == NULL)
+               if (ret == MAP_FAILED)
                        return MAP_FAILED;
 
                if (addr != 0 && ret != (void *)(uintptr_t)addr) {
-                       SysMessage("psxMap: warning: wanted to map @%08x, got %p\n",
+                       SysMessage("psxMap: tried to map @%08x, got %p\n",
                                addr, ret);
                        if (is_fixed) {
                                psxUnmap(ret, size, tag);
 
   // to be able to reuse 1024-width code better (triangle setup,
   // dithering phase, lines).
   egpu.enhancement_buf_ptr = gpu.mmap(ENHANCEMENT_BUF_SIZE);
-  if (egpu.enhancement_buf_ptr == NULL) {
+  if (egpu.enhancement_buf_ptr == NULL || egpu.enhancement_buf_ptr == (void *)(intptr_t)-1) {
     fprintf(stderr, "failed to map enhancement buffer\n");
+    egpu.enhancement_buf_ptr = NULL;
     gpu.get_enhancement_bufer = NULL;
   }
   else {