davotttbl.c

来自「FDI Intel开发的FLASH文件系统,功能很强大」· C语言 代码 · 共 1,362 行 · 第 1/3 页

C
1,362
字号
  ###
  ### DESCRIPTION:
  ###   This function will set the destination offset for the page
  ###   object entry.
  ###
  ### PARAMETERS:
  ###   aPtr   - Pointer to the ott leader
  ###   aDWord - The value of the offset
  ###
  ### RETURNS:
  ###   None
  ###*/
void OTTTBL_SetOttEntryDestination(OTTTBL_EntryPtr aPtr, UINT32 aDWord)
{
   aPtr->Destination = aDWord & DWORDMAX;
}


/*#################################################################
  ### OTTTBL_GetOttEntrySize
  ###
  ### DESCRIPTION:
  ###   This function will extract the page object size (in pages)
  ###   from the ott entry.
  ###
  ### PARAMETERS:
  ###   aPtr - Pointer to the ott entry
  ###
  ### RETURNS:
  ###   A object's size in pages
  ###*/
UINT32 OTTTBL_GetOttEntrySize(OTTTBL_EntryPtr aPtr)
{
   return aPtr->Size;
}
/*#################################################################
  ### OTTTBL_SetOttEntrySize
  ###
  ### DESCRIPTION:
  ###   This function will set the size for the page object in
  ###   pages.
  ###
  ### PARAMETERS:
  ###   aPtr   - Pointer to the ott entry
  ###   aDWord - The value of the size
  ###
  ### RETURNS:
  ###   None
  ###*/
void OTTTBL_SetOttEntrySize(OTTTBL_EntryPtr aPtr, UINT32 aDWord)
{
   aPtr->Size = aDWord & DWORDMAX;
}


/*#################################################################
  ### OTTTBL_GetOttEntryPinStatus
  ###
  ### DESCRIPTION:
  ###   This function will extract the pin status for a object in
  ###   page space from the ott entry.
  ###
  ### PARAMETERS:
  ###   aPtr - Pointer to the ott entry
  ###
  ### RETURNS:
  ###   A object's pin status (pinned, not pinned, pinned for backup ,etc.)
  ###*/
UINT8 OTTTBL_GetOttEntryPinStatus(OTTTBL_EntryPtr aPtr)
{
   return aPtr->PinStatusKeepStatus & SNIBBLEMAX;
}
/*#################################################################
  ### OTTTBL_SetOttEntryPinStatus
  ###
  ### DESCRIPTION:
  ###   This function will set the pin status for the page object
  ###
  ### PARAMETERS:
  ###   aPtr   - Pointer to the ott entry
  ###   aDWord - The pin status
  ###
  ### RETURNS:
  ###   None
  ###*/
void OTTTBL_SetOttEntryPinStatus(OTTTBL_EntryPtr aPtr, UINT8 aNibble)
{
   aPtr->PinStatusKeepStatus = (aPtr->PinStatusKeepStatus & FNIBBLEMAX) | (aNibble & SNIBBLEMAX);
}


/*#################################################################
  ### OTTTBL_GetOttEntryKeepStatus
  ###
  ### DESCRIPTION:
  ###   This function will extract the entry keep status for a object
  ###   in page space from the ott entry. This is only used when
  ###   building the ott in sram.
  ###
  ### PARAMETERS:
  ###   aPtr - Pointer to the ott entry
  ###
  ### RETURNS:
  ###   A object's keep status (for flash, not for flash, etc.)
  ###*/
UINT8 OTTTBL_GetOttEntryKeepStatus(OTTTBL_EntryPtr aPtr)
{
   return (aPtr->PinStatusKeepStatus >> 4) & SNIBBLEMAX;

}
/*#################################################################
  ### OTTTBL_SetOttEntryKeepStatus
  ###
  ### DESCRIPTION:
  ###   This function will set the keep status for a object in page
  ###   space. This is only used when building the ott in sram.
  ###
  ### PARAMETERS:
  ###   aPtr   - Pointer to the ott entry
  ###   aDWord - The keep status
  ###
  ### RETURNS:
  ###   None
  ###*/
void OTTTBL_SetOttEntryKeepStatus(OTTTBL_EntryPtr aPtr, UINT8 aNibble)
{
   aPtr->PinStatusKeepStatus = ((aNibble << 4) & FNIBBLEMAX) | (aPtr->PinStatusKeepStatus & SNIBBLEMAX);
}


/*#################################################################
  ### OTTTBL_GetOttEntryType
  ###
  ### DESCRIPTION:
  ###   This function will extract the entry type for a object in
  ###   page space from the ott entry. The initial type of the
  ###   object can change after it is placed in the ott table.
  ###
  ### PARAMETERS:
  ###   aPtr - Pointer to the ott entry
  ###
  ### RETURNS:
  ###   A object's entry type (dirty, free, user, etc.)
  ###*/
UINT8 OTTTBL_GetOttEntryType(OTTTBL_EntryPtr aPtr)
{ 
   return aPtr->Type;
}
/*#################################################################
  ### OTTTBL_SetOttEntryType
  ###
  ### DESCRIPTION:
  ###   This function will set the entry type for the object in
  ###   page space.
  ###
  ### PARAMETERS:
  ###   aPtr   - Pointer to the ott entry
  ###   aDWord - The entry type
  ###
  ### RETURNS:
  ###   None
  ###*/
void OTTTBL_SetOttEntryType(OTTTBL_EntryPtr aPtr, UINT8 aByte)
{
   aPtr->Type = aByte & BYTEMAX;
}


/*=============================== SEARCH =============================*/
/*#################################################################
  ### OTTTBL_GetFirstOttEntry
  ###
  ### DESCRIPTION:
  ###   This function will retreive the first ott entry along with
  ###   ott table leader.
  ###
  ### PARAMETERS:
  ###   aHandlePtr - Handle to the ott entry
  ###   aEntryPtr  - Pointer to the ott entry
  ###   aLeaderPtr - Pointer to the ott leader
  ###
  ### 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 OTTTBL_GetFirstOttEntry(FDI_Handle* aHandlePtr, OTTTBL_EntryPtr aEntryPtr, OTTTBL_LeaderPtr aLeaderPtr)
{
   ERR_CODE status;

   HDR_Header             allHeaders;
   SEARCH_SearchInfo      result;
   SEARCH_CompareInfo     compare_info;

   OTTTBL_ObjectTablePtr  tablePtr = 0; /* Zero, is used to compute offset */
   UINT32                 offset;
   FDI_Handle             aFHDRHandle;

   CFGTBL_PgReclaimEntry  cfgPageEntry;
   FDI_Handle             cfgPageHandle;
   UINT8                  ctIndex;
   FDI_Handle             ctEntryHandle;

   /* Find the fixed header for the table */
   result.HeaderPtr                   = &allHeaders;
   result.CurrObj.HeaderAddress       = 0;
   result.CurrObj.ObjectAddress       = 0;
   result.CurrObj.ObjectSize          = 0;
   compare_info.CompareValue          = FDI_HT_ObjTrkTable; /* OTT */
   status = SEARCH_GetNextHeader(&result, SEARCH_ByValidType/*SEARCH_ByTypeOnly*/, &compare_info, FALSE);
   if(status != ERR_NONE)
   {
      if(status == ERR_NO_MORE_ENTRIES)
      {
         /* Fixed Header does not exists */
         aFHDRHandle = 0;
         UTIL_ClearVariable((UINT8*)aLeaderPtr, sizeof(OTTTBL_Leader), 0xFF);
      }
      else
      {
         return status;
      }
   }
   else
   {
      /* A fixed header exists */
      /* PERF_FIX Need to find a more efficent means */
      aFHDRHandle = (FDI_Handle)result.CurrObj.HeaderAddress;
      UTIL_CopyVariable((UINT8*)&result.HeaderPtr->OHdr, (UINT8*)aLeaderPtr, sizeof(OTTTBL_Leader));
   }


   /* Find a valid Ct Entry for the table */
   if(CFGTBL_GetCtEntryByStatus(FDI_ParaSpaceAddressBottom, CFGTBL_CtEntryType_OTT, CFGTBL_CtEntryStatus_EntryValid,
      &cfgPageEntry, &cfgPageHandle, &ctIndex, &ctEntryHandle) == FALSE)
   {
      /* Ct entry does not exist */
      /* No ct entry handle or first entry to copy */
      UTIL_ClearVariable((UINT8*)aEntryPtr,  sizeof(OTTTBL_Entry),  0xFF);
      *aHandlePtr = 0;
      status     = ERR_NOTEXISTS;
   }
   else
   {
      if(CFGTBL_GetCtAllocationStatus(&cfgPageEntry.CtEntry[ctIndex]) == CFGTBL_CtAllocationStatus_Valid)
      {
         /* Compute and copy the ct entry handle */
         offset       = (UINT32)&tablePtr->OttEntry[0] - (UINT32)tablePtr;
         *aHandlePtr  = (UINT32)result.CurrObj.ObjectAddress + (UINT32)offset;

         /* Read in the first entry */
         status = FLASH_ReadBuffer(*aHandlePtr, (UINT8*)aEntryPtr, sizeof(OTTTBL_Entry));
         if(status != ERR_NONE)
         {
            return status;
         }
      }
      else
      {
         /* Ct entry exists, however, the allocation is invalid */
         UTIL_ClearVariable((UINT8*)aEntryPtr,  sizeof(OTTTBL_Entry),  0xFF);
         *aHandlePtr = 0;
         status     = ERR_NOTEXISTS;
      }
   }

   /* Status of a ct entry that is valid */
   /* If a powerloss, it is possible that the ct entry exists, however, the fixed header does not */
   return status;
}
/*#################################################################
  ### OTTTBL_GetNextOttEntry
  ###
  ### DESCRIPTION:
  ###   This function will retreive the next ott entry with respect
  ###   to the current ott entry handle.
  ###
  ### PARAMETERS:
  ###   aFlashNextPtr    - Upon return, handle to the next ott entry
  ###   aEntryPtr        - Upon return, pointer to the ott entry
  ###   aFlashCurrentPtr - Handle to the a ott entry
  ###
  ### 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 OTTTBL_GetNextOttEntry(FDI_Handle* aFlashNextPtr, OTTTBL_EntryPtr aEntryPtr, FDI_Handle* aFlashCurrentPtr)
{
   ERR_CODE status;

   /* Compute the next entry address*/
   *aFlashNextPtr = ((FDI_Handle)*aFlashCurrentPtr + (FDI_Handle)sizeof(OTTTBL_Entry));

   /* Read the next entry */
   status = FLASH_ReadBuffer((UINT32)*aFlashNextPtr, (UINT8*)aEntryPtr, sizeof(OTTTBL_Entry));
   if(status != ERR_NONE)
   {
      return status;
   }

   return status;
}


ERR_CODE OTTTBL_SortObjects (OTTTBL_EntryPtr aOTTTBL, UINT16 aNumObjects, EnSortOrder aSortOrder)
{
   ERR_CODE     status       = ERR_NONE;

   BOOLEAN      OrderChanged = TRUE;
   UINT16       Count;
   OTTTBL_Entry TempEntry;

   if (aNumObjects <= 0)
   {
      status = ERR_NOTHING_TO_DO;
   }
   else
   {
      if (aSortOrder == enByDestinationAddress)
      {
         /* Keep on sorting until we go all the way through and don't change anything */
         while (OrderChanged == TRUE)
         {
			   /* set OrderChanged = false each pass through the loop */
            OrderChanged = FALSE;
			   for (Count = aNumObjects - 1; Count > 0; Count--)
            {
				   /* if EntireObjList(count).dest_addr < EntireObjList(count -1).dest_addr */
               if (OTTTBL_GetOttEntryDestination(&aOTTTBL[Count]) < OTTTBL_GetOttEntryDestination(&aOTTTBL[Count-1]))
               {
					   /* swap positions of nodes */
                  TempEntry        = aOTTTBL[Count];
                  aOTTTBL[Count]   = aOTTTBL[Count-1];
                  aOTTTBL[Count-1] = TempEntry;

					   /* set OrderChanged = true */
                  OrderChanged     = TRUE;
               }
            } /* for */
         } /* while */
      }
	   else /* SortOrder = enBySourceAddress */
      {
         /* Keep on sorting until we go all the way through and don't change anything */
         while (OrderChanged == TRUE)
         {
			   /* set OrderChanged = false each pass through the loop */
            OrderChanged = FALSE;
			   for (Count = aNumObjects - 1; Count > 0; Count--)
            {
				   /* if EntireObjList(count).src_addr < EntireObjList(count -1).src_addr */
               if (OTTTBL_GetOttEntrySource(&aOTTTBL[Count]) < OTTTBL_GetOttEntrySource(&aOTTTBL[Count-1]))
               {
					   /* swap positions of nodes */
                  TempEntry        = aOTTTBL[Count];
                  aOTTTBL[Count]   = aOTTTBL[Count-1];
                  aOTTTBL[Count-1] = TempEntry;

					   /* set OrderChanged = true */
                  OrderChanged     = TRUE;
               }
            } /* for */
         } /* while */
      }
   }
   return status;
}

ERR_CODE OTTTBL_SortRangeNodesByRangeSize(RECL_RangeNode* aMoveableRangeTable, UINT8 aNumberOfMoveableRanges)
{
   ERR_CODE status = ERR_NONE;

   BOOLEAN        orderChanged = TRUE;
   UINT16         count;
   RECL_RangeNode tempNode;

   if(aNumberOfMoveableRanges <= 0)
   {
      status = ERR_NOTHING_TO_DO;
   }
   else
   {
      /* Keep on sorting until we go all the way through and don't change anything */
      while(orderChanged == TRUE)
      {
         /* set OrderChanged = false each pass through the loop */
         orderChanged = FALSE;
			for (count=aNumberOfMoveableRanges-1; count>0; count--)
         {
            /* If the size of our MoveableRange > the size of the one right before it */
            if(aMoveableRangeTable[count].RangeSize > aMoveableRangeTable[count-1].RangeSize)
            {
               /* swap positions of the nodes */
               tempNode                     = aMoveableRangeTable[count];
               aMoveableRangeTable[count]   = aMoveableRangeTable[count-1];
               aMoveableRangeTable[count-1] = tempNode;

               /* set OrderChanged = true */
               orderChanged                 = TRUE;
            }
         } /* for */
      } /* while */
   }

   return status;
}

ERR_CODE OTTTBL_SortRangeNodesByIndex(RECL_RangeNode* aMoveableRangeTable, UINT8 aNumberOfMoveableRanges)
{
   ERR_CODE status = ERR_NONE;

   BOOLEAN        orderChanged = TRUE;
   UINT16         count;
   RECL_RangeNode tempNode;

   if(aNumberOfMoveableRanges <= 0)
   {
      status = ERR_NOTHING_TO_DO;
   }
   else
   {
      /* Keep on sorting until we go all the way through and don't change anything */
      while(orderChanged == TRUE)
      {
         /* set OrderChanged = false each pass through the loop */
         orderChanged = FALSE;
			for (count=aNumberOfMoveableRanges-1; count>0; count--)
         {
            /* If the size of our MoveableRange > the size of the one right before it */
            if(aMoveableRangeTable[count].StartingObjListIndex < aMoveableRangeTable[count-1].StartingObjListIndex)
            {
               /* swap positions of the nodes */
               tempNode                     = aMoveableRangeTable[count];
               aMoveableRangeTable[count]   = aMoveableRangeTable[count-1];
               aMoveableRangeTable[count-1] = tempNode;

               /* set OrderChanged = true */
               orderChanged                 = TRUE;
            }
         } /* for */
      } /* while */
   }

   return status;
}

OTTTBL_EntryPtr OTTTBL_FindBestFitObject(OTTTBL_EntryPtr aEntryPtr, UINT32 aSize, RECL_RangeNode* aMoveableRangeTable, UINT8 aNumberOfMoveableRanges, UINT8 aStartingRange)
{
   OTTTBL_EntryPtr ottEntryPtr;

   UINT8  moveableRangeIndex;
   UINT16 objectIndex;
   UINT32 objectSize;
   UINT16 bestCandidateIndex      = 0;
   UINT32 bestCandidateObjectSize = 0;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?