原创 uC-GUI 3.24版的内存管理源码分析

2009-1-19 23:12 6203 10 11 分类: MCU/ 嵌入式

首先,这里我对主要使用的变量,宏和代码作下简单注释,


顺便说一下,这个内存管理算法在双向链表上耗费了比较多的内存,但在回收时比较方便。


我们先看下其使用的变量,


GUI_HEAP GUI_Heap;       /* 堆 */
static tBlock aBlock[GUI_MAXBLOCKS];/*用于记录内存块信息 2009.01.16*/


struct {
  int       NumUsedBlocks, NumFreeBlocks, NumFreeBlocksMin;        /* 用于信息统计 */
  tALLOCINT NumUsedBytes,  NumFreeBytes,  NumFreeBytesMin;
} GUI_ALLOC;


static char   IsInitialized =0; // 用于记录堆是否初始化
转载请注明出处:青藤门客播报站


宏:


/*根据句柄得到实际的内存地址 2009.01.16*/
/*uC-GUI中用到的窗口句柄其实际值为:内存块的索引值 2009.01.16*/
#define HMEM2PTR(hMem) (void*)&GUI_Heap.abHeap[aBlock[hMem].Off]


函数:


/*将size转换为合法的块大小:内存分配时要求对齐 2009.01.16*/
static int Size2LegalSize(int size)
{
    return (size + ((1<<GUI_BLOCK_ALIGN)-1)) & ~((1<<GUI_BLOCK_ALIGN)-1);
}


/*查找未分配的内存块,并返回句柄(索引值) 2009.01.16 */
static GUI_HMEM FindFreeHandle(void) {
  int i;
  for (i=1; i< GUI_MAXBLOCKS; i++) {
    if (aBlock.Size ==0)
   return i;
  }
  GUI_DEBUG_ERROROUT1("Insufficient memory handles configured (GUI_MAXBLOCKS == %d (See GUIConf.h))", GUI_MAXBLOCKS);
  return GUI_HMEM_NULL;
}
/*找到要分配的内存区域      2009.01.16
  返回可分配内存的前一个句柄值,(-1)表示没有可以分配的内存
  (计算内存实际地址方法:aBlock[r].Off+aBlock[r].Size)*/
static GUI_HMEM FindHole(int Size)
{
    int i, iNext;
    for (i=0; (iNext = aBlock.Next) != 0; i = iNext)
    {
        /*计算两个内存块之间的内存碎片大小  2009.01.16*/
        /*注意:双链表从头到尾的内存块是有序的 2009.01.16*/
        int NumFreeBytes = aBlock[iNext].Off- (aBlock.Off+aBlock.Size);
        if (NumFreeBytes>=Size)
            return i;
    }
    /*检测最后一个有效内存块之后剩余的内存大小*/
    if (GUI_ALLOC_SIZE - (aBlock.Off+aBlock.Size) >= Size)
        return i;
    return -1;
}


/*_CreateHole:整理内存碎片,并申请Size大小的内存 2009.01.16
* Return value:
*   Offset to the memory hole (if available)
*   -1 if not available       */
static GUI_HMEM CreateHole(int Size)
{
  int i, iNext;
  int r = -1;
  for (i=0; (iNext =aBlock.Next) !=0; i= iNext)
  { /*计算两个内存块之间的内存碎片大小  2009.01.16*/
    int NumFreeBytes = aBlock[iNext].Off- (aBlock.Off+aBlock.Size);
    if (NumFreeBytes < Size)
    {
      int NumBytesBeforeBlock = aBlock[iNext].Off - (aBlock.Off+aBlock.Size);
      if (NumBytesBeforeBlock)
      { /*如果两个内存块间存在碎片,则移动内存 2009.01.16 */
        U8* pData = &GUI_Heap.abHeap[aBlock[iNext].Off];
        memmove(pData-NumBytesBeforeBlock, pData, aBlock[iNext].Size);
        aBlock[iNext].Off -=NumBytesBeforeBlock;
      }
    }
  }
/* Check last block */
  if (GUI_ALLOC_SIZE - (aBlock.Off+aBlock.Size) >= Size)
    r = i;
  return r;
}
/*内存分配,返回句柄值,0表示分配失败 2009.01.16 */
static GUI_HMEM _Alloc(int size)
{
  GUI_HMEM hMemNew, hMemIns;
  CheckInit();
  size = Size2LegalSize(size);
  /* Check if memory is available at all ...*/
  if (size > GUI_ALLOC.NumFreeBytes) {
    GUI_DEBUG_WARN1("GUI_ALLOC_Alloc: Insufficient memory configured (Trying to alloc % bytes)", size);
    return 0;
  }
  /* Locate free handle */
  if ((hMemNew = FindFreeHandle()) == 0)
    return 0;
  /* Locate or Create hole of sufficient size */
  hMemIns = FindHole(size);
  #if GUI_ALLOC_AUTDEFRAG
    if (hMemIns == -1) /*内存块都太小,整理内存碎片 2009.01.16*/
    {
      hMemIns = CreateHole(size);
    }
  #endif
/* Occupy hole */
  if (hMemIns==-1) /*内存块分配失败 2009.01.16 */
  {
    GUI_DEBUG_ERROROUT1("GUI_ALLOC_Alloc: Could not allocate %d bytes",size);
    return 0;
  }
  {
   /*计算可以分配的内存实际地址 2009.01.16 */
    int Off = aBlock[hMemIns].Off+aBlock[hMemIns].Size;
    int Next = aBlock[hMemIns].Next;
    aBlock[hMemNew].Size  = size;
    aBlock[hMemNew].Off   = Off;
    /*双链表插入 2009.01.16*/
    if ((aBlock[hMemNew].Next  = Next) >0) {
      aBlock[Next].Prev = hMemNew; 
    }
    aBlock[hMemNew].Prev  = hMemIns;
    aBlock[hMemIns].Next  = hMemNew;
  }
/* Keep track of number of blocks and av. memory */
  GUI_ALLOC.NumUsedBlocks++;
  GUI_ALLOC.NumFreeBlocks--;
  if (GUI_ALLOC.NumFreeBlocksMin > GUI_ALLOC.NumFreeBlocks) {
    GUI_ALLOC.NumFreeBlocksMin = GUI_ALLOC.NumFreeBlocks;
  }
  GUI_ALLOC.NumUsedBytes += size;
  GUI_ALLOC.NumFreeBytes -= size;
  if (GUI_ALLOC.NumFreeBytesMin > GUI_ALLOC.NumFreeBytes) {
    GUI_ALLOC.NumFreeBytesMin = GUI_ALLOC.NumFreeBytes;
  }
/* In order to be on the safe side, zeroinit ! */
  memset(HMEM2PTR(hMemNew), 0, size);
  return hMemNew;
}


/*内存分配 2009.01.16 */
GUI_HMEM GUI_ALLOC_Alloc(int size)
{
  GUI_HMEM hMem;
 /* First make sure that init has been called */
  GUI_LOCK();
  GUI_DEBUG_LOG2("\nGUI_ALLOC_Alloc... requesting %d, %d avail", size, GUI_ALLOC.NumFreeBytes);
  hMem = _Alloc(size);
  GUI_DEBUG_LOG1("\nGUI_ALLOC_Alloc : Handle", hMem);
  GUI_UNLOCK();
  return hMem;
}


/* 释放句柄占用的内存  2009.01.16 */
void GUI_ALLOC_Free(GUI_HMEM hMem)
{
  int Size;
  if (hMem == GUI_HMEM_NULL)  /* Note: This is not an error, it is permitted */
    return;
  GUI_LOCK();
  GUI_DEBUG_LOG1("\nGUI_ALLOC_Free(%d)", hMem);
  /* Do some error checking ... */
  #if GUI_DEBUG_LEVEL>0
    /* Block not allocated ? */
    if (aBlock[hMem].Size==0) {
      GUI_DEBUG_ERROROUT("GUI_ALLOC_Free(): Invalid hMem");
      return;
    }
  #endif
  Size = aBlock[hMem].Size;
  #ifdef WIN32 /*Windows环境下,将内存初始化为0xCC 2009.01.16 */
    memset(&GUI_Heap.abHeap[aBlock[hMem].Off], 0xcc, Size);
  #endif
  GUI_ALLOC.NumFreeBytes += Size;
  GUI_ALLOC.NumUsedBytes -= Size;
  aBlock[hMem].Size = 0;
  { /*双链表,删除一个节点*/
    int Next = aBlock[hMem].Next;
    int Prev = aBlock[hMem].Prev;
    aBlock[Prev].Next = Next;
    if (Next)
      aBlock[Next].Prev = Prev;
  } 
  GUI_ALLOC.NumFreeBlocks++;
  GUI_ALLOC.NumUsedBlocks--;
  GUI_UNLOCK();
}


/*返回句柄的实际内存地址  2009.01.16 */
void*       GUI_ALLOC_h2p   (GUI_HMEM  hMem)
{
  #if GUI_DEBUG_LEVEL>0
    if (!hMem) {
      GUI_DEBUG_ERROROUT("\n"__FILE__ " GUI_ALLOC_h2p: illegal argument (0 handle)");
      return 0;
    }
  #endif
  return HMEM2PTR(hMem);
}


/* 释放句柄指针  2009.01.16 */
void GUI_ALLOC_FreePtr(GUI_HMEM *ph) {
  GUI_LOCK();
  GUI_ALLOC_Free(*ph);
  *ph =0;
  GUI_UNLOCK();
}



int GUI_GetUsedMem(void)
{
  int i;
  int NumUsedBytes="0";
  GUI_LOCK();
  CheckInit();
  for (i=1; i; i = aBlock.Next)
  {
    NumUsedBytes += aBlock.Size;
  }
  GUI_UNLOCK();
  return NumUsedBytes;
}


/*获取空余的内存大小  2009.01.16 */
int GUI_ALLOC_GetNumFreeBytes(void)
{
  CheckInit();
  return GUI_ALLOC.NumFreeBytes; 
}


/*  不整理碎片的情况下,返回最大的内存块大小  2009.01.16
*   Returns the biggest available blocksize (without relocation).
*/
int GUI_ALLOC_GetMaxSize(void) {
  int r="0";
  int NumFreeBytes;
  int i, iNext;
  GUI_LOCK();
  CheckInit();
  for (i=0; (iNext =aBlock.Next) !=0; i= iNext) {
    NumFreeBytes = aBlock[iNext].Off- (aBlock.Off+aBlock.Size);
    if (NumFreeBytes > r) {
      r = NumFreeBytes;
    }
  }
/* Check last block */
  NumFreeBytes = (GUI_ALLOC_SIZE - (aBlock.Off+aBlock.Size));
  if (NumFreeBytes > r) {
    r = NumFreeBytes;
  }
  GUI_UNLOCK();
  return r;
}

文章评论1条评论)

登录后参与讨论

天外来客 2024-4-23 12:06

相关推荐阅读
walnutcy_696810119 2012-11-21 08:37
Linux下使用smartCOM调试串口
在Windows下的串口调试一直使用sscom,在Linux下只找到一个cutecom,用了几次,很不喜欢,就着手开发了一款自己的串口调试工具,smartCOM。 smartCOM介绍:http...
walnutcy_696810119 2012-03-29 18:12
【博客大赛】原创--测量基础:什么是测量
写在正文之前: 适逢EDNChina搞活动,而我本人也算在测量业工作,就一起作下笔记吧。若有错误,请大家一起斧正。笔者写本文一方面是梳理知识,另一方面也希望与大家探讨有关测量的知识、应用等,希...
walnutcy_696810119 2011-12-20 18:29
GLONASS 15年来,首次实现24颗星在轨可用
  EDN的博客改版后,不太好用,在SINA重开一个,不过重点改为关注GNSS行业新闻 http://blog.sina.com.cn/s/blog_7420cd1701012en9....
walnutcy_696810119 2011-12-20 17:49
逆向工程第一步:通信协议分析
工程中常有这样的事,想分析下其他知名公司产品中的通信协议,以便生产设计兼容产品。 1) 逆向工程,首先要了解产品,知己知彼,百战不怠;     去年受命想仿一款GARMIN的导航盒,但无法...
walnutcy_696810119 2011-11-14 11:30
通用代码调试方法 (Keil, VCC)
调试代码一般需要定位问题,这里给出一个解决方案, 一般的编译器均支持这些宏指令。   #define DEBUG_WALT_1113     1 extern void log_t...
我要评论
1
10
关闭 站长推荐上一条 /2 下一条