Dynamic Block Memory Allocator - LUFA/MemoryAllocator/DynAlloc.h


Defines

#define DEREF(handle, type)   (*(type*)handle)
#define ALLOCABLE_BYTES   (1UL * NUM_BLOCKS * BLOCK_SIZE)

Typedefs

typedef const void ** Mem_Handle_t
typedef uint32_t Alloc_Size_t
typedef uint32_t Block_Number_t
typedef uint32_t Handle_Number_t

Functions

Mem_Handle_t Mem_Alloc (const Alloc_Size_t Bytes)
Mem_Handle_t Mem_Calloc (const Alloc_Size_t Bytes)
Mem_Handle_t Mem_Realloc (Mem_Handle_t CurrAllocHdl, const Alloc_Size_t Bytes)
void Mem_Free (Mem_Handle_t CurrAllocHdl)
Block_Number_t Mem_TotalFreeBlocks (void)
Handle_Number_t Mem_TotalFreeHandles (void)

Detailed Description

Module Source Dependencies

The following files must be built with any user project that uses this module:

Description

Dynamic, auto-defragmenting block memory allocator library. This library provides a convenient replacement for the standard avr-libc dynamic memory allocation routines. Memory is handed out in block chunks, to reduce the management memory overhead.

Unlike the normal memory allocation routines, this library gives out handles to memory which must be dereferenced at the exact time of use, rather than handing back direct memory pointers. By using library managed handles instead of pointers, allocated memory blocks can be shifted around as needed transparently to defragment the memory as more blocks are requested.

The memory heap is static, thus the total memory usage of the compiled application (as reported by the avr-size tool of the AVR-GCC toolchain) includes the dynamic memory heap.

The constants NUM_BLOCKS, BLOCK_SIZE and NUM_HANDLES must be defined in the project makefile (and passed to the preprocessor via the -D GCC switch) for this library to compile.

NUM_BLOCKS indicates the number of memory blocks in the memory psudoheap which can be chained together and handed to the application via a memory handle. NUM_HANDLES is the maximum number of memory handles (pointing to one or more chained memory blocks) which can be handed out simultaneously before requiring a handle (and its associated memory) to be freed. BLOCK_SIZE gives the number of bytes in each memory block.


Define Documentation

#define ALLOCABLE_BYTES   (1UL * NUM_BLOCKS * BLOCK_SIZE)

Constant, giving the total heap size in bytes.

#define DEREF ( handle,
type   )     (*(type*)handle)

Macro to dereference a given memory handle into the given type. The given type should be a pointer if the memory is to contain an array of items, or should be a standard type (such as a primitive or structure) if the memory is to hold a single item of a single type.


Typedef Documentation

typedef uint32_t Alloc_Size_t

Type define for the size (in bytes) for an allocation for passing to the library functions. The exact type width varies depending on the value of ALLOCABLE_BYTES to ensure that a single allocation can request the entire heap if needed.

typedef uint32_t Block_Number_t

Type define for a block number in the heap. The exact type width varies depending on the value of NUM_BLOCKS to ensure that the type can store an index to any block in the block pool.

typedef uint32_t Handle_Number_t

Type define for a handle number. The exact type width varies depending on the value of NUM_HANDLES to ensure that the type can store the index of any handle in the handle pool.

typedef const void** Mem_Handle_t

Memory handle type, used to store handles given by the library functions.


Function Documentation

Mem_Handle_t Mem_Alloc ( const Alloc_Size_t  Bytes  ) 

Allocates a given number of blocks from the heap (calculated from the requested number of bytes) and returns a handle to the newly allocated memory.

Parameters:
Bytes The number of bytes requested to be allocated from the heap
Returns:
NULL handle if the allocation fails, or handle to the allocated memory if the allocation succeeds

Mem_Handle_t Mem_Calloc ( const Alloc_Size_t  Bytes  ) 

Allocates a given number of blocks from the heap (calculated from the requested number of bytes) and returns a handle to the newly allocated memory. Calloced memory is automatically cleared to all 0x00 values at the time of allocation.

Parameters:
Bytes The number of pre-cleared bytes requested to be allocated from the heap
Returns:
NULL handle if the allocation fails, or handle to the allocated memory if the allocation succeeds

void Mem_Free ( Mem_Handle_t  CurrAllocHdl  ) 

Deallocates a given previously allocated section of memory from the heap.

Parameters:
CurrAllocHdl Handle to a previously allocated section of memory in the heap

Mem_Handle_t Mem_Realloc ( Mem_Handle_t  CurrAllocHdl,
const Alloc_Size_t  Bytes 
)

Deallocates a given memory handle, and attempts to allocates the given number of blocks from the heap (calculated from the requested number of bytes) immediately following the deallocation. The new memory may be located in the same area as the previous memory, but this is not guaranteed.

Parameters:
CurrAllocHdl Handle to an already allocated section of memory in the heap to deallocate
Bytes The number of bytes requested to be allocated from the heap following the deallocation
Returns:
NULL handle if the allocation fails, or handle to the allocated memory if the allocation succeeds
Warning:
Even if the allocation fails, the deallocation will still occur. Care should be taken to ensure that the previously allocated memory is not used following an unsuccessful realloc().

Block_Number_t Mem_TotalFreeBlocks ( void   ) 

Returns the total number of unallocated blocks in the heap.

Returns:
Number of free blocks in the heap, as a Block_Number_t integer

Handle_Number_t Mem_TotalFreeHandles ( void   ) 

Returns the total number of unallocated handles in the handle pool.

Returns:
Number of free handles in the handle pool, as a Handle_Number_t integer


Generated on Fri Jun 5 16:30:20 2009 for LUFA Library by  doxygen 1.5.7.1