Memory management in Delphi[3]
// Build the heap; note that the size specified during the build is also aligned according to the page size (PageSize), for example, if 15k is specified, 16K will be allocated. HeapCreate ( flOptions: DWORD; {heap attribute options, see table below} dwInitialSize: DWORD; {initial size, the unit is byte; the size will be submitted directly to the actual memory} dwMaximumSize: DWORD {Maximum size, set to 0 if the maximum value is not limited} ): THandle; {return heap handle; 0 is returned for failure, but if exception is allowed by parameter flOptions, exception ID is returned for failure} // flOptions parameter optional values: HEAP_NO_SERIALIZE = 1; {Non-mutually exclusive, this flag allows multiple threads to access this heap at the same time} HEAP_GENERATE_EXCEPTIONS = 4; {When creating a heap error, this flag can raise an exception and return the exception ID} HEAP_ZERO_MEMORY = 8; {Initialize the allocated memory to 0} // flOptions parameter may return exceptions when HEAP_GENERATE_EXCEPTIONS is specified: STATUS_ACCESS_VIOLATION = DWORD ($ C0000005); {Parameter error} STATUS_NO_MEMORY = DWORD ($ C0000017); {Out of memory} // Destroy the heap HeapDestroy ( hHeap: THandle {heap handle} ): BOOL; {} // Apply memory from the heap HeapAlloc ( hHeap: THandle; {heap handle} dwFlags: DWORD; {Memory attribute options, see table below} dwBytes: DWORD {size of application memory, unit is byte} ): Pointer; {return memory pointer; failure returns 0 or exception, the situation is the same as the establishment of the heap} // dwFlags parameter optional values: HEAP_NO_SERIALIZE = 1; {Non-mutually exclusive, this flag allows multiple threads to access this heap at the same time} HEAP_GENERATE_EXCEPTIONS = 4; {When creating a heap error, this flag can raise an exception and return the exception ID} HEAP_ZERO_MEMORY = 8; {Initialize the allocated memory to 0} {It can be seen that this is the same as the heap attribute options; if the dwFlags parameter is set to 0, the heap attributes will be used; if re-specified, the heap attributes will be overwritten} {Also: if the heap is the default heap, that is, the heap handle is from GetProcessHeap, the dwFlags parameter is ignored} // Change the size of the heap memory, that is, reallocate HeapReAlloc ( hHeap: THandle; {handle} dwFlags: DWORD; {Memory attribute option; this parameter has one more option than HeapAlloc, see the table below} lpMem: Pointer; {raw memory pointer} dwBytes: DWORD {new size} ): Pointer; {with HeapAlloc} // dwFlags parameter optional values: HEAP_NO_SERIALIZE = 1; {Non-mutually exclusive, this flag allows multiple threads to access this heap at the same time} HEAP_GENERATE_EXCEPTIONS = 4; {When creating a heap error, this flag can raise an exception and return the exception ID} HEAP_ZERO_MEMORY = 8; {Initialize the allocated memory to 0} HEAP_REALLOC_IN_PLACE_ONLY = 16; {This tag does not allow changing the original memory location} // Get the size of a block of memory in the heap HeapSize ( hHeap: THandle; {heap handle} dwFlags: DWORD; {memory attribute; optional value is 0 or HEAP_NO_SERIALIZE, the latter ensures synchronous access} lpMem: Pointer {memory pointer} ): DWORD; {successfully returns the size in bytes; failure returns $ FFFFFFFF} // Free the specified memory block in the heap HeapFree ( hHeap: THandle; {heap handle} dwFlags: DWORD; {memory attribute; optional value is 0 or HEAP_NO_SERIALIZE} lpMem: Pointer {memory pointer} ): BOOL; {} // verify the heap HeapValidate ( hHeap: THandle; {} dwFlags: DWORD; {} lpMem: Pointer {} ): BOOL; {} // sort the heap HeapCompact ( hHeap: THandle; {} dwFlags: DWORD {} ): UINT; {} // Lock the heap HeapLock ( hHeap: THandle {} ): BOOL; {} // Unlock after locking HeapUnlock ( hHeap: THandle {} ): BOOL; {} // List memory blocks in the heap HeapWalk ( hHeap: THandle; {} var lpEntry: TProcessHeapEntry {} ): BOOL; {}
Let’s put an example down.
Orignal link:https://www.cnblogs.com/del/archive/2008/05/08/1188715.html