Memory Management 3

Source: Internet
Author: User

The memory allocated by virtualalloc is 4 K as the smallest unit and continuous memory address (but it is not necessarily continuous when mapped to the real memory). As mentioned above, it is not suitable for allocating small memory (for example, variables with only a few bytes). Local variables are automatically managed by programs in the "stack". What about global small variables? This requires "Heap ".
9 q %/+ Q1 L,} 1 F2 x $ D5 D! A
+ N! Q4 o % a Q5 x0 R3 Y: In this case, the memory allocated by virtualalloc is neither "stack" nor "Heap"; the memory address allocated by virtualalloc is continuous, the content in the "Heap" is generally discontinuous, so it is troublesome to manage the "Heap". It is managed through the structure of the double-line linked list. The program can have several "Heap ", each "Heap" has a handle. when accessing the content in "Heap", you must first find this "Heap" and then repeat the chain table, this may be the root cause of "Heap" being slower than "stack.
% T: Q5 B1 I + [/E9 R' K # F--------------------------------------------------------------------------------2 P9 D6 W; y (h $ l0 M7 O, P
0G $ J % {! L + M K8 I/c
Before allocating memory (heapalloc) in "Heap", you must create a "heapcreate", just as the program has a default "stack, each program has a default "Heap" (you can use getprocessheap to obtain the "Default Heap" handle). When we use "Heap" in Delphi, this "Default Heap" is used ". if you want your program to have multiple "heaps" more flexibly, you must use API functions. 5 J0 g % y9 L4 K0 Q

, T. P L1 G & D (E submits real memory at the same time when creating a "Heap", which will be very slow when applying for large memory, so the default heap is only 1 m, however, the "Default Heap" does not limit the size. It will increase dynamically as needed. * l-E:] 8 K (E1 K # F

! I + m; G * V4 s;/; O) x' does it have "Default Heap"? Is it necessary to apply for another "Heap? This can only be reflected in multiple threads. Unlike the stack, the program assigns a "Stack zone" to each thread, while the default heap is shared by all threads in the process, when a thread uses the "Default Heap", the other thread that needs to use the "Heap" must first suspend and wait, that is, they cannot be used at the same time; only private stacks created through API functions are mutually independent and most efficient. 5 C "O3 Z (E4 R8 U" R & t0?
------------------------------------------------------------------------------; K0 W4 ['r # F1 W & R2 A1 B,

+ O5 R &! L. D4 D; D3 _ first understand the functions related to "Heap".) B 'J2 A * m (d % K % C) I; M7 g
--------------------------------------------------------------------------------

  1. // Create a heap. Note that the size specified during creation is also aligned by page size (pagesize). For example, if 15 K is specified, 16 K is actually allocated.
  2. Heapcreate (
  3. Floptions: DWORD; {heap attribute options, as shown in the table below}
  4. Dwinitialsize: DWORD; {initial size, in bytes; this size will be submitted directly to the actual memory}
  5. Dwmaximumsize: DWORD {maximum size. If the maximum size is not limited, set it to 0}
  6. ): Thandle; {heap handle is returned; 0 is returned for failure. However, if the floptions parameter permits exceptions, an exception identifier is returned for failure}
  7. // Optional value of floptions parameter:
  8. Heap_no_serialize = 1; {non-mutex. This flag allows multiple threads to access the heap at the same time}
  9. Heap_generate_exceptions = 4; {when a heap creation error occurs, this flag can trigger an exception and return an exception identifier}
  10. Heap_zero_memory = 8; {initialize the allocated memory to 0}
  11. // If the floptions parameter specifies heap_generate_exceptions, an exception may be returned:
  12. Status_access_violation = DWORD ($ c0000005); {parameter error}
  13. Status_no_memory = DWORD ($ c0000017); {insufficient memory}

Copy code

--------------------------------------------------------------------------------

  1. // Destroy the heap
  2. Heapdestroy (
  3. Hheap: thandle {heap handle}
  4. ): Bool ;{}

Copy code

--------------------------------------------------------------------------------

  1. // Apply for memory from the heap
  2. Heapalloc (
  3. Hheap: thandle; {heap handle}
  4. Dwflags: DWORD; {Memory attribute options, as shown in the table below}
  5. Dwbytes: DWORD {size of the applied memory, in bytes}
  6. ): Pointer; {memory pointer returned; 0 or an exception is returned if the heap fails to be created}
  7. // Optional dwflags values:
  8. Heap_no_serialize = 1; {non-mutex. This flag allows multiple threads to access the heap at the same time}
  9. Heap_generate_exceptions = 4; {when a heap creation error occurs, this flag can trigger an exception and return an exception identifier}
  10. Heap_zero_memory = 8; {initialize the allocated memory to 0}
  11. {It can be seen that this is the same as the heap attribute option. If the dwflags parameter is set to 0, the heap attribute will be used. If it is re-specified, the heap attribute will be overwritten}
  12. {In addition, if the heap is a default heap, that is, the heap handle comes from getprocessheap, The dwflags parameter will be ignored}

Copy code

--------------------------------------------------------------------------------

  1. // Change the heap memory size, that is, reallocation
  2. Heaprealloc (
  3. Hheap: thandle; {handle}
  4. Dwflags: DWORD; {Memory attribute option; this parameter is more than heapalloc, as shown in the table below}
  5. Lpmem: pointer; {original memory pointer}
  6. Dwbytes: DWORD {New Size}
  7. ): Pointer; {same as heapalloc}
  8. // Optional dwflags values:
  9. Heap_no_serialize = 1; {non-mutex. This flag allows multiple threads to access the heap at the same time}
  10. Heap_generate_exceptions = 4; {when a heap creation error occurs, this flag can trigger an exception and return an exception identifier}
  11. Heap_zero_memory = 8; {initialize the allocated memory to 0}
  12. Heap_realloc_in_place_only = 16; {This mark cannot change the original memory location}

Copy code

--------------------------------------------------------------------------------

  1. // Obtain the memory size of a block in the heap.
  2. Heapsize (
  3. Hheap: thandle; {heap handle}
  4. Dwflags: DWORD; {Memory attribute; optional values: 0 or heap_no_serialize, which ensures synchronous access}
  5. Lpmem: pointer {memory pointer}
  6. ): DWORD; {size of the successful returned bytes; $ ffffffff returned if the failure is returned}

Copy code

--------------------------------------------------------------------------------

  1. // Release the specified memory block in the heap.
  2. Heapfree (
  3. Hheap: thandle; {heap handle}
  4. Dwflags: DWORD; {Memory attribute; optional values: 0 or heap_no_serialize}
  5. Lpmem: pointer {memory pointer}
  6. ): Bool ;{}

Copy code

--------------------------------------------------------------------------------

  1. // Verify the heap
  2. Heapvalidate (
  3. Hheap: thandle ;{}
  4. Dwflags: DWORD ;{}
  5. Lpmem: pointer {}
  6. ): Bool ;{}

Copy code

--------------------------------------------------------------------------------

  1. // Organize the heap
  2. Heapcompact (
  3. Hheap: thandle ;{}
  4. Dwflags: DWORD {}
  5. ): Uint ;{}

Copy code

--------------------------------------------------------------------------------

  1. // Lock the heap
  2. Heaplock (
  3. Hheap: thandle {}
  4. ): Bool ;{}

Copy code

--------------------------------------------------------------------------------

  1. // Unlock after lock
  2. Heapunlock (
  3. Hheap: thandle {}
  4. ): Bool ;{}

Copy code

--------------------------------------------------------------------------------

  1. // List memory blocks in the heap
  2. Heapwalk (
  3. Hheap: thandle ;{}
  4. VaR lpentry: tprocessheapentry {}
  5. ): Bool ;{}

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.