📄 lib_mem.c
字号:
/*
*********************************************************************************************************
* uC/LIB
* CUSTOM LIBRARY MODULES
*
* (c) Copyright 2004-2008; Micrium, Inc.; Weston, FL
*
* All rights reserved. Protected by international copyright laws.
*
* uC/LIB is provided in source form for FREE evaluation, for educational
* use or peaceful research. If you plan on using uC/LIB in a commercial
* product you need to contact Micrium to properly license its use in your
* product. We provide ALL the source code for your convenience and to
* help you experience uC/LIB. The fact that the source code is provided
* does NOT mean that you can use it without paying a licensing fee.
*
* Knowledge of the source code may NOT be used to develop a similar product.
*
* Please help us continue to provide the Embedded community with the finest
* software available. Your honesty is greatly appreciated.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*
* STANDARD MEMORY OPERATIONS
*
* Filename : lib_mem.c
* Version : V1.26
* Programmer(s) : ITJ
* FGK
*********************************************************************************************************
* Note(s) : (1) NO compiler-supplied standard library functions are used in library or product software.
*
* (a) ALL standard library functions are implemented in the custom library modules :
*
* (1) \<Custom Library Directory>\lib*.*
*
* (2) \<Custom Library Directory>\Ports\<cpu>\<compiler>\lib*_a.*
*
* where
* <Custom Library Directory> directory path for custom library software
* <cpu> directory name for specific processor (CPU)
* <compiler> directory name for specific compiler
*
* (b) Product-specific library functions are implemented in individual products.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#define LIB_MEM_MODULE
#include <lib_mem.h>
/*$PAGE*/
/*
*********************************************************************************************************
* LOCAL DEFINES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL CONSTANTS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL DATA TYPES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL TABLES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/
#if (LIB_MEM_CFG_ALLOC_EN == DEF_ENABLED)
MEM_POOL *Mem_PoolTbl; /* Mem pool/seg tbl. */
MEM_POOL Mem_PoolHeap; /* Mem heap pool/seg. */
CPU_INT08U Mem_Heap[LIB_MEM_CFG_HEAP_SIZE]; /* Mem heap. */
#endif
/*
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
#if (LIB_MEM_CFG_ALLOC_EN == DEF_ENABLED) /* -------------- MEM POOL FNCTS -------------- */
#if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
static CPU_BOOLEAN Mem_PoolBlkIsValidAddr(MEM_POOL *pmem_pool,
void *pmem_blk);
#endif
static CPU_SIZE_T Mem_PoolSegCalcTotSize(void *pmem_addr,
CPU_SIZE_T blk_nbr,
CPU_SIZE_T blk_size,
CPU_SIZE_T blk_align);
static void *Mem_PoolSegAlloc (MEM_POOL *pmem_pool,
CPU_SIZE_T size,
CPU_SIZE_T align);
#endif
/*
*********************************************************************************************************
* LOCAL CONFIGURATION ERRORS
*********************************************************************************************************
*/
/*$PAGE*/
/*
*********************************************************************************************************
* Mem_Init()
*
* Description : (1) Initialize Memory Management Module :
*
* (a) Initialize heap memory pool
* (b) Initialize memory pool table
*
*
* Argument(s) : none.
*
* Return(s) : none.
*
* Caller(s) : Application.
*
* Note(s) : none.
*********************************************************************************************************
*/
void Mem_Init (void)
{
#if (LIB_MEM_CFG_ALLOC_EN == DEF_ENABLED)
MEM_POOL *pmem_pool;
/* --------- INIT MEM HEAP SEG / POOL --------- */
pmem_pool = (MEM_POOL *)&Mem_PoolHeap;
pmem_pool->Type = (LIB_MEM_TYPE) LIB_MEM_TYPE_HEAP;
pmem_pool->SegPrevPtr = (MEM_POOL *) 0;
pmem_pool->SegNextPtr = (MEM_POOL *) 0;
pmem_pool->PoolPrevPtr = (MEM_POOL *) 0;
pmem_pool->PoolNextPtr = (MEM_POOL *) 0;
pmem_pool->PoolAddrStart = (void *) 0;
pmem_pool->PoolAddrEnd = (void *) 0;
pmem_pool->PoolPtrs = (void **) 0;
pmem_pool->BlkSize = (CPU_SIZE_T ) 0;
pmem_pool->BlkNbr = (CPU_SIZE_T ) 0;
pmem_pool->BlkIx = (MEM_POOL_IX ) 0;
pmem_pool->SegAddr = (void *)&Mem_Heap[0];
pmem_pool->SegAddrNextAvail = (void *)&Mem_Heap[0];
pmem_pool->SegSizeTot = (CPU_SIZE_T ) LIB_MEM_CFG_HEAP_SIZE;
pmem_pool->SegSizeRem = (CPU_SIZE_T ) LIB_MEM_CFG_HEAP_SIZE;
/* ------------ INIT MEM POOL TBL ------------- */
Mem_PoolTbl = &Mem_PoolHeap;
#endif
}
/*$PAGE*/
/*
*********************************************************************************************************
* Mem_Clr()
*
* Description : Clear data buffer (see Note #2).
*
* Argument(s) : pmem Pointer to memory buffer to clear.
*
* size Number of data buffer octets to clear.
*
* Return(s) : none.
*
* Caller(s) : Application.
*
* Note(s) : (1) Null clears allowed (i.e. 0-octet size).
*
* See also 'Mem_Set() Note #1'.
*
* (2) Clear data by setting each data octet to 0.
*********************************************************************************************************
*/
void Mem_Clr (void *pmem,
CPU_SIZE_T size)
{
Mem_Set((void *)pmem,
(CPU_INT08U)0, /* See Note #2. */
(CPU_SIZE_T)size);
}
/*$PAGE*/
/*
*********************************************************************************************************
* Mem_Set()
*
* Description : Fill data buffer with specified data octet.
*
* Argument(s) : pmem Pointer to memory buffer to fill with specified data octet.
*
* data_val Data fill octet value.
*
* size Number of data buffer octets to fill.
*
* Return(s) : none.
*
* Caller(s) : Application.
*
* Note(s) : (1) Null sets allowed (i.e. 0-octet size).
*
* (2) For best CPU performance, optimized to fill data buffer using 'CPU_ALIGN'-sized data
* words.
*
* (a) Since many word-aligned processors REQUIRE that multi-octet words be accessed on
* word-aligned addresses, 'CPU_ALIGN'-sized words MUST be accessed on 'CPU_ALIGN'd
* addresses.
*
* (3) Modulo arithmetic is used to determine whether a memory buffer starts on a 'CPU_ALIGN'
* address boundary.
*
* Modulo arithmetic in ANSI-C REQUIREs operations performed on integer values. Thus,
* address values MUST be cast to an appropriately-sized integer value PRIOR to any
* mem_align_modulo arithmetic operation.
*********************************************************************************************************
*/
void Mem_Set (void *pmem,
CPU_INT08U data_val,
CPU_SIZE_T size)
{
CPU_SIZE_T size_rem;
CPU_ALIGN data_align;
CPU_ALIGN *pmem_align;
CPU_INT08U *pmem_08;
CPU_INT08U mem_align_modulo;
CPU_INT08U i;
if (size < 1) { /* See Note #1. */
return;
}
if (pmem == (void *)0) {
return;
}
data_align = 0;
for (i = 0; i < sizeof(CPU_ALIGN); i++) { /* Fill each data_align octet with data val. */
data_align <<= DEF_OCTET_NBR_BITS;
data_align |= (CPU_ALIGN)data_val;
}
size_rem = (CPU_SIZE_T)size;
mem_align_modulo = (CPU_INT08U)((CPU_ADDR)pmem % sizeof(CPU_ALIGN)); /* See Note #3. */
pmem_08 = (CPU_INT08U *)pmem;
if (mem_align_modulo != 0) { /* If leading octets avail, ... */
i = mem_align_modulo;
while ((size_rem > 0) && /* ... start mem buf fill with leading octets ... */
(i < sizeof(CPU_ALIGN ))) { /* ... until next CPU_ALIGN word boundary. */
*pmem_08++ = data_val;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -