Contiki 2.5
Files | Macros | Functions
Managed memory allocator

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

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...
 

Functions

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...
 
int mmem_realloc (struct mmem *mem, unsigned int size)
 Change the size of allocated memory. More...
 
void mmem_reparent (struct mmem *m_old, struct mmem *m_new)
 Assign a chunk of memory form one mem struct to another. More...
 
void mmem_init (void)
 Initialize the managed memory module. More...
 

Detailed Description

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.

Macro Definition Documentation

#define MMEM_PTR (   m)

Function Documentation

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.

Definition at line 99 of file mmem.c.

References list_add().

Referenced by bundle_create_bundle(), elfloader_arch_allocate_ram(), and routing_flooding_new_bundle().

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().

Definition at line 144 of file mmem.c.

References list_remove(), and NULL.

Referenced by elfloader_arch_allocate_ram(), routing_flooding_delete_bundle(), and routing_flooding_new_bundle().

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.

Definition at line 255 of file mmem.c.

References list_init().

Referenced by main(), and storage_mmem_init().

int mmem_realloc ( struct mmem *  mem,
unsigned int  size 
)

Change the size of allocated memory.

Parameters
memmmem chunk whose size should be changed
sizeSize to change the chunk to
Returns
1 on success, 0 on failure
Author
Daniel Willmann
        This function is the mmem equivalent of realloc(). If the size
        could not be changed the original chunk is preserved.

Definition at line 183 of file mmem.c.

References NULL.

Referenced by bundle_add_block(), and storage_coffee_read_bundle().

void mmem_reparent ( struct mmem *  m_old,
struct mmem *  m_new 
)

Assign a chunk of memory form one mem struct to another.

Parameters
m_oldOld mmem struct that contains the memory
m_newNew mmem struct that will contain the memory after this call
Author
Daniel Willmann
        This function is needed as mmem expects the mmem structs to be
        in the same order as the chunks of memory are. The new struct
        will be inserted into mmem's internal list at the same place
        the old one was.

Definition at line 235 of file mmem.c.

References list_insert(), and list_remove().