Jason Engelman / Mbed 2 deprecated mymalloc

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #include <stdio.h>
00004 #include <string.h>
00005 
00006 // Found on the web @
00007 // http://simplestcodings.blogspot.com.au/2010/08/custom-malloc-function-implementation.html
00008 
00009 extern "C" void HardFault_Handler() {
00010     printf("Hard Fault!\n");
00011 }
00012 
00013 Serial pc(USBTX, USBRX); // tx, rx
00014 
00015 char extmem[0x4000] __attribute__((section("AHBSRAM1")));
00016 
00017 typedef
00018 struct
00019 {
00020     int is_available;
00021     int size;
00022 } MCB, *MCB_P;
00023 
00024 
00025 char *mem_start_p;
00026 int max_mem;
00027 int allocated_mem; /* this is the memory in use. */
00028 int mcb_count;
00029 
00030 char *heap_end;
00031 
00032 MCB_P memallocate(MCB_P ,int );
00033 
00034 enum {NEW_MCB=0,NO_MCB,REUSE_MCB};
00035 enum {FREE,IN_USE};
00036 
00037 
00038 void * myalloc(int elem_size) {
00039     /* check whether any chunk (allocated before) is free first */
00040 
00041     MCB_P p_mcb;
00042     int flag = NO_MCB;
00043     int sz;
00044 
00045     p_mcb = (MCB_P)mem_start_p;
00046 
00047 
00048     sz = sizeof(MCB);
00049 
00050     if ( (elem_size + sz) > (max_mem - (allocated_mem + mcb_count * sz ) ) ) {
00051         return NULL;
00052     }
00053     while ( heap_end > ( (char *)p_mcb + elem_size + sz) ) { //<<-- problem here doesnt iterate the first block
00054 
00055         if ( p_mcb->is_available == 0) {
00056 
00057             if ( p_mcb->size == 0) {
00058              //   printf("\NEW mem: %d ",p_mcb->size);  
00059                 flag = NEW_MCB;
00060                 break;
00061             }
00062             if ( p_mcb->size >= (elem_size + sz) ) {
00063              //   printf("\REUSE mem: %d ",p_mcb->size); 
00064                 flag = REUSE_MCB;
00065                 break;
00066             }
00067         }
00068         p_mcb = (MCB_P) ( (char *)p_mcb + p_mcb->size);
00069 
00070 
00071     }
00072 
00073     if ( flag != NO_MCB) {
00074         p_mcb->is_available = 1;
00075 
00076         if ( flag == NEW_MCB) {
00077             p_mcb->size = elem_size + sizeof(MCB);
00078         } else if ( flag == REUSE_MCB) {
00079             elem_size = p_mcb->size - sizeof(MCB);
00080         }
00081         mcb_count++;
00082         allocated_mem += elem_size;
00083         return ( (char *) p_mcb + sz);
00084     }
00085 
00086     return NULL;
00087 
00088 
00089     /* if size of the available chunk is equal to greater than required size, use that chunk */
00090 
00091 
00092 }
00093 
00094 void myfree(void *p) {
00095     /* Mark in MCB that this chunk is free */
00096    
00097     MCB_P ptr = (MCB_P)p;
00098     ptr--;
00099 
00100     if (ptr->is_available != FREE) {
00101         mcb_count--;
00102         ptr->is_available = FREE;
00103         allocated_mem -= (ptr->size - sizeof(MCB));
00104     }
00105     
00106     /* Mark in MCB that this chunk is free */  
00107     /*
00108     MCB_P ptr = (MCB_P)p;   
00109     ptr--;  
00110  
00111     mcb_count--;  
00112     ptr->is_available = FREE;  
00113     printf("\nAllocated mem: %d ",ptr->size);  
00114     allocated_mem -= (ptr->size - sizeof(MCB));  
00115     printf("\nAllocated mem: %d ",allocated_mem);  
00116     printf("\nMemory Freed...");  
00117     */
00118 }
00119 
00120 void * myrealloc(void *p) {
00121     //I would like to implement this
00122     return NULL;
00123 }
00124 
00125 void InitMem(char *ptr, int size_in_bytes) {
00126     /* store the ptr and size_in_bytes in global variable */
00127 
00128     max_mem = size_in_bytes;
00129     mem_start_p = ptr;
00130     mcb_count = 0;
00131     allocated_mem = 0;
00132     heap_end = mem_start_p + size_in_bytes;
00133     memset(mem_start_p,0x00,max_mem);
00134     myalloc(2); //<<-- myalloc doesnt iterate the first block, added as temporary fix
00135     /* This function is complete :-) */
00136 
00137 }
00138 
00139 
00140 int main() {
00141 
00142     pc.baud(57600);
00143     InitMem(extmem,sizeof(extmem));
00144 
00145     int blockSize = 256;
00146     int i = 1;
00147 
00148     printf("Checking extmem memory with blocksize %d char ...\n", blockSize);
00149     while (true) {
00150         char *p = (char *) myalloc(i * blockSize);
00151         printf("%d @ 0x%08X\n", i * blockSize, p);
00152       //  printf("\nMCB count: %-3d \tAllocated Memory: %-10d",mcb_count,allocated_mem); 
00153         if (p == NULL)
00154             break;
00155         myfree(p);
00156         ++i;
00157     }
00158     printf("%d\n\r",(i - 1) * blockSize);
00159 
00160 /*
00161    printf("Checking main memory with blocksize %d char ...\n", blockSize);
00162     while (true) {
00163         char *p = (char *) malloc(i * blockSize);
00164         printf("%d @ 0x%08X\n", i * blockSize, p);
00165         if (p == NULL)
00166             break;
00167         free(p);
00168         ++i;
00169     }
00170     printf("%d\n\r",(i - 1) * blockSize);
00171 */
00172 }
00173