单片机(不基于os)下如何实现简单的内存管理(malloc,realloc和free函数的重新实现)


实现的原理是将内存分成小的片段进行管理,代码如下:

  1. #define MEM_BLOCK_SIZE 4096  
  2. #define MEM_LARGE_BLOCK_THRESHOLD       40960       //>MEM_LARGE_BLOCK_THRESHOLD :requested size is large block   
  3. #define MEM_BASE_ADDRESS                (0x90000000)  
  4. #define MEM_ALLOC_TABLE_FIRST_ENTRY 0  
  5. #define MAX_MEM_SIZE 7*1024*1024  
  6. #define MEM_ALLOC_TABLE_SIZE    (MAX_MEM_SIZE/MEM_BLOCK_SIZE)  
  7. static INT16 memory_map[MEM_ALLOC_TABLE_SIZE];  
  8. static char isMemoryManagementReady=0;  
  9. void *memset(void *s, int c, size_t count)  
  10. {  
  11.     char *xs = s;  
  12.     while (count--)  
  13.         *xs++ = c;  
  14.     return s;  
  15. }  
  16.   
  17. INT32 ssd_mem_init(void)  
  18. {  
  19.     memset(memory_map, 0, sizeof(memory_map));  
  20.     isMemoryManagementReady=1;  
  21. }  
  22. INT16   ssd_mem_percentage_used()  
  23. {  
  24.     int used=0;  
  25.     int i;  
  26.     for(i=0;i<MEM_ALLOC_TABLE_SIZE;i++)  
  27.     {  
  28.         if(memory_map[i])  
  29.         {  
  30.             used++;  
  31.         }  
  32.     }  
  33.     return used*100/MEM_ALLOC_TABLE_SIZE;  
  34. }  
  35. //return -1:FAIL  
  36. //>=0:   return allocated address offset  
  37. INT32 ssd_mem_malloc(UINT32 size)  
  38. {  
  39.     int offset=0;  
  40.     int startEntry=MEM_ALLOC_TABLE_FIRST_ENTRY;  
  41.     int nmemb;  
  42.     int i;  
  43.     if(!isMemoryManagementReady)  
  44.     {  
  45.            ssd_mem_init();  
  46.     }  
  47.       
  48.     if(size==0)  
  49.     {  
  50.         return -1;  
  51.     }  
  52.     nmemb=size/MEM_BLOCK_SIZE;  
  53.     if(size%MEM_BLOCK_SIZE)  
  54.     {  
  55.         nmemb++;  
  56.     }  
  57.           
  58.     if(size > MEM_LARGE_BLOCK_THRESHOLD)  
  59.     {  
  60.         for(offset=startEntry;offset<MEM_ALLOC_TABLE_SIZE-nmemb;offset++)  
  61.         {  
  62.             if(!memory_map[offset])   
  63.             {  
  64.                 int vacantSize=0;  
  65.                 for(vacantSize=0;vacantSize<nmemb && !memory_map[offset+vacantSize];vacantSize++);  
  66.                 if(vacantSize==nmemb)  
  67.                 {  
  68.                     for(i=0;i<nmemb;i++)  
  69.                     {  
  70.                         memory_map[offset+i]=nmemb;  
  71.                     }  
  72.                     return (offset*MEM_BLOCK_SIZE);  
  73.                 }  
  74.             }  
  75.         }  
  76.     }  
  77.     else  
  78.     {  
  79.         for(offset=MEM_ALLOC_TABLE_SIZE-1;offset>=0;offset--)  
  80.         {     
  81.             if(!memory_map[offset] && ((offset+nmemb)<=MEM_ALLOC_TABLE_SIZE))//search start of vacant block  
  82.             {  
  83.                 
  84.                 int vacantSize=0;  
  85.                 for(vacantSize=0;vacantSize<nmemb && !memory_map[offset+vacantSize];vacantSize++);  
  86.                 if(vacantSize==nmemb)  
  87.                 {  
  88.                     for(i=0;i<nmemb;i++)  
  89.                     {  
  90.                         memory_map[offset+i]=nmemb;  
  91.                     }  
  92.                     return (offset*MEM_BLOCK_SIZE);  
  93.                 }  
  94.             }  
  95.         }  
  96.     }  
  97.     puts("malloc size erorr=");  
  98.     putInt32(size);  
  99.     return -1;  
  100. }  
  101. //return 0:OK  
  102. //return 1:Out of bound  
  103. INT32 ssd_mem_free(INT32 offset)  
  104. {  
  105.     int i;  
  106.     if(!isMemoryManagementReady)  
  107.     {  
  108.         ssd_mem_init();  
  109.         return 1;  
  110.     }  
  111.     if(offset<MAX_MEM_SIZE)  
  112.     {  
  113.         int index=offset/MEM_BLOCK_SIZE;  
  114.         int nmemb=memory_map[index];  
  115.         for(i=0;i<nmemb;i++)  
  116.         {  
  117.             memory_map[index+i]=0;  
  118.         }  
  119.         return 0;  
  120.     }  
  121.     else  
  122.     {  
  123.        return 1;//out of bound  
  124.     }  
  125. }  
  126. void free(void *ptr)  
  127. {  
  128.     if(ptr==NULL)  
  129.         return;  
  130.     INT32 offset;  
  131.     offset=ptr-MEM_BASE_ADDRESS;  
  132.     ssd_mem_free(offset);     
  133. }  
  134. void * malloc(UINT32 size)  
  135. {  
  136.     INT32 offset;  
  137.     offset=ssd_mem_malloc(size);  
  138.     if(offset==-1)  
  139.         {  
  140.         return NULL;  
  141.         }  
  142.     else  
  143.         return MEM_BASE_ADDRESS+offset;  
  144.       
  145. }  
  146.   
  147. void *realloc(void *ptr,UINT32 size)  
  148. {  
  149.     INT32 offset;  
  150.     offset=ssd_mem_malloc(size);  
  151.     if(offset==-1)  
  152.         {  
  153.         puts("realloc error/n");  
  154.         return NULL;  
  155.         }  
  156.     else  
  157.         {  
  158.             memcpy((void*)(MEM_BASE_ADDRESS+offset),ptr,size);  
  159.             free(ptr);  
  160.             return MEM_BASE_ADDRESS+offset;       
  161.         }  


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值