Skip to content

Commit

Permalink
vk_mem_alloc.h: Patch for C++14 compilation on Linux, BSDs
Browse files Browse the repository at this point in the history
  • Loading branch information
past-due committed Oct 15, 2023
1 parent baedca4 commit cadedba
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
47 changes: 47 additions & 0 deletions lib/ivis_opengl/3rdparty/vk_mem_alloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# pragma clang diagnostic ignored "-Wmissing-field-initializers"
# pragma clang diagnostic ignored "-Wunused-private-field"
# pragma clang diagnostic ignored "-Wcast-align"
# pragma clang diagnostic ignored "-Wunused-function"
# if defined(__APPLE__)
# pragma clang diagnostic ignored "-Wnullability-completeness" // Warning triggered on newer Xcode
# endif
Expand All @@ -45,17 +46,63 @@
# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
# pragma GCC diagnostic ignored "-Wmissing-noreturn"
# pragma GCC diagnostic ignored "-Wcast-align"
# pragma GCC diagnostic ignored "-Wunused-function"
#elif defined(_MSC_VER)
# pragma warning( push )
# pragma warning( disable : 4189 ) // warning C4189: 'identifier' : local variable is initialized but not referenced
# pragma warning( disable : 4324 ) // warning C4324: 'struct_name' : structure was padded due to alignment specifier
# pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant
# pragma warning( disable : 4505 ) // warning C4505: unreferenced function with internal linkage has been removed
#endif

#define VMA_IMPLEMENTATION
#define VMA_STATIC_VULKAN_FUNCTIONS 0
#define VMA_DYNAMIC_VULKAN_FUNCTIONS 0
#define VMA_ASSERT(expr) ASSERT(expr, "VMA_ASSERT failed")
#include <cstdio>

// The VMA_NULLABLE macro is defined to be _Nullable when compiling with Clang.
// see: https://clang.llvm.org/docs/AttributeReference.html#nullable
#ifndef VMA_NULLABLE
#ifdef __clang__
#define VMA_NULLABLE _Nullable
#else
#define VMA_NULLABLE
#endif
#endif

// WZ Patch for C++14 compilation
#if (__cplusplus >= 201402L && __cplusplus < 201703L)
# if defined(__ANDROID_API__) && (__ANDROID_API__ < 16)
// do not do anything - is handled by vk_mem_alloc
# elif defined(__APPLE__) || defined(__ANDROID__)
// do not do anything - is handled by vk_mem_alloc
# elif defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) || defined(__NetBSD__)
// On C++14, implement for Linux + BSDs (until we can require C++17)
// Reference: https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator/issues/332
#include <cstdlib>
static void* wz_vma_aligned_alloc(size_t alignment, size_t size)
{
// alignment must be >= sizeof(void*)
if(alignment < sizeof(void*))
{
alignment = sizeof(void*);
}

void *pointer;
if(posix_memalign(&pointer, alignment, size) == 0)
return pointer;
return nullptr;
}
static void wz_vma_aligned_free(void* VMA_NULLABLE ptr)
{
free(ptr);
}
# define VMA_SYSTEM_ALIGNED_MALLOC(size, alignment) wz_vma_aligned_alloc((alignment), (size))
# define VMA_SYSTEM_ALIGNED_FREE(ptr) wz_vma_aligned_free(ptr)
# endif
#endif

#include "vk_mem_alloc.h"

#if defined(__clang__)
Expand Down
3 changes: 3 additions & 0 deletions lib/ivis_opengl/3rdparty/vk_mem_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2813,6 +2813,9 @@ static void* vma_aligned_alloc(size_t alignment, size_t size)
#else
static void* vma_aligned_alloc(size_t alignment, size_t size)
{
# ifndef VMA_SYSTEM_ALIGNED_MALLOC
#error No available implementation for vma_aligned_alloc
# endif
VMA_ASSERT(0 && "Could not implement aligned_alloc automatically. Please enable C++17 or later in your compiler or provide custom implementation of macro VMA_SYSTEM_ALIGNED_MALLOC (and VMA_SYSTEM_ALIGNED_FREE if needed) using the API of your system.");
return VMA_NULL;
}
Expand Down

0 comments on commit cadedba

Please sign in to comment.