⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ml_mem.c

📁 是一个手机功能的模拟程序
💻 C
📖 第 1 页 / 共 5 页
字号:
}

/*+FHDR E*/
/*
+------------------------------------------------------------------------------+
|  Functionname : xmk_GetFreeMem                                               |
|  Author       : S&P Media GmbH Germany                                       |
+------------------------------------------------------------------------------+
|                                                                              |
|  Description  :                                                              |
|  This function returns the amount of free memory.                            |
|  If for example a value of 100 bytes of free memory is returned to the       |
|  caller, and the caller wants to have 100 bytes of memory with the next      |
|  xmk_Malloc/xmk_Calloc call, there will be a failure.  The reason is, that   |
|  there is an extra overhead per each allocated block. The overhead can be    |
|  calculated by looking to the xmk_T_MBLOCK structure.                        |
|                                                                              |
|  Another reason for "cannot get more memory" might be that there are memory  |
|  leaks. Memory leaks occur if the memory is free-ed in another order than it |
|  was allocated. In that case the memory is cut into pieces and there is no   |
|  no piece which is large enough.                                             |
|                                                                              |
|  For preventing memory leaks, the following guidelines could help the user:  |
|                                                                              |
|   * Restrict the use of dynamic memory allocation, whereever possible        |
|   * Use fixed blocksizes wherever possible (see macro XMK_MEM_MIN_BLKSIZE    |
|     and XMK_USE_MIN_BLKSIZE)                                                 |
|   * Set the XMK_MEM_MIN_BLKSIZE value to the greatest memory block size      |
|     that is ever allocated.                                                  |
|                                                                              |
|  Parameter    : void                                                         |
|                                                                              |
|  Return       : size_t                                                       |
|                                                                              |
+------------------------------------------------------------------------------+
*/
/*-FHDR E*/

/*+FDEF E*/

#ifndef XNOPROTO
  size_t xmk_GetFreeMem ( void )
#else
  size_t xmk_GetFreeMem (  )
#endif

/*-FDEF E*/

{
  return (XMK_MAX_MALLOC_SIZE - xmk_cur_memory_fill);
}


/*+FHDR E*/
/*
+------------------------------------------------------------------------------+
|  Functionname : xmk_CleanPool                                                |
|  Author       : S&P Media GmbH Germany                                       |
+------------------------------------------------------------------------------+
|                                                                              |
|  Description  :                                                              |
|  This function tries to clean the memory pool for memory leaks.              |
|  Memory leaks occur when there are too many allocations and deallocations on |
|  too different block sizes in an unfavourable order.                         |
|                                                                              |
|  The memory pool can only be cleaned if there are no allocated blocks left.  |
|  This means that the memory pool must be empty, which may occur only, if     |
|      *there are no signals in the queue, nor save or unsaved signals         |
|      *No SDL predefined sorts are used based on dynamic memory allocation    |
|      *the user deallocated any requested memory                              |
|      *the Cmicro Tester is not used (because there are some memory alloca-   |
|       tions)                                                                 |
|                                                                              |
|  The function may be called from time to time by the user.                   |
|                                                                              |
|  Exactly spoken the functions performs a memory reinitialization, which is   |
|  very fast.                                                                  |
|                                                                              |
|  Any profiling information is not reinitialized, e.g.can be still requested  |
|  after the memory pool was cleaned, with the following exceptions :          |
|      *the amount of used memory is initialized to 0                          |
|                                                                              |
|                                                                              |
|  Parameter    : void                                                         |
|                                                                              |
|  Return       : size_t                                                       |
|                                                                              |
+------------------------------------------------------------------------------+
*/
/*-FHDR E*/

/*+FDEF E*/

#ifndef XNOPROTO
  int xmk_CleanPool ( void )
#else
  int xmk_CleanPool (  )
#endif

/*-FDEF E*/

{
  
  char * _mbegin; 
  char * _mend; 

  #ifdef XMK_ADD_PRINTF_MEMORY
    XMK_FUNCTION("xmk_CleanPool");
  #endif

  XMK_BEGIN_CRITICAL_PATH;

  if ( xmk_cur_memory_fill != 0 )
  {
    /* 
    ** Cannot clean up (reinitialize) memory
    */
    return (XMK_FALSE);
  }

  /*
  ** Destroy all blocks in between first and last
  ** by initializing the links
  */
  first_block->next             = xmk_LastMemoryBlockPtr ;
  xmk_LastMemoryBlockPtr->prev  = first_block ;

  XMK_END_CRITICAL_PATH;

  /* 
  ** Memory pool cleaned up
  */
  return (XMK_TRUE);
}

#endif /* ... defined(XMK_ADD_PROFILE) && defined(XMK_USE_EXPANDED_KERNEL) */

#if defined(XMK_ADD_PROFILE) || (defined(XMK_USE_DEBUGGING) && defined(XMK_ADD_CQUERY_MEM))

/*+FHDR E*/
/*
+------------------------------------------------------------------------------+
|  Functionname : xmk_QueryMemory                                              |
|  Author       : S&P Media GmbH Germany                                       |
+------------------------------------------------------------------------------+
|                                                                              |
|  Description  :                                                              |
|     The function evaluates the current memory and returns the following      |
|     information to the caller :                                              |
|                                                                              |
|     * Size of memory pool                                                    |
|     * Largest Block that was ever requested.                                 |
|     * How many blocks are currently in the pool ?                            |
|     * A pointer to the physical Address of the Memory.                       |
|                                                                              |
|  Parameter    : xmk_M_STATE    *minfo                                        |
|                                                                              |
|  Return       : -                                                            |
|                                                                              |
+------------------------------------------------------------------------------+
*/
/*-FHDR E*/

/*+FDEF E*/

#ifndef XNOPROTO
  void xmk_QueryMemory ( xmk_T_CMD_QUERY_MEMORY_CNF xmk_RAM_ptr minfo )
#else
  void xmk_QueryMemory ( minfo )
  xmk_T_CMD_QUERY_MEMORY_CNF xmk_RAM_ptr minfo;
#endif

/*-FDEF E*/

{
  #ifdef XMK_ADD_PRINTF_MEM
    XMK_FUNCTION("xmk_QueryMemory");
  #endif

  XMK_BEGIN_CRITICAL_PATH;

  minfo->mem_pool_size       = XMK_MAX_MALLOC_SIZE ;
  minfo->current_memory_fill = xmk_cur_memory_fill;
  minfo->current_blocks      = xmk_cur_blk_cnt ;
  minfo->peak_amount_blocks  = xmk_max_blk_cnt ;
  minfo->largest_block       = xmk_largest_blk ;
  minfo->overhead            = sizeof (xmk_T_MBLOCK) ;

  #ifdef XMK_USE_MIN_BLKSIZE
    minfo->min_blk_size      = XMK_MEM_MIN_BLKSIZE; 
  #else
    /* No minimum blocksize defined */
    minfo->min_blk_size      = 0; 
  #endif

  #if defined(IARC51) || defined(KEIL_C51)
    minfo->address             = (long)(int) first_block  ;
  #else
    minfo->address             = (long) first_block  ;
  #endif 

  #ifdef XMK_ADD_PRINTF
    PRINTF (("M-STATE:Memory pool size incl.overhead     =%d\n", minfo->mem_pool_size ));
    PRINTF (("M-STATE:Current memory fill                =%d\n", minfo->current_memory_fill ));
    PRINTF (("M-STATE:Current amount of blocks in pool   =%d\n", minfo->current_blocks ));
    PRINTF (("M-STATE:Peak hold: Amount of blocks        =%d\n", minfo->peak_amount_blocks ));
    PRINTF (("M-STATE:Peak hold: Largest block           =%d\n", minfo->largest_block ));
    PRINTF (("M-STATE:Overhead per block (in bytes)      =%d\n", minfo->overhead ));
    PRINTF (("M-STATE:Minimum block size                 =%d\n", minfo->min_blk_size ));
    PRINTF (("M-STATE:Memory pool address (hex)          =%x\n", minfo->address  ));
    PRINTF (("M-STATE:(Probably there are memory leaks)\n"));
  #endif

  XMK_END_CRITICAL_PATH;

  #ifdef XMK_ADD_PRINTF_QUEUE
    XMK_TRACE_EXIT("xmk_QueryMemory");
  #endif
}

/*
+------------------------------------------------------------------------------+
+---------------------------- End of function ---------------------------------+
+------------------------------------------------------------------------------+
*/

#endif /* ...defined(XMK_ADD_PROFILE) || (defined(XMK_USE_DEBUGGING) && defined(XMK_ADD_CQUERY_MEM)) */

#endif /* ... XMK_USE_SDL_MEM */


#ifdef XMK_USE_MIN_BLKSIZE

/*+FHDR S*/
/*
+------------------------------------------------------------------------------+
|  Functionname : xmk_EvaluateExp2Size                                         |
|  Author       : S&P Media GmbH Germany                                       |
+------------------------------------------------------------------------------+
|                                                                              |
|  Description  :                                                              |
|  This function evaluates from the given length a length value, which is      |
|  is in any case a 2 exp N value. This is used to reduce the risc of memory   |
|  leaks that sometimes occur in dynamic memory management systems.            |
|                                                                              |
|  It may be used for the memory functions of this module but also for the     |
|  memory functions of an RTOS or C compiler.                                  |
|                                                                              |
|  If the minimum blocksize is in any case greater than the greatest block     |
|  that would ever be requested, then there is no risc for memory leaks.       |
|                                                                              |
|  The return result may be one of the following only :                        |
|  >is in any case a minimum of XMK_MEM_MIN_BLKSIZE, but might be a value, e.g.|
|  64,128,256,512,1024,2048,4096,8192,16384,32768,65536                        |
|                                                                              |
|  Parameter    :                                                              |
|    size_t  rl           - requested blocksize                                |
|                                                                              |
|  Return       :                                                              |
|    int                  - calculated blocksize                               |
|                                                                              |
+------------------------------------------------------------------------------+
*/
/*-FHDR S*/
 
/*+FDEF S*/
 
#ifndef XNOPROTO
  size_t xmk_EvaluateExp2Size ( size_t rl )
#else
  size_t xmk_EvaluateExp2Size ( rl )
  size_t rl;
#endif
 
/*-FDEF E*/

{
  size_t res;
  int    i;
  int    x;
 
  if (rl <= XMK_MEM_MIN_BLKSIZE) return (XMK_MEM_MIN_BLKSIZE);
  if (rl >= 65536)
  {
    ErrorHandler (ERR_N_MEM_NO_FREE);
    return (rl);
  }
 
  x=rl;
  for (i=0; i < 32; i++)
  {
    x = x >> 1;
    if (!x)
    {
      res=1 << i;
      if (rl > res)
        res= (res << 1);
      return (res);
    }
  }
     
  return (rl) ;
 
} /* END OF FUNCTION */
 
#endif /* ... XMK_USE_MIN_BLKSIZE */

#ifdef XMK_USE_memset

/*+FHDR E*/
/*
+------------------------------------------------------------------------------+
|  Functionname : memset                                                       |
|  Author       : S&P Media GmbH Germany                                       |
+------------------------------------------------------------------------------+
|                                                                              |
|  Description  :                                                              |
|  This function is a template for the memset () implementation.               |
|                                                                              |
|  CAUTION :                                                                   |
|  -------------                                                               |
|  Take care in the case of using preemption !                                 |
|                                                                              |
+------------------------------------------------------------------------------+
*/
/*-FHDR E*/

/*+FDEF E*/

#ifndef XNOPROTO
  void  * XMK_memset_NAME ( void * p, xmk_OPT_INT val, size_t length)
#else
  void * XMK_memset_NAME ( p, val, length)
  void         * p;
  xmk_OPT_INT  val;
  size_t       length;
#endif

/*-FDEF E*/

{
  char * p1;
  
  #ifdef XMK_ADD_PRINTF_MEMORY
    XMK_FUNCTION("memset");
  #endif

  p1 = p;

   while (length --)
   { *p1 = val; p1++; };

  #ifdef XMK_ADD_PRINTF_MEMORY
    XMK_TRACE_EXIT("memset");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -