📄 memcheck.c
字号:
/* *---------------------------------------------------------------------- * T-Kernel / Standard Extension * * Copyright (C) 2006 by Ken Sakamura. All rights reserved. * T-Kernel / Standard Extension is distributed * under the T-License for T-Kernel / Standard Extension. *---------------------------------------------------------------------- * * Version: 1.00.00 * Released by T-Engine Forum(http://www.t-engine.org) at 2006/8/11. * *---------------------------------------------------------------------- *//* * memcheck.c (libtkse) * * Memory allocation library * * memalloc.c test routine * (*) Note that it must be reentrant. */#include "mem.h"#include <sys/syslog.h>#include <sys/tkseconf.h>LOCAL BOOL chkalloc( void *ptr, int mode, MACB *macb );#define TSD_CAL_SZ_3 3#define TSD__CAL_MOD_M1 (-1)/* * Check if there is no error in memory allocation information. * If mode < 0, dump the usage. * If ptr != NULL, check if ptr corresponds correctly * to the allocated block. * If it is normal, return "True". */LOCAL BOOL chkalloc( void *ptr, int mode, MACB *macb ){ QUEUE *aq, *nq; size_t usesz = 0, fresz = 0, sz; W usebk = 0, frebk = 0, npage = 0; BOOL newpg, ptr_ok; /* Check each space in turn. */ newpg = TRUE; ptr_ok = ( ptr == NULL )? TRUE: FALSE; for ( aq = macb->areaque.next; aq != &macb->areaque; aq = aq->next ) { if ( newpg && !chkAreaFlag(aq, AREA_TOP) ) { goto err_found; } if ( chkAreaFlag(aq, AREA_END) ) { if ( newpg != 0 ) { goto err_found; } newpg = TRUE; fresz += sizeof(QUEUE); npage++; continue; } newpg = FALSE; nq = aq->next; if (( Mask(aq->next) != nq )||( nq <= aq )||( Mask(nq->prev) != aq )) { goto err_found; } sz = (size_t)((VB*)nq - (VB*)aq); if ( sz < (sizeof(QUEUE) * TSD_CAL_SZ_3) ) { goto err_found; } if ( chkAreaFlag(aq, AREA_USE) ) { usesz += sz; ++usebk; if ( ptr == (void*)(aq + 1) ) { ptr_ok = TRUE; } if ( mode < TSD__CAL_MOD_M1 ) { SYSLOG((LOG_NOTICE, "malloc ptr: 0x%08x [%d B]", aq + 1, AreaSize(aq))); } } else { fresz += sz; ++frebk; } } if ( !newpg ) { goto err_found; } if ( !ptr_ok ) { SYSLOG((LOG_ERR, "MALLOC: illegal ptr: 0x%08x", ptr)); return FALSE; } if ( mode < 0 ) { SYSLOG((LOG_NOTICE, "MALLOC: %d pages, used: %d [%d blks] free: %d [%d blks]", npage, usesz, usebk, fresz, frebk)); } return TRUE;err_found: SYSLOG((LOG_ERR, "MALLOC: block corrupted at 0x%08x", aq)); return FALSE;}/* ------------------------------------------------------------------------ *//* * Test * mode = 0 Normal mode (default) * 1 Debug mode * -1 Dump usage * -2 Dump usage detail */EXPORT void _mem_malloctest( int mode, MACB *_macb ){ MACB *macb = AlignMACB(_macb); if ( mode >= 0 ) { /* Change test mode */ macb->testmode = mode; _mem_chkalloc = chkalloc; } else { /* Dump usage */ chkalloc(NULL, mode, macb); }}/* * Check errors in memory allocation * If ptr != NULL, check if ptr corresponds correctly to the allocated block, * and check errors in memory allocation. * If ptr == NULL, check errors in memory allocation only. * If it is normal, return "True". */EXPORT BOOL _mem_malloccheck( void *ptr, MACB *_macb ){ MACB *macb = AlignMACB(_macb); return chkalloc(ptr, 0, macb);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -