<commit_before>﻿using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Threading;

namespace Pellared.Owned
{
    public class Disposable : IDisposable
    {
        const int DisposedFlag = 1;
        int _isDisposed;

        private readonly Finalizer finalizer;

        public Disposable(bool withFinalizer = false)
        {
#if !DEBUG
            if (withFinalizer)
#endif
            {
                finalizer = new Finalizer(OnFinalize);
            }
        }

        private void OnFinalize()
        {
            DisposeUnmanaged();
            Debug.Fail(GetType() + " in not disposed");
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        [SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly", Justification = "Dispose is implemented correctly")]
        public void Dispose()
        {
            int wasDisposed = Interlocked.Exchange(ref _isDisposed, DisposedFlag);
            EnsureNotDisposed(wasDisposed);

            DisposeManaged();
            DisposeUnmanaged();

            if (finalizer != null)
            {
                finalizer.SuppressFinalize();
            }
        }

        protected virtual void DisposeManaged()
        {
        }

        protected virtual void DisposeUnmanaged()
        {
        }

        public bool IsDisposed
        {
            get
            {
                Thread.MemoryBarrier();
                return _isDisposed == DisposedFlag;
            }
        }

        private void EnsureNotDisposed(int wasDisposed)
        {
            if (wasDisposed == DisposedFlag)
            {
                string typeName = GetType().FullName;
                string stackTrace = new StackTrace().ToString();
                throw new ObjectDisposedException(typeName, stackTrace);<commit_msg>Disposable refinements<commit_after>﻿using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Threading;

namespace Pellared.Owned
{
    public class Disposable : IDisposable
    {
        private const int DisposedFlag = 1;
        
        private readonly Finalizer finalizer;
        private readonly StackTrace creationStackTrace;

        private int _isDisposed;

        public Disposable(bool withFinalizer = false)
        {
#if DEBUG
            creationStackTrace = new StackTrace();
#else
            if (withFinalizer)
#endif
            {
                finalizer = new Finalizer(OnFinalize);
            }
        }

        private void OnFinalize()
        {
            DisposeUnmanaged();
#if DEBUG
            Debug.Fail(GetType() + " in not disposed" + Environment.NewLine + creationStackTrace);
#endif
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        [SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly", Justification = "Dispose is implemented correctly")]
        public void Dispose()
        {
            int wasDisposed = Interlocked.Exchange(ref _isDisposed, DisposedFlag);
            if (wasDisposed != DisposedFlag)
            {
                DisposeResources();
            }
        }

        protected virtual void DisposeManaged()
        {
        }

        protected virtual void DisposeUnmanaged()
        {
        }

        protected void RequireNotDisposed()
        {
            if (IsDisposed)
            {
                string typeName = GetType().FullName;
                throw new ObjectDisposedException(typeName);
            }
        }

        public bool IsDisposed
        {
            get
            {
                Thread.MemoryBarrier();
                return _isDisposed == DisposedFlag;
            }
        }

        private void DisposeResources()
        {
            DisposeManaged();
            DisposeUnmanaged();

            if (finalizer != null)
            {
                finalizer.SuppressFinalize();
{'fraction_non_alphanumeric': 0.07812876778394019, 'fraction_numerical': 0.0024113817217265494, 'mean_word_length': 1.6778566817301486, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4866889', 'n_tokens_mistral': 1040, 'n_tokens_neox': 997, 'n_words': 240}