From c4cd82fa7c146ad7caca14d199cfdaa9147eeefb Mon Sep 17 00:00:00 2001
From: Jan Beich <jbeich@FreeBSD.org>
Date: Tue, 27 Jul 2021 20:00:09 +0000
Subject: [PATCH 1/2] host_memory: Enable Linux implementation on FreeBSD

HW.Memory <Critical> common/host_memory.cpp:HostMemory:492: Fastmem unavailable, falling back to VirtualBuffer for memory allocation
---
 src/common/host_memory.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/common/host_memory.cpp b/src/common/host_memory.cpp
index 2a5a7596c1..49e58d250e 100644
--- a/src/common/host_memory.cpp
+++ b/src/common/host_memory.cpp
@@ -6,7 +6,7 @@
 #include <windows.h>
 #include "common/dynamic_library.h"
 
-#elif defined(__linux__) // ^^^ Windows ^^^ vvv Linux vvv
+#elif defined(__linux__) || defined(__FreeBSD__) // ^^^ Windows ^^^ vvv Linux vvv
 
 #ifndef _GNU_SOURCE
 #define _GNU_SOURCE
@@ -343,7 +343,7 @@ private:
     std::unordered_map<size_t, size_t> placeholder_host_pointers; ///< Placeholder backing offset
 };
 
-#elif defined(__linux__) // ^^^ Windows ^^^ vvv Linux vvv
+#elif defined(__linux__) || defined(__FreeBSD__) // ^^^ Windows ^^^ vvv Linux vvv
 
 class HostMemory::Impl {
 public:

From 353be2306c7b3b640755fbf9b95aa0c75c6a7cfe Mon Sep 17 00:00:00 2001
From: Jan Beich <jbeich@FreeBSD.org>
Date: Tue, 27 Jul 2021 20:06:25 +0000
Subject: [PATCH 2/2] host_memory: Add workaround for FreeBSD 12

src/common/host_memory.cpp:360:14: error: use of undeclared identifier
      'memfd_create'
        fd = memfd_create("HostMemory", 0);
             ^
---
 src/common/host_memory.cpp | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/common/host_memory.cpp b/src/common/host_memory.cpp
index 49e58d250e..6661244cfb 100644
--- a/src/common/host_memory.cpp
+++ b/src/common/host_memory.cpp
@@ -357,7 +357,12 @@ public:
         });
 
         // Backing memory initialization
+#if defined(__FreeBSD__) && __FreeBSD__ < 13
+        // XXX Drop after FreeBSD 12.* reaches EOL on 2024-06-30
+        fd = shm_open(SHM_ANON, O_RDWR, 0600);
+#else
         fd = memfd_create("HostMemory", 0);
+#endif
         if (fd == -1) {
             LOG_CRITICAL(HW_Memory, "memfd_create failed: {}", strerror(errno));
             throw std::bad_alloc{};