2019-11-25 01:29:37 +01:00
|
|
|
using System;
|
2019-12-31 23:09:49 +01:00
|
|
|
using System.Collections;
|
2019-10-13 08:02:07 +02:00
|
|
|
using System.Collections.Generic;
|
2021-09-19 14:22:26 +02:00
|
|
|
using System.Runtime.CompilerServices;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
Memory Read/Write Tracking using Region Handles (#1272)
* WIP Range Tracking
- Texture invalidation seems to have large problems
- Buffer/Pool invalidation may have problems
- Mirror memory tracking puts an additional `add` in compiled code, we likely just want to make HLE access slower if this is the final solution.
- Native project is in the messiest possible location.
- [HACK] JIT memory access always uses native "fast" path
- [HACK] Trying some things with texture invalidation and views.
It works :)
Still a few hacks, messy things, slow things
More work in progress stuff (also move to memory project)
Quite a bit faster now.
- Unmapping GPU VA and CPU VA will now correctly update write tracking regions, and invalidate textures for the former.
- The Virtual range list is now non-overlapping like the physical one.
- Fixed some bugs where regions could leak.
- Introduced a weird bug that I still need to track down (consistent invalid buffer in MK8 ribbon road)
Move some stuff.
I think we'll eventually just put the dll and so for this in a nuget package.
Fix rebase.
[WIP] MultiRegionHandle variable size ranges
- Avoid reprotecting regions that change often (needs some tweaking)
- There's still a bug in buffers, somehow.
- Might want different api for minimum granularity
Fix rebase issue
Commit everything needed for software only tracking.
Remove native components.
Remove more native stuff.
Cleanup
Use a separate window for the background context, update opentk. (fixes linux)
Some experimental changes
Should get things working up to scratch - still need to try some things with flush/modification and res scale.
Include address with the region action.
Initial work to make range tracking work
Still a ton of bugs
Fix some issues with the new stuff.
* Fix texture flush instability
There's still some weird behaviour, but it's much improved without this. (textures with cpu modified data were flushing over it)
* Find the destination texture for Buffer->Texture full copy
Greatly improves performance for nvdec videos (with range tracking)
* Further improve texture tracking
* Disable Memory Tracking for view parents
This is a temporary approach to better match behaviour on master (where invalidations would be soaked up by views, rather than trigger twice)
The assumption is that when views are created to a texture, they will cover all of its data anyways. Of course, this can easily be improved in future.
* Introduce some tracking tests.
WIP
* Complete base tests.
* Add more tests for multiregion, fix existing test.
* Cleanup Part 1
* Remove unnecessary code from memory tracking
* Fix some inconsistencies with 3D texture rule.
* Add dispose tests.
* Use a background thread for the background context.
Rather than setting and unsetting a context as current, doing the work on a dedicated thread with signals seems to be a bit faster.
Also nerf the multithreading test a bit.
* Copy to texture with matching alignment
This extends the copy to work for some videos with unusual size, such as tutorial videos in SMO. It will only occur if the destination texture already exists at XCount size.
* Track reads for buffer copies. Synchronize new buffers before copying overlaps.
* Remove old texture flushing mechanisms.
Range tracking all the way, baby.
* Wake the background thread when disposing.
Avoids a deadlock when games are closed.
* Address Feedback 1
* Separate TextureCopy instance for background thread
Also `BackgroundContextWorker.InBackground` for a more sensible idenfifier for if we're in a background thread.
* Add missing XML docs.
* Address Feedback
* Maybe I should start drinking coffee.
* Some more feedback.
* Remove flush warning, Refocus window after making background context
2020-10-16 22:18:35 +02:00
|
|
|
namespace Ryujinx.Memory.Range
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
Memory Read/Write Tracking using Region Handles (#1272)
* WIP Range Tracking
- Texture invalidation seems to have large problems
- Buffer/Pool invalidation may have problems
- Mirror memory tracking puts an additional `add` in compiled code, we likely just want to make HLE access slower if this is the final solution.
- Native project is in the messiest possible location.
- [HACK] JIT memory access always uses native "fast" path
- [HACK] Trying some things with texture invalidation and views.
It works :)
Still a few hacks, messy things, slow things
More work in progress stuff (also move to memory project)
Quite a bit faster now.
- Unmapping GPU VA and CPU VA will now correctly update write tracking regions, and invalidate textures for the former.
- The Virtual range list is now non-overlapping like the physical one.
- Fixed some bugs where regions could leak.
- Introduced a weird bug that I still need to track down (consistent invalid buffer in MK8 ribbon road)
Move some stuff.
I think we'll eventually just put the dll and so for this in a nuget package.
Fix rebase.
[WIP] MultiRegionHandle variable size ranges
- Avoid reprotecting regions that change often (needs some tweaking)
- There's still a bug in buffers, somehow.
- Might want different api for minimum granularity
Fix rebase issue
Commit everything needed for software only tracking.
Remove native components.
Remove more native stuff.
Cleanup
Use a separate window for the background context, update opentk. (fixes linux)
Some experimental changes
Should get things working up to scratch - still need to try some things with flush/modification and res scale.
Include address with the region action.
Initial work to make range tracking work
Still a ton of bugs
Fix some issues with the new stuff.
* Fix texture flush instability
There's still some weird behaviour, but it's much improved without this. (textures with cpu modified data were flushing over it)
* Find the destination texture for Buffer->Texture full copy
Greatly improves performance for nvdec videos (with range tracking)
* Further improve texture tracking
* Disable Memory Tracking for view parents
This is a temporary approach to better match behaviour on master (where invalidations would be soaked up by views, rather than trigger twice)
The assumption is that when views are created to a texture, they will cover all of its data anyways. Of course, this can easily be improved in future.
* Introduce some tracking tests.
WIP
* Complete base tests.
* Add more tests for multiregion, fix existing test.
* Cleanup Part 1
* Remove unnecessary code from memory tracking
* Fix some inconsistencies with 3D texture rule.
* Add dispose tests.
* Use a background thread for the background context.
Rather than setting and unsetting a context as current, doing the work on a dedicated thread with signals seems to be a bit faster.
Also nerf the multithreading test a bit.
* Copy to texture with matching alignment
This extends the copy to work for some videos with unusual size, such as tutorial videos in SMO. It will only occur if the destination texture already exists at XCount size.
* Track reads for buffer copies. Synchronize new buffers before copying overlaps.
* Remove old texture flushing mechanisms.
Range tracking all the way, baby.
* Wake the background thread when disposing.
Avoids a deadlock when games are closed.
* Address Feedback 1
* Separate TextureCopy instance for background thread
Also `BackgroundContextWorker.InBackground` for a more sensible idenfifier for if we're in a background thread.
* Add missing XML docs.
* Address Feedback
* Maybe I should start drinking coffee.
* Some more feedback.
* Remove flush warning, Refocus window after making background context
2020-10-16 22:18:35 +02:00
|
|
|
/// Sorted list of ranges that supports binary search.
|
2019-12-31 04:22:58 +01:00
|
|
|
/// </summary>
|
Memory Read/Write Tracking using Region Handles (#1272)
* WIP Range Tracking
- Texture invalidation seems to have large problems
- Buffer/Pool invalidation may have problems
- Mirror memory tracking puts an additional `add` in compiled code, we likely just want to make HLE access slower if this is the final solution.
- Native project is in the messiest possible location.
- [HACK] JIT memory access always uses native "fast" path
- [HACK] Trying some things with texture invalidation and views.
It works :)
Still a few hacks, messy things, slow things
More work in progress stuff (also move to memory project)
Quite a bit faster now.
- Unmapping GPU VA and CPU VA will now correctly update write tracking regions, and invalidate textures for the former.
- The Virtual range list is now non-overlapping like the physical one.
- Fixed some bugs where regions could leak.
- Introduced a weird bug that I still need to track down (consistent invalid buffer in MK8 ribbon road)
Move some stuff.
I think we'll eventually just put the dll and so for this in a nuget package.
Fix rebase.
[WIP] MultiRegionHandle variable size ranges
- Avoid reprotecting regions that change often (needs some tweaking)
- There's still a bug in buffers, somehow.
- Might want different api for minimum granularity
Fix rebase issue
Commit everything needed for software only tracking.
Remove native components.
Remove more native stuff.
Cleanup
Use a separate window for the background context, update opentk. (fixes linux)
Some experimental changes
Should get things working up to scratch - still need to try some things with flush/modification and res scale.
Include address with the region action.
Initial work to make range tracking work
Still a ton of bugs
Fix some issues with the new stuff.
* Fix texture flush instability
There's still some weird behaviour, but it's much improved without this. (textures with cpu modified data were flushing over it)
* Find the destination texture for Buffer->Texture full copy
Greatly improves performance for nvdec videos (with range tracking)
* Further improve texture tracking
* Disable Memory Tracking for view parents
This is a temporary approach to better match behaviour on master (where invalidations would be soaked up by views, rather than trigger twice)
The assumption is that when views are created to a texture, they will cover all of its data anyways. Of course, this can easily be improved in future.
* Introduce some tracking tests.
WIP
* Complete base tests.
* Add more tests for multiregion, fix existing test.
* Cleanup Part 1
* Remove unnecessary code from memory tracking
* Fix some inconsistencies with 3D texture rule.
* Add dispose tests.
* Use a background thread for the background context.
Rather than setting and unsetting a context as current, doing the work on a dedicated thread with signals seems to be a bit faster.
Also nerf the multithreading test a bit.
* Copy to texture with matching alignment
This extends the copy to work for some videos with unusual size, such as tutorial videos in SMO. It will only occur if the destination texture already exists at XCount size.
* Track reads for buffer copies. Synchronize new buffers before copying overlaps.
* Remove old texture flushing mechanisms.
Range tracking all the way, baby.
* Wake the background thread when disposing.
Avoids a deadlock when games are closed.
* Address Feedback 1
* Separate TextureCopy instance for background thread
Also `BackgroundContextWorker.InBackground` for a more sensible idenfifier for if we're in a background thread.
* Add missing XML docs.
* Address Feedback
* Maybe I should start drinking coffee.
* Some more feedback.
* Remove flush warning, Refocus window after making background context
2020-10-16 22:18:35 +02:00
|
|
|
/// <typeparam name="T">Type of the range.</typeparam>
|
|
|
|
public class RangeList<T> : IEnumerable<T> where T : IRange
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2021-09-19 14:22:26 +02:00
|
|
|
private readonly struct RangeItem<TValue> where TValue : IRange
|
|
|
|
{
|
|
|
|
public readonly ulong Address;
|
|
|
|
public readonly ulong EndAddress;
|
|
|
|
|
|
|
|
public readonly TValue Value;
|
|
|
|
|
|
|
|
public RangeItem(TValue value)
|
|
|
|
{
|
|
|
|
Value = value;
|
|
|
|
|
|
|
|
Address = value.Address;
|
|
|
|
EndAddress = value.Address + value.Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public bool OverlapsWith(ulong address, ulong endAddress)
|
|
|
|
{
|
|
|
|
return Address < endAddress && address < EndAddress;
|
|
|
|
}
|
|
|
|
}
|
2019-11-25 01:29:37 +01:00
|
|
|
|
2021-10-04 20:38:59 +02:00
|
|
|
private const int BackingInitialSize = 1024;
|
2021-09-19 14:22:26 +02:00
|
|
|
private const int ArrayGrowthSize = 32;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2021-09-19 14:22:26 +02:00
|
|
|
private RangeItem<T>[] _items;
|
2021-10-04 20:38:59 +02:00
|
|
|
private readonly int _backingGrowthSize;
|
|
|
|
|
2021-09-19 14:22:26 +02:00
|
|
|
public int Count { get; protected set; }
|
Memory Read/Write Tracking using Region Handles (#1272)
* WIP Range Tracking
- Texture invalidation seems to have large problems
- Buffer/Pool invalidation may have problems
- Mirror memory tracking puts an additional `add` in compiled code, we likely just want to make HLE access slower if this is the final solution.
- Native project is in the messiest possible location.
- [HACK] JIT memory access always uses native "fast" path
- [HACK] Trying some things with texture invalidation and views.
It works :)
Still a few hacks, messy things, slow things
More work in progress stuff (also move to memory project)
Quite a bit faster now.
- Unmapping GPU VA and CPU VA will now correctly update write tracking regions, and invalidate textures for the former.
- The Virtual range list is now non-overlapping like the physical one.
- Fixed some bugs where regions could leak.
- Introduced a weird bug that I still need to track down (consistent invalid buffer in MK8 ribbon road)
Move some stuff.
I think we'll eventually just put the dll and so for this in a nuget package.
Fix rebase.
[WIP] MultiRegionHandle variable size ranges
- Avoid reprotecting regions that change often (needs some tweaking)
- There's still a bug in buffers, somehow.
- Might want different api for minimum granularity
Fix rebase issue
Commit everything needed for software only tracking.
Remove native components.
Remove more native stuff.
Cleanup
Use a separate window for the background context, update opentk. (fixes linux)
Some experimental changes
Should get things working up to scratch - still need to try some things with flush/modification and res scale.
Include address with the region action.
Initial work to make range tracking work
Still a ton of bugs
Fix some issues with the new stuff.
* Fix texture flush instability
There's still some weird behaviour, but it's much improved without this. (textures with cpu modified data were flushing over it)
* Find the destination texture for Buffer->Texture full copy
Greatly improves performance for nvdec videos (with range tracking)
* Further improve texture tracking
* Disable Memory Tracking for view parents
This is a temporary approach to better match behaviour on master (where invalidations would be soaked up by views, rather than trigger twice)
The assumption is that when views are created to a texture, they will cover all of its data anyways. Of course, this can easily be improved in future.
* Introduce some tracking tests.
WIP
* Complete base tests.
* Add more tests for multiregion, fix existing test.
* Cleanup Part 1
* Remove unnecessary code from memory tracking
* Fix some inconsistencies with 3D texture rule.
* Add dispose tests.
* Use a background thread for the background context.
Rather than setting and unsetting a context as current, doing the work on a dedicated thread with signals seems to be a bit faster.
Also nerf the multithreading test a bit.
* Copy to texture with matching alignment
This extends the copy to work for some videos with unusual size, such as tutorial videos in SMO. It will only occur if the destination texture already exists at XCount size.
* Track reads for buffer copies. Synchronize new buffers before copying overlaps.
* Remove old texture flushing mechanisms.
Range tracking all the way, baby.
* Wake the background thread when disposing.
Avoids a deadlock when games are closed.
* Address Feedback 1
* Separate TextureCopy instance for background thread
Also `BackgroundContextWorker.InBackground` for a more sensible idenfifier for if we're in a background thread.
* Add missing XML docs.
* Address Feedback
* Maybe I should start drinking coffee.
* Some more feedback.
* Remove flush warning, Refocus window after making background context
2020-10-16 22:18:35 +02:00
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
Memory Read/Write Tracking using Region Handles (#1272)
* WIP Range Tracking
- Texture invalidation seems to have large problems
- Buffer/Pool invalidation may have problems
- Mirror memory tracking puts an additional `add` in compiled code, we likely just want to make HLE access slower if this is the final solution.
- Native project is in the messiest possible location.
- [HACK] JIT memory access always uses native "fast" path
- [HACK] Trying some things with texture invalidation and views.
It works :)
Still a few hacks, messy things, slow things
More work in progress stuff (also move to memory project)
Quite a bit faster now.
- Unmapping GPU VA and CPU VA will now correctly update write tracking regions, and invalidate textures for the former.
- The Virtual range list is now non-overlapping like the physical one.
- Fixed some bugs where regions could leak.
- Introduced a weird bug that I still need to track down (consistent invalid buffer in MK8 ribbon road)
Move some stuff.
I think we'll eventually just put the dll and so for this in a nuget package.
Fix rebase.
[WIP] MultiRegionHandle variable size ranges
- Avoid reprotecting regions that change often (needs some tweaking)
- There's still a bug in buffers, somehow.
- Might want different api for minimum granularity
Fix rebase issue
Commit everything needed for software only tracking.
Remove native components.
Remove more native stuff.
Cleanup
Use a separate window for the background context, update opentk. (fixes linux)
Some experimental changes
Should get things working up to scratch - still need to try some things with flush/modification and res scale.
Include address with the region action.
Initial work to make range tracking work
Still a ton of bugs
Fix some issues with the new stuff.
* Fix texture flush instability
There's still some weird behaviour, but it's much improved without this. (textures with cpu modified data were flushing over it)
* Find the destination texture for Buffer->Texture full copy
Greatly improves performance for nvdec videos (with range tracking)
* Further improve texture tracking
* Disable Memory Tracking for view parents
This is a temporary approach to better match behaviour on master (where invalidations would be soaked up by views, rather than trigger twice)
The assumption is that when views are created to a texture, they will cover all of its data anyways. Of course, this can easily be improved in future.
* Introduce some tracking tests.
WIP
* Complete base tests.
* Add more tests for multiregion, fix existing test.
* Cleanup Part 1
* Remove unnecessary code from memory tracking
* Fix some inconsistencies with 3D texture rule.
* Add dispose tests.
* Use a background thread for the background context.
Rather than setting and unsetting a context as current, doing the work on a dedicated thread with signals seems to be a bit faster.
Also nerf the multithreading test a bit.
* Copy to texture with matching alignment
This extends the copy to work for some videos with unusual size, such as tutorial videos in SMO. It will only occur if the destination texture already exists at XCount size.
* Track reads for buffer copies. Synchronize new buffers before copying overlaps.
* Remove old texture flushing mechanisms.
Range tracking all the way, baby.
* Wake the background thread when disposing.
Avoids a deadlock when games are closed.
* Address Feedback 1
* Separate TextureCopy instance for background thread
Also `BackgroundContextWorker.InBackground` for a more sensible idenfifier for if we're in a background thread.
* Add missing XML docs.
* Address Feedback
* Maybe I should start drinking coffee.
* Some more feedback.
* Remove flush warning, Refocus window after making background context
2020-10-16 22:18:35 +02:00
|
|
|
/// Creates a new range list.
|
2019-12-31 04:22:58 +01:00
|
|
|
/// </summary>
|
2021-10-04 20:38:59 +02:00
|
|
|
/// <param name="backingInitialSize">The initial size of the backing array</param>
|
|
|
|
public RangeList(int backingInitialSize = BackingInitialSize)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2021-10-04 20:38:59 +02:00
|
|
|
_backingGrowthSize = backingInitialSize;
|
|
|
|
_items = new RangeItem<T>[backingInitialSize];
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Adds a new item to the list.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The item to be added</param>
|
2019-10-13 08:02:07 +02:00
|
|
|
public void Add(T item)
|
|
|
|
{
|
2019-11-08 20:39:12 +01:00
|
|
|
int index = BinarySearch(item.Address);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-11-08 20:39:12 +01:00
|
|
|
if (index < 0)
|
|
|
|
{
|
|
|
|
index = ~index;
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
2019-11-08 20:39:12 +01:00
|
|
|
|
2021-09-19 14:22:26 +02:00
|
|
|
Insert(index, new RangeItem<T>(item));
|
|
|
|
}
|
|
|
|
|
Memory: Faster Split for NonOverlappingRangeList (#4451)
I noticed that in Xenoblade 2, the game can end up spending a lot of time adding and removing tracking handles. One of the main causes of this is actually splitting existing handles, which does the following:
- Remove existing handle from list
- Update existing handle to end at split address, create new handle starting at split address
- Add updated handle (left) to list
- Add new handle (right) to list
This costs 1 deletion and 2 insertions. When there are more handles, this gets a lot more expensive, as insertions are done by copying all values to the right, and deletions by copying values to the left.
This PR simply allows it to look up the handle being split, and replace its entry with the new end address without insertion or deletion. This makes a split only cost one insertion and a binary search lookup (very cheap). This isn't all of the cost on Xenoblade 2, but it does significantly reduce it.
There might be something else to this - we could find a way to reduce the handle count for the game (merging on deletion? buffer deletion?), we could use a different structure for virtual regions, as the current one is optimal for buffer lookups which nearly always read, memory tracking has more of a balance between read/write. That's for a later date though, this was an easy improvment.
2023-02-21 10:53:38 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Updates an item's end address on the list. Address must be the same.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The item to be updated</param>
|
|
|
|
/// <returns>True if the item was located and updated, false otherwise</returns>
|
|
|
|
public bool Update(T item)
|
|
|
|
{
|
|
|
|
int index = BinarySearch(item.Address);
|
|
|
|
|
|
|
|
if (index >= 0)
|
|
|
|
{
|
|
|
|
while (index > 0 && _items[index - 1].Address == item.Address)
|
|
|
|
{
|
|
|
|
index--;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (index < Count)
|
|
|
|
{
|
|
|
|
if (_items[index].Value.Equals(item))
|
|
|
|
{
|
|
|
|
_items[index] = new RangeItem<T>(item);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_items[index].Address > item.Address)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-19 14:22:26 +02:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
private void Insert(int index, RangeItem<T> item)
|
|
|
|
{
|
|
|
|
if (Count + 1 > _items.Length)
|
|
|
|
{
|
2021-10-04 20:38:59 +02:00
|
|
|
Array.Resize(ref _items, _items.Length + _backingGrowthSize);
|
2021-09-19 14:22:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (index >= Count)
|
|
|
|
{
|
|
|
|
if (index == Count)
|
|
|
|
{
|
|
|
|
_items[Count++] = item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Array.Copy(_items, index, _items, index + 1, Count - index);
|
|
|
|
|
|
|
|
_items[index] = item;
|
|
|
|
Count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
private void RemoveAt(int index)
|
|
|
|
{
|
|
|
|
if (index < --Count)
|
|
|
|
{
|
|
|
|
Array.Copy(_items, index + 1, _items, index, Count - index);
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
2020-01-02 00:14:18 +01:00
|
|
|
/// Removes an item from the list.
|
2019-12-31 04:22:58 +01:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The item to be removed</param>
|
|
|
|
/// <returns>True if the item was removed, or false if it was not found</returns>
|
2019-10-13 08:02:07 +02:00
|
|
|
public bool Remove(T item)
|
|
|
|
{
|
2019-11-08 20:39:12 +01:00
|
|
|
int index = BinarySearch(item.Address);
|
|
|
|
|
|
|
|
if (index >= 0)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2021-09-19 14:22:26 +02:00
|
|
|
while (index > 0 && _items[index - 1].Address == item.Address)
|
2019-11-08 20:39:12 +01:00
|
|
|
{
|
|
|
|
index--;
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2021-09-19 14:22:26 +02:00
|
|
|
while (index < Count)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2021-09-19 14:22:26 +02:00
|
|
|
if (_items[index].Value.Equals(item))
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2021-09-19 14:22:26 +02:00
|
|
|
RemoveAt(index);
|
2019-11-08 20:39:12 +01:00
|
|
|
|
|
|
|
return true;
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2021-09-19 14:22:26 +02:00
|
|
|
if (_items[index].Address > item.Address)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-08 20:39:12 +01:00
|
|
|
break;
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-11-08 20:39:12 +01:00
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-11-08 20:39:12 +01:00
|
|
|
return false;
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2021-09-19 14:22:26 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Updates an item's end address.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The item to be updated</param>
|
|
|
|
public void UpdateEndAddress(T item)
|
|
|
|
{
|
|
|
|
int index = BinarySearch(item.Address);
|
|
|
|
|
|
|
|
if (index >= 0)
|
|
|
|
{
|
|
|
|
while (index > 0 && _items[index - 1].Address == item.Address)
|
|
|
|
{
|
|
|
|
index--;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (index < Count)
|
|
|
|
{
|
|
|
|
if (_items[index].Value.Equals(item))
|
|
|
|
{
|
|
|
|
_items[index] = new RangeItem<T>(item);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_items[index].Address > item.Address)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the first item on the list overlapping in memory with the specified item.
|
2020-01-01 16:39:09 +01:00
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
2019-12-31 04:22:58 +01:00
|
|
|
/// Despite the name, this has no ordering guarantees of the returned item.
|
|
|
|
/// It only ensures that the item returned overlaps the specified item.
|
2020-01-01 16:39:09 +01:00
|
|
|
/// </remarks>
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <param name="item">Item to check for overlaps</param>
|
|
|
|
/// <returns>The overlapping item, or the default value for the type if none found</returns>
|
2019-10-13 08:02:07 +02:00
|
|
|
public T FindFirstOverlap(T item)
|
|
|
|
{
|
|
|
|
return FindFirstOverlap(item.Address, item.Size);
|
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the first item on the list overlapping the specified memory range.
|
2020-01-01 16:39:09 +01:00
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
2019-12-31 04:22:58 +01:00
|
|
|
/// Despite the name, this has no ordering guarantees of the returned item.
|
|
|
|
/// It only ensures that the item returned overlaps the specified memory range.
|
2020-01-01 16:39:09 +01:00
|
|
|
/// </remarks>
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <param name="address">Start address of the range</param>
|
2020-01-02 00:14:18 +01:00
|
|
|
/// <param name="size">Size in bytes of the range</param>
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <returns>The overlapping item, or the default value for the type if none found</returns>
|
2019-10-13 08:02:07 +02:00
|
|
|
public T FindFirstOverlap(ulong address, ulong size)
|
|
|
|
{
|
2021-09-19 14:22:26 +02:00
|
|
|
int index = BinarySearch(address, address + size);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-11-08 20:39:12 +01:00
|
|
|
if (index < 0)
|
|
|
|
{
|
|
|
|
return default(T);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
2019-11-08 20:39:12 +01:00
|
|
|
|
2021-09-19 14:22:26 +02:00
|
|
|
return _items[index].Value;
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Gets all items overlapping with the specified item in memory.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">Item to check for overlaps</param>
|
|
|
|
/// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
|
|
|
|
/// <returns>The number of overlapping items found</returns>
|
2019-11-25 01:29:37 +01:00
|
|
|
public int FindOverlaps(T item, ref T[] output)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-25 01:29:37 +01:00
|
|
|
return FindOverlaps(item.Address, item.Size, ref output);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Gets all items on the list overlapping the specified memory range.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="address">Start address of the range</param>
|
2020-01-02 00:14:18 +01:00
|
|
|
/// <param name="size">Size in bytes of the range</param>
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
|
|
|
|
/// <returns>The number of overlapping items found</returns>
|
2019-11-25 01:29:37 +01:00
|
|
|
public int FindOverlaps(ulong address, ulong size, ref T[] output)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-25 01:29:37 +01:00
|
|
|
int outputIndex = 0;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
ulong endAddress = address + size;
|
|
|
|
|
2021-09-19 14:22:26 +02:00
|
|
|
for (int i = 0; i < Count; i++)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2021-09-19 14:22:26 +02:00
|
|
|
ref RangeItem<T> item = ref _items[i];
|
|
|
|
|
Memory Read/Write Tracking using Region Handles (#1272)
* WIP Range Tracking
- Texture invalidation seems to have large problems
- Buffer/Pool invalidation may have problems
- Mirror memory tracking puts an additional `add` in compiled code, we likely just want to make HLE access slower if this is the final solution.
- Native project is in the messiest possible location.
- [HACK] JIT memory access always uses native "fast" path
- [HACK] Trying some things with texture invalidation and views.
It works :)
Still a few hacks, messy things, slow things
More work in progress stuff (also move to memory project)
Quite a bit faster now.
- Unmapping GPU VA and CPU VA will now correctly update write tracking regions, and invalidate textures for the former.
- The Virtual range list is now non-overlapping like the physical one.
- Fixed some bugs where regions could leak.
- Introduced a weird bug that I still need to track down (consistent invalid buffer in MK8 ribbon road)
Move some stuff.
I think we'll eventually just put the dll and so for this in a nuget package.
Fix rebase.
[WIP] MultiRegionHandle variable size ranges
- Avoid reprotecting regions that change often (needs some tweaking)
- There's still a bug in buffers, somehow.
- Might want different api for minimum granularity
Fix rebase issue
Commit everything needed for software only tracking.
Remove native components.
Remove more native stuff.
Cleanup
Use a separate window for the background context, update opentk. (fixes linux)
Some experimental changes
Should get things working up to scratch - still need to try some things with flush/modification and res scale.
Include address with the region action.
Initial work to make range tracking work
Still a ton of bugs
Fix some issues with the new stuff.
* Fix texture flush instability
There's still some weird behaviour, but it's much improved without this. (textures with cpu modified data were flushing over it)
* Find the destination texture for Buffer->Texture full copy
Greatly improves performance for nvdec videos (with range tracking)
* Further improve texture tracking
* Disable Memory Tracking for view parents
This is a temporary approach to better match behaviour on master (where invalidations would be soaked up by views, rather than trigger twice)
The assumption is that when views are created to a texture, they will cover all of its data anyways. Of course, this can easily be improved in future.
* Introduce some tracking tests.
WIP
* Complete base tests.
* Add more tests for multiregion, fix existing test.
* Cleanup Part 1
* Remove unnecessary code from memory tracking
* Fix some inconsistencies with 3D texture rule.
* Add dispose tests.
* Use a background thread for the background context.
Rather than setting and unsetting a context as current, doing the work on a dedicated thread with signals seems to be a bit faster.
Also nerf the multithreading test a bit.
* Copy to texture with matching alignment
This extends the copy to work for some videos with unusual size, such as tutorial videos in SMO. It will only occur if the destination texture already exists at XCount size.
* Track reads for buffer copies. Synchronize new buffers before copying overlaps.
* Remove old texture flushing mechanisms.
Range tracking all the way, baby.
* Wake the background thread when disposing.
Avoids a deadlock when games are closed.
* Address Feedback 1
* Separate TextureCopy instance for background thread
Also `BackgroundContextWorker.InBackground` for a more sensible idenfifier for if we're in a background thread.
* Add missing XML docs.
* Address Feedback
* Maybe I should start drinking coffee.
* Some more feedback.
* Remove flush warning, Refocus window after making background context
2020-10-16 22:18:35 +02:00
|
|
|
if (item.Address >= endAddress)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
Memory Read/Write Tracking using Region Handles (#1272)
* WIP Range Tracking
- Texture invalidation seems to have large problems
- Buffer/Pool invalidation may have problems
- Mirror memory tracking puts an additional `add` in compiled code, we likely just want to make HLE access slower if this is the final solution.
- Native project is in the messiest possible location.
- [HACK] JIT memory access always uses native "fast" path
- [HACK] Trying some things with texture invalidation and views.
It works :)
Still a few hacks, messy things, slow things
More work in progress stuff (also move to memory project)
Quite a bit faster now.
- Unmapping GPU VA and CPU VA will now correctly update write tracking regions, and invalidate textures for the former.
- The Virtual range list is now non-overlapping like the physical one.
- Fixed some bugs where regions could leak.
- Introduced a weird bug that I still need to track down (consistent invalid buffer in MK8 ribbon road)
Move some stuff.
I think we'll eventually just put the dll and so for this in a nuget package.
Fix rebase.
[WIP] MultiRegionHandle variable size ranges
- Avoid reprotecting regions that change often (needs some tweaking)
- There's still a bug in buffers, somehow.
- Might want different api for minimum granularity
Fix rebase issue
Commit everything needed for software only tracking.
Remove native components.
Remove more native stuff.
Cleanup
Use a separate window for the background context, update opentk. (fixes linux)
Some experimental changes
Should get things working up to scratch - still need to try some things with flush/modification and res scale.
Include address with the region action.
Initial work to make range tracking work
Still a ton of bugs
Fix some issues with the new stuff.
* Fix texture flush instability
There's still some weird behaviour, but it's much improved without this. (textures with cpu modified data were flushing over it)
* Find the destination texture for Buffer->Texture full copy
Greatly improves performance for nvdec videos (with range tracking)
* Further improve texture tracking
* Disable Memory Tracking for view parents
This is a temporary approach to better match behaviour on master (where invalidations would be soaked up by views, rather than trigger twice)
The assumption is that when views are created to a texture, they will cover all of its data anyways. Of course, this can easily be improved in future.
* Introduce some tracking tests.
WIP
* Complete base tests.
* Add more tests for multiregion, fix existing test.
* Cleanup Part 1
* Remove unnecessary code from memory tracking
* Fix some inconsistencies with 3D texture rule.
* Add dispose tests.
* Use a background thread for the background context.
Rather than setting and unsetting a context as current, doing the work on a dedicated thread with signals seems to be a bit faster.
Also nerf the multithreading test a bit.
* Copy to texture with matching alignment
This extends the copy to work for some videos with unusual size, such as tutorial videos in SMO. It will only occur if the destination texture already exists at XCount size.
* Track reads for buffer copies. Synchronize new buffers before copying overlaps.
* Remove old texture flushing mechanisms.
Range tracking all the way, baby.
* Wake the background thread when disposing.
Avoids a deadlock when games are closed.
* Address Feedback 1
* Separate TextureCopy instance for background thread
Also `BackgroundContextWorker.InBackground` for a more sensible idenfifier for if we're in a background thread.
* Add missing XML docs.
* Address Feedback
* Maybe I should start drinking coffee.
* Some more feedback.
* Remove flush warning, Refocus window after making background context
2020-10-16 22:18:35 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2021-09-19 14:22:26 +02:00
|
|
|
if (item.OverlapsWith(address, endAddress))
|
Memory Read/Write Tracking using Region Handles (#1272)
* WIP Range Tracking
- Texture invalidation seems to have large problems
- Buffer/Pool invalidation may have problems
- Mirror memory tracking puts an additional `add` in compiled code, we likely just want to make HLE access slower if this is the final solution.
- Native project is in the messiest possible location.
- [HACK] JIT memory access always uses native "fast" path
- [HACK] Trying some things with texture invalidation and views.
It works :)
Still a few hacks, messy things, slow things
More work in progress stuff (also move to memory project)
Quite a bit faster now.
- Unmapping GPU VA and CPU VA will now correctly update write tracking regions, and invalidate textures for the former.
- The Virtual range list is now non-overlapping like the physical one.
- Fixed some bugs where regions could leak.
- Introduced a weird bug that I still need to track down (consistent invalid buffer in MK8 ribbon road)
Move some stuff.
I think we'll eventually just put the dll and so for this in a nuget package.
Fix rebase.
[WIP] MultiRegionHandle variable size ranges
- Avoid reprotecting regions that change often (needs some tweaking)
- There's still a bug in buffers, somehow.
- Might want different api for minimum granularity
Fix rebase issue
Commit everything needed for software only tracking.
Remove native components.
Remove more native stuff.
Cleanup
Use a separate window for the background context, update opentk. (fixes linux)
Some experimental changes
Should get things working up to scratch - still need to try some things with flush/modification and res scale.
Include address with the region action.
Initial work to make range tracking work
Still a ton of bugs
Fix some issues with the new stuff.
* Fix texture flush instability
There's still some weird behaviour, but it's much improved without this. (textures with cpu modified data were flushing over it)
* Find the destination texture for Buffer->Texture full copy
Greatly improves performance for nvdec videos (with range tracking)
* Further improve texture tracking
* Disable Memory Tracking for view parents
This is a temporary approach to better match behaviour on master (where invalidations would be soaked up by views, rather than trigger twice)
The assumption is that when views are created to a texture, they will cover all of its data anyways. Of course, this can easily be improved in future.
* Introduce some tracking tests.
WIP
* Complete base tests.
* Add more tests for multiregion, fix existing test.
* Cleanup Part 1
* Remove unnecessary code from memory tracking
* Fix some inconsistencies with 3D texture rule.
* Add dispose tests.
* Use a background thread for the background context.
Rather than setting and unsetting a context as current, doing the work on a dedicated thread with signals seems to be a bit faster.
Also nerf the multithreading test a bit.
* Copy to texture with matching alignment
This extends the copy to work for some videos with unusual size, such as tutorial videos in SMO. It will only occur if the destination texture already exists at XCount size.
* Track reads for buffer copies. Synchronize new buffers before copying overlaps.
* Remove old texture flushing mechanisms.
Range tracking all the way, baby.
* Wake the background thread when disposing.
Avoids a deadlock when games are closed.
* Address Feedback 1
* Separate TextureCopy instance for background thread
Also `BackgroundContextWorker.InBackground` for a more sensible idenfifier for if we're in a background thread.
* Add missing XML docs.
* Address Feedback
* Maybe I should start drinking coffee.
* Some more feedback.
* Remove flush warning, Refocus window after making background context
2020-10-16 22:18:35 +02:00
|
|
|
{
|
|
|
|
if (outputIndex == output.Length)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
Memory Read/Write Tracking using Region Handles (#1272)
* WIP Range Tracking
- Texture invalidation seems to have large problems
- Buffer/Pool invalidation may have problems
- Mirror memory tracking puts an additional `add` in compiled code, we likely just want to make HLE access slower if this is the final solution.
- Native project is in the messiest possible location.
- [HACK] JIT memory access always uses native "fast" path
- [HACK] Trying some things with texture invalidation and views.
It works :)
Still a few hacks, messy things, slow things
More work in progress stuff (also move to memory project)
Quite a bit faster now.
- Unmapping GPU VA and CPU VA will now correctly update write tracking regions, and invalidate textures for the former.
- The Virtual range list is now non-overlapping like the physical one.
- Fixed some bugs where regions could leak.
- Introduced a weird bug that I still need to track down (consistent invalid buffer in MK8 ribbon road)
Move some stuff.
I think we'll eventually just put the dll and so for this in a nuget package.
Fix rebase.
[WIP] MultiRegionHandle variable size ranges
- Avoid reprotecting regions that change often (needs some tweaking)
- There's still a bug in buffers, somehow.
- Might want different api for minimum granularity
Fix rebase issue
Commit everything needed for software only tracking.
Remove native components.
Remove more native stuff.
Cleanup
Use a separate window for the background context, update opentk. (fixes linux)
Some experimental changes
Should get things working up to scratch - still need to try some things with flush/modification and res scale.
Include address with the region action.
Initial work to make range tracking work
Still a ton of bugs
Fix some issues with the new stuff.
* Fix texture flush instability
There's still some weird behaviour, but it's much improved without this. (textures with cpu modified data were flushing over it)
* Find the destination texture for Buffer->Texture full copy
Greatly improves performance for nvdec videos (with range tracking)
* Further improve texture tracking
* Disable Memory Tracking for view parents
This is a temporary approach to better match behaviour on master (where invalidations would be soaked up by views, rather than trigger twice)
The assumption is that when views are created to a texture, they will cover all of its data anyways. Of course, this can easily be improved in future.
* Introduce some tracking tests.
WIP
* Complete base tests.
* Add more tests for multiregion, fix existing test.
* Cleanup Part 1
* Remove unnecessary code from memory tracking
* Fix some inconsistencies with 3D texture rule.
* Add dispose tests.
* Use a background thread for the background context.
Rather than setting and unsetting a context as current, doing the work on a dedicated thread with signals seems to be a bit faster.
Also nerf the multithreading test a bit.
* Copy to texture with matching alignment
This extends the copy to work for some videos with unusual size, such as tutorial videos in SMO. It will only occur if the destination texture already exists at XCount size.
* Track reads for buffer copies. Synchronize new buffers before copying overlaps.
* Remove old texture flushing mechanisms.
Range tracking all the way, baby.
* Wake the background thread when disposing.
Avoids a deadlock when games are closed.
* Address Feedback 1
* Separate TextureCopy instance for background thread
Also `BackgroundContextWorker.InBackground` for a more sensible idenfifier for if we're in a background thread.
* Add missing XML docs.
* Address Feedback
* Maybe I should start drinking coffee.
* Some more feedback.
* Remove flush warning, Refocus window after making background context
2020-10-16 22:18:35 +02:00
|
|
|
Array.Resize(ref output, outputIndex + ArrayGrowthSize);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
Memory Read/Write Tracking using Region Handles (#1272)
* WIP Range Tracking
- Texture invalidation seems to have large problems
- Buffer/Pool invalidation may have problems
- Mirror memory tracking puts an additional `add` in compiled code, we likely just want to make HLE access slower if this is the final solution.
- Native project is in the messiest possible location.
- [HACK] JIT memory access always uses native "fast" path
- [HACK] Trying some things with texture invalidation and views.
It works :)
Still a few hacks, messy things, slow things
More work in progress stuff (also move to memory project)
Quite a bit faster now.
- Unmapping GPU VA and CPU VA will now correctly update write tracking regions, and invalidate textures for the former.
- The Virtual range list is now non-overlapping like the physical one.
- Fixed some bugs where regions could leak.
- Introduced a weird bug that I still need to track down (consistent invalid buffer in MK8 ribbon road)
Move some stuff.
I think we'll eventually just put the dll and so for this in a nuget package.
Fix rebase.
[WIP] MultiRegionHandle variable size ranges
- Avoid reprotecting regions that change often (needs some tweaking)
- There's still a bug in buffers, somehow.
- Might want different api for minimum granularity
Fix rebase issue
Commit everything needed for software only tracking.
Remove native components.
Remove more native stuff.
Cleanup
Use a separate window for the background context, update opentk. (fixes linux)
Some experimental changes
Should get things working up to scratch - still need to try some things with flush/modification and res scale.
Include address with the region action.
Initial work to make range tracking work
Still a ton of bugs
Fix some issues with the new stuff.
* Fix texture flush instability
There's still some weird behaviour, but it's much improved without this. (textures with cpu modified data were flushing over it)
* Find the destination texture for Buffer->Texture full copy
Greatly improves performance for nvdec videos (with range tracking)
* Further improve texture tracking
* Disable Memory Tracking for view parents
This is a temporary approach to better match behaviour on master (where invalidations would be soaked up by views, rather than trigger twice)
The assumption is that when views are created to a texture, they will cover all of its data anyways. Of course, this can easily be improved in future.
* Introduce some tracking tests.
WIP
* Complete base tests.
* Add more tests for multiregion, fix existing test.
* Cleanup Part 1
* Remove unnecessary code from memory tracking
* Fix some inconsistencies with 3D texture rule.
* Add dispose tests.
* Use a background thread for the background context.
Rather than setting and unsetting a context as current, doing the work on a dedicated thread with signals seems to be a bit faster.
Also nerf the multithreading test a bit.
* Copy to texture with matching alignment
This extends the copy to work for some videos with unusual size, such as tutorial videos in SMO. It will only occur if the destination texture already exists at XCount size.
* Track reads for buffer copies. Synchronize new buffers before copying overlaps.
* Remove old texture flushing mechanisms.
Range tracking all the way, baby.
* Wake the background thread when disposing.
Avoids a deadlock when games are closed.
* Address Feedback 1
* Separate TextureCopy instance for background thread
Also `BackgroundContextWorker.InBackground` for a more sensible idenfifier for if we're in a background thread.
* Add missing XML docs.
* Address Feedback
* Maybe I should start drinking coffee.
* Some more feedback.
* Remove flush warning, Refocus window after making background context
2020-10-16 22:18:35 +02:00
|
|
|
|
2021-09-19 14:22:26 +02:00
|
|
|
output[outputIndex++] = item.Value;
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 01:29:37 +01:00
|
|
|
return outputIndex;
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Gets all items overlapping with the specified item in memory.
|
2020-01-01 16:39:09 +01:00
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
2019-12-31 04:22:58 +01:00
|
|
|
/// This method only returns correct results if none of the items on the list overlaps with
|
|
|
|
/// each other. If that is not the case, this method should not be used.
|
|
|
|
/// This method is faster than the regular method to find all overlaps.
|
2020-01-01 16:39:09 +01:00
|
|
|
/// </remarks>
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <param name="item">Item to check for overlaps</param>
|
|
|
|
/// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
|
|
|
|
/// <returns>The number of overlapping items found</returns>
|
2019-11-25 01:29:37 +01:00
|
|
|
public int FindOverlapsNonOverlapping(T item, ref T[] output)
|
2019-11-08 20:39:12 +01:00
|
|
|
{
|
2019-11-25 01:29:37 +01:00
|
|
|
return FindOverlapsNonOverlapping(item.Address, item.Size, ref output);
|
2019-11-08 20:39:12 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Gets all items on the list overlapping the specified memory range.
|
2020-01-01 16:39:09 +01:00
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
2019-12-31 04:22:58 +01:00
|
|
|
/// This method only returns correct results if none of the items on the list overlaps with
|
|
|
|
/// each other. If that is not the case, this method should not be used.
|
|
|
|
/// This method is faster than the regular method to find all overlaps.
|
2020-01-01 16:39:09 +01:00
|
|
|
/// </remarks>
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <param name="address">Start address of the range</param>
|
2020-01-02 00:14:18 +01:00
|
|
|
/// <param name="size">Size in bytes of the range</param>
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
|
|
|
|
/// <returns>The number of overlapping items found</returns>
|
2019-11-25 01:29:37 +01:00
|
|
|
public int FindOverlapsNonOverlapping(ulong address, ulong size, ref T[] output)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-08 20:39:12 +01:00
|
|
|
// This is a bit faster than FindOverlaps, but only works
|
|
|
|
// when none of the items on the list overlaps with each other.
|
2019-11-25 01:29:37 +01:00
|
|
|
int outputIndex = 0;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2021-09-19 14:22:26 +02:00
|
|
|
ulong endAddress = address + size;
|
|
|
|
|
|
|
|
int index = BinarySearch(address, endAddress);
|
2019-11-08 20:39:12 +01:00
|
|
|
|
|
|
|
if (index >= 0)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2021-09-19 14:22:26 +02:00
|
|
|
while (index > 0 && _items[index - 1].OverlapsWith(address, endAddress))
|
2019-11-08 20:39:12 +01:00
|
|
|
{
|
|
|
|
index--;
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-11-08 20:39:12 +01:00
|
|
|
do
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-25 01:29:37 +01:00
|
|
|
if (outputIndex == output.Length)
|
|
|
|
{
|
|
|
|
Array.Resize(ref output, outputIndex + ArrayGrowthSize);
|
|
|
|
}
|
|
|
|
|
2021-09-19 14:22:26 +02:00
|
|
|
output[outputIndex++] = _items[index++].Value;
|
2019-11-08 20:39:12 +01:00
|
|
|
}
|
2021-09-19 14:22:26 +02:00
|
|
|
while (index < Count && _items[index].OverlapsWith(address, endAddress));
|
2019-11-08 20:39:12 +01:00
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-11-25 01:29:37 +01:00
|
|
|
return outputIndex;
|
2019-11-08 20:39:12 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Gets all items on the list with the specified memory address.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="address">Address to find</param>
|
|
|
|
/// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
|
|
|
|
/// <returns>The number of matches found</returns>
|
2019-11-25 01:29:37 +01:00
|
|
|
public int FindOverlaps(ulong address, ref T[] output)
|
2019-11-08 20:39:12 +01:00
|
|
|
{
|
|
|
|
int index = BinarySearch(address);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-11-25 01:29:37 +01:00
|
|
|
int outputIndex = 0;
|
|
|
|
|
2019-11-08 20:39:12 +01:00
|
|
|
if (index >= 0)
|
|
|
|
{
|
2021-09-19 14:22:26 +02:00
|
|
|
while (index > 0 && _items[index - 1].Address == address)
|
2019-11-08 20:39:12 +01:00
|
|
|
{
|
|
|
|
index--;
|
|
|
|
}
|
|
|
|
|
2021-09-19 14:22:26 +02:00
|
|
|
while (index < Count)
|
2019-11-08 20:39:12 +01:00
|
|
|
{
|
2021-09-19 14:22:26 +02:00
|
|
|
ref RangeItem<T> overlap = ref _items[index++];
|
2019-11-08 20:39:12 +01:00
|
|
|
|
|
|
|
if (overlap.Address != address)
|
|
|
|
{
|
|
|
|
break;
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
2019-11-08 20:39:12 +01:00
|
|
|
|
2019-11-25 01:29:37 +01:00
|
|
|
if (outputIndex == output.Length)
|
|
|
|
{
|
|
|
|
Array.Resize(ref output, outputIndex + ArrayGrowthSize);
|
|
|
|
}
|
|
|
|
|
2021-09-19 14:22:26 +02:00
|
|
|
output[outputIndex++] = overlap.Value;
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 01:29:37 +01:00
|
|
|
return outputIndex;
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Performs binary search on the internal list of items.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="address">Address to find</param>
|
|
|
|
/// <returns>List index of the item, or complement index of nearest item with lower value on the list</returns>
|
2019-10-13 08:02:07 +02:00
|
|
|
private int BinarySearch(ulong address)
|
|
|
|
{
|
|
|
|
int left = 0;
|
2021-09-19 14:22:26 +02:00
|
|
|
int right = Count - 1;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
while (left <= right)
|
|
|
|
{
|
|
|
|
int range = right - left;
|
|
|
|
|
|
|
|
int middle = left + (range >> 1);
|
|
|
|
|
2021-09-19 14:22:26 +02:00
|
|
|
ref RangeItem<T> item = ref _items[middle];
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
if (item.Address == address)
|
|
|
|
{
|
|
|
|
return middle;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (address < item.Address)
|
|
|
|
{
|
|
|
|
right = middle - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
left = middle + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ~left;
|
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Performs binary search for items overlapping a given memory range.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="address">Start address of the range</param>
|
2021-09-19 14:22:26 +02:00
|
|
|
/// <param name="endAddress">End address of the range</param>
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <returns>List index of the item, or complement index of nearest item with lower value on the list</returns>
|
2021-09-19 14:22:26 +02:00
|
|
|
private int BinarySearch(ulong address, ulong endAddress)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
|
|
|
int left = 0;
|
2021-09-19 14:22:26 +02:00
|
|
|
int right = Count - 1;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
while (left <= right)
|
|
|
|
{
|
|
|
|
int range = right - left;
|
|
|
|
|
|
|
|
int middle = left + (range >> 1);
|
|
|
|
|
2021-09-19 14:22:26 +02:00
|
|
|
ref RangeItem<T> item = ref _items[middle];
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2021-09-19 14:22:26 +02:00
|
|
|
if (item.OverlapsWith(address, endAddress))
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
|
|
|
return middle;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (address < item.Address)
|
|
|
|
{
|
|
|
|
right = middle - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
left = middle + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ~left;
|
|
|
|
}
|
2019-12-31 23:09:49 +01:00
|
|
|
|
|
|
|
public IEnumerator<T> GetEnumerator()
|
|
|
|
{
|
2021-09-19 14:22:26 +02:00
|
|
|
for (int i = 0; i < Count; i++)
|
|
|
|
{
|
|
|
|
yield return _items[i].Value;
|
|
|
|
}
|
2019-12-31 23:09:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
|
|
{
|
2021-09-19 14:22:26 +02:00
|
|
|
for (int i = 0; i < Count; i++)
|
|
|
|
{
|
|
|
|
yield return _items[i].Value;
|
|
|
|
}
|
2019-12-31 23:09:49 +01:00
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
}
|