现在的位置: 首页 > 综合 > 正文

CLR内核调试之:Malloc函数实现

2013年05月04日 ⁄ 综合 ⁄ 共 3119字 ⁄ 字号 评论关闭

         自从可以动态调试SSCLI之后,发现这个玩意还真是个宝山,越玩越有意思,就像捅开一扇门,发现门后面还有一座宝山……不光CLR的内部实现细节,可以象是放电影一样呈现在眼前,Visual Studio里面的一些底层的技术,还有OS的底层技术,查看起来那是相当的便捷。

         这里就说说malloc函数是具体如何实现的,能够F10,F11动态的在VS里面跟踪其一步一步的实现的源码,真是一件痛快的事情。

 

         为嘛要说malloc的实现呢?因为在main开始以后,要为传递进来的参数分配内存地址。而这个时候我不小心按了F11,不小心看到了malloc是具体如何运作的:

        

         Clix.cppint __cdecl main(int argc, char **argv)之后,

         首先得到参数行,然后分配一个空间把这个参数行保存起来:

 

    pwzCmdLine = ::GetCommandLineW();

    // Allocate for the worst case storage requirement.

WCHAR *pAlloc = (WCHAR*)malloc((wcslen(pwzCmdLine) + 1) * sizeof(WCHAR));

 

首先获得到命令行,然后使用malloc分配ygie空间给存起来。sizeof(WCHAR)的含义ms是在后面加上一个终止的标记。

Malloc函数,首先跳转到了d:/Rotor/sscli20/pal/win32/win32pal.c下:

One:

PALIMPORT

void *

__cdecl

PAL_malloc(size_t bytes)

{

         //mark how much bytes had successful allocated.

    void *Ret;

    LOGAPI("malloc(bytes=%p)/n", bytes);

    Ret = malloc(bytes);

 

    LOGAPI("malloc returns void* %p/n", Ret);

    return Ret;

}

这是一个PAL_malloc,是sscli里面针对特定的操作系统的PAL层的实现,因为我用的是操作系统是Windows XP En,这里就开始调用操作系统里面的malloc的实现了,在看malloc之前,先看看LOGAPI是干嘛的:

Two

void

__cdecl

PalLogApi(const char *fmt,...)

{

    va_list list;

    // Assert that the PAL APIs are only being called when the PAL

    // is properly initialized.

 

    // This assert is disabled because of gcc startup code calls Win32 functions before calling PAL initialize.

// PALASSERT(PalReferenceCount != 0);

 

    if (LogFileHandle == INVALID_HANDLE_VALUE) {

        // Logging isn't enabled

        return;

    }

    va_start(list, fmt);

    PalLogApiCore(fmt, list);

    va_end(list);

}

         首先,我很奇怪C++里面的是个怎么样子的参数传递方法….

         打开一看,咱就知道了,这个是和SSCLI的调试功能紧密结合在一起的。下面截图了个:

 

 

         这下看到了,LogFileHandle表示如果开启了SSCLIlog功能,就把这次malloc的情况给记录下来。

然后这里没有开启LOG功能,这个变量就是0xfffffff,然后就直接return了。继续回到One里面的Ret = malloc(bytes);这个地方。这个方法的目的,是Allocate of block of memory of at least size bytes from the heap and return a pointer to it.下跳转到了Malloc的本地实现了,到了文件:

Three

C:/Program Files/Microsoft Visual Studio 9.0/VC/crt/src/dbgmalloc.c

 

extern "C" _CRTIMP void * __cdecl malloc (size_t nSize)

{

        void *res = _nh_malloc_dbg(nSize, _newmode, _NORMAL_BLOCK, NULL, 0);

 

        RTCCALLBACK(_RTC_Allocate_hook, (res, nSize, 0));

        return res;

}

 

然后继续看调用的_nh_malloc_dbg来如何实现内存的分配,这下来到了

C:/Program Files/Microsoft Visual Studio 9.0/VC/crt/src/dbgheap.c,这个文件中:

Four

extern "C" void * __cdecl _nh_malloc_dbg (

        size_t nSize,

        int nhFlag,

        int nBlockUse,

        const char * szFileName,

        int nLine

        )

{

        int errno_tmp = 0;

        void * pvBlk = _nh_malloc_dbg_impl(nSize, nhFlag, nBlockUse, szFileName, nLine, &errno_tmp);

 

        if ( pvBlk == NULL && errno_tmp != 0 && _errno())

        {

            errno = errno_tmp; // recall, #define errno *_errno()

        }

        return pvBlk;

}

 

从名字上来看,是在Debug heap上面给分配的空间。这个方法的purpose,is Allocate of block of memory of at least size bytes from the debug heap and return a pointer to it. Assumes heap already locked.If no blocks available, call new handler.Allocates any type of supported memory block.

Five

extern "C" static void * __cdecl _nh_malloc_dbg_impl (

        size_t nSize,

        int nhFlag,

        int nBlockUse,

        const char * szFileName,

        int nLine,

        int * errno_tmp

        )

{

        void * pvBlk;

        for (;;)

        {

            /* do the allocation*/

            pvBlk = _heap_alloc_dbg_impl(nSize, nBlockUse, szFileName, nLine, errno_tmp);

            if (pvBlk)

            {

         

抱歉!评论已关闭.