v1.6 Custom allocators API
You can configure enkiTS with custom allocators using the
Developed based on request Feature suggestion: custom allocators #41 by @sergeyreznik.
In addition you can now sponsor enkiTS through Github Sponsors, and to boost community funding, GitHub will match your contribution!
Custom Allocator usage in C++:
- full example in example/CustomAllocator.cpp
- C example in example/CustomAllocator_c.c
#include "TaskScheduler.h"
#include <stdio.h>
#include <thread>
using namespace enki;
TaskScheduler g_TS;
struct ParallelTaskSet : ITaskSet
{
virtual void ExecuteRange( TaskSetPartition range_, uint32_t threadnum_ )
{
printf(" This could run on any thread, currently thread %d\n", threadnum_);
}
};
struct CustomData
{
const char* domainName;
size_t totalAllocations;
};
void* CustomAllocFunc( size_t align_, size_t size_, void* userData_, const char* file_, int line_ )
{
CustomData* data = (CustomData*)userData_;
data->totalAllocations += size_;
printf("Allocating %g bytes in domain %s, total %g. File %s, line %d.\n",
(double)size_, data->domainName, (double)data->totalAllocations, file_, line_ );
return DefaultAllocFunc( align_, size_, userData_, file_, line_ );
};
void CustomFreeFunc( void* ptr_, size_t size_, void* userData_, const char* file_, int line_ )
{
CustomData* data = (CustomData*)userData_;
data->totalAllocations -= size_;
printf("Freeing %p in domain %s, total %g. File %s, line %d.\n",
ptr_, data->domainName, (double)data->totalAllocations, file_, line_ );
DefaultFreeFunc( ptr_, size_, userData_, file_, line_ );
};
int main(int argc, const char * argv[])
{
enki::TaskSchedulerConfig config;
config.customAllocator.alloc = CustomAllocFunc;
config.customAllocator.free = CustomFreeFunc;
CustomData data{ "enkITS", 0 };
config.customAllocator.userData = &data;
g_TS.Initialize( config );
ParallelTaskSet task;
g_TS.AddTaskSetToPipe( &task );
g_TS.WaitforTask( &task );
g_TS.WaitforAllAndShutdown(); // ensure we shutdown before user data is destroyed.
return 0;
}