Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Managed memory allocator

The managed memory allocator is a fragmentation-free memory manager.

It keeps the allocated memory free from fragmentation by compacting the memory when blocks are freed. A program that uses the managed memory module cannot be sure that allocated memory stays in place. Therefore, a level of indirection is used: access to allocated memory must always be done using a special macro.

Note
This module has not been heavily tested.

Data Structures

struct  mmem
 

Files

file  mmem.c
 
    Implementation of the managed memory allocator

 
file  mmem.h
 
    Header file for the managed memory allocator

 

Macros

#define MMEM_PTR(m)
 Get a pointer to the managed memory. More...
 
#define MMEM_SIZE   4096
 

Functions

 LIST (mmemlist)
 
int mmem_alloc (struct mmem *m, unsigned int size)
 Allocate a managed memory block. More...
 
void mmem_free (struct mmem *m)
 Deallocate a managed memory block. More...
 
void mmem_init (void)
 Initialize the managed memory module. More...
 

Variables

unsigned int avail_memory
 
static char memory [MMEM_SIZE]
 

#define MMEM_PTR (   m)

Get a pointer to the managed memory.

Parameters
mA pointer to the struct mmem
Returns
A pointer to the memory block, or NULL if memory could not be allocated.
Author
Adam Dunkels
        This macro is used to get a pointer to a memory block
        allocated with mmem_alloc().
#define MMEM_SIZE   4096

Referenced by mmem_alloc(), mmem_free(), and mmem_init().

LIST ( mmemlist  )
int mmem_alloc ( struct mmem m,
unsigned int  size 
)

Allocate a managed memory block.

Parameters
mA pointer to a struct mmem.
sizeThe size of the requested memory block
Returns
Non-zero if the memory could be allocated, zero if memory was not available.
Author
Adam Dunkels
        This function allocates a chunk of managed memory. The
        memory allocated with this function must be deallocated
        using the mmem_free() function.

        \note This function does NOT return a pointer to the
        allocated memory, but a pointer to a structure that
        contains information about the managed memory. The
        macro MMEM_PTR() is used to get a pointer to the
        allocated memory.

References avail_memory, list_add(), memory, MMEM_SIZE, ptr, and size.

void mmem_free ( struct mmem m)

Deallocate a managed memory block.

Parameters
mA pointer to the managed memory block
Author
Adam Dunkels
        This function deallocates a managed memory block that
        previously has been allocated with mmem_alloc().

References avail_memory, list_remove(), memory, MMEM_SIZE, n, next, NULL, ptr, and size.

void mmem_init ( void  )

Initialize the managed memory module.

Author
Adam Dunkels
        This function initializes the managed memory module and
        should be called before any other function from the
        module.

References avail_memory, list_init(), and MMEM_SIZE.

unsigned int avail_memory

Referenced by mmem_alloc(), mmem_free(), and mmem_init().

char memory[MMEM_SIZE]
static

Referenced by mmem_alloc(), and mmem_free().