首先,这里我对主要使用的变量,宏和代码作下简单注释,
顺便说一下,这个内存管理算法在双向链表上耗费了比较多的内存,但在回收时比较方便。
我们先看下其使用的变量,
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;
}
天外来客 2024-4-23 12:06