Function
uni_setmemfunc
Register a memory allocator.
Parameters 🔗
| user_data | in | User pointer passed to unimemfunc. |
| allocf | in | Memory management routine. |
Return Value 🔗
| UNI_OK | If the memory allocator was modified. |
| UNI_FEATURE_DISABLED | If |
Discussion 🔗
Sets allocf as the implementation for dynamic memory allocations. If allocf is null then the implementation reverts to its default allocator which may be the C standard allocator or a dummy allocator that always fails to allocate memory. The latter is only present when the C standard library allocators are disabled.
Support for C standard library allocators must be enabled in the JSON configuration file. If the standard allocators are not enabled, then dummy allocators that always return NULL are used. In this case, a custom allocator must be supplied.
{
"hasStandardAllocators": true
}
Examples 🔗
This example registers a custom memory allocator with Unicorn. In the example, the custom allocator wraps the C standard memory allocators, however, this is redundant because Unicorn already initializes with a custom allocator that wraps the C standard allocators. They are wrapped here for example purposes only. An actual implementation would implement and wrap its own custom memory allocator.
#include <unicorn.h>
#include <stdlib.h>
static void *allocfree(void *ud, void *ptr, size_t osize, size_t nsize)
{
if (nsize == 0)
{
free(ptr);
return NULL;
}
else
{
return realloc(ptr, nsize);
}
}
int main(void)
{
uni_setmemfunc(NULL, allocfree);
return 0;
}