📄 cellinfo.c
字号:
/**
* CellInfo.c
*
* This file contains methods for storing and defining
* cell information. It is designed to be used by Format
* and Initialize routines.
*
* CellInfo functions are used by many different functions through out the
* DMS software.
*
* Functions in this file do not read or write to the flash
* they only organize and store the data given to them.
*
* Usage durring Format is as follows:
* dms_CellInfoDefineCellList is called at the beginning of a
* where all cell information is provided.
* Then dms_CellInfoCheckValidity() is called to check cell values.
* dms_CellInfoGetCount(), dms_CellInfoGetLargestCellBlockCount()
* dms_CellInfoGetTotalBlockCount(), and dms_CellInfoGetBlockCount(),
* are used through out the format process.
* dms_CellInfoFinalize() is called at the end of a Format.
*
* Usage is as follows:
* Durring Initialization dms_CellInfoDefineCellCount() is called after
* the cell count is read from the sector header.
* After all blocks in all sectors are read in and inserted into the
* CellTable then dms_CellInfoDefineCellBlockCount() is called for each
* cell. The block count based on the number of blocks found for that cell.
* dms_CellInfoCheckValidity() is called at the end of Initialization.
* dms_CellInfoGetCount(), dms_CellInfoGetLargestCellBlockCount()
* dms_CellInfoGetTotalBlockCount(), and dms_CellInfoGetBlockCount(),
* are used through out Reading, Writing, and Finalizing.
* dms_CellInfoFinalize() is called at the end of a Shutdown.
*
*/
#include "CellInfo.h"
#include <malloc.h>
/* locally used variables */
static WORD lwCellCount = 0;
static WORD *lpwCellBlockCount = NULL;
/**
* dms_CellInfoDefineCellList
* Creates a local copy of the cell information. Performs some
* basic validity checks on the cell information.
* This function or dms_CellInfoDefineCellCount,
* dms_CellInfoDefineIncrementCellBlockCount, and dms_CellInfoCheckValidity
* must be called before any other functions in this file are called.
*
* Prerequisites:
* None.
* Uses:
* dms_CellInfoDefineCellCount()
* dms_CellInfoCheckValidity()
* Used By:
* dms_CellFormat()
*
*/
DMS_STATUS dms_CellInfoDefineCellList(DWORD *apdwCellSize, WORD awCellCount){
DMS_STATUS eTmpStatus;
WORD wCellIndex;
eTmpStatus = dms_CellInfoDefineCellCount(awCellCount);
if (eTmpStatus != No_Error){
return eTmpStatus;
}
for (wCellIndex = 0; wCellIndex < awCellCount; wCellIndex++){
if (apdwCellSize[wCellIndex] % sizeof(DATABLOCK) != 0) {
return Uneven_Cell_Size;
}
lpwCellBlockCount[wCellIndex] = (WORD) (apdwCellSize[wCellIndex] / sizeof(DATABLOCK));
}
eTmpStatus = dms_CellInfoCheckValidity();
if (eTmpStatus != No_Error){
return eTmpStatus;
}
return No_Error;
}
/**
* dms_CellInfoFinalize
* Frees memory used by these functions.
*
* Prerequisites:
* None.
* Uses:
* dms_CellInfoDefineCellCount()
* dms_CellInfoCheckValidity()
* Used By:
* dms_CellFormat()
* dms_CellTableInitialize()
* dms_CellTableFinalize()
*/
DMS_STATUS dms_CellInfoFinalize(void){
if (lpwCellBlockCount != NULL){
free(lpwCellBlockCount);
lpwCellBlockCount = NULL;
}
lwCellCount = 0;
return No_Error;
}
/**
* dms_CellInfoDefineCellCount
* Allocates memory for the local copy of the cell info.
* This function is designed to be called by the Initializion
* routine before reading the sector block table.
*
* Prerequisites:
* None.
* Uses:
* malloc()
* dms_CellInfoFinalize()
* Used By:
* dms_CellTableInitialize(WORD awCellCount){
*
*/
DMS_STATUS dms_CellInfoDefineCellCount(WORD awCellCount){
WORD wCellIndex;
if (lpwCellBlockCount != NULL) {
/* assume memory from old needs freed */
dms_CellInfoFinalize();
}
lpwCellBlockCount = (WORD*) malloc(sizeof(WORD) * awCellCount);
if (lpwCellBlockCount == NULL) {
return Out_Of_Memory_Error;
}
for (wCellIndex = 0; wCellIndex < awCellCount; wCellIndex++){
lpwCellBlockCount[wCellIndex] = 0;
}
lwCellCount = awCellCount;
return No_Error;
}
/**
* dms_CellInfoDefineCellBlockCount
* This function is designed to be called by initialization.
* NOTE: dms_CellInfoCheckValidity should be called after all
* calls to this function are complete. (ie after all
* sectors are read.
*
* Prerequisites:
* dms_CellInfoDefineCellCount()
* Uses:
* Nothing outside this file.
* Used By:
* dms_CellTableCheckValidity()
*/
void dms_CellInfoDefineCellBlockCount(WORD awCellIndex, WORD awBlockCount){
lpwCellBlockCount[awCellIndex] = awBlockCount;
}
/**
* dms_CellInfoCheckValidity()
* Performs some basic checks on the cells.
* Checks for total cell size verse sector size are
* done by initialization software.
*
* Prerequisites:
* dms_CellInfoDefineCellCount()
* dms_CellInfoDefineCellBlockCount()
* Uses:
* Nothing outside this file.
* Used By:
* dms_CellTableCheckValidity(void)
*/
DMS_STATUS dms_CellInfoCheckValidity(void){
WORD wCellIndex;
/* check that block count for each cell >= 0 */
for (wCellIndex = 0; wCellIndex < lwCellCount; wCellIndex++){
if (lpwCellBlockCount[wCellIndex] <= 0) {
return Cell_Size_Too_Small;
}
}
return No_Error;
}
/**
* dms_CellInfoGetCount()
* Returns the number of cells.
*
* Prerequisites:
* dms_CellInfoDefineCellCount()
* Uses:
* Nothing outside this file.
* Used By:
* dms_CellTableCheckValidity()
* dms_CellTableFinalize()
* dms_CellRead ()
* dms_CellWrite()
* dms_CellFormat ()
* dms_SectorInitialize()
* dms_SectorCleanup()
*/
WORD dms_CellInfoGetCount(void){
return lwCellCount;
}
/**
* dms_CellInfoGetBlockCount()
* Returns the number of blocks in the requested cell.
* NOTE: Cell Index must be valid otherwise result
* is undefined or function may crash.
* A valid index is >= 0 and < dms_CellInfoGetCount()
*
* Prerequisites:
* dms_CellInfoDefineCellCount()
* dms_CellInfoDefineCellBlockCount()
* Uses:
* Nothing outside this file.
* Used By:
* dms_CellWrite()
* dms_CellFormat()
* dms_CellInfoGetLargestCellBlockCount()
* dms_CellInfoGetTotalBlockCount()
*/
WORD dms_CellInfoGetBlockCount(WORD awCellIndex){
return lpwCellBlockCount[awCellIndex];
}
/**
* dms_CellInfoGetTotalBlockCount
* Returns the sum of dms_CellInfoGetBlockCount for
* all cells.
*
* Prerequisites:
* dms_CellInfoDefineCellCount()
* dms_CellInfoDefineCellBlockCount()
* Uses:
* dms_CellInfoGetBlockCount()
* Used By:
* dms_CellTableCheckValidity()
*/
WORD dms_CellInfoGetTotalBlockCount(void){
WORD wCellIndex;
WORD wTotalBlockCount = 0;
for (wCellIndex = 0; wCellIndex < lwCellCount; wCellIndex++){
wTotalBlockCount = (WORD) (wTotalBlockCount + dms_CellInfoGetBlockCount(wCellIndex));
}
return wTotalBlockCount;
}
/**
* dms_CellInfoGetLargestCellBlockCount
* Returns the size in blocks of the largest cell.
*
* Prerequisites:
* dms_CellInfoDefineCellCount()
* dms_CellInfoDefineCellBlockCount()
* Uses:
* dms_CellInfoGetBlockCount()
* Used By:
* dms_CellTableCheckValidity()
*/
WORD dms_CellInfoGetLargestCellBlockCount(void){
WORD wCellIndex;
WORD wLargestCellBlockCount = 0;
WORD wCellBlockCount;
for (wCellIndex = 0; wCellIndex < lwCellCount; wCellIndex++){
wCellBlockCount = dms_CellInfoGetBlockCount(wCellIndex);
if (wCellBlockCount > wLargestCellBlockCount) {
wLargestCellBlockCount = wCellBlockCount;
}
}
return wLargestCellBlockCount;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -