📄 guialloc.c
字号:
return 0;
/* Locate or Create hole of sufficient size */
hMemIns = _FindHole(size);
#if GUI_ALLOC_AUTDEFRAG
if (hMemIns == -1) {
hMemIns = _CreateHole(size);
}
#endif
/* Occupy hole */
if (hMemIns==-1) {
GUI_DEBUG_ERROROUT1("GUI_ALLOC_Alloc: Could not allocate %d bytes",size);
return 0;
}
{
GUI_ALLOC_DATATYPE Off = aBlock[hMemIns].Off + aBlock[hMemIns].Size;
int Next = aBlock[hMemIns].Next;
aBlock[hMemNew].Size = size;
aBlock[hMemNew].Off = Off;
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;
}
return hMemNew;
}
/*********************************************************************
*
* Exported routines
*
**********************************************************************
*/
/*********************************************************************
*
* GUI_ALLOC_Init
*/
void GUI_ALLOC_Init(void) {
GUI_DEBUG_LOG("\nGUI_ALLOC_Init...");
GUI_ALLOC.NumFreeBlocksMin = GUI_ALLOC.NumFreeBlocks = GUI_MAXBLOCKS-1;
GUI_ALLOC.NumFreeBytesMin = GUI_ALLOC.NumFreeBytes = GUI_ALLOC_SIZE;
GUI_ALLOC.NumUsedBlocks = 0;
GUI_ALLOC.NumUsedBytes = 0;
aBlock[0].Size = (1<<GUI_BLOCK_ALIGN); /* occupy minimum for a block */
aBlock[0].Off = 0;
aBlock[0].Next = 0;
IsInitialized =1;
}
/*********************************************************************
*
* GUI_ALLOC_AllocNoInit
*/
GUI_HMEM GUI_ALLOC_AllocNoInit(GUI_ALLOC_DATATYPE Size) {
GUI_HMEM hMem;
if (Size == 0) {
return (GUI_HMEM)0;
}
GUI_LOCK();
GUI_DEBUG_LOG2("\nGUI_ALLOC_AllocNoInit... requesting %d, %d avail", Size, GUI_ALLOC.NumFreeBytes);
hMem = _Alloc(Size);
GUI_DEBUG_LOG1("\nGUI_ALLOC_AllocNoInit : Handle", hMem);
GUI_UNLOCK();
return hMem;
}
/*********************************************************************
*
* GUI_ALLOC_h2p
*/
void* GUI_ALLOC_h2p(GUI_HMEM hMem) {
GUI_ASSERT_LOCK();
#if GUI_DEBUG_LEVEL > 0
if (!hMem) {
GUI_DEBUG_ERROROUT("\n"__FILE__ " GUI_ALLOC_h2p: illegal argument (0 handle)");
return 0;
}
if (aBlock[hMem].Size == 0) {
GUI_DEBUG_ERROROUT("Dereferencing free block");
}
#endif
return HMEM2PTR(hMem);
}
/*********************************************************************
*
* GUI_ALLOC_GetNumFreeBytes
*/
GUI_ALLOC_DATATYPE GUI_ALLOC_GetNumFreeBytes(void) {
_CheckInit();
return GUI_ALLOC.NumFreeBytes;
}
/*********************************************************************
*
* GUI_ALLOC_GetMaxSize
*
* Purpose:
* Returns the biggest available blocksize (without relocation).
*/
GUI_ALLOC_DATATYPE GUI_ALLOC_GetMaxSize(void) {
GUI_ALLOC_DATATYPE r = 0;
GUI_ALLOC_DATATYPE NumFreeBytes;
int i, iNext;
GUI_LOCK();
_CheckInit();
for (i=0; (iNext =aBlock[i].Next) !=0; i= iNext) {
NumFreeBytes = aBlock[iNext].Off- (aBlock[i].Off+aBlock[i].Size);
if (NumFreeBytes > r) {
r = NumFreeBytes;
}
}
/* Check last block */
NumFreeBytes = (GUI_ALLOC_SIZE - (aBlock[i].Off+aBlock[i].Size));
if (NumFreeBytes > r) {
r = NumFreeBytes;
}
GUI_UNLOCK();
return r;
}
#else
/*********************************************************************
*
* External memory management functions
*
* The functions below will generate code only if the GUI memory
* management is not used (GUI_ALLOC_ALLOC defined).
*
* Note:
* The memory block allocated is bigger than the requested one, as we
* store some add. information (size of the memory block) there.
*
**********************************************************************
*/
typedef struct {
union {
GUI_ALLOC_DATATYPE Size;
int Dummy; /* Needed to guarantee alignment on 32 / 64 bit CPUs */
} Info; /* Unnamed would be best, but is not supported by all compilers */
} INFO;
/*********************************************************************
*
* _GetSize
*/
static GUI_ALLOC_DATATYPE _GetSize(GUI_HMEM hMem) {
INFO * pInfo;
pInfo = (INFO *)GUI_ALLOC_H2P(hMem);
return pInfo->Info.Size;
}
/*********************************************************************
*
* _Free
*/
static void _Free(GUI_HMEM hMem) {
GUI_ALLOC_FREE(hMem);
}
/*********************************************************************
*
* GUI_ALLOC_AllocNoInit
*/
GUI_HMEM GUI_ALLOC_AllocNoInit(GUI_ALLOC_DATATYPE Size) {
GUI_HMEM hMem;
if (Size == 0) {
return (GUI_HMEM)0;
}
hMem= GUI_ALLOC_ALLOC(Size + sizeof(INFO));
/* Init info structure */
if (hMem) {
INFO * pInfo;
pInfo = (INFO *)GUI_ALLOC_H2P(hMem);
pInfo->Info.Size = Size;
}
return hMem;
}
/*********************************************************************
*
* GUI_ALLOC_h2p
*/
void* GUI_ALLOC_h2p(GUI_HMEM hMem) {
U8* p = (U8*)GUI_ALLOC_H2P(hMem); /* Pointer to memory block from memory manager */
p += sizeof(INFO); /* Convert to pointer to usable area */
return p;
}
/*********************************************************************
*
* GUI_ALLOC_GetMaxSize
*/
GUI_ALLOC_DATATYPE GUI_ALLOC_GetMaxSize(void) {
return GUI_ALLOC_GETMAXSIZE();
}
/*********************************************************************
*
* GUI_ALLOC_Init
*/
void GUI_ALLOC_Init(void) {
#ifdef GUI_ALLOC_INIT
GUI_ALLOC_INIT();
#endif
}
#endif
/*********************************************************************
*
* Public code, common memory management functions
*
**********************************************************************
*/
/*********************************************************************
*
* GUI_ALLOC_GetSize
*/
GUI_ALLOC_DATATYPE GUI_ALLOC_GetSize(GUI_HMEM hMem) {
/* Do the error checking first */
#if GUI_DEBUG_LEVEL>0
if (!hMem) {
GUI_DEBUG_ERROROUT("\n"__FILE__ " GUI_ALLOC_h2p: illegal argument (0 handle)");
return 0;
}
#endif
return _GetSize(hMem);
}
/*********************************************************************
*
* GUI_ALLOC_Free
*/
void GUI_ALLOC_Free(GUI_HMEM hMem) {
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);
_Free(hMem);
GUI_UNLOCK();
}
/*********************************************************************
*
* GUI_ALLOC_FreePtr
*/
void GUI_ALLOC_FreePtr(GUI_HMEM *ph) {
GUI_LOCK();
GUI_ALLOC_Free(*ph);
*ph =0;
GUI_UNLOCK();
}
/*************************** End of file ****************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -