📄 sralloc.cpp
字号:
#ifdef MAKE_INTERRUPT_SAFE
GIEH = saveGIEH;
#endif
return (0);
}
// Init the pointer to the heap
pHeap = (SALLOC *)_uDynamicHeap;
while (1)
{
#ifdef ENABLE_DEBUG
ConsolePutROMString( (ROM char * const)" check " );
PrintChar( (BYTE)((WORD)pHeap>>8) );
PrintChar( (BYTE)((WORD)pHeap&0xff) );
#endif
// Get the header of the segment
segHeader = *pHeap;
// Extract the segment length from the segment
segLen = segHeader.bits.count - 1;
// A null segment indicates the end of the table
if (segHeader.byte == 0)
{
#ifdef ENABLE_DEBUG
ConsolePutROMString( (ROM char * const)" TableEnd\r\n" );
#endif
#ifdef MAKE_INTERRUPT_SAFE
GIEH = saveGIEH;
#endif
return (0);
}
// If this segment is not allocated then attempt to allocate it
if (!(segHeader.bits.alloc))
{
// If the free segment is too small then attempt to merge
if (nBytes > segLen)
{
#ifdef ENABLE_DEBUG
ConsolePutROMString( (ROM char * const)" merge" );
#endif
// If the merge fails them move on to the next segment
if (!(_SRAMmerge(pHeap)))
{
pHeap +=(WORD)(segHeader.bits.count);
}
// Fall through so we can check the newly created segment.
}
else
// If the segment length matches the request then allocate the
// header and return the pointer
if (nBytes == segLen)
{
// Allocate the segment
(*pHeap).bits.alloc = 1;
// Return the pointer to the caller
#ifdef MAKE_INTERRUPT_SAFE
GIEH = saveGIEH;
#endif
#ifdef ENABLE_DEBUG
ConsolePutROMString( (ROM char * const)" Found\r\n" );
#endif
return ((unsigned char *)(pHeap + 1));
}
// Else create a new segment
else
{
// Reset the header to point to a new segment
(*pHeap).byte = nBytes + 0x81;
// Remember the pointer to the first segment
temp = pHeap + 1;
// Point to the new segment
pHeap += (WORD)(nBytes + 1);
// Insert the header for the new segment
(*pHeap).byte = segLen - nBytes;
// Return the pointer to the user
#ifdef MAKE_INTERRUPT_SAFE
GIEH = saveGIEH;
#endif
#ifdef ENABLE_DEBUG
ConsolePutROMString( (ROM char * const)" Found\r\n" );
#endif
return ((unsigned char *) temp);
}
}
// else set the pointer to the next segment header in the heap
else
{
pHeap += (WORD)segHeader.bits.count;
}
}
#ifdef MAKE_INTERRUPT_SAFE
GIEH = saveGIEH;
#endif
#ifdef ENABLE_DEBUG
ConsolePutROMString( (ROM char * const)" ???\r\n" );
#endif
return (0);
}
/*********************************************************************
* Function: void SRAMfree(unsigned char * pSRAM)
*
* PreCondition: The pointer must have been returned from a
* previously allocation via SRAMalloc().
*
* Input: unsigned char * pSRAM - pointer to the allocated
*
* Output: void
*
* Side Effects:
*
* Overview: This function de-allocates a previously allocated
* segment of memory.
*
* Note: The pointer must be a valid pointer returned from
* SRAMalloc(); otherwise, the segment may not be
* successfully de-allocated, and the heap may be
* corrupted.
********************************************************************/
void SRAMfree(unsigned char * NEAR pSRAM)
{
// Release the segment
(*(SALLOC *)(pSRAM - 1)).bits.alloc = 0;
#ifdef ENABLE_DEBUG
ConsolePutROMString( (ROM char * const)"\r\nFree " );
PrintChar((*(SALLOC *)(pSRAM - 1)).byte - 1);
ConsolePutROMString( (ROM char * const)" at " );
PrintChar( (BYTE)((WORD)(pSRAM - 1)>>8) );
PrintChar( (BYTE)((WORD)(pSRAM - 1)&0xff) );
#endif
}
/*********************************************************************
* Function: void SRAMInitHeap(void)
*
* PreCondition:
*
* Input: void
*
* Output: void
*
* Side Effects:
*
* Overview: This function initializes the dynamic heap. It
* inserts segment headers to maximize segment space.
*
* Note: This function must be called at least one time.
* And it could be called more times to reset the
* heap.
********************************************************************/
void SRAMInitHeap(void)
{
unsigned char * NEAR pHeap;
NEAR unsigned int count;
pHeap = _uDynamicHeap;
count = _MAX_HEAP_SIZE;
while (1)
{
if (count > _MAX_SEGMENT_SIZE)
{
*pHeap = _MAX_SEGMENT_SIZE;
pHeap += _MAX_SEGMENT_SIZE;
count = count - _MAX_SEGMENT_SIZE;
}
else
{
*pHeap = count;
*(pHeap + count) = 0;
return;
}
}
}
/*********************************************************************
* Function: unsigned char _SRAMmerge(SALLOC * NEAR pSegA)
*
* PreCondition:
*
* Input: SALLOC * NEAR pSegA - pointer to the first segment.
*
* Output: usnigned char - returns the length of the
* merged segment or zero if failed to merge.
*
* Side Effects:
*
* Overview: This function tries to merge adjacent segments
* that have not been allocated. The largest possible
* segment is merged if possible.
*
* Note:
********************************************************************/
BOOL _SRAMmerge(SALLOC * NEAR pSegA)
{
SALLOC * NEAR pSegB;
NEAR SALLOC uSegA, uSegB, uSum;
// Init the pointer to the heap
pSegB = pSegA + (WORD)(*pSegA).bits.count;
// Extract the headers for faster processing
uSegA = *pSegA;
uSegB = *pSegB;
// Quit if the tail has been found
if (uSegB.byte == 0)
{
return (FALSE);
}
// If either segment is allocated then do not merge
if (uSegA.bits.alloc || uSegB.bits.alloc)
{
return (FALSE);
}
// If the first segment is max then nothing to merge
if (uSegA.bits.count == _MAX_SEGMENT_SIZE)
{
return (FALSE);
}
// Get the sum of the two segments
uSum.byte = uSegA.byte + uSegB.byte;
// If the sum of the two segments are > than the largest segment
// then create a new segment equal to the max segment size and
// point to the next segments
if ((uSum.byte) > _MAX_SEGMENT_SIZE)
{
(*pSegA).byte = _MAX_SEGMENT_SIZE;
pSegA += _MAX_SEGMENT_SIZE; //(*pSeg1).byte;
// pSegB += uSegB.byte; //(*pSeg2).byte ;
// (*pSegA).byte = pSegB - pSegA;
(*pSegA).byte = uSum.byte - _MAX_SEGMENT_SIZE;
return (TRUE);
}
// Else combine the two segments into one segment and
// do not adjust the pointers to the next segment
else
{
(*pSegA).byte = uSum.byte;
return (TRUE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -