📄 ml_mem.c
字号:
/*+MHDR*/
/*
# clearcase: CmicroRel2.3
+------------------------------------------------------------------------------+
| Modulname : ML_MEM.C |
| Author : S&P Media GmbH Germany |
+------------------------------------------------------------------------------+
| |
| Description : |
| This module is a template for |
| - implementation of a dynamic memory management, |
| - implementation of some string and memory functions. |
| |
| The functions for dynamic memory management from this module can be used |
| (from the mk_cpu module for example) if the flag XMK_USE_SDL_MEM is set |
| (use the BodyBuilder). |
| The functions for string and memory from this module can be used if the |
| appropriate flag XMK_USE_<functionname> is set (again, use the BodyBuilder).|
| |
| Using the functions contained in this module makes sense or is absolutely |
| necessary, if : |
| |
| -the C-Compiler or RTOS which is to be used, does not support dynamic memory|
| management or string and memory functions, |
| |
| -if the functionality of dynamic memory management provided from the C |
| compiler or RTOS does not meet the requirements of the user. |
| The possible differences between C compilers/RTOS memory functions and the |
| memory functions of this module are |
| -the search- and maintenance policy of dynamic memory management, |
| the users may wish to allocate memoryblocks always of the same size, for |
| instance, which sometimes allows it to reduce code and increase |
| performance) |
| -the testability of dynamic memory management of this module might be better|
| because if the dynamic memory management functions of this module are used,|
| there is a Cmicro Tester command that allows it to observe the memory |
| occupation and more. |
| |
| To offer more comfort, an additional functionality represented by the |
| function "xmk_Memshrink ()" is exported. An application running, possibly |
| doesn't know the exact size of the memory block to be allocated. It is |
| then possible to allocate a maximum-size-block, do some evaluations within |
| the application, and then, if the exact size of the memory block is known, |
| to "shrink down" the previously requested memory block by calling |
| xmk_Memshrink (). |
| |
| CAUTION: |
| ---------------- |
| -The functions xmk_Malloc/xmk_Calloc/xmk_Free/xmk_Memshrink are able to |
| maintain one memory pool only. |
| -The functions xmk_Malloc/xmk_Calloc/xmk_Free/xmk_Memshrink are in principle|
| not able to handle reentrancy. |
| -The call to xmk_Malloc/xmk_Calloc and then xmk_Memshrink is to be |
| considered as an critical path. |
| |
| Basic idea is, that the user provides memory and calls "xmk_MemInit()" of |
| this module. |
| |
| Scaling of this module : See headerfile and manual. |
| |
| M1 Errormessages, which are routed to the ErrorHandler |
| -------------------------------------------------------- |
| ERR_N_INIT_SDL_MEM - Memory was not initialized but a try to allocate |
| memory occured. |
| ERR_N_MEM_PARAM - Invalid parameter in function call |
| ERR_N_MEM_NO_FREE - No more memory available |
| ERR_N_MEM_ILLMBLOCK - A try occured to free a block that was never |
| allocated. |
| |
| M2 Exported functions of this module : |
| -------------------------------------------------------- |
| void xmk_MemInit ( ) *1 |
| void * xmk_Malloc ( ) *1 |
| void * xmk_Calloc ( ) *1 |
| void xmk_Free ( ) *1 |
| void xmk_Memshrink ( ) *1*2 |
| size_t xmk_GetOccupiedMem ( ) *1*3 |
| size_t xmk_GetFreeMem ( ) *1*3 |
| void xmk_QueryMemory ( ) *1*4 |
| |
| size_t xmk_EvaluateExp2Size ( size_t rl ) *5 |
| |
| void memset ( ) *6 |
| void memcpy ( ) *6 |
| int strlen ( ) *6 |
| char * strcpy ( ) *6 |
| char * strncpy ( ) *6 |
| int strcmp ( ) *6 |
| |
| The above functions are conditionally compiled with : |
| |
| *1 XMK_USE_SDL_MEM |
| *2 XMK_USE_memshrink |
| *3 XMK_USE_EXPANDED_KERNEL |
| *4 XMK_USE_PROFILE |
| *5 XMK_USE_MIN_BLKSIZE |
| *6 XMK_USE_<functionname>, like XMK_USE_memset |
| |
| conditionally by specifying a flag XMK_USE_<functionname>, |
| i.e.XMK_USE_strcpy. Functions marked with *2 can be conditionally |
| compiled by specifying XMK_USE_SDL_MEM. |
| Functions marked with *3 can be conditionally compiled by specifying |
| XMK_USE_MIN_BLKSIZE. |
| |
| M3 Static functions of this module : |
| -------------------------------------------------------- |
| .... _memalloc ( ... ) |
| .... _memshrink ( ... ) |
| |
+------------------------------------------------------------------------------+
*/
/*-MHDR*/
/*
+------------------------------------------------------------------------------+
| |
| Copyright by Telelogic AB 1993 - 1998 |
| Copyright by S&P Media GmbH Germany 1993 - 1998 |
| |
| This Program is owned by Telelogic and is protected by national |
| copyright laws and international copyright treaties. Telelogic |
| grants you the right to use this Program on one computer or in |
| one local computer network at any one time. |
| Under this License you may only modify the source code for the purpose |
| of adapting it to your environment. You must reproduce and include |
| any copyright and trademark notices on all copies of the source code. |
| You may not use, copy, merge, modify or transfer the Program except as |
| provided in this License. |
| Telelogic does not warrant that the Program will meet your |
| requirements or that the operation of the Program will be |
| uninterrupted and error free. You are solely responsible that the |
| selection of the Program and the modification of the source code |
| will achieve your intended results and that the results are actually |
| obtained. |
| |
+------------------------------------------------------------------------------+
*/
#ifndef __ML_MEM_C_
#define __ML_MEM_C_
/*+IMPORT*/
/*==================== I M P O R T =========================================*/
#include "ml_typw.h"
/*============================================================================*/
/*-IMPORT*/
#ifdef XSYSID
/*
** CAUTION !
** ============
** This module cannot be used when partitioning is used. The reason
** for this is that the functions of this module operate on global
** variables and partitioned systems may preempt each other when
** an Real Time Operating System is used.
** However, users can use this module as a basis for implementing
** their own memory management functions.
*/
#error "ERROR_in_ml_mem_c_Module_cannot_be_used_for_Partitioning"
#endif /* ... XSYSID */
/*+MGG*/
/*==================== V A L U E S O F T H I S M O D U L E =============*/
/*-------------------- Constants, Macros ----------------------------------*/
#ifndef XMK_memset_NAME
#define XMK_memset_NAME memset
#endif
#ifndef XMK_memcpy_NAME
#define XMK_memcpy_NAME memcpy
#endif
#ifndef XMK_strlen_NAME
#define XMK_strlen_NAME strlen
#endif
#ifndef XMK_strcpy_NAME
#define XMK_strcpy_NAME strcpy
#endif
#ifndef XMK_strncpy_NAME
#define XMK_strncpy_NAME strncpy
#endif
#ifndef XMK_strcmp_NAME
#define XMK_strcmp_NAME strcmp
#endif
#ifndef XMK_MEM_MIN_BLKSIZE
/*
** Define a minimum blocksize of 64 bytes,
** if not specified otherwise.
**
** CAUTION !
** ----------
** Do not specify negative values, or zero.
*/
#define XMK_MEM_MIN_BLKSIZE 64
#endif
/*
** This define is essential for making the functions of this
** module work appropriate.
**
** Define the CPU's word size outside this module
** For example 80486 CPU does have a word size of 32Bit.
*/
#ifndef XMK_CPU_WORD_SIZE
/*
** If the XMK_CPU_WORD_SIZE is not defined
** externally, then it gets the default values.
*/
#if defined(ARM_THUMB)
#define XMK_CPU_WORD_SIZE 4 /* 4 Bytes are one word */
#elif defined(_GCC_)
#define XMK_CPU_WORD_SIZE 8 /* 8 Bytes are one word */
#else
#define XMK_CPU_WORD_SIZE 1 /* 1 Bytes is one word */
#endif
#endif /* ... XMK_CPU_WORD_SIZE */
#ifdef XMK_MAX_MALLOC_SIZE
#if (XMK_MAX_MALLOC_SIZE % XMK_CPU_WORD_SIZE) != 0
#error "ERROR02_in_ml_mem_c_BufferSizeCannotBeDevidedThroughWordSize"
#endif
#endif
#ifdef XMK_USE_MIN_BLKSIZE
#if (XMK_MEM_MIN_BLKSIZE % XMK_CPU_WORD_SIZE) != 0
#error "ERROR01_in_ml_mem_c_BufferSizeCannotBeDevidedThroughWordSize"
#endif
#endif
#ifdef XMK_ADD_PRINTF_MEMORY
/*
** Users may enable the use of printf within this module
** only if it can be ensured, that printf does not call
** one of the functions within this module.
**
** CAUTION !!
** ============
** This would introduce recursive function calls
** with an program crash caused by a stack overflow.
** Users do not enable XMK_ADD_PRINTF_MEMORY if they
** want to go for safe operation.
*/
#undef XMK_ADD_PRINTF_MEMORY
#endif
/*
** Prevent collision in naming (some C compilers do define p or/and rsize)
*/
#ifdef p
#undef p
#endif
#ifdef rl
#undef rl
#endif
#ifdef rsize
#undef rsize
#endif
#ifdef p1
#undef p1
#endif
#ifdef p2
#undef p2
#endif
#ifdef c1
#undef c1
#endif
#ifdef c2
#undef c2
#endif
#ifdef next
#undef next
#endif
#ifdef prev
#undef prev
#endif
/*-------------------- Typedefinitions ----------------------------------*/
#ifndef xmk_T_BOOL
typedef xmk_T_BOOL ;
#define XMK_TRUE (1==1)
#define XMK_FALSE (!XMK_TRUE)
#endif
#ifndef NULL
#define NULL (void *)0L
#endif
typedef struct _T_MBLOCK
{
struct _T_MBLOCK * next ;
struct _T_MBLOCK * prev ;
#if defined(XMK_ADD_PROFILE) || defined(_GCC_)
/*
** Used to store the blocksize per each block
*/
size_t used ;
#else
xmk_T_BOOL used ;
#endif
#if defined(ARM_THUMB)
/*
** this is used to align structures
** to word margins. A word is 32 bit.
*/
unsigned char _filler_ [3];
#elif defined(_GCC_)
/*
** this is used to align structures
** to word margins. A word is 64 bits == 8 bytes on UNIX
** So calculate 8 plus 3 for the filler
*/
unsigned char _filler_ [8];
#endif
} xmk_T_MBLOCK ;
/*-------------------- Functions -----------------------------------------*/
#ifdef XMK_USE_SDL_MEM
static xmk_T_MBLOCK * __memalloc XPP(( size_t ));
static void __memshrink XPP(( xmk_T_MBLOCK * , size_t size ));
#endif
/*-------------------- Variables ----------------------------------------*/
#if defined(XMK_ADD_PROFILE) || (defined(XMK_USE_DEBUGGING) && defined(XMK_ADD_CQUERY_MEM))
#ifndef NO_GLOBAL_VARS
/*
** counter for the amount of memory that is currently occupied
*/
int xmk_cur_memory_fill;
/*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -