avoid usage of goto`

This commit is contained in:
Gatsby Van
2024-06-25 10:59:54 +08:00
parent 884d9bcca9
commit f8212892a2
8 changed files with 144 additions and 103 deletions
+41 -25
View File
@@ -278,7 +278,7 @@ static int32_t Mpc_SystemConfig(MPC *instPtr, uint8_t *msg)
* @param InstPtr A double pointer to MPC instance.
* @param config A pointer to MPC configurations.
*
* @return None.
* @return Error code.
*
******************************************************************************/
int32_t Mpc_Init(MPC **instPtr, MPC_CONFIG *config)
@@ -290,7 +290,7 @@ int32_t Mpc_Init(MPC **instPtr, MPC_CONFIG *config)
(*instPtr) = malloc(sizeof(MPC));
if ((*instPtr) == NULL)
{
return FAILURE;
return ENOMEM;
}
(*instPtr)->rxQueue = NULL;
(*instPtr)->txQueue = NULL;
@@ -307,14 +307,15 @@ int32_t Mpc_Init(MPC **instPtr, MPC_CONFIG *config)
bramConfigPtr = XBram_LookupConfig(config->bramId);
if (bramConfigPtr == NULL)
{
Status = EDCONF;
goto mpc_init_error;
Mpc_Destroy(*instPtr);
return EDCONF;
}
Status = XBram_CfgInitialize(&mpcBram, bramConfigPtr,
bramConfigPtr->CtrlBaseAddress);
if (Status != SUCCESS)
{
goto mpc_init_error;
Mpc_Destroy(*instPtr);
return Status;
}
/*
* Execute the BRAM driver selftest.
@@ -322,7 +323,8 @@ int32_t Mpc_Init(MPC **instPtr, MPC_CONFIG *config)
Status = XBram_SelfTest(&mpcBram, 0);
if (Status != SUCCESS)
{
goto mpc_init_error;
Mpc_Destroy(*instPtr);
return Status;
}
mpcHwConfig.txBuffAddr = mpcBram.Config.MemBaseAddress;
@@ -332,32 +334,47 @@ int32_t Mpc_Init(MPC **instPtr, MPC_CONFIG *config)
Status = MpcHw_Init(&((*instPtr)->mpchwDesc), &mpcHwConfig);
if (Status != SUCCESS)
{
Status = ENOMEM;
goto mpc_init_error;
Mpc_Destroy(*instPtr);
return ENOMEM;
}
if (CircQueueCreate(&((*instPtr)->txQueue), (*instPtr)->config.txQueueMax) == false)
{
Status = ENOMEM;
goto mpc_init_error;
Mpc_Destroy(*instPtr);
return ENOMEM;
}
if (CircQueueCreate(&((*instPtr)->rxQueue), (*instPtr)->config.rxQueueMax) == false)
{
Status = ENOMEM;
goto mpc_init_error;
Mpc_Destroy(*instPtr);
return ENOMEM;
}
(*instPtr)->isEnable = false; // nothing received.
(*instPtr)->isRxDone = false; // nothing received.
(*instPtr)->isTxDone = true; // nothing to be sent.
return SUCCESS;
mpc_init_error:
free((*instPtr)->mpchwDesc);
free((*instPtr)->txQueue);
free((*instPtr)->rxQueue);
free(*instPtr);
return Status;
}
/*****************************************************************************/
/**
* @brief Destroy MPC instance.
*
* @param InstPtr A pointer to MPC instance.
*
* @return None.
*
******************************************************************************/
void Mpc_Destroy(MPC *instPtr)
{
if (instPtr == NULL)
{
return;
}
free(instPtr->mpchwDesc);
free(instPtr->txQueue);
free(instPtr->rxQueue);
free(instPtr);
}
/*****************************************************************************/
/**
* @brief Generate a remote test response message.
@@ -534,17 +551,16 @@ int32_t Mpc_RxIrqHandler(MPC *instPtr)
Status = MpcHw_Recv(instPtr->mpchwDesc, mpcReq);
if (Status != SUCCESS)
{
goto recv_error;
DBG_ERROR("SystemBus::RX error!\n");
free(mpcReq);
return Status;
}
if (CircQueuePush(instPtr->rxQueue, (void *)mpcReq) == false)
{
DBG_ERROR("SystemBus::RX Queue overflowed!\n");
Status = EQUEUE;
goto recv_error;
free(mpcReq);
return EQUEUE;
}
instPtr->isRxDone = false; // another reception is undergoing.
return SUCCESS;
recv_error:
free(mpcReq);
return Status;
}
+1
View File
@@ -52,6 +52,7 @@ typedef struct
/************************** Function Prototypes ******************************/
int32_t Mpc_Init(MPC **instPtr, MPC_CONFIG *config);
void Mpc_Destroy(MPC *instPtr);
int32_t Mpc_HandleCmd(MPC *instPtr);
int32_t Mpc_PendRemoteTest(MPC *instPtr);