📄 msapi_memory.c
字号:
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2006-2007 MStar Semiconductor, Inc.
// All rights reserved.
//
// Unless otherwise stipulated in writing, any and all information contained
// herein regardless in any format shall remain the sole proprietary of
// MStar Semiconductor Inc. and be kept in strict confidence
// (¨MStar Confidential Information〃) by the recipient.
// Any unauthorized act including without limitation unauthorized disclosure,
// copying, use, reproduction, sale, distribution, modification, disassembling,
// reverse engineering and compiling of the contents of MStar Confidential
// Information is unlawful and strictly prohibited. MStar hereby reserves the
// rights to any and all damages, losses, costs and expenses resulting therefrom.
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
/// @file msAPI_Memory.h
/// @brief API for memory buffer pool management
/// @author MStar Semiconductor, Inc.
//
////////////////////////////////////////////////////////////////////////////////
#define MSAPI_MEMORY_C
/******************************************************************************/
/* Header Files */
/******************************************************************************/
#include <stdio.h>
#include <string.h>
#include "DataType.h"
#include "sysinfo.h"
#include "msAPI_Memory.h"
#include "debug.h"
#include "board.h"
#define MEM_DBINFO(y) //y
/********************************************************************************/
/* Macro */
/********************************************************************************/
#define MAX_MEMORY_ALLOCATE_NUM 10
/********************************************************************************/
/* Local */
/* ******************************************************************************/
static U8 au8MemAllocPool[MEMALLOC_POOL_SIZE] _at_ XDATA_DRAM_START_ADDR;
static U16 au16BlockStartAddr[MAX_MEMORY_ALLOCATE_NUM + 1];
static U16 au16BlockLength[MAX_MEMORY_ALLOCATE_NUM + 1];
static BOOLEAN abMemUsed[MAX_MEMORY_ALLOCATE_NUM + 1];
/********************************************************************************/
/* Functions */
/********************************************************************************/
/******************************************************************************/
/// API for check usage status of memory buffer pool
/******************************************************************************/
void msAPI_Memory_Status(void)
{
U8 u8_loop;
U16 u16MemAllocLength;
u16MemAllocLength = 0;
MEM_DBINFO(printf("memory buffer pool status:\r\n"));
MEM_DBINFO(printf("# StartAddr Length Status\n"));
for(u8_loop = 0; u8_loop < MAX_MEMORY_ALLOCATE_NUM; u8_loop++)
{
MEM_DBINFO(printf("%bu 0x%04x 0x%04x ", u8_loop, au16BlockStartAddr[u8_loop], au16BlockLength[u8_loop]));
if(abMemUsed[u8_loop] == TRUE)
{
u16MemAllocLength += au16BlockLength[u8_loop];
MEM_DBINFO(printf("Allocated\n"));
}
else
{
MEM_DBINFO(printf("Free\n"));
}
}
MEM_DBINFO(printf("Total memory = %u, Allocated = %u, Free: %u\n\n", (U16) MEMALLOC_POOL_SIZE, u16MemAllocLength, (U16) (MEMALLOC_POOL_SIZE - u16MemAllocLength)));
}
/******************************************************************************/
/// API for Init memory buffer pool at system boot-up
/******************************************************************************/
void msAPI_Memory_Init(void)
{
memset(au16BlockStartAddr, 0, sizeof(au16BlockStartAddr));
memset(au16BlockLength, 0, sizeof(au16BlockLength));
memset(abMemUsed, 0, sizeof(abMemUsed));
au16BlockLength[0] = MEMALLOC_POOL_SIZE;
}
/******************************************************************************/
/// API for Allocate memory
/// @param u16NumOfBytes \b IN number of bytes to be allocated
/// @return void *:
///- NULL: FAIL
///- XDATA_DRAM_START_ADDR ~ (XDATA_DRAM_START_ADDR+MEMALLOC_POOL_SIZE): SUCCESS
/******************************************************************************/
void * msAPI_Memory_Allocate(U16 u16NumOfBytes)
{
U8 u8Alloc_Idx, u8Free_Idx;
if(u16NumOfBytes)
{
for(u8Alloc_Idx = 0; u8Alloc_Idx < MAX_MEMORY_ALLOCATE_NUM; u8Alloc_Idx++)
{
if(abMemUsed[u8Alloc_Idx] == FALSE && au16BlockLength[u8Alloc_Idx] >= u16NumOfBytes)
{
for(u8Free_Idx = 0; u8Free_Idx <= MAX_MEMORY_ALLOCATE_NUM; u8Free_Idx++)
{
if(au16BlockLength[u8Free_Idx] == 0)
{
au16BlockStartAddr[u8Free_Idx] = au16BlockStartAddr[u8Alloc_Idx] + u16NumOfBytes;
au16BlockLength[u8Free_Idx] = au16BlockLength[u8Alloc_Idx] - u16NumOfBytes;
au16BlockLength[u8Alloc_Idx] = u16NumOfBytes;
abMemUsed[u8Alloc_Idx] = TRUE;
MEM_DBINFO(printf("___Allocate buffer %u bytes at 0x%04x\n", u16NumOfBytes, (U16)&au8MemAllocPool[au16BlockStartAddr[u8Alloc_Idx]]));
return &au8MemAllocPool[au16BlockStartAddr[u8Alloc_Idx]];
}
}
}
}
}
msWarning(ERR_OUT_OF_MEMORY);
(printf("Error>> Allocate buffer %u bytes fail!\r\n", u16NumOfBytes));
MEM_DBINFO(msAPI_Memory_Status()); //debugging only
#if 0//leo_0626 for debugging
printf("Error>> memalloc %u bytes fail!\r\n", u16NumOfBytes);
getchar();
#endif
return (void *)NULL;
}
/********************************************************************************/
static BOOLEAN msAPI_Memory_CombineFreeBlocks(U8 u8Alloc_Idx, U8 *pu8Index)
{
U8 u8Free_Idx;
for(u8Free_Idx = 0; u8Free_Idx <= MAX_MEMORY_ALLOCATE_NUM; u8Free_Idx++)
{
if(au16BlockLength[u8Free_Idx] == 0 || abMemUsed[u8Free_Idx] == TRUE)
continue;
if(au16BlockStartAddr[u8Free_Idx] == (au16BlockStartAddr[u8Alloc_Idx] + au16BlockLength[u8Alloc_Idx]))
{
*pu8Index = u8Alloc_Idx;
au16BlockLength[u8Alloc_Idx] += au16BlockLength[u8Free_Idx];
au16BlockLength[u8Free_Idx] = 0;
return TRUE;
}
else if(au16BlockStartAddr[u8Alloc_Idx] == (au16BlockStartAddr[u8Free_Idx] + au16BlockLength[u8Free_Idx]))
{
*pu8Index = u8Free_Idx;
au16BlockLength[u8Free_Idx] += au16BlockLength[u8Alloc_Idx];
au16BlockLength[u8Alloc_Idx] = 0;
return TRUE;
}
}
return FALSE;
}
/******************************************************************************/
/// API for Free allocated memory
/// @param pBuffer \b IN pointer to be released
/******************************************************************************/
void msAPI_Memory_Free(void *pBuffer)
{
U8 u8Alloc_Idx1, u8Alloc_Idx2;
U16 u16Addr_1, u16Addr_2;
MEM_DBINFO(printf("___Free::0x%04x", (U16)pBuffer));
for(u8Alloc_Idx1 = 0; u8Alloc_Idx1 < MAX_MEMORY_ALLOCATE_NUM; u8Alloc_Idx1++)
{
if(abMemUsed[u8Alloc_Idx1] == TRUE)
{
u16Addr_1 = au16BlockStartAddr[u8Alloc_Idx1];
u16Addr_2 = au16BlockStartAddr[u8Alloc_Idx1] + au16BlockLength[u8Alloc_Idx1];
if(pBuffer >= &au8MemAllocPool[u16Addr_1] && pBuffer < &au8MemAllocPool[u16Addr_2])
{
abMemUsed[u8Alloc_Idx1] = FALSE;
MEM_DBINFO(printf("::%u bytes\n", au16BlockLength[u8Alloc_Idx1]));
if(msAPI_Memory_CombineFreeBlocks(u8Alloc_Idx1, &u8Alloc_Idx2) == TRUE)
{
u8Alloc_Idx1 = u8Alloc_Idx2;
msAPI_Memory_CombineFreeBlocks(u8Alloc_Idx1, &u8Alloc_Idx2);
}
return;
}
}
}
msWarning(ERR_MEMORY_NOT_ALLOCATED);
}
/********************************************************************************/
#undef MSAPI_MEMORY_C
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -