FM

Dependencies:   SimpleDMA eeprom mbed-rtos mbed FreescaleIAP

Fork of CDMS_CODE by shubham c

Committer:
chaithanyarss
Date:
Sun Jan 08 14:52:55 2017 +0000
Revision:
344:1675dbde0bae
Some changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chaithanyarss 344:1675dbde0bae 1 /* Using malloc() to determine free memory.*/
chaithanyarss 344:1675dbde0bae 2
chaithanyarss 344:1675dbde0bae 3 #include <stdio.h>
chaithanyarss 344:1675dbde0bae 4 #include <stdlib.h>
chaithanyarss 344:1675dbde0bae 5 #define FREEMEM_CELL 100
chaithanyarss 344:1675dbde0bae 6 struct elem { /* Definition of a structure that is FREEMEM_CELL bytes in size.) */
chaithanyarss 344:1675dbde0bae 7 struct elem *next;
chaithanyarss 344:1675dbde0bae 8 char dummy[FREEMEM_CELL-2];
chaithanyarss 344:1675dbde0bae 9 };
chaithanyarss 344:1675dbde0bae 10 int FreeMem(void) {
chaithanyarss 344:1675dbde0bae 11 int counter;
chaithanyarss 344:1675dbde0bae 12 struct elem *head, *current, *nextone;
chaithanyarss 344:1675dbde0bae 13 current = head = (struct elem*) malloc(sizeof(struct elem));
chaithanyarss 344:1675dbde0bae 14 if (head == NULL)
chaithanyarss 344:1675dbde0bae 15 return 0; /*No memory available.*/
chaithanyarss 344:1675dbde0bae 16 counter = 0;
chaithanyarss 344:1675dbde0bae 17 // __disable_irq();
chaithanyarss 344:1675dbde0bae 18 do {
chaithanyarss 344:1675dbde0bae 19 counter++;
chaithanyarss 344:1675dbde0bae 20 current->next = (struct elem*) malloc(sizeof(struct elem));
chaithanyarss 344:1675dbde0bae 21 current = current->next;
chaithanyarss 344:1675dbde0bae 22 } while (current != NULL);
chaithanyarss 344:1675dbde0bae 23 /* Now counter holds the number of type elem
chaithanyarss 344:1675dbde0bae 24 structures we were able to allocate. We
chaithanyarss 344:1675dbde0bae 25 must free them all before returning. */
chaithanyarss 344:1675dbde0bae 26 current = head;
chaithanyarss 344:1675dbde0bae 27 do {
chaithanyarss 344:1675dbde0bae 28 nextone = current->next;
chaithanyarss 344:1675dbde0bae 29 free(current);
chaithanyarss 344:1675dbde0bae 30 current = nextone;
chaithanyarss 344:1675dbde0bae 31 } while (nextone != NULL);
chaithanyarss 344:1675dbde0bae 32 // __enable_irq();
chaithanyarss 344:1675dbde0bae 33
chaithanyarss 344:1675dbde0bae 34 return counter*FREEMEM_CELL;
chaithanyarss 344:1675dbde0bae 35 }