Code Analysis: wine heapfree Function

Source: Internet
Author: User

Heapfree

For code location, life, and references, refer to the previous article :)

Heapfree-> rtlfreeheap
This function calls the rtlfrreheap directly. Check the parameters and determine the flag.

Pinuse = (arena_inuse *) PTR-1;
If (! (Subheap = heap_findsubheap (heapptr, pinuse) goto error;
Call the heap_findsubheap function to locate the sub-heap pointer of the inuse block before the current inuse block, and check whether the block is a valid block.

Then call the heap_makeinuseblockfree () function to change the inuse block to idle. Let's take a look at this function:
Heapfree-> rtlfreeheap-> heap_makeinuseblockfree
If (PARENA-> size & arena_flag_prev_free)
First, check whether the size of the space to be released and the previous block are idle. If the previous block is also idle, merge the first idle block and the released block as a larger idle block. If no, only the current block is converted to the idle block.

Then, call heap_createfreeblock to convert the original inuse block into a free block. In fact, it is to change the structure, then convert the flag bit to free, and attach it to the idle queue.
For example
If the free block is not the last block, return to rtlfreeheap and continue execution. If the block converted to free is
The last block of the sub-heap, so we can continue to see that this sub-heap is not the first subheap and is empty. Then the whole subheap is released together.

Code: select all
545     if (((char *)pFree == (char *)subheap->base + subheap->headerSize) &&
546         (subheap != &subheap->heap->subheap))
547     {
548         SIZE_T size = 0;
549         void *addr = subheap->base;
550         /* Remove the free block from the list */
551         list_remove( &pFree->entry );
552         /* Remove the subheap from the list */
553         list_remove( &subheap->entry );
554         /* Free the memory */
555         subheap->magic = 0;
556         NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
557         return;
558     }

Then return to rtlfreeheap (). If the share flag is set and enters the critical section, exit the critical section and return true, indicating that the release is successful.
If an error occurs in the middle of the process, the system redirects to The Goto location, removes the critical section, returns the error code, and returns false.

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.