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 |
|
| Returns |
Returns a pointer to the memory block allocated by the function.
|
| Requires |
Before calling this function, heap size must be set in the Edit Project and initialized via MM_Init function. |
| 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 |
|
| 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. |
Library Example
The example shows use how to use Memory Manager library.
// Structure that describes Item object
typedef struct {
long id; // Item number
char * name; // Item name
} tItem;
// Allocate and initialize a new Item object and returns pointer to created Item
tItem * make_item(char *name) {
tItem * item;
item = (tItem*) malloc(sizeof(tItem)); // Allocate a block of memory for a new Item object
if (item == 0) // If allocation failed return null
return (tItem *)0;
memset(item, 0, sizeof(tItem)); // Initialize the members of the new Item
item->id = -1;
item->name = (char *)0;
item->name = malloc(strlen(name) + 1); // Allocate a block of memory for a name in the Item
if (item->name == 0) { // If allocation failed return null
Free( (char*)item, sizeof(tItem));
return (tItem *)0;
}
strcpy(item->name, name); //Save a copy of the name in the new Item
return item; // Return the created Item object
}
// Deallocate an Item object
void destroy_item(tItem *item) {
if (item == 0) // Check for a null object pointer
return;
if (item->name != 0) {
Free(item->name, strlen(item->name) + 1);// Deallocate the name string saved within the Item
item->name = (char *)0;
}
Free((char*)item, sizeof(tItem)); // Deallocate the Item object itself
}
tItem *it[3]; // Array of pointers to Item objects
char txt[15];
long freeMemSize;
long largerstFreeBlock;
void main() {
MM_Init();
freeMemSize = MM_TotalFreeMemSize(); // Get free memory size before allocating
largerstFreeBlock = MM_LargestFreeMemBlock(); // Get largest free memory block size before allocating
it[0] = make_item("one"); // Allocate object
it[1] = make_item("two");
it[2] = make_item("three");
freeMemSize = MM_TotalFreeMemSize(); // Get free memory size
largerstFreeBlock = MM_LargestFreeMemBlock(); // Get largest free memory block size
strcpy(txt,it[0]->name);
strcpy(txt,it[1]->name);
strcpy(txt,it[2]->name);
destroy_item(it[0]); // Deallocate the Item object
it[0] = make_item("instead one"); // Make another Item object
strcpy(txt,it[0]->name);
strcpy(txt,it[1]->name);
strcpy(txt,it[2]->name);
destroy_item(it[1]); // Deallocate Items
destroy_item(it[2]);
freeMemSize = MM_TotalFreeMemSize(); // Get free memory size
}
What do you think about this topic ? Send us feedback!




