C

StackAllocator Class

class Qul::Platform::StackAllocator

Provides a simple memory allocator for functions that might temporarily require some extra memory for caching. More...

Header: #include <platform/alloc.h>
Since: Qt 1.5

This class was introduced in Qt 1.5.

Public Types

class Allocation

Static Public Members

int32_t available()

Detailed Description

StackAllocator might give less memory than asked for and functions are supposed to cope gracefully with that.

The platform library needs to initialize static member variables to the buffer reserved for the StackAllocator:

  • m_buffer to the beginning of the buffer.
  • m_top to the current top address. This variable must be initialized to m_buffer.
  • m_size to the buffer size in bytes.

A typical intialization of StackAllocator:

#include <platform/alloc.h>

static char scratch_buffer[16 * 1024];
char *StackAllocator::m_buffer = scratch_buffer;
char *StackAllocator::m_top = StackAllocator::m_buffer;
int32_t StackAllocator::m_size = sizeof(scratch_buffer);

A typical use of StackAllocator:

#include <platform/alloc.h>

Qul::Platform::StackAllocator::Allocation alloc(requestedSize);
if (requestedSize == alloc.size()) {
    doStuff(alloc.data());
} else {
    error();
}

Memory is freed by the destructor when alloc goes out of scope.

See also Qul::Platform::StackAllocator::Allocation.

Member Function Documentation

[static] int32_t StackAllocator::available()

Returns number of bytes available for allocation.

This function returns zero if the member variables of StackAllocator were not initialized.

Available under certain Qt licenses.
Find out more.