⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 davalloc.c

📁 Flash file system
💻 C
📖 第 1 页 / 共 2 页
字号:

void ALLOC_CalcFreeParaSpace(UINT32_PTR avail_space_ptr, UINT32_PTR end_space_ptr)
{
UINT32 used_space;

   /* The free space for paragraph objects is equal to the difference  */
   /*  between the end of used space minus the end of paragraph space. */
   
   /* Calculate the end of paragraph space (address). */
   *end_space_ptr = ALLOC_CalcEndOfParaSpace();

   /* Calculate the amount of the paragraph object space used. */
   used_space = FDI_LastObjectAddress + 1 - 
               (ALLOC_CalcReclaimableParas() + 
                  FDI_MemUsage.ParaSpace.Valid +
                     FDI_MemUsage.ParaOverhead.Valid + 
                        FDI_MemUsage.PageOverhead.Valid +
                           FDI_MemUsage.ParaOverhead.ReclaimReserves);
         
   *avail_space_ptr = used_space - *end_space_ptr;
}

void ALLOC_CalcFreePageSpace(UINT32_PTR avail_space_ptr, UINT32_PTR end_space_ptr)
{
UINT32 used_space;

   /* The free space for paragraph objects is equal to the difference  */
   /*  between the end of page space minus the end of used page space. */
   
   /* Calculate the end of page space (address). */
   *end_space_ptr = ALLOC_CalcEndOfPageSpace();
   
   if (*end_space_ptr == FDI_StartAddress)
   {
      *avail_space_ptr = 0;
      return;
   }
   
   /* Calculate the amount of the page object space used. */
   used_space = FDI_StartAddress + 
            FDI_MemUsage.PageSpace.Invalid + 
               FDI_MemUsage.PageSpace.Valid + 
                  FDI_MemUsage.PageOverhead.ReclaimReserves;
                                  
   *avail_space_ptr = *end_space_ptr - used_space;
}

/*#################################################################
  ### ALLOC_Check4ReclaimCleanup
  ###
  ### DESCRIPTION:
  ###   This function will GetMemoryStatistics and then issue a
  ###   paragraph reclaim if system reserves are in use.
  ###
  ### PARAMETERS:
  ###   None.
  ###
  ### 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 ALLOC_Check4ReclaimCleanup(UINT32 new_page_space, 
                                      UINT32 new_para_space)
{
ERR_CODE status;
UINT32     para_boundary, page_boundary;
 
   /* Now check if system reserves were used and a reclaim
    * is need to clear the used system reserves.
    */
   
   /* Calculate used space as worst case - including obj reserves */
   page_boundary = (ALLOC_CalcL12UsedPageSpace() + 
                   new_page_space + FDI_StartAddress - 1) + 
                                          ALLOC_CalcInvalidPageSpace();
               
   /* the 1 paragraph buffer between para and
    * page space.
    */            
   para_boundary = (FDI_LastObjectAddress - 
                    ((ALLOC_CalcL12UsedParaSpace() - 1) +
                     FDI_ParagraphLength)) -
                   (new_para_space - ALLOC_CalcInvalidParaSpace());
   
   /* If the boundaries + the invalid space boundaries
    * cross, then issue a reclaim.
    */ 
   if (CheckBoundaries(page_boundary, para_boundary))
   {
      status = RECLAIM_Cleanup(ReclaimAllObjects, FALSE);
      ExitOnError(status);
   }

   return ERR_NONE;
}

/*##################################################################
  ### FDI_AllocateFlash
  ###
  ### DESCRIPTION:
  ###   This function allocates memory for a new object by creating 
  ###   a new header with the characteristics defined by 
  ###   obj_info_ptr.  For objects with recovery Level0 or 1, the
  ###   header will be written as WriteInProgress and will need to 
  ###   have WriteComplete called.  Objects with recovery Level 2 
  ###   will have a the header written as Valid.
  ###
  ### 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_AllocateFlash( FDI_ObjectInfoPtr obj_info_ptr )
{
UINT32      required_para_space, required_page_space;
UINT32      used_para_space, used_page_space;
UINT32      new_para_boundary, new_page_boundary;
FDI_Handle  handle;
ERR_CODE  status;

HDR_CompareInfo compare_info;

   /* Check range of ObjectType */
   if(obj_info_ptr->ObjectType < MIN_DAV_TYPE )
   {
      return ERR_PARAM;
   }

   /* 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();

   /* Check if maximum number of objects is reached */
   if (FDI_PageObjectCounter >= DAV_NUM_CLASS_FILES)
   {
      FDI_APIUnlock();
      return ERR_MAX_EXISTS;
   } 

   /* A WIP object exists, do not allow allocation or */
   /*  reallocation until the object is complete.     */
   if (FDI_LockoutObjectAllocation)
   {
      FDI_APIUnlock();
      return ERR_WIP;
   }

   /* Clear Global flag for System State */
   ClrSystemState(FDI_SystemState, FDI_ST_ReclaimFlag);

   /* Parameter Checking *
    **********************
    */
    
   /* Make sure that the interface cannot see any system objects. */
   if (obj_info_ptr->ObjectType <= FDI_HT_EndReservedTypes)
   {
      FDI_APIUnlock();
      return ERR_PARAM;
   }
         
   /* Check the max limits on the size. */
   obj_info_ptr->Alignment = FDI_ALIGN_Page; /* always set the Alignment to Page */
   obj_info_ptr->RecoveryLevel = FDI_PLR_Level0; /* always set recovery level to 
                                                    PLR_Level0 */
   if (obj_info_ptr->ObjectSize >= FDI_MaxPageSize)
   {
      FDI_APIUnlock();
      return ERR_PARAM;
   }
   
     /* Check the min limits on the size. */
   if (obj_info_ptr->ObjectSize == 0)
   {
      FDI_APIUnlock();
      return ERR_PARAM;
   }
   
   /** ==================================================================== **/
   /** Check for an object with the same name & type.                       **/
   /**   (not allowed - accept for re-allocate)                             **/
   /** ==================================================================== **/
   HDR_InitSearchInfo((&FDI_SearchInfo), (&FDI_SearchHeader));             
   compare_info.CompareValue = obj_info_ptr->ObjectType;                   
   compare_info.NamePtr = (HDR_NamePtr)(obj_info_ptr->Name);
   compare_info.NameSize = obj_info_ptr->NameSize;
   status = GetNextHeader(&FDI_SearchInfo, HDR_ByNameType, &compare_info, FALSE); 
   if (status == ERR_NONE)
   {                                                                       
      /* Found an existing object */
                                                                           
      /* There was no override of duplicate object checking and */         
      /*  we found a duplicate object.                          */         
      if (!FDI_ReAllocateOverrideFlag)                                     
      {                                                                    
         FDI_APIUnlock();
         return ERR_EXISTS;                                   
      }                                                                    
   }                                                                       
   else                                                                    
   {                                                                       
      /* Object was not found */
                                                                           
      /* There was no duplicate and some error was returned, or */         
      /*  there was no duplicate and we were expecting one!     */         
      if ((status != ERR_NO_MORE_ENTRIES) || FDI_ReAllocateOverrideFlag)     
      {                                                                    
         FDI_APIUnlock();
         return status;                                                    
      }                                                                    
   }                                                                       
   
   /* Calculate Required Memory  *
    ******************************
    */
    
   /* This will contain the total page space requested */
   required_page_space = 0;
   
   /* This will contain the total paragraph space requested */
   required_para_space = HDR_CalcHdrSize(obj_info_ptr->NameSize);
   
   /* Calculate the requested memory.  We consider only page objects here, since
   we don't allow the user to create paragraph objects */
   
   
      /* Page objects use paragraph space for headers and page */
      /*  space for the actual object.                         */
      required_page_space += obj_info_ptr->ObjectSize * FDI_PageSize;
      
   
   if (!FDI_ReAllocateOverrideFlag)
   {
     /* Level 0 - Allocate
      *   if (Requested > Object Reserves)
      *      NEW RESERVES = Requested - Object Reserves
      *
      *   REQUIRED = Requested + New Reserves
      */
      if (required_para_space > ALLOC_CalcParaObjReserves())
      {
         required_para_space += required_para_space - 
                                 ALLOC_CalcParaObjReserves();
      }
                                 
      if (required_page_space > ALLOC_CalcPageObjReserves())
      {
         required_page_space += required_page_space - 
                                 ALLOC_CalcPageObjReserves();
      }                           
   }

   /* Check for available memory *
    ******************************
    */
   if (FDI_ReAllocateOverrideFlag)
   {
     /* Level 0 - Reallocate
      *   USED = Valid + System Reserves
      */
     used_para_space = ALLOC_CalcL0UsedParaSpace();
     used_page_space = ALLOC_CalcL0UsedPageSpace();
   }
   else
   {
     /* All others
      *   USED = Valid + System Reserves + Object Reserves
      */
     used_para_space = ALLOC_CalcL12UsedParaSpace();
     used_page_space = ALLOC_CalcL12UsedPageSpace();
   }

   new_page_boundary = used_page_space + 
               required_page_space + FDI_StartAddress - 1;
               
   new_para_boundary = (FDI_LastObjectAddress - 
              (used_para_space + required_para_space - 1));
   
   status = CheckBoundaries(new_page_boundary, new_para_boundary);
   ExitOnError(status);
   
   /* Request Granted, Check Reclaim *
    **********************************
    */

   /* Add PLR State Recording Support */
   
   RECOVER_MarkDatabaseBusy();
   
   if(ReclaimState.ReclaimInProgress != TRUE)       
      status = RECLAIM_Cleanup(ReclaimParagraphObjects, FALSE);

   ExitOnError(status);

   /* Perform Allocation *
    **********************
    */
   
   HDR_ObjectInfo2Header(FDI_SearchInfo.HeaderPtr, obj_info_ptr);
   
   /* Write the header to flash! */
   status = HDR_CreateHeaderEntry(&handle, FDI_SearchInfo.HeaderPtr);
   if(status == ERR_PLR_TEST_FAILURE)
   {
      FDI_APIUnlock();
      return(status);
   }

   CleanupAndExitOnError(status);

   RECOVER_MarkDatabaseReady();
   
   /* Locate the new object, and return the address to the caller */
   FDI_SearchInfo.CurrObj.HeaderAddress = 0;
   FDI_SearchInfo.CurrObj.ConfigEntry.Offset = 0;
   FDI_SearchInfo.CurrObj.ConfigEntry.Status = 0;

   status = GetNextHeader(&FDI_SearchInfo, HDR_ByNameType, &compare_info, FALSE);
   obj_info_ptr->ObjectAddress = FDI_SearchInfo.CurrObj.ObjectAddress;

   /* if the allocation passes, we lockout the interface
    * while a WIP object exists.  The application must call
    * FDI_WriteComplete to close this object..
    */
   if (!status)
   {
      /* A WIP object exists, do not allow allocation or */
      /*  reallocation until the object is complete.     */
      if ((obj_info_ptr->RecoveryLevel == FDI_PLR_Level0) ||
                         (obj_info_ptr->RecoveryLevel == FDI_PLR_Level1))
      {
         FDI_LockoutObjectAllocation = TRUE;
      }
      status = HDR_CalcMemoryStatistics(FALSE);
   }

   /* Increment the number of existing page object headers in flash */
   if (!status)
   {
      FDI_PageObjectCounter++;
   }

   FDI_APIUnlock();
   return status;
}

#endif /* DIRECT_ACCESS_VOLUME */

⌨️ 快捷键说明

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