Add allocator impl

This commit is contained in:
Kim Kulling
2026-03-02 16:42:45 +01:00
parent 56414694b3
commit d403434c76
4 changed files with 57 additions and 15 deletions

View File

@@ -58,6 +58,7 @@ SET( COMPILER_HEADERS
SOURCE_GROUP( Compiler FILES ${COMPILER_HEADERS})
SET( PUBLIC_HEADERS
${HEADER_PATH}/allocator.h
${HEADER_PATH}/anim.h
${HEADER_PATH}/aabb.h
${HEADER_PATH}/ai_assert.h
@@ -167,6 +168,7 @@ SET( Logging_SRCS
SOURCE_GROUP(Logging FILES ${Logging_SRCS})
SET( Common_SRCS
Common/allocator.cpp
Common/StbCommon.h
Common/Compression.cpp
Common/Compression.h

View File

@@ -69,7 +69,7 @@ using namespace Assimp;
namespace Assimp {
// underlying structure for aiPropertyStore
using PropertyMap = BatchLoader::PropertyMap ;
using PropertyMap = BatchLoader::PropertyMap ;
#if defined(__has_warning)
# if __has_warning("-Wordered-compare-function-pointers")

12
code/Common/allocator.cpp Normal file
View File

@@ -0,0 +1,12 @@
#include <assimp/allocator.h>
aiAllocator gAllocatoMod;
void *aiAlloc(size_t size) {
return aiAllocator::allocFunc(size);
}
void aiFree(void *ptr) {
aiAllocator::freeFunc(ptr);
}

View File

@@ -42,28 +42,56 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef AI_ALLOCATOR_H_INC
#define AI_ALLOCATOR_H_INC
#include <assimp/types.h>
#include <stddef.h>
#include <stdlib.h>
#include <memory.h>
typedef void* (*alloc_func)(size_t size);
typedef void (*free_func)(void* ptr);
struct Allocator {
static alloc_func alloc{nullptr};
static free_func free{nullptr};
static void init(alloc_func alloc_fn, free_func free_fn) {
alloc = alloc_fn;
free = free_fn;
}
};
static void aiRegisterAllocator(alloc_func alloc_fn, free_func free_fn) {
Allocator::init(alloc_fn, free_fn);
static void *aiDefaultAlloc(size_t size) {
return malloc(size);
}
#define AI_NEW(ai_type,...) new ai_type(__VA_ARGS__)
#define AI_NEW_ARRAY(ai_type,count) new ai_type[count]()
static void aiDefaultFree(void *ptr) {
free(ptr);
}
struct aiAllocator {
static alloc_func allocFunc;
static free_func freeFunc;
static void DefaultInit() {
allocFunc = aiDefaultAlloc;
freeFunc = aiDefaultFree;
}
static void init(alloc_func alloc_fn, free_func free_fn) {
allocFunc = alloc_fn;
freeFunc = free_fn;
}
} gAllocatoMod;
static void aiRegisterAllocator(alloc_func alloc_fn, free_func free_fn) {
aiAllocator::init(alloc_fn, free_fn);
}
ASSIMP_API void *aiAlloc(size_t size);
ASSIMP_API void aiFree(void *ptr);
#ifdef __cplusplus
extern "C" {
#endif
#define AI_NEW(ai_type, ...) new ai_type(__VA_ARGS__)
#define AI_NEW_ARRAY(ai_type, count) new ai_type[count]()
#define AI_DELETE(ptr) delete ptr
#define AI_DELETE_ARRAY(ptr) delete[] ptr
#ifdef __cplusplus
}
#endif
#endif // AI_ALLOCATOR_H_INC