您的位置:首页 > 编程语言 > C语言/C++

CoreCLR系列随笔 之ClrJit项目之alloc.cpp文件分析(1)

2017-06-03 22:13 429 查看
  首先声明,自己对CLR了解得不多,只是个人爱好,可能有错误,请指出,文件源码如下(可能不是最新的)

inline LPVOID ClrAllocInProcessHeap(DWORD dwFlags, S_SIZE_T dwBytes)
{
STATIC_CONTRACT_SUPPORTS_DAC_HOST_ONLY;
if (dwBytes.IsOverflow())
{
return NULL;
}

#ifndef SELF_NO_HOST
return __ClrAllocInProcessHeap(dwFlags, dwBytes.Value());
#else
#undef HeapAlloc
#undef GetProcessHeap
static HANDLE ProcessHeap = NULL;
if (ProcessHeap == NULL)
ProcessHeap = GetProcessHeap();
return ::HeapAlloc(ProcessHeap,dwFlags,dwBytes.Value());
#define HeapAlloc(hHeap, dwFlags, dwBytes) Dont_Use_HeapAlloc(hHeap, dwFlags, dwBytes)
#define GetProcessHeap() Dont_Use_GetProcessHeap()
#endif
}


View Code

LPVOID          nraVirtualAlloc(LPVOID lpAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect)
{
#if defined(DEBUG)
assert(lpAddress == 0 && flAllocationType == MEM_COMMIT && flProtect == PAGE_READWRITE);
if (nraDirectAlloc())
{
#undef GetProcessHeap
#undef HeapAlloc
return ::HeapAlloc(GetProcessHeap(), 0, dwSize);
}
else
return DbgNew(dwSize);
#else
return nraMemoryManager->ClrVirtualAlloc(lpAddress, dwSize, flAllocationType, flProtect);
#endif
}


  下面的代码就是做一些基本的赋值和检查。

if  (!newPage)
NOMEM();

#ifdef DEBUG
newPage->nrpSelfPtr = newPage;
#endif


  下面就是把新的page追加到list的后端。

/* Append the new page to the end of the list */

newPage->nrpNextPage = 0;
newPage->nrpPageSize = sizPage;
newPage->nrpPrevPage = nraPageLast;
newPage->nrpUsedSize = 0;  // nrpUsedSize is meaningless until a new page is allocated.
// Instead of letting it contain garbage (so to confuse us),
// set it to zero.

if  (nraPageLast)
nraPageLast->nrpNextPage = newPage;
else
nraPageList              = newPage;
nraPageLast = newPage;


  最后重新设置一下next和last指针,总之这个是个公共方法,只是nraInit里面只用到了为0的情况: pThis->nraAllocNewPage(0);

/* Set up the 'next' and 'last' pointers */

nraFreeNext = newPage->nrpContents + sz;
nraFreeLast = newPage->nrpPageSize + (BYTE *)newPage;

assert(nraFreeNext <= nraFreeLast);

return  newPage->nrpContents;


  回到init函数然后执行如下,结束nraInit方法

impJitErrorTrap()  // ERROR TRAP: The following block handles errors
{
result = true;
}
endErrorTrap()  // ERROR TRAP: End


  写完了,知道写得不太好,请指出错误,轻喷。晚安。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: