From e5f7a2ef0bd688951a2335a5290a0a82a8f28844 Mon Sep 17 00:00:00 2001
From: Tobias Frisch <tfrisch@uni-koblenz.de>
Date: Mon, 12 Jul 2021 22:33:39 +0200
Subject: [PATCH] [#93] Adjusted the aligned allocation and mutex of VMA using
 MinGW

Signed-off-by: Tobias Frisch <tfrisch@uni-koblenz.de>
---
 config/lib/vma/vma.cpp | 47 ++++++++++++++++++++++++++++++++++--------
 1 file changed, 38 insertions(+), 9 deletions(-)

diff --git a/config/lib/vma/vma.cpp b/config/lib/vma/vma.cpp
index d7c184dc..c21d5003 100644
--- a/config/lib/vma/vma.cpp
+++ b/config/lib/vma/vma.cpp
@@ -4,24 +4,53 @@
 #endif
 
 #ifndef _MSVC_LANG
+#ifdef __MINGW32__
 #include <stdlib.h>
 
-#ifdef __MINGW32__
 class VmaMutex {
 public:
-	void Lock() {} // TODO: This should actually lock!
-	void Unlock() {} // TODO: This should actually unlock!
+	VmaMutex() : m_locked(false) {}
+	
+	void Lock() {
+		while (m_locked);
+		m_locked = true;
+	}
+	
+	void Unlock() {
+		m_locked = false;
+	}
+private:
+	bool m_locked;
 };
 
 #define VMA_MUTEX VmaMutex
 
-// TODO: This is not actually a valid way to do aligned allocations!
-#define VMA_SYSTEM_ALIGNED_MALLOC(size, alignment) (malloc(size))
-#else
-#define VMA_SYSTEM_ALIGNED_MALLOC(size, alignment) (aligned_alloc((alignment), (size)))
-#endif
+template <typename T>
+T* custom_overestimate_malloc(size_t size) {
+	return new T[size + (sizeof(T) - 1) / sizeof(T)];
+}
 
-#define VMA_SYSTEM_FREE(ptr) free(ptr)
+void* custom_aligned_malloc(size_t alignment, size_t size) {
+	if (alignment > 4) {
+		return custom_overestimate_malloc<u_int64_t>(size);
+	} else
+	if (alignment > 2) {
+		return custom_overestimate_malloc<u_int32_t>(size);
+	} else
+	if (alignment > 1) {
+		return custom_overestimate_malloc<u_int16_t>(size);
+	} else {
+		return custom_overestimate_malloc<u_int8_t>(size);
+	}
+}
+
+void custom_free(void *ptr) {
+	delete[] reinterpret_cast<u_int8_t*>(ptr);
+}
+
+#define VMA_SYSTEM_ALIGNED_MALLOC(size, alignment) (custom_aligned_malloc(alignment, size))
+#define VMA_SYSTEM_FREE(ptr) (custom_free(ptr))
+#endif
 #endif
 
 #define VMA_IMPLEMENTATION
-- 
GitLab