📄 davalloc.c
字号:
ExitOnError(status);
break;
case ALLOC_FREE_POOL:
status = MEM_CalcSizeAndHandleOfFreePoolInPageSpace(&availPageSpace, &pageHandle);
#ifndef DISABLE_BACKUP_RESERVE
if ((availPageSpace - (FDI_MemUsage.PageSpace.Reserves/FDI_PageSize)) < requiredPageSpace)
{
status = ERR_FLASH_FULL;
ExitOnError(status);
}
#else
if (availPageSpace < requiredPageSpace)
{
status = ERR_FLASH_FULL;
ExitOnError(status);
}
#endif
break;
#endif
*/
/*Unsupported Option End*/
default:
status = ERR_PARAM;
ExitOnError(status);
break;
}
/* Perform Allocation *
**********************
*/
/** ==================================================================== **/
/** Check for an free chunk at this address. If found, we are **/
/** converting a free chunk, and not using the free pool **/
/** ==================================================================== **/
SEARCH_Header2SearchInfo((&searchInfo), (&FDI_SearchHeader));
compareInfo.CompareValue = pageHandle;
compareInfo.NamePtr = 0;
compareInfo.NameSize = 0;
status = SEARCH_GetNextHeader(&searchInfo, SEARCH_ByObjectHeaderAddress, &compareInfo, FALSE);
if (status == ERR_NONE)
{
/* Found an existing object, but is it a free chunk? */
if (FHDR_GetAllocationStatus(&(searchInfo.HeaderPtr->FHdr)) != HDR_ALLOC_EMPTY)
{
/* MEM_CalcSizeAndHandleOfFreePoolInPageSpace should've returned a free chunk or free pool */
status = ERR_STATE;
ExitOnError(status);
}
else
{
convertingFreeChunk = TRUE;
}
}
#ifndef DISABLE_BACKUP_RESERVE
if (!convertingFreeChunk)
{
/* make sure we keep the appropriate amount of free
space reserved in the free pool */
if (requiredPageSpace > (FDI_MemUsage.PageSpace.Reserves/FDI_PageSize))
{
/* the new object is larger than the largest
object currently in DAV, so make sure we
have space for the new object and its backup */
if (availPageSpace < (requiredPageSpace*2))
{
status = ERR_FLASH_FULL;
ExitOnError(status);
}
}
else
{
/* the new object is smaller than the largest
object currently in DAV, so make sure we
have space for the backing up the largest
object after this one has been allocated */
if ((availPageSpace - requiredPageSpace) < (FDI_MemUsage.PageSpace.Reserves/FDI_PageSize))
{
status = ERR_FLASH_FULL;
ExitOnError(status);
}
}
}
#endif
if (convertingFreeChunk)
{
status = FHDR_AbsorbingHeaderInFlash(searchInfo.CurrObj.HeaderAddress, &(searchInfo.HeaderPtr->FHdr));
ExitOnError(status);
}
/* Translate to a fixed header */
SEARCH_ObjectInfo2Header(FDI_SearchInfo.HeaderPtr, obj_info_ptr);
/* set the object offset in the fixed header */
FHDR_SetHeaderIndexOffset(
&(FDI_SearchInfo.HeaderPtr->FHdr),
FHDR_CalcObjectOffset(pageHandle));
/* adjust size to include object header */
FHDR_SetSize(
&(FDI_SearchInfo.HeaderPtr->FHdr),
requiredPageSpace);
/* Write the new object header to flash */
status = HDR_CreateNextFixedHeaderEntry(&handle, &(FDI_SearchInfo.HeaderPtr->FHdr));
if(status == ERR_PLR_TEST_FAILURE)
{
CleanupAndExitOnError(status);
}
/* if we're converting a free chunk, determine if we
need to create another free chunk just past the
new object */
if (convertingFreeChunk)
{
newObjectSizeInPages = FHDR_GetSize(&(FDI_SearchInfo.HeaderPtr->FHdr));
freeChunkSizeInPages = FHDR_GetSize(&(searchInfo.HeaderPtr->FHdr));
if (newObjectSizeInPages < freeChunkSizeInPages)
{
/* Tracks the last object header */
FDI_LastHeaderEntry = FDI_NextFreeHeaderEntry;
/* Set next free header pointer below name field */
FDI_NextFreeHeaderEntry -= FHDR_CalcHeaderSize();
status = FHDR_CreateFreeChunkHeader(FDI_LastHeaderEntry,
FHDR_GetHeaderIndexOffset(&(FDI_SearchInfo.HeaderPtr->FHdr)) + newObjectSizeInPages,
freeChunkSizeInPages-newObjectSizeInPages);
CleanupAndExitOnError(status);
}
/* regardless, absorb the old free chunk header */
status = FHDR_AbsorbedHeaderInFlash(searchInfo.CurrObj.HeaderAddress, &(searchInfo.HeaderPtr->FHdr));
CleanupAndExitOnError(status);
}
/* Locate the header we just wrote, in order to calculate the object address */
FDI_SearchInfo.CurrObj.HeaderAddress = 0;
compareInfo.CompareValue = FDI_HT_NormalObject;
compareInfo.CompareValue2 = FHDR_GetHeaderIndexOffset(&(FDI_SearchInfo.HeaderPtr->FHdr));
status = SEARCH_GetNextHeader(&FDI_SearchInfo, SEARCH_ByPartialAllocFixedHdrComplete, &compareInfo, FALSE);
CleanupAndExitOnError(status);
/* create an object header to the write to the top of the object */
objHeader.UniqueId1 = 0xF0F0;
objHeader.Type = obj_info_ptr->ObjectType;
objHeader.NameSize = obj_info_ptr->NameSize;
objHeader.SecureId = obj_info_ptr->SecurityKey;
objHeader.RFU16_1 = 0xFFFF;
objHeader.RFU8_1 = 0xFF;
objHeader.UniqueId2 = 0xF0F0;
for(ii=0; ii<objHeader.NameSize; ii++) objHeader.Name[ii] = obj_info_ptr->Name[ii];
for( ; ii<FDI_MaxNameLength; ii++) objHeader.Name[ii] = 0;
status = OHDR_WriteObjectHeader(FDI_SearchInfo.CurrObj.ObjectAddress, &objHeader);
CleanupAndExitOnError(status);
/* return the address of the start of the object data to the caller */
obj_info_ptr->ObjectAddress = FDI_SearchInfo.CurrObj.ObjectAddress + OHDR_CalcHeaderSize();
/* A WIP object exists, do not allow allocation or */
/* reallocation until the object is complete. */
FDI_LockoutObjectAllocation = TRUE;
status = MEM_CalcMemoryStatistics(FALSE);
FDI_APIUnlock();
return status;
}
/*##################################################################
### FDI_AllocateFlashFreeChunk
###
### DESCRIPTION:
### This function allocates memory for a new free chunk. It is
### for internal use only, and is not an API that the customer
### should use, or would need to use.
###
### PARAMETERS:
### obj_info_ptr IN: obj_info is filled with all fields.
### OUT: Not used.
###
### RETURNS:
### When this function passes with no errors a value of 0 is
### returned otherwise, it returns a status of type ERR_CODE.
###
*/
ERR_CODE FDI_AllocateFlashFreeChunk( FDI_HandlePtr aHandlePtr, UINT32 aFreeSizeInPages )
{
ERR_CODE status;
UINT32 requiredParaSpace, requiredPageSpace;
UINT32 availParaSpace;
UINT32 availPageSpace;
FDI_Handle paraHandle;
FDI_Handle pageHandle;
HDR_FixedHeader freeHeader;
/* Multitasking API exclusivity. (This may not be necessary to the
full extent as it is done here, but for now it is the safest way.) */
FDI_APILock();
/* Clear Global flag for System State */
ClrSystemState(FDI_SystemState, FDI_ST_ReclaimFlag);
/* Parameter Checking *
**********************
*/
/* Check the min limits on the size. */
if (aFreeSizeInPages == 0)
{
status = ERR_PARAM;
ExitOnError(status);
}
/* Calculate Required Memory *
******************************
*/
/* This will contain the total page space requested */
requiredPageSpace = aFreeSizeInPages;
/* This will contain the total paragraph space requested */
requiredParaSpace = FHDR_CalcHeaderSize();
/* Calculate Available Memory *
* Always allocate the free *
* chunk on the free pool *
******************************
*/
status = MEM_CalcSizeAndHandleOfFreePoolInParaSpace(&availParaSpace, ¶Handle);
ExitOnError(status);
if (availParaSpace < requiredParaSpace)
{
/* trigger a paragraph reclaim instead of returning error */
status = RECLAIM_PARAGRAPH_PerformReclaim(enPerformCfgEntryReclaim, FALSE);
ExitOnError(status);
}
status = MEM_CalcSizeAndHandleOfFreePoolInPageSpace(&availPageSpace, &pageHandle);
ExitOnError(status);
if (availPageSpace < requiredPageSpace)
{
status = ERR_FLASH_FULL;
ExitOnError(status);
}
/* Perform Allocation *
**********************
*/
/* set the object offset in the fixed header */
FHDR_SetHeaderIndexOffset(&freeHeader, FHDR_CalcObjectOffset(pageHandle));
/* adjust size to include object header */
FHDR_SetSize(&freeHeader, requiredPageSpace);
/* Write the new object header to flash */
status = HDR_CreateNextFixedHeaderEntryFree(¶Handle, &freeHeader);
CleanupAndExitOnError(status);
/* return handle to free chunk in page space */
*aHandlePtr = pageHandle;
status = MEM_CalcMemoryStatistics(FALSE);
FDI_APIUnlock();
return status;
}
#endif /* DIRECT_ACCESS_VOLUME */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -