Skip to content

Commit

Permalink
Merge pull request #404 from marzer/msvc-callconv
Browse files Browse the repository at this point in the history
Added `MESHOPTIMIZER_ALLOC_CALLCONV` (fixes #403)
  • Loading branch information
zeux authored Jan 30, 2022
2 parents f8a03cc + f4b4105 commit 70b6cc3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/allocator.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file is part of meshoptimizer library; see meshoptimizer.h for version/license details
#include "meshoptimizer.h"

void meshopt_setAllocator(void* (*allocate)(size_t), void (*deallocate)(void*))
void meshopt_setAllocator(void* (MESHOPTIMIZER_ALLOC_CALLCONV *allocate)(size_t), void (MESHOPTIMIZER_ALLOC_CALLCONV *deallocate)(void*))
{
meshopt_Allocator::Storage::allocate = allocate;
meshopt_Allocator::Storage::deallocate = deallocate;
Expand Down
21 changes: 15 additions & 6 deletions src/meshoptimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
#define MESHOPTIMIZER_API
#endif

/* Set the calling-convention for alloc/dealloc function pointers */
#ifndef MESHOPTIMIZER_ALLOC_CALLCONV
#ifdef _MSC_VER
#define MESHOPTIMIZER_ALLOC_CALLCONV __cdecl
#else
#define MESHOPTIMIZER_ALLOC_CALLCONV
#endif
#endif

/* Experimental APIs have unstable interface and might have implementation that's not fully tested or optimized */
#define MESHOPTIMIZER_EXPERIMENTAL MESHOPTIMIZER_API

Expand Down Expand Up @@ -348,7 +357,7 @@ MESHOPTIMIZER_EXPERIMENTAL size_t meshopt_simplifyPoints(unsigned int* destinati

/**
* Experimental: Returns the error scaling factor used by the simplifier to convert between absolute and relative extents
*
*
* Absolute error must be *divided* by the scaling factor before passing it to meshopt_simplify as target_error
* Relative error returned by meshopt_simplify via result_error must be *multiplied* by the scaling factor to get absolute error.
*/
Expand Down Expand Up @@ -514,7 +523,7 @@ MESHOPTIMIZER_EXPERIMENTAL void meshopt_spatialSortTriangles(unsigned int* desti
* Note that all algorithms only allocate memory for temporary use.
* allocate/deallocate are always called in a stack-like order - last pointer to be allocated is deallocated first.
*/
MESHOPTIMIZER_API void meshopt_setAllocator(void* (*allocate)(size_t), void (*deallocate)(void*));
MESHOPTIMIZER_API void meshopt_setAllocator(void* (MESHOPTIMIZER_ALLOC_CALLCONV *allocate)(size_t), void (MESHOPTIMIZER_ALLOC_CALLCONV *deallocate)(void*));

#ifdef __cplusplus
} /* extern "C" */
Expand Down Expand Up @@ -696,8 +705,8 @@ class meshopt_Allocator
template <typename T>
struct StorageT
{
static void* (*allocate)(size_t);
static void (*deallocate)(void*);
static void* (MESHOPTIMIZER_ALLOC_CALLCONV *allocate)(size_t);
static void (MESHOPTIMIZER_ALLOC_CALLCONV *deallocate)(void*);
};

typedef StorageT<void> Storage;
Expand Down Expand Up @@ -728,8 +737,8 @@ class meshopt_Allocator
};

// This makes sure that allocate/deallocate are lazily generated in translation units that need them and are deduplicated by the linker
template <typename T> void* (*meshopt_Allocator::StorageT<T>::allocate)(size_t) = operator new;
template <typename T> void (*meshopt_Allocator::StorageT<T>::deallocate)(void*) = operator delete;
template <typename T> void* (MESHOPTIMIZER_ALLOC_CALLCONV *meshopt_Allocator::StorageT<T>::allocate)(size_t) = operator new;
template <typename T> void (MESHOPTIMIZER_ALLOC_CALLCONV *meshopt_Allocator::StorageT<T>::deallocate)(void*) = operator delete;
#endif

/* Inline implementation for C++ templated wrappers */
Expand Down

0 comments on commit 70b6cc3

Please sign in to comment.