Memory Manager Library

This library provides routines for manipulating dynamic memory allocation. Dynamic memory allocation (also known as heap-based memory allocation) is the allocation of memory storage for use in a program during the runtime of that program.
Dynamically allocated memory exists until it is released. This is in contrast to static memory allocation, which has a fixed duration. It is said that an object so allocated has a dynamic lifetime.

The heap memory size can be configured in the Edit Project window. Also, user can override heap memory size in the code, by setting the HEAP_SIZE constant.

Library Routines

MM_Init

Prototype

void MM_Init();

Description

Sets Heap size.

Parameters

None.

Returns

Nothing.

Requires

Nothing.

Example
MM_Init();             // set Heap size
Notes

None.

Malloc

Prototype

void *Malloc(unsigned long Size);

Description

Allocates a block of size bytes of memory, returning a pointer to the beginning of the block. The content of the newly allocated block of memory is not initialized, remaining with indeterminate values..

Parameters
  • Size: Size of the memory block, in bytes.
Returns

Returns a pointer to the memory block allocated by the function.
If the function failed to allocate the requested block of memory, a null pointer is returned.

Requires

Nothing.

Example
int *pi;      // pointer to integer
int ai[100];  // array of integers

void main() {
  pi = (int *)Malloc(sizeof ai);  // pi will point to a memory block where the array is allocated
}
Notes

The type of this pointer is always void, which can be cast to the desired type of data pointer in order to be dereferenceable.

Free

Prototype

void Free(char * P, unsigned long Size);

Description

This function is used to free memory block allocated by Malloc.

Parameters
  • P: pointer to the memory block
  • Size: actual size of the memory block.
Returns

Nothing.

Requires

Nothing.

Example
int *pi;      // pointer to integer
int ai[100];  // array of integers

void main() {
  pi = (int *)Malloc(sizeof ai);  // pi will point to a memory block in the Heap where the array is allocated
  Free(pi, sizeof(pi));           // frees memory block from the Heap allocated by Malloc, pointed to by the pi pointer
}
Notes

None.

MM_LargestFreeMemBlock

Prototype

unsigned long MM_LargestFreeMemBlock();

Description

This function is used to determine largest available free memory block for the Heap.

Parameters

None.

Returns

Largest free memory block for the Heap.

Requires

Nothing.

Example
unsigned long block;      

void main() {
  block = MM_LargestFreeMemBlock();
}
Notes

None.

MM_TotalFreeMemSize

Prototype

unsigned long MM_TotalFreeMemSize();

Description

This function is used to determine total free memory size.

Parameters

None.

Returns

Total free memory size.

Requires

Nothing.

Example
unsigned long total;      

void main() {
  block = MM_TotalFreeMemSize();
}
Notes

None.

Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code