gonzui


Format: Advanced Search

tkernel_2/lib/libtk/src/memcheck.cbare sourcepermlink (0.04 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    T-Kernel 2.0 Software Package
    4:  *
    5:  *    Copyright 2011 by Ken Sakamura.
    6:  *    This software is distributed under the latest version of T-License 2.x.
    7:  *----------------------------------------------------------------------
    8:  *
    9:  *    Released by T-Engine Forum(http://www.t-engine.org/) at 2011/05/17.
   10:  *    Modified by TRON Forum(http://www.tron.org/) at 2015/06/01.
   11:  *
   12:  *----------------------------------------------------------------------
   13:  */
   14: 
   15: /*
   16:  *      @(#)memcheck.c (libtk)
   17:  *
   18:  *      Memory allocation library
   19:  *
   20:  *      memalloc.c test routine
   21:  *      * Must be reentrant *
   22:  */
   23: 
   24: #include "mem.h"
   25: #include <sys/syslog.h>
   26: 
   27: /*
   28:  * Checks for errors in memory allocation information. When mode < 0,
   29:  * dumps the usage status. When ptr != NULL, checks to see that
   30:  * memory allocation corresponds properly with ptr allocated blocks.
   31:  * If so, returns True.
   32:  */
   33: LOCAL BOOL chkalloc( void *ptr, int mode, MACB *macb )
   34: {
   35:         QUEUE  *aq, *nq;
   36:         size_t usesz = 0, fresz = 0, sz;
   37:         int    usebk = 0, frebk = 0, npage = 0;
   38:         BOOL   newpg, ptr_ok;
   39: 
   40:         /* Checks each area in turn */
   41:         newpg = TRUE;
   42:         ptr_ok = ( ptr == NULL )? TRUE: FALSE;
   43:         for ( aq = macb->areaque.next; aq != &macb->areaque; aq = aq->next ) {
   44: 
   45:                 if ( newpg && !chkAreaFlag(aq, AREA_TOP) ) {
   46:                         goto err_found;
   47:                 }
   48: 
   49:                 if ( chkAreaFlag(aq, AREA_END) ) {
   50:                         if ( newpg ) {
   51:                                 goto err_found;
   52:                         }
   53:                         newpg = TRUE;
   54:                         fresz += sizeof(QUEUE);
   55:                         npage++;
   56:                         continue;
   57:                 }
   58:                 newpg = FALSE;
   59: 
   60:                 nq = aq->next;
   61:                 if ( Mask(aq->next) != nq || nq <= aq || Mask(nq->prev) != aq ) {
   62:                         goto err_found;
   63:                 }
   64:                 sz = (size_t)((VB*)nq - (VB*)aq);
   65:                 if ( sz < sizeof(QUEUE)*3 ) {
   66:                         goto err_found;
   67:                 }
   68: 
   69:                 if ( chkAreaFlag(aq, AREA_USE) ) {
   70:                         usesz += sz;
   71:                         ++usebk;
   72:                         if ( ptr == (void*)(aq+1) ) {
   73:                                 ptr_ok = TRUE;
   74:                         }
   75:                         if ( mode < -1 ) {
   76:                                 syslog(LOG_NOTICE, "malloc ptr: 0x%08x [%d B]",
   77:                                                         aq+1, AreaSize(aq));
   78:                         }
   79:                 } else {
   80:                         fresz += sz;
   81:                         ++frebk;
   82:                 }
   83:         }
   84:         if ( !newpg ) {
   85:                 goto err_found;
   86:         }
   87: 
   88:         if ( !ptr_ok ) {
   89:                 syslog(LOG_ERR, "MALLOC: illegal ptr: 0x%08x", ptr);
   90:                 return FALSE;
   91:         }
   92: 
   93:         if ( mode < 0 ) {
   94:                 syslog(LOG_NOTICE,
   95:                 "MALLOC: %d pages, used: %d [%d blks] free: %d [%d blks]",
   96:                 npage, usesz, usebk, fresz, frebk);
   97:         }
   98: 
   99:         return TRUE;
  100: 
  101: err_found:
  102:         syslog(LOG_ERR, "MALLOC: block corrupted at 0x%08x", aq);
  103:         return FALSE;
  104: }
  105: 
  106: /* ------------------------------------------------------------------------ */
  107: 
  108: /*
  109:  * Test
  110:  *      mode =       0     Normal mode (default)
  111:  *              1   Debug mode
  112:  *              -1  Usage status dump
  113:  *              -2  Detailed usage status dump
  114:  */
  115: EXPORT void  _mem_malloctest( int mode, MACB *_macb )
  116: {
  117:         MACB   *macb = AlignMACB(_macb);
  118: 
  119:         if ( mode >= 0 ) {
  120:                 /* Change test mode */
  121:                 macb->testmode = mode;
  122:                 _mem_chkalloc = chkalloc;
  123:         } else {
  124:                 /* Dump usage status */
  125:                 chkalloc(NULL, mode, macb);
  126:         }
  127: }
  128: 
  129: /*
  130:  * Memory allocation error check
  131:  *      If ptr != NULL, performs a memory allocation error check,
  132:  *      including a check to see whether memory allocation corresponds
  133:  *      properly with ptr allocated blocks.
  134:  *      If ptr == NULL, performs the memory allocation error check
  135:  *      only and returns TRUE if all is OK.
  136:  */
  137: EXPORT BOOL _mem_malloccheck( void *ptr, MACB *_macb )
  138: {
  139:         MACB   *macb = AlignMACB(_macb);
  140: 
  141:         return chkalloc(ptr, 0, macb);
  142: }