Memory allocation in Qt Quick Ultralite platform abstraction

All memory allocations from the Qt Quick Ultralite core libary use a dedicated set of functions, enabling you to handle dynamic memory allocation requests depending on the platform you are porting to.

Memory allocation

Essentially, these three functions must be implemented to handle all requirements.

Most likely you can just forward these calls to the platform-specific functions that are provided by the SDK for the platform, like shown below.

void *qul_malloc(std::size_t size)
    return std::malloc(size);

void qul_free(void *ptr)

void *qul_realloc(void *ptr, size_t s)
    return std::realloc(ptr, s);

Memory statistics

The platform offers some functions for getting memory statistics like heap and stack sizes.

The Qul::Platform::printHeapStats and Qul::Platform::printStackStats functions must be implemented to print such information. You could use the malloc_stats() function, if it is available on your platform.

void printHeapStats(void)
    malloc_stats(); // gcc function prining stats to standard error

void printStackStats(void)
    // Replace this with actual measuring for your platform
    uint32_t maxUsedStackSize = 0;
    fprintf(stderr, "max used stack size: %u\r\n", maxUsedStackSize);

Fast memory allocations

For short term allocation of memory use Qul::Platform::StackAllocator, which is faster than a regular allocation. A usage example can be found in its class documentation.

Preallocate some static memory to prepare the allocator. This is done by reserving a static amount of memory and initializing the StackAllocator's static members.

static char qul_scratch_buffer[16 * 1024];

char *StackAllocator::m_buffer = qul_scratch_buffer;
char *StackAllocator::m_top = StackAllocator::m_buffer;
int32_t StackAllocator::m_size = sizeof(qul_scratch_buffer);

The code in the example above reserves 16kB of buffer, and sets its pointers and size to the StackAllocator.

Note: The stack allocator must be initialized so that the Qt Quick Ultralite core library can use it.

Available under certain Qt licenses.
Find out more.