📄 avm_allo.c
字号:
if (Cur_p->BlockAbove_p != Cur_p)
{
Cur_p = Cur_p->BlockAbove_p;
}
else
{
Cur_p = NULL;
STTBX_Print(("Error: memory block list is CORRUPTED !!!\n"));
RetErr = FALSE;
}
}
STTBX_Print(("MEMORY BLOCKS FROM TOP:\n"));
Cur_p = VeryTopBlock_p;
while (Cur_p != NULL)
{
Size = Cur_p->Size;
Address = ((U32) Cur_p->StartAddr_p);
switch (Cur_p->AllocMode)
{
case STAVMEM_ALLOC_MODE_BOTTOM_TOP:
strcpy(AllocModeStr, "BottomTop");
break;
case STAVMEM_ALLOC_MODE_TOP_BOTTOM:
strcpy(AllocModeStr, "TopBottom");
break;
case STAVMEM_ALLOC_MODE_RESERVED:
strcpy(AllocModeStr, "RESERVED!");
break;
case STAVMEM_ALLOC_MODE_FORBIDDEN:
strcpy(AllocModeStr, "FORBIDDEN");
break;
default:
strcpy(AllocModeStr, "-Invalid-");
RetErr = FALSE;
break;
}
if ((!RetErr) && (!Cur_p->IsUsed))
{
strcpy(AllocModeStr, "--FREE!--");
}
STTBX_Print(("%#08x-%#08x : %s (%#x byte%c) \n", Address, Address + Size - 1, AllocModeStr, Size, (Size>1)?'s':'.'));
if (Cur_p->BlockBelow_p != Cur_p)
{
Cur_p = Cur_p->BlockBelow_p;
}
else
{
Cur_p = NULL;
STTBX_Print(("Memory block list is CORRUPTED !!!\n"));
RetErr = FALSE;
}
}
/* STAVMEM_GetFreeSize(OpenHandle, &AskedFreeSize);*/
/* if (AskedFreeSize == CalculatedFreeSize)
{*/
STTBX_Print(("with %#x bytes free.\n", CalculatedFreeSize));
/* }
else
{
STTBX_Print(("Error in free size: %#x / %#x bytes.\n", CalculatedFreeSize, AskedFreeSize));
RetErr = FALSE;
}*/
return(RetErr);
}
#endif
/*
--------------------------------------------------------------------------------
Get total free memory available in memory map
--------------------------------------------------------------------------------
*/
ST_ErrorCode_t STAVMEM_GetFreeSize(const ST_DeviceName_t DeviceName, U32 *TotalFreeSize_p)
{
MemBlockDesc_t *Block_p;
U32 TotalSize = 0;
stavmem_Device_t *Device_p;
ST_ErrorCode_t Err = ST_NO_ERROR;
/* Exit if bad parameter */
if (TotalFreeSize_p == NULL)
{
return(ST_ERROR_BAD_PARAMETER);
}
/* Check if device already initialised and return error if not so */
Device_p = stavmem_GetPointerOnDeviceNamed(DeviceName);
if (Device_p == NULL)
{
/* Device name not found */
return(ST_ERROR_UNKNOWN_DEVICE);
}
#ifdef USE_SEMAPHORE
/* Wait for the allocations locking semaphore to be free */
semaphore_wait(&(Device_p->LockAllocSemaphore));
#endif
Block_p = Device_p->SpaceVeryTopBlock_p;
while (Block_p != NULL)
{
if (!Block_p->IsUsed)
{
TotalSize += Block_p->Size;
}
Block_p = Block_p->BlockBelow_p;
}
#ifdef USE_SEMAPHORE
/* Free the allocations locking semaphore */
semaphore_signal(&(Device_p->LockAllocSemaphore));
#endif
*TotalFreeSize_p = TotalSize;
return(Err);
}
/*
--------------------------------------------------------------------------------
Get total free memory available in partition
--------------------------------------------------------------------------------
*/
ST_ErrorCode_t STAVMEM_GetPartitionFreeSize(STAVMEM_PartitionHandle_t PartitionHandle, U32 *PartitionFreeSize_p)
{
MemBlockDesc_t *Block_p;
U32 TotalSize = 0;
stavmem_Partition_t *Partition_p;
ST_ErrorCode_t Err = ST_NO_ERROR;
/* Exit if bad parameter */
if (PartitionFreeSize_p == NULL)
{
return(ST_ERROR_BAD_PARAMETER);
}
/* Get pointer to partition the block belongs to */
Partition_p = (stavmem_Partition_t *) PartitionHandle;
/* Exit if the partition is not valid */
if (Partition_p->PartitionValidity != AVMEM_VALID_PARTITION)
{
return(STAVMEM_ERROR_INVALID_PARTITION_HANDLE);
}
#ifdef USE_SEMAPHORE
/* Wait for the allocations locking semaphore to be free */
semaphore_wait(&(Partition_p->Device_p->LockAllocSemaphore));
#endif
Block_p = Partition_p->PartitionVeryTopBlock_p;
while (Block_p != NULL)
{
if (!Block_p->IsUsed)
{
TotalSize += Block_p->Size;
}
Block_p = Block_p->BlockBelow_p;
}
#ifdef USE_SEMAPHORE
/* Free the allocations locking semaphore */
semaphore_signal(&(Partition_p->Device_p->LockAllocSemaphore));
#endif
*PartitionFreeSize_p = TotalSize;
return(Err);
}
/*
--------------------------------------------------------------------------------
De-allocate a previously allocated memory block
--------------------------------------------------------------------------------
*/
ST_ErrorCode_t STAVMEM_FreeBlock(const STAVMEM_FreeBlockParams_t *FreeBlockParams_p, STAVMEM_BlockHandle_t *BlockHandle_p)
{
stavmem_Partition_t *Partition_p;
ST_ErrorCode_t Err = ST_NO_ERROR;
/* Exit if bad parameter */
if ((BlockHandle_p == NULL) ||
(FreeBlockParams_p == NULL))
{
return(ST_ERROR_BAD_PARAMETER);
}
/* Get pointer to partition the block belongs to */
Partition_p = (stavmem_Partition_t *) FreeBlockParams_p->PartitionHandle;
/* Exit if the partition is not valid */
if (Partition_p->PartitionValidity != AVMEM_VALID_PARTITION)
{
return(STAVMEM_ERROR_INVALID_PARTITION_HANDLE);
}
/* Exit if block handle is invalid */
if (*BlockHandle_p == STAVMEM_INVALID_BLOCK_HANDLE)
{
/* Bad block handle */
return(STAVMEM_ERROR_INVALID_BLOCK_HANDLE);
}
#ifdef USE_SEMAPHORE
/* Wait for the allocations locking semaphore to be free */
semaphore_wait(&(Partition_p->Device_p->LockAllocSemaphore));
#endif
/* Free block */
Err = FreeNoSemaphore(Partition_p, *BlockHandle_p);
/* Always success */
#ifdef USE_SEMAPHORE
/* Free the allocations locking semaphore */
semaphore_signal(&(Partition_p->Device_p->LockAllocSemaphore));
#endif
return(Err);
}
/*******************************************************************************
Name : FreeNoSemaphore
Description : De-allocate a previously allocated memory block
Merge the unused block generated with the below and above eventual ones
Parameters : pointer to the partition structure the block belongs to and
memory block handle
Assumptions : pointer is not NULL, BlockHandle is valid
Limitations :
Returns : ST_NO_ERROR (success)
*******************************************************************************/
static ST_ErrorCode_t FreeNoSemaphore(stavmem_Partition_t *Partition_p, STAVMEM_BlockHandle_t BlockHandle)
{
MemBlockDesc_t *Top_p, *Block_p, *Bot_p;
/* Get pointer to the block */
Block_p = (MemBlockDesc_t *) BlockHandle;
/* Exit if block is not in partition:
Should never happend !!! */
/* Mark block as unused */
Block_p->IsUsed = FALSE;
/* Update counter to respect MaxUsedBlocks */
Partition_p->Device_p->CurrentlyUsedBlocks--;
/* Consider block above and merge if it is unused */
Top_p = Block_p->BlockAbove_p;
if ((Top_p != NULL) && (!Top_p->IsUsed))
{
/* Merge the unused block above into the current unused memory block */
Block_p->Size += Top_p->Size;
/* Remove block from partition list of blocks */
RemoveBlock(&(Partition_p->PartitionVeryTopBlock_p), &(Partition_p->PartitionVeryBottomBlock_p), Top_p);
/* Free off block structure */
FreeBlockDescriptor(&(Partition_p->Device_p->TopOfSpaceFreeBlocksList_p), Top_p);
}
/* Consider block below and merge if it is unused */
Bot_p = Block_p->BlockBelow_p;
if ((Bot_p != NULL) && (!Bot_p->IsUsed))
{
/* Merge the current unused memory block into the unused block below */
Bot_p->Size += Block_p->Size;
/* Remove block from partition list of blocks */
RemoveBlock(&(Partition_p->PartitionVeryTopBlock_p), &(Partition_p->PartitionVeryBottomBlock_p), Block_p);
/* Free off block structure */
FreeBlockDescriptor(&(Partition_p->Device_p->TopOfSpaceFreeBlocksList_p), Block_p);
}
return(ST_NO_ERROR);
}
/*
--------------------------------------------------------------------------------
Allocate a memory block
--------------------------------------------------------------------------------
*/
ST_ErrorCode_t STAVMEM_AllocBlock(const STAVMEM_AllocBlockParams_t *AllocBlockParams_p, STAVMEM_BlockHandle_t *BlockHandle_p)
{
stavmem_Partition_t *Partition_p;
ST_ErrorCode_t Err = ST_NO_ERROR;
/* Exit if bad parameter */
if ((BlockHandle_p == NULL) ||
(AllocBlockParams_p == NULL) ||
((AllocBlockParams_p->NumberOfForbiddenRanges > 0) && (AllocBlockParams_p->ForbiddenRangeArray_p == NULL)) ||
((AllocBlockParams_p->NumberOfForbiddenBorders > 0) && (AllocBlockParams_p->ForbiddenBorderArray_p == NULL)) ||
((AllocBlockParams_p->AllocMode != STAVMEM_ALLOC_MODE_TOP_BOTTOM) && (AllocBlockParams_p->AllocMode != STAVMEM_ALLOC_MODE_BOTTOM_TOP)))
{
return(ST_ERROR_BAD_PARAMETER);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -