C + + Essay: GC exploration of. NET CORECLR (1)

Source: Internet
Author: User

Always was. NET programmer, but. NET's core is actually C + +, so I'm going to spend a little time studying coreclr and Corefx. Hopefully this series of articles will help you.

GC code has a lot of, and the structure of the hierarchy for a beginner, it is difficult to quickly or very slow to master, so my advice is to seize a function, to see the actual. Originally wanted to tell you from the beginning, but look at the beginning is also messy, anyway, there is not much experience, simply speaking casually.

Returns the direct precursor node of the second parameter seg heap_segment* heap_segment_prev (heap_segment* begin, heap_segment* seg) {//Determines whether the begin pointer points to NULL    assert (begin! = 0);//define a local variable so that the first parameter begin points to the local pointer.    heap_segment* prev = begin;//First gets the address of the next heap fragment based on the Begin base    heap_segment* current = Heap_segment_next (begin);// The loop determines if the "next" address is not the address of the second parameter of the function is pointing to the same block of memory while    (present && current!!        prev = current;//to try to get the next address current        = Heap_segment_next (current);    } The last loop ends, if the current segment equals the formal parameter, returns the previous    if (present = = seg)    {        return prev;    } If the current segment is a head node, then there is no direct precursor, return null    else    {        return 0;    }}


Note that the heap_segment here is the class name, defined in Gcpriv.h, and Heap_segment_prev is the name of the method. Heap_segment, my understanding is actually very simple, is "the collection of heaps", each element of the collection, is one of its segment, each element is a chain-like connection.

Let's take a look at the tolerance distortion of this class, it's important to note that many of their types are uint8_t, so what are the characteristics of this structure? According to the POSIX standard, the *_t type corresponding to the generic shaping is:

1 bytes uint8_t
2 bytes uint16_t
4 bytes uint32_t
8 bytes uint64_t

So we can get a very important message that the fragments they store are all in one byte and why they are stored in this way, and I don't understand.

class heap_segment{public:uint8_t* allocated;//Allocated Space uint8_t* Committed The uint8_t* reserved has been submitted; Uint8_t* used that have been stored;  uint8_t* mem already in use; Space size_t flags; Mark Ptr_heap_segment Next; Fragments of the next heap uint8_t* plan_allocated; "Will" allocated space//if BACKGROUND_GC is defined, execute the following code (background GC) #ifdef background_gc uint8_t* background_allocated; Background assignment uint8_t* saved_bg_allocated;   The saved background assignment #endif//background_gc//multiple heaps (more than one heap) #ifdef multiple_heaps gc_heap* Heap; After all, this class is complicated and will be specifically drawn out later in the chapter #endif//multiple_heaps#ifdef _msc_ver//Disable This warning-we intentionally want __declspec (    Align ()) to insert padding for Us#pragma warning (disable:4324)//structure is padded due to __declspec (align ()) #endif Aligned_plug_and_gap padandplug; #ifdef _msc_ver#pragma Warning (default:4324)//structure was padded due to __declspec ( Align ()) #endif}; 

Where I'll explain ptr_heap_segment, it's actually a custom type

typedef DPTR (class heap_segment)               ptr_heap_segment;

Here we go back to heap_segment_prev this method, if you can understand the picture below me, you should be able to understand this method exactly what to do. In fact, the intention is obviously, we can think of the heap as a linked list, for the time being we do not know what the list is linked list, this is not important, it is important, we must first know which node we want to start, and then to find which node, is the corresponding first parameter and the second parameter, First we go into a while loop, and if we get the first non-null and the next node of the heap (segment) does not match the second parameter, how does it fit? is 2 addresses is not pointing to the same block of memory! Until it is found, return the predecessor node of the heap at the specified location.

Heap_segment_next This function, we see that it is actually pointing to the next heap of Heap_segment and returning as an address.

Inlineptr_heap_segment & Heap_segment_next (heap_segment* inst) {  return inst->next;}

Let's take a look at the Heap_segment_in_range function. Let's look at its definition first.

Inlinebool heap_segment_in_range_p (heap_segment* inst) {//it returns a bool type, of course this bool is a custom    return (!) ( Inst->flags & heap_segment_flags_readonly) | |            ((Inst->flags & heap_segment_flags_inrange)! = 0));}

Of course we have to know a basic definition, and the following variables are defined inside themselves.

#define HEAP_SEGMENT_FLAGS_READONLY     1#define heap_segment_flags_inrange      2

From this it can be inferred thatheap_segment_in_range_p is true.

Let's take a look at this method and note that I've already hit it, but I wonder why the detection boundary is tested in such a chained way that we're going to run the entire list once and wonder if it's the best way.

Check the heap boundaries, that is, the last element heap_segment* Heap_segment_in_range (heap_segment* ns) {//whether this will be performed, depends on some features of heap_segment (I don't know, So not nonsense)    if ((ns = = 0) | | heap_segment_in_range_p (NS)    ) {        return ns;    }    else    {        do        {///This piece of code is actually a loop, its function is to detect a "right" boundary of the heap            ns = Heap_segment_next (ns);        } while ((ns! = 0) & &!heap_segment_in_range_p (NS));        return ns;    }}

Today also want to write, but very late, or tomorrow to work again not to come .... Sleep first, goodnight.

C + + Essay: GC exploration of. NET CORECLR (1)

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.