粗解“new”之来龙去脉(三)

来源:互联网 发布:cf视频软件中文版 编辑:程序博客网 时间:2024/05/19 11:48
 


我们暂且停止对内存分配的查找,看看这些变量是何时被初始化的,通过查找源代码我们可以看到是在__sbh_heap_init中对它赋值的。

HANDLE _crtheap;
int __cdecl __sbh_heap_init (void){    if (!(__sbh_pHeaderList = HeapAlloc(_crtheap, 0, 16 * sizeof(HEADER))))        return FALSE;    __sbh_pHeaderScan = __sbh_pHeaderList;    __sbh_pHeaderDefer = NULL;    __sbh_cntHeaderList = 0;    __sbh_sizeHeaderList = 16;    return TRUE;}

_crtheap是个句柄,它又是什么时候获得的呢,我们接着查找代码: 

#define BYTES_PER_PARA      16#define PARAS_PER_PAGE      256     //  tunable value#define BYTES_PER_PAGE      (BYTES_PER_PARA * PARAS_PER_PAGE)int __cdecl _heap_init (        int mtflag        ){        //  Initialize the "big-block" heap first.        if ( (_crtheap = HeapCreate( mtflag ? 0 : HEAP_NO_SERIALIZE,                                     BYTES_PER_PAGE, 0 )) == NULL )            return 0;        //  Initialize the small-block heap        if (__sbh_heap_init() == 0)        {            HeapDestroy(_crtheap);            return 0;        }        return 1;}


很明显是在这里获得的,调用HeapCreate获得的,HeapCreate这个函数的详细解释可以参考msdn的说明,我直接把他摘录过来,方便大家查阅

flOptions [in] The heap allocation options. These options affect subsequent access to the new heap through calls to the heap functions. This parameter can be 0 or one or more of the following values. HEAP_CREATE_ENABLE_EXECUTE 0x00040000  All memory blocks that are allocated from this heap allow code execution, if the hardware enforces data execution prevention. Use this flag heap in applications that run code from the heap. If HEAP_CREATE_ENABLE_EXECUTE is not specified and an application attempts to run code from a protected page, the application receives an exception with the status code STATUS_ACCESS_VIOLATIONHEAP_GENERATE_EXCEPTIONS 0x00000004  The system raises an exception to indicate failure (for example, an out-of-memory condition) for calls to HeapAlloc and HeapReAlloc instead of returning NULL. HEAP_NO_SERIALIZE 0x00000001  Serialized access is not used when the heap functions access this heap. This option applies to all subsequent heap function calls. Alternatively, you can specify this option on individual heap function calls. The low-fragmentation heap (LFH) cannot be enabled for a heap created with this option.A heap created with this option cannot be locked.For more information about serialized access, see the Remarks section of this topic dwInitialSize [in] The initial size of the heap, in bytes. This value determines the initial amount of memory that is committed for the heap. The value is rounded up to a multiple of the system page size. The value must be smaller than dwMaximumSize.If this parameter is 0, the function commits one page. To determine the size of a page on the host computer, use the GetSystemInfo function.dwMaximumSize [in] The maximum size of the heap, in bytes. The HeapCreate function rounds dwMaximumSize up to a multiple of the system page size and then reserves a block of that size in the process's virtual address space for the heap. If allocation requests made by the HeapAlloc or HeapReAlloc functions exceed the size specified by dwInitialSize, the system commits additional pages of memory for the heap, up to the heap's maximum size. If dwMaximumSize is not zero, the heap size is fixed and cannot grow beyond the maximum size. Also, the largest memory block that can be allocated from the heap is slightly less than 512 KB for a 32-bit process and slightly less than 1,024 KB for a 64-bit process. Requests to allocate larger blocks fail, even if the maximum size of the heap is large enough to contain the block. If dwMaximumSize is 0, the heap can grow in size. The heap's size is limited only by the available memory. Requests to allocate memory blocks larger than the limit for a fixed-size heap do not automatically fail; instead, the system calls the VirtualAlloc function to obtain the memory that is needed for large blocks. Applications that need to allocate large memory blocks should set dwMaximumSize to 0.HANDLE WINAPI HeapCreate(  __in  DWORD flOptions,  __in  SIZE_T dwInitialSize,  __in  SIZE_T dwMaximumSize);

    这个函数的返回值是一个句柄,后面很多操作都会用到这个句柄。

   我们查阅文档说明得知所有内存分配都必须在这些都初始化完成后才能使用,那_heap_init又是什么时候调用的?咱们可以在main函数的第一个语句就开始调用new去分配内存,它当然要比调用main还早就初始化了,那只能是系统调用的函数了,那什么函数会在main之前调用呢?

原创粉丝点击