From 5f5abaa24735726ff2db367dc74f98055d4f4cba Mon Sep 17 00:00:00 2001 From: FICTURE7 <ficture7@gmail.com> Date: Sat, 17 Apr 2021 15:49:57 +0400 Subject: [PATCH] POH approach, but the sequel --- ARMeilleure/Common/EntryTable.cs | 47 ++++----------------------- ARMeilleure/Translation/Translator.cs | 2 -- 2 files changed, 6 insertions(+), 43 deletions(-) diff --git a/ARMeilleure/Common/EntryTable.cs b/ARMeilleure/Common/EntryTable.cs index bb171dc67c..5f5a0e8473 100644 --- a/ARMeilleure/Common/EntryTable.cs +++ b/ARMeilleure/Common/EntryTable.cs @@ -10,13 +10,13 @@ namespace ARMeilleure.Common /// address through out the table's lifetime. /// </summary> /// <typeparam name="TEntry">Type of the entry in the table</typeparam> - class EntryTable<TEntry> : IDisposable where TEntry : unmanaged + class EntryTable<TEntry> where TEntry : unmanaged { private bool _disposed; private int _freeHint; private readonly int _pageCapacity; // Number of entries per page. private readonly int _pageLogCapacity; - private readonly Dictionary<int, IntPtr> _pages; + private readonly Dictionary<int, TEntry[]> _pages; private readonly BitMap _allocated; /// <summary> @@ -41,7 +41,7 @@ namespace ARMeilleure.Common } _allocated = new BitMap(); - _pages = new Dictionary<int, IntPtr>(); + _pages = new Dictionary<int, TEntry[]>(); _pageLogCapacity = BitOperations.Log2((uint)(pageSize / sizeof(TEntry))); _pageCapacity = 1 << _pageLogCapacity; } @@ -149,49 +149,14 @@ namespace ARMeilleure.Common { var pageIndex = (int)((uint)(index & ~(_pageCapacity - 1)) >> _pageLogCapacity); - if (!_pages.TryGetValue(pageIndex, out IntPtr page)) + if (!_pages.TryGetValue(pageIndex, out TEntry[] page)) { - page = Marshal.AllocHGlobal(sizeof(TEntry) * _pageCapacity); + page = GC.AllocateUninitializedArray<TEntry>(_pageCapacity, pinned: true); _pages.Add(pageIndex, page); } - return new Span<TEntry>((void*)page, _pageCapacity); - } - - /// <summary> - /// Releases all resources used by the <see cref="EntryTable{TEntry}"/> instance. - /// </summary> - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - /// <summary> - /// Releases all unmanaged and optionally managed resources used by the <see cref="EntryTable{TEntry}{T}"/> - /// instance. - /// </summary> - /// <param name="disposing"><see langword="true"/> to dispose managed resources also; otherwise just unmanaged resouces</param> - protected virtual void Dispose(bool disposing) - { - if (!_disposed) - { - foreach (var page in _pages.Values) - { - Marshal.FreeHGlobal(page); - } - - _disposed = true; - } - } - - /// <summary> - /// Frees resources used by the <see cref="EntryTable{TEntry}"/> instance. - /// </summary> - ~EntryTable() - { - Dispose(false); + return page; } } } diff --git a/ARMeilleure/Translation/Translator.cs b/ARMeilleure/Translation/Translator.cs index 9f88f17a9b..db800918d4 100644 --- a/ARMeilleure/Translation/Translator.cs +++ b/ARMeilleure/Translation/Translator.cs @@ -174,8 +174,6 @@ namespace ARMeilleure.Translation _jumpTable.Dispose(); _jumpTable = null; - CountTable.Dispose(); - GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce; } }