From 70131eba869b4b9222a898b0a38b106f7962d19d Mon Sep 17 00:00:00 2001 From: hollow87 Date: Tue, 17 Jul 2018 12:25:53 -0500 Subject: [PATCH] Fixed (hopefully) a concurency bug in regards to aob scan --- Memory/Class1.cs | 54 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/Memory/Class1.cs b/Memory/Class1.cs index 775cc64..0b26859 100644 --- a/Memory/Class1.cs +++ b/Memory/Class1.cs @@ -1784,32 +1784,50 @@ public async Task> AoBScan(long start, long end, string search memRegionList.Add(memRegion); } - ConcurrentBag bagResult = new ConcurrentBag(); - - // Allocate memory for the region buffer now - byte[] dumpBufferBytes = new byte[maxBufferLength]; + ConcurrentBag results = new ConcurrentBag(); + ConcurrentQueue dataBuffers = new ConcurrentQueue(); + for (var i = 0; i < Environment.ProcessorCount; i++) + { + dataBuffers.Enqueue(new byte[maxBufferLength]); + } + byte[] aobPattern = new byte[stringByteArray.Length]; - Parallel.ForEach(memRegionList, - (item, parallelLoopState, index) => - { - byte[] aobPattern = new byte[stringByteArray.Length]; + for (int i = 0; i < stringByteArray.Length; i++) + aobPattern[i] = (byte)(Convert.ToByte(stringByteArray[i], 16) & mask[i]); - for (int i = 0; i < stringByteArray.Length; i++) - aobPattern[i] = (byte)(Convert.ToByte(stringByteArray[i], 16) & mask[i]); + if (mask.Length != aobPattern.Length) + throw new ArgumentException($"{nameof(aobPattern)}.Length != {nameof(mask)}.Length"); - if (mask.Length != aobPattern.Length) - throw new ArgumentException($"{nameof(aobPattern)}.Length != {nameof(mask)}.Length"); + Parallel.For(0, memRegionList.Count, + new ParallelOptions {MaxDegreeOfParallelism = Environment.ProcessorCount}, + (index, state) => + { + for(var i = 0; i < 5 || !dataBuffers.IsEmpty; i++) + { + Task.Delay(100); + } - long[] compareResults = CompareScan(item, aobPattern, mask, dumpBufferBytes); + if (!dataBuffers.TryDequeue(out var buffer)) + buffer = null; - foreach(long result in compareResults) - bagResult.Add(result); - }); + long[] compareResults = CompareScan(memRegionList[index], aobPattern, mask, buffer); + foreach (var item in compareResults) + { + results.Add(item); + } + + + if (buffer != null) + dataBuffers.Enqueue(buffer); + }); - dumpBufferBytes = null; + // We empty the data bufers to try and help the GC + while (dataBuffers.TryDequeue(out var buffer)) + { + } - return bagResult.ToList().OrderBy(c => c); + return results.OrderBy(c => c); } ///