Home · All Classes · Grouped Classes · Annotated · Functions

QMallocPool Class Reference

The QMallocPool class allows management of allocations within a designated memory region. More...

    #include <QMallocPool>

Public Types

Public Functions


Detailed Description

The QMallocPool class allows management of allocations within a designated memory region.

QMallocPool provides heap management capabilities into a fixed region of memory. Primarily this is useful for managing allocations in a shared memory region, but could be used in other scenarios.

The QMallocPool class provides equivalents for most standard memory management functions, such as malloc, calloc, realloc and free. However, unlike these standard functions which acquire their memory from the system kernel, QMallocPool operators on a region of memory provided to it during construction.

QMallocPool is based on dlmalloc, a public domain malloc implementation written by Doug Lea. dlmalloc is used as the default allocator in many projects, including several versions of Linux libc.

QMallocPool is not thread safe.


Member Type Documentation

enum QMallocPool::PoolType

Controls the type of pool to be created. In order to manage memory, a small amount of book keeping information is maintained. While this information is not required for reading from the managed pool, it is required for allocations. The PoolType controls where this bookkeeping data is stored.

ConstantValueDescription
QMallocPool::Owned0The bookkeeping data is maintained in the QMallocPool instance. Allocation to the pool is only possible via this instance.
QMallocPool::NewShared1The bookkeeping data is maintained in the managed region itself. This allows multiple QMallocPool instances, possibly in separate processes, to allocate from the pool.

The NewShared PoolType also initializes this bookkeeping data to its default state. Thus, while the bookkeeping data is shared, only one of the sharing instances should use a NewShared type. All other instances should use the Shared pool type.

The malloc pool bookkeeping data contains absolute pointers. As such, if multiple processes intend to allocate into the malloc pool, is is essential that they map the memory region to the same virtual address location.

ConstantValueDescription
QMallocPool::Shared2The bookkeeping data is stored in the managed region, and has previously been initialized by another QMallocPool instance constructed using the NewShared pool type.

The malloc pool bookkeeping data contains absolute pointers. As such, if multiple processes intend to allocate into the malloc pool, is is essential that they map the memory region to the same virtual address location.


Member Function Documentation

QMallocPool::QMallocPool ()

Creates an invalid QMallocPool.

QMallocPool::QMallocPool ( void * poolBase, unsigned int poolLength, PoolType type = Owned, const QString & name = QString() )

Creates a QMallocPool on the memory region poolBase of length poolLength. The pool will be constructed with the passed type and name. The name is used for diagnostics purposes only.

QMallocPool::~QMallocPool ()

Destroys the malloc pool.

void * QMallocPool::calloc ( size_t nmemb, size_t size )

Allocates memory for an array of nmemb elements of size each and returns a pointer to the allocated memory. The memory is set to zero. Returns 0 if the memory could not be allocated.

void QMallocPool::dumpStats () const

Outputs statistical information regarding the state of the malloc pool using qLog().

void QMallocPool::free ( void * ptr )

Frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behaviour occurs. If ptr is 0, no operation is performed.

bool QMallocPool::isValid () const

Returns true if this is a valid malloc pool. Invalid malloc pools cannot be allocated from.

void * QMallocPool::malloc ( size_t size )

Allocates size bytes and returns a pointer to the allocated memory. The memory is not cleared. Returns 0 if the memory could not be allocated.

MemoryStats QMallocPool::memoryStatistics () const

Returns a MemoryStats structure containing information about the memory use of this pool.

void * QMallocPool::realloc ( void * ptr, size_t size )

Changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged to the minimum of the old and new sizes; newly allocated memory will be uninitialized. If ptr is 0, the call is equivalent to malloc(size); if size is equal to zero, the call is equivalent to free(ptr). Unless ptr is 0, it must have been returned by an earlier call to malloc(), calloc() or realloc(). If the area pointed to was moved, a free(ptr) is done.

size_t QMallocPool::size_of ( void * mem )

Returns the allocated size of mem, assuming mem was previously returned by malloc(), calloc() or realloc().


Copyright © 2008 Nokia Trademarks
Qtopia 4.3.3