davsearch.c

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

C
1,358
字号
            (FHDR_IsPageObject(&currHeader))
         )
      {
	      /* compare the offset in this header with the current offset */
	      /* save it if it's higher */
         candidateOffset = FHDR_GetHeaderIndexOffset(&currHeader);
         if (candidateOffset < currOffset)
	      {
	         /* this object exists in page space below the current object */
            /* and it's below than any previous objects found */
            currOffset = candidateOffset;
            lastHeaderHandle = currHeaderHandle;
         }
      }
      currHeaderHandle = SEARCH_CalcNextHdrAddr(currHeaderHandle);
   } while ((status == ERR_NONE) && 
            (FHDR_GetHeaderStatus(&currHeader) != HDR_HEADER_UNUSED));

   ExitOnError(status);

   if (currOffset == HDR_INDEX_OFFSET_UNUSED)
   {
      /* the page space is empty */
      return ERR_NO_MORE_ENTRIES;
   }
   else
   {
      *aHandlePtr = lastHeaderHandle;
   }
   return status;
}

/*#################################################################
  ### SEARCH_CalcLastUserObjectHdrAddr
  ### 
  ### DESCRIPTION:
  ###   This function will compute the address of the header of
  ###   the last user object, dirty chunk, or free chunk that
  ###   exists in page space.
  ### 
  ### PARAMETERS:
  ###   aCurrentHeaderHandle - Address of the current header.
  ###   aNextHeaderHandlePtr - Ptr to address of the next header.
  ### 
  ### RETURNS:
  ###   Error status.
  ###*/
ERR_CODE SEARCH_CalcLastUserObjectHdrAddr(FDI_HandlePtr aHandlePtr)
{
ERR_CODE                  status = ERR_NONE;
FDI_Handle                currHeaderHandle = DAV_MEM_FirstHeaderAddr;
FDI_Handle                lastHeaderHandle = DAV_MEM_FirstHeaderAddr;
HDR_FixedHeader           currHeader;
UINT32                    currOffset = 0;
UINT32                    candidateOffset = 0;

   /* check to be sure that at least one page object exists */
   if (FDI_LastHeaderEntry > DAV_MEM_FirstHeaderAddr)
   {
      /* the page space is empty */
      return ERR_NO_MORE_ENTRIES;
   }

   HDR_InitHeaderAttr1(currHeader.Attr1);
   HDR_InitHeaderAttr2(currHeader.Attr2);

   do
   {
      status = FHDR_ReadFixedHeader(currHeaderHandle, &currHeader, FALSE);
      /* if the header points to a real user object, dirty chunk, or free chunk in page space */
      if (
            (status == ERR_NONE) &&
            (FHDR_GetHeaderStatus(&currHeader) == HDR_HEADER_VALID) && 
            (FHDR_GetAbsorbStatus(&currHeader) != HDR_ABSORBED) && 
            (FHDR_IsPageObject(&currHeader))
         )
      {
	      /* compare the offset in this header with the current offset */
	      /* save it if it's higher */
         candidateOffset = FHDR_GetHeaderIndexOffset(&currHeader);
         if (candidateOffset >= currOffset)
	      {
	         /* this object exists in page space above the current object */
            /* and it's above than any previous objects found */
            currOffset = candidateOffset;
            lastHeaderHandle = currHeaderHandle;
         }
      }
      currHeaderHandle = SEARCH_CalcNextHdrAddr(currHeaderHandle);
   } while ((status == ERR_NONE) && 
            (FHDR_GetHeaderStatus(&currHeader) != HDR_HEADER_UNUSED));

   ExitOnError(status);

   *aHandlePtr = lastHeaderHandle;

   return status;
}


/*#################################################################
  ### SEARCH_CalcFirstInternalObjectHdrAddr
  ### 
  ### DESCRIPTION:
  ###   This function will compute the address of the header of
  ###   the first user object, dirty chunk, or free chunk that
  ###   exists in page space.
  ### 
  ### PARAMETERS:
  ###   aCurrentHeaderHandle - Address of the current header.
  ###   aNextHeaderHandlePtr - Ptr to address of the next header.
  ### 
  ### RETURNS:
  ###   Error status.
  ###*/
ERR_CODE SEARCH_CalcFirstInternalObjectHdrAddr(FDI_HandlePtr aHandlePtr)
{
ERR_CODE                  status = ERR_NONE;
FDI_Handle                currHeaderHandle = DAV_MEM_FirstHeaderAddr;
FDI_Handle                lastHeaderHandle = DAV_MEM_FirstHeaderAddr;
HDR_FixedHeader           currHeader;
UINT32                    currOffset = HDR_INDEX_OFFSET_UNUSED;
UINT32                    candidateOffset = 0;

   HDR_InitHeaderAttr1(currHeader.Attr1);
   HDR_InitHeaderAttr2(currHeader.Attr2);

   do
   {
      status = FHDR_ReadFixedHeader(currHeaderHandle, &currHeader, FALSE);
      /* if the header points to a real user object, dirty chunk, or free chunk in page space */
      if (
            (status == ERR_NONE) &&
            (FHDR_GetHeaderStatus(&currHeader) == HDR_HEADER_VALID) && 
            (FHDR_GetAbsorbStatus(&currHeader) != HDR_ABSORBED) && 
            (FHDR_IsParaObject(&currHeader))
         )
      {
	      /* compare the offset in this header with the current offset */
	      /* save it if it's higher */
         candidateOffset = FHDR_GetHeaderIndexOffset(&currHeader);
         if (candidateOffset < currOffset)
	      {
	         /* this object exists in para space below the current object */
            /* and it's below than any previous objects found */
            currOffset = candidateOffset;
            lastHeaderHandle = currHeaderHandle;
         }
      }
      currHeaderHandle = SEARCH_CalcNextHdrAddr(currHeaderHandle);
   } while ((status == ERR_NONE) && 
            (FHDR_GetHeaderStatus(&currHeader) != HDR_HEADER_UNUSED));

   ExitOnError(status);

   if (currOffset == HDR_INDEX_OFFSET_UNUSED)
   {
      /* the page space is empty */
      return ERR_NO_MORE_ENTRIES;
   }
   else
   {
      *aHandlePtr = lastHeaderHandle;
   }
   return status;
}


/*#################################################################
  ### SEARCH_CalcNextUserObjectHdrAddr
  ### 
  ### DESCRIPTION:
  ###   This function will take the current header handle and use it to
  ###   calculate the address of the header of the object that
  ###   physically resides above and closest to the current object
  ###   in the page space.
  ### 
  ### PARAMETERS:
  ###   aCurrentHeaderHandle - Address of the current header.
  ###   aNextHeaderHandlePtr - Ptr to address of the next header.
  ### 
  ### RETURNS:
  ###   Error status.
  ###*/
ERR_CODE SEARCH_CalcNextUserObjectHdrAddr(FDI_Handle    aCurrentHeaderHandle, 
                                          FDI_HandlePtr aNextHeaderHandlePtr)
{
ERR_CODE        status = ERR_NONE;

HDR_FixedHeader currHeader;
UINT32          currOffset = 0;
FDI_Handle      currHeaderHandle = aCurrentHeaderHandle;
UINT32          nextOffset = HDR_INDEX_OFFSET_UNUSED;
UINT32          candidateOffset;

   /* set the initial value for currHeaderHandle if not valid */
   if (currHeaderHandle > FDI_ParaSpaceAddressTop || currHeaderHandle < FDI_ParaSpaceAddressBottom)
   {
      /* current header handle is not valid; set up to find first user object in page space */
      status = SEARCH_CalcFirstUserObjectHdrAddr(&currHeaderHandle);
      ExitOnError(status);
      /* if invalid current object hdr handle was given, return handle to first object header */
      *aNextHeaderHandlePtr = currHeaderHandle;
      return status;
   }

   /* set the initial value for the next header to the first free header entry */
   *aNextHeaderHandlePtr = FDI_NextFreeHeaderEntry;

   /* Read the current header */
   status = FHDR_ReadFixedHeader(currHeaderHandle, &currHeader, FALSE);
   ExitOnError(status);
   if (
      (FHDR_GetHeaderStatus(&currHeader) == HDR_HEADER_VALID) && 
      (FHDR_GetAbsorbStatus(&currHeader) != HDR_ABSORBED) && 
      (FHDR_IsPageObject(&currHeader))
      )
   {
      currOffset = FHDR_GetHeaderIndexOffset(&currHeader);
   }
   else
   {
      /* can't compute the next user object header given an internal object header */
      status = ERR_NOT_DONE;
   }
   ExitOnError(status);

   currHeaderHandle = DAV_MEM_FirstHeaderAddr;

   do
   {
      status = FHDR_ReadFixedHeader(currHeaderHandle, &currHeader, FALSE);
      ExitOnError(status);
      /* if the header points to a real user object, dirty chunk or free chunk */
      if (
            (FHDR_GetHeaderStatus(&currHeader) == HDR_HEADER_VALID) && 
            (FHDR_GetAbsorbStatus(&currHeader) != HDR_ABSORBED) && 
            (FHDR_IsPageObject(&currHeader))
         )
      {
	      /* compare the offset in this header with the current header */
	      /* and with the header offset of the closest object found so far */
         candidateOffset = FHDR_GetHeaderIndexOffset(&currHeader);
         if ((candidateOffset > currOffset) && (candidateOffset < nextOffset))
	      {
	         /* this object exists in page space above the current object */
            /* and it's closer than any previous objects found */
            nextOffset = candidateOffset;
            *aNextHeaderHandlePtr = currHeaderHandle;
         }
      }
     currHeaderHandle = SEARCH_CalcNextHdrAddr(currHeaderHandle);
   } while ((status == ERR_NONE) && (FHDR_GetHeaderStatus(&currHeader) != HDR_HEADER_UNUSED));

   if (*aNextHeaderHandlePtr == FDI_NextFreeHeaderEntry)
   {
      status = ERR_NO_MORE_ENTRIES;
   }

   return status;
}

/*#################################################################
  ### SEARCH_CalcPrevUserObjectHdrAddr
  ### 
  ### DESCRIPTION:
  ###   This function will take the current header handle and use it to
  ###   calculate the address of the header of the object that
  ###   physically resides below and closest to the current object
  ###   in the page space.
  ### 
  ### PARAMETERS:
  ###   aCurrentHeaderHandle - Address of the current header.
  ###   aPrevHeaderHandlePtr - Ptr to address of the next header.
  ### 
  ### RETURNS:
  ###   Error status.
  ###*/
ERR_CODE SEARCH_CalcPrevUserObjectHdrAddr(FDI_Handle    aCurrentHeaderHandle, 
                                          FDI_HandlePtr aPrevHeaderHandlePtr)
{
ERR_CODE        status = ERR_NONE;

HDR_FixedHeader currHeader;
UINT32          currOffset = 0;
FDI_Handle      currHeaderHandle = aCurrentHeaderHandle;
UINT32          prevOffset = 0;
UINT32          candidateOffset;

   /* set the initial value for currHeaderHandle if not valid */
   if (currHeaderHandle > FDI_ParaSpaceAddressTop || currHeaderHandle < FDI_ParaSpaceAddressBottom)
   {
      /* current header handle is not valid; set up to find first user object in page space */
      status = SEARCH_CalcLastUserObjectHdrAddr(&currHeaderHandle);
      ExitOnError(status);

      /* if invalid current object hdr handle was given, return handle to last object header */
      *aPrevHeaderHandlePtr = currHeaderHandle;
      return status;
   }

   /* set the initial value for the next header to the first free header entry */
   *aPrevHeaderHandlePtr = FDI_NextFreeHeaderEntry;

   /* Read the current header */
   status = FHDR_ReadFixedHeader(currHeaderHandle, &currHeader, FALSE);
   ExitOnError(status);
   if (
      (FHDR_GetHeaderStatus(&currHeader) == HDR_HEADER_VALID) && 
      (FHDR_GetAbsorbStatus(&currHeader) != HDR_ABSORBED) && 
      (FHDR_IsPageObject(&currHeader))
      )
   {
      currOffset = FHDR_GetHeaderIndexOffset(&currHeader);
   }
   else
   {
      /* can't compute the next user object header given an internal object header */
      status = ERR_NOT_DONE;
   }
   ExitOnError(status);

   currHeaderHandle = DAV_MEM_FirstHeaderAddr;

   do
   {
      status = FHDR_ReadFixedHeader(currHeaderHandle, &currHeader, FALSE);
      ExitOnError(status);
      /* if the header points to a real user object, dirty chunk or free chunk */
      if (
            (FHDR_GetHeaderStatus(&currHeader) == HDR_HEADER_VALID) && 
            (FHDR_GetAbsorbStatus(&currHeader) != HDR_ABSORBED) && 
            (FHDR_IsPageObject(&currHeader))
         )
      {
	      /* compare the offset in this header with the current header */
	      /* and with the header offset of the closest object found so far */
         candidateOffset = FHDR_GetHeaderIndexOffset(&currHeader);
         if ((candidateOffset < currOffset) && (candidateOffset > prevOffset))
	      {

⌨️ 快捷键说明

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