[Zz] windows heap management functions

Source: Internet
Author: User

The memory allocated by the user's memory allocation function is in the heap, so the heap management function is used to allocate and release the memory.
In Windows, each process has its own heap, and the number of heap processes varies. In Windows, the so-called heap is not a memory block,
A memory management object is a form of Memory organization. A process can allocate and release memory from its own stack.

Heap is a type of memory management object. A process has several of these heap. before allocating memory, you must specify the heap from which the heap is allocated. The heap handle uniquely identifies the heap.
The following two methods are provided:
1. Obtain the created heap in the process.
2. The process creates a heap by itself. With the heap handle, the memory can be allocated.
Allocate memory in the heap. You do not need to consider paging and alignment. memory blocks can be of any size.

1. heapcreate
Creates a new heap for the process and requests virtual memory paging.
HandleHeapcreate (
DWORDFloptions,
Size_t dwinitialsize,
Size_t dwmaximumsize
);
Floptions: enter a parameter to create a heap.
Heap_create_enable_execute memory allocation can be used for Code Execution
Heap_generate_exceptions if the analysis memory fails, an exception occurs, instead of returning null
Heap_no_serialize is not stored continuously
The initialization size (in bytes) of the dwinitialsize heap. 0. The system automatically allocates a memory page size.
Dwmaximumsize indicates the maximum heap size. 0 indicates the heap size increases upwards.
Return NULL for failure

2. getprocessheap
Obtains a heap in the current process and returns the heap handle.
HandleGetprocessheap (void );
Return NULL for failure

3. getprocessheaps
Obtain all the heap in the process, including the number of heap and heap handle.
DWORDGetprocessheaps (
DWORDNumberofheaps,
Phandle processheaps
);
Numberofheaps: Number of handles that can be stored in the buffer zone processheap.
Processheaps: the output parameter pointing to the memory block used to save all heap handles in the process. The handle value of the return value of the function will be saved in it.
Return Value:
DWORDType of data, number of heaps in the process. If 0 is returned, it will fail.
The returned value is greater than numberofheaps, indicating that the actual number of heaps in the process is greater than numberofheaps. The memory area used to store the handle value is too small,
Processheaps points to the memory block with no handle value. If the value is smaller than the value, processheaps stores the handle values of all the heaps.

4. allocate memory blocks from the specified Stack
Lpvoid heapalloc (
Handle
Hheap,
DWORD
Dwflags,
Size_t dwbytes
);
Hheap: Enter a parameter to allocate memory on the stack specified by this parameter.
Dwflags: Input parameter, memory allocation flag
Heap_generate_exceptions: if an allocation error occurs, an exception is thrown instead of null.
(The status_no_memory memory is insufficient and the status_access_violationc access is invalid)
Heap_no_serialize does not use continuous Storage
Heap_zero_memory clears all content in the allocated memory block.
Dwbytes: input parameter, memory area size to be allocated, in bytes.
Returns the newly allocated memory pointer. If the pointer fails to be null

5. lpvoid heaprealloc (
Handle hheap,
DWORD dwflags,
LpvoidLpmem,
Size_t dwbytes
);
Hheap: Enter a parameter to allocate memory on the stack specified by this parameter.
Dwflags: Input parameter, memory allocation flag.
Heap_generate_exceptions: if an allocation error occurs, an exception is thrown instead of null.
(The status_no_memory memory is insufficient and the status_access_violationc access is invalid)
Heap_no_serialize does not use continuous Storage
Heap_zero_memory and heapalloc have different functions. When this flag is set, the new memory will be cleared and the data in the original memory block will not change.
Heap_realloc_in_place_only,
Heap_realloc_in_place_only indicates that the location of the re-allocated memory should not be changed.
Lpmem: Enter the parameter, the address of the original memory block.
Dwbytes: input parameter. The value after memory block size adjustment can be larger or smaller than the original one.
Returned value: success. lpvoid type data is returned, which is a pointer to the memory block after the size is adjusted.

6. getsysteminfo
Obtain system information, including OEM information, memory block size, memory allocation granularity, processor information, maximum and minimum memory space.
Void getsysteminfo (
Lpsystem_info lpsysteminfo
);
Lpsysteminfo: output function, pointing to the system_info structure pointer, save and obtain information.

7.HeapsizeObtains the size of a specified heap. The heap size is returned in bytes.
Size_tHeapsize(
Handle hheap,
DWORD dwflags,
Lpvoid lpmem
);
Hheap: Enter the parameter to obtain the heap handle of the memory block.
Dwflags: Input parameter, memory block flag, which can be heap_no_serialize
Lpmem: The memory block pointer of the desired size.
The size of the size_t memory block is returned. The failure value is-1.

8. heapfree
Release the memory allocated by heapalloc and heaprealloc. The function prototype is as follows:
Bool heapfree (
Handle hheap,
DWORD dwflags,
LpvoidLpmem
);
Hheap: Input parameters. The heapalloc and heaprealloc must be used to allocate the same heap memory.
Dwflags: Input parameter, memory block flag, which can be set to heap_no_serialize
Lpmem: Enter the parameter, the memory block pointer to be released.

9. heapdestroy
Destroy the heapcreate heap. The function prototype is as follows:
Bool heapdestroy (
Handle hheap
);
Hheap: Enter a parameter to destroy the heap handle.
Returns the bool value, indicating whether it is successful.

Code:

/* Header file */
# Include <windows. h>
# Include <stdio. h>

/*************************************
*DWORDPrintheapsize (Handle hheap, Lpvoid
Lpmem)
* Obtain and print the heap size.
*
* ParametersHandle hheap, Heap handle
* LpvoidLpmem, Memory address pointer
*
* If the return value is 0, the execution is completed. If the return value is 1, an error occurs in the code.
**************************************/
DWORDPrintheapsize (Handle hheap, Lpvoid
Lpmem)
{
Size_t dwheapsize;
Dwheapsize =Heapsize(Hheap, Heap_no_serialize,Lpmem);
If (dwheapsize =-1)
{
Printf ("GetHeapsizeError: % d ", getlasterror ());
Return 1;
}
Printf ("memory block size: 0x % x \ n", dwheapsize );
Return 0;
}

/*************************************
* Int main (INT argc, pchar argv [])
* The function shows how to use the heap.
*
* If the argv parameter [1] is "-s", use the process heap.
* Argv [1] creates a heap of a variable size if it is "-".
* If argv [1] is another type, create a heap with the maximum size.
*
*
**************************************/
Int main (INT argc, pchar argv [])
{
System_info Si; // system information
Handle hheap; // Heap handle
LpvoidLpmem; // Memory block pointer
Lpvoid lprealloc; // pointer after memory block size adjustment
DWORDDwheapsize; // heap memory port size
HandleHheaps [24]; // used to save all heap handles in the process
DWORDDwheapnum; // Number of heaps in the Process \
// Obtain system information
Getsysteminfo (& Si );
// Print the system memory paging size and memory allocation granularity.
Printf ("system memory page size: 0x % x \ n system memory allocation granularity: 0x % x \ n ",
Si. dwpagesize, Si. dwallocationgranularity );
// Analyze the input parameters. If it is "-a", create a heap with a maximum size of 10 pages.
If (argc = 2 & 0 = lstrcmp (argv [1], "-"))
{
Hheap= Heapcreate (heap_no_serialize,
Si. dwpagesize, Si. dwpagesize * 10 );
Printf ("Create a heap. The initialization size is 1 page, and the maximum size is 10 pages \ n ");
}
// If the input parameter is "-s", use the heap that already exists during process initialization.
Else if (argc = 2 & 0 = lstrcmp (argv [1], "-s "))
{
Hheap= Getprocessheap ();
Printf ("obtain the heap \ n existing in the system ");
}
// Create a heap that can grow if you enter other items
Else
{
Hheap= Heapcreate (heap_no_serialize, 0, 0 );
Printf ("Create a heap. The initialization size is 1 page and the size is variable \ n ");
}
// Determine whether the heap is created/obtained successfully
If (Hheap= NULL)
{
Printf ("process heap creation or retrieval error: % d", getlasterror ());
Return 1;
}
// Obtain how many heaps are printed in the current process, and whether a new heap exists. The value will be different.
Dwheapnum = getprocessheaps (24, hheaps );
If (dwheapnum = 0)
{
Printf ("getprocessheaps error: % d", getlasterror ());
}
Else
{
Printf ("the current process has a total of % d heap \ n", dwheapnum );
}
// Analyze memory on the heap, with three page sizes
Lpmem= Heapalloc (Hheap, Heap_zero_memory, Si. dwpagesize * 3 );
If (Lpmem= NULL)
{
Printf ("heapalloc error: % d", getlasterror ());
Return 1;
}
Printf ("memory allocated successfully on the stack, starting at 0x % x \ n ",Lpmem);
// Print the size of the current heap memory block
Printheapsize (Hheap,Lpmem);
// Reallocate the memory and adjust the memory size to 11 pages,
// If you use the first method to create a heap, an error occurs.
Lprealloc = heaprealloc (Hheap, Heap_zero_memory,
Lpmem, Si. dwpagesize * 11 );
If (lprealloc = NULL)
{
Printf ("heaprealloc error: % d", getlasterror ());
Return 1;
}
Printf ("memory allocation on the stack, address: 0x % x, original address: 0x % x \ n", lprealloc,Lpmem);
// Print the heap memory block size after resizing
Printheapsize (Hheap, Lprealloc );

// Release the memory
If (! Heapfree (Hheap, Heap_no_serialize, lprealloc ))
{
Printf ("heapfree error: % d", getlasterror ());
Return 1;
}
Printf ("memory released successfully \ n ");
// If a new heap is created, the heap is destroyed.
If (argc! = 2 | 0! = Lstrcmp (argv [1], "-s "))
{
Printf ("Destroy heapcreate created heap \ n ");
If (! Heapdestroy (Hheap))
{
Printf ("heapdestroy error: % d", getlasterror ());
Return 1;
}
Printf ("heap destroyed successfully \ n ");
}
Return 0;
}

Original link: http://hi.baidu.com/bodogbo/blog/item/c2b1663bf4c846ff3b87ce5e.html

Question: What is the usage of lpmem?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.