Qualia  0.2
Public Member Functions | Public Attributes | Protected Member Functions | List of all members
StaticAllocator Class Reference

#include <StaticAllocator.h>

Inheritance diagram for StaticAllocator:
Inheritance graph
[legend]
Collaboration diagram for StaticAllocator:
Collaboration graph
[legend]

Public Member Functions

 StaticAllocator (unsigned char *buffer, size_t size)
 
- Public Member Functions inherited from Allocator
virtual ~Allocator ()
 

Public Attributes

unsigned char * buffer
 
size_t bufferSize
 
unsigned int bufferIdx
 
unsigned char * lastPtr
 
unsigned int nLeaks
 
unsigned char * lastLeak
 

Protected Member Functions

virtual void * malloc (size_t size)
 
virtual void * calloc (size_t num, size_t size)
 
virtual void * realloc (void *ptr, size_t size)
 
virtual void free (void *ptr)
 
virtual void freeAll ()
 

Detailed Description

An allocator (see Allocator) that "allocates" memory based on a pre-allocated static memory pool/buffer. Useful to manage memory on architectures that don't support well dynamic allocation (such as AVR-based systems). On such systems, it is usually recommended NOT to use dynamic allocation to avoid problems. StaticAllocator makes sure the memory is allocated from the stack rather than from the heap.

WARNING: Calling StaticAllocator::free() does NOT free the pointer at all. You should in fact NEVER have to call that function (because the memory then becomes completely useless). The object keeps track of any calls to free() by incrementing the nLeaks counter. The variable lastLeak is also updated with the value of the pointer on which free() was called last.

NOTE: An alternative is to tune the heap start and end in malloc. http://www.nongnu.org/avr-libc/user-manual/malloc.html

Usage:

unsigned char mybuffer[100];
StaticAllocator alloc(mybuffer, 100);
int* myarray = (int*)alloc.malloc(10*sizeof(int));
Object* myobject = new(alloc.malloc(sizeof(Object)) Object(1,2);

Constructor & Destructor Documentation

StaticAllocator::StaticAllocator ( unsigned char *  buffer,
size_t  size 
)

Member Function Documentation

void * StaticAllocator::calloc ( size_t  num,
size_t  size 
)
protectedvirtual

Reimplemented from Allocator.

void StaticAllocator::free ( void *  ptr)
protectedvirtual

Reimplemented from Allocator.

void StaticAllocator::freeAll ( )
protectedvirtual
void * StaticAllocator::malloc ( size_t  size)
protectedvirtual

Reimplemented from Allocator.

void * StaticAllocator::realloc ( void *  ptr,
size_t  size 
)
protectedvirtual

Reimplemented from Allocator.

Member Data Documentation

unsigned char* StaticAllocator::buffer
unsigned int StaticAllocator::bufferIdx
size_t StaticAllocator::bufferSize
unsigned char* StaticAllocator::lastLeak
unsigned char* StaticAllocator::lastPtr
unsigned int StaticAllocator::nLeaks

The documentation for this class was generated from the following files: