<commit_before>﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using Microsoft.Diagnostics.Runtime.DacInterface;

namespace Microsoft.Diagnostics.Runtime.Desktop
{
    internal abstract class DesktopGCHeap : HeapBase
    {
        public DesktopGCHeap(DesktopRuntimeBase runtime)
            : base(runtime)
        {
            DesktopRuntime = runtime;
            _types = new List<ClrType>(1000);
            Revision = runtime.Revision;

            // Prepopulate a few important method tables.
            _arrayType = new Lazy<ClrType>(CreateArrayType);
            _exceptionType = new Lazy<ClrType>(() => GetTypeByMethodTable(DesktopRuntime.ExceptionMethodTable, 0, 0));
            ErrorType = new ErrorType(this);

            StringType = DesktopRuntime.StringMethodTable != 0 ? GetTypeByMethodTable(DesktopRuntime.StringMethodTable, 0, 0) : ErrorType;
            ObjectType = DesktopRuntime.ObjectMethodTable != 0 ? GetTypeByMethodTable(DesktopRuntime.ObjectMethodTable, 0, 0) : ErrorType;
            if (DesktopRuntime.FreeMethodTable != 0)
            {
                ClrType free = GetTypeByMethodTable(DesktopRuntime.FreeMethodTable, 0, 0);

                ((DesktopHeapType)free).Shared = true;
                ((BaseDesktopHeapType)free).DesktopModule = ObjectType.Module as DesktopModule;
                Free = free;
            }
            else
            {
                Free = ErrorType;
            }

            InitSegments(runtime);
        }

        private ClrType CreateFree()
        {
            ClrType free = GetTypeByMethodTable(DesktopRuntime.FreeMethodTable, 0, 0);

            ((DesktopHeapType)free).Shared = true;
            ((BaseDesktopHeapType)free).DesktopModule = (DesktopModule)ObjectType.Module;
            return free;
        }

        private ClrType CreateArrayType()
        {
            ClrType type = GetTypeByMethodTable(DesktopRuntime.ArrayMethodTable, DesktopRuntime.ObjectMethodTable, 0);
            type.ComponentType = ObjectType;
            return type;
        }
<commit_msg>Additional null checks in DesktopGCHeap (#250)<commit_after>﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using Microsoft.Diagnostics.Runtime.DacInterface;

namespace Microsoft.Diagnostics.Runtime.Desktop
{
    internal abstract class DesktopGCHeap : HeapBase
    {
        public DesktopGCHeap(DesktopRuntimeBase runtime)
            : base(runtime)
        {
            DesktopRuntime = runtime;
            _types = new List<ClrType>(1000);
            Revision = runtime.Revision;

            // Prepopulate a few important method tables.
            ErrorType = new ErrorType(this);
            _arrayType = new Lazy<ClrType>(CreateArrayType);
            _exceptionType = new Lazy<ClrType>(() => GetTypeByMethodTable(DesktopRuntime.ExceptionMethodTable, 0, 0) ?? ErrorType);

            StringType = GetTypeByMethodTable(DesktopRuntime.StringMethodTable, 0, 0) ?? ErrorType;
            ObjectType = GetTypeByMethodTable(DesktopRuntime.ObjectMethodTable, 0, 0) ?? ErrorType;
            Free = CreateFree();

            InitSegments(runtime);
        }

        private ClrType CreateFree()
        {
            ClrType free = GetTypeByMethodTable(DesktopRuntime.FreeMethodTable, 0, 0);
            if (free == null)
                return ErrorType;

            ((DesktopHeapType)free).Shared = true;
            ((BaseDesktopHeapType)free).DesktopModule = (DesktopModule)ObjectType.Module;
            return free;
        }

        private ClrType CreateArrayType()
        {
            ClrType type = GetTypeByMethodTable(DesktopRuntime.ArrayMethodTable, DesktopRuntime.ObjectMethodTable, 0);
            if (type == null)
                return ErrorType;

            type.ComponentType = ObjectType;
            return type;
        }

{'fraction_non_alphanumeric': 0.081275258194881, 'fraction_numerical': 0.007633587786259542, 'mean_word_length': 2.4886452623335944, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6815188', 'n_tokens_mistral': 1197, 'n_tokens_neox': 1109, 'n_words': 287}