Lock cache while using it in ReadOnlyNativeArray

This commit is contained in:
Deukhoofd 2021-01-16 19:50:43 +01:00
parent d5bbc18b1a
commit d38338ddc3
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
1 changed files with 20 additions and 19 deletions

View File

@ -71,38 +71,39 @@ namespace PkmnLibSharp.Utilities
return -1;
}
private unsafe IntPtr GetPtr(int index)
{
if (index >= Count)
throw new IndexOutOfRangeException();
// Where's your god now?
// (We add the offset of the index to the pointer, then dereference the pointer pointer to get the actual pointer to the object we want.)
return new IntPtr(*(void**)IntPtr.Add(_ptr, index * IntPtr.Size).ToPointer());
}
internal T? GetDontInitialise(int index)
{
unsafe
var p = GetPtr(index);
if (p == IntPtr.Zero)
return null;
lock (_cache)
{
if (index >= Count)
throw new IndexOutOfRangeException();
// Where's your god now?
// (We add the offset of the index to the pointer, then dereference the pointer pointer to get the actual pointer to the object we want.)
var p = new IntPtr(*(void**)IntPtr.Add(_ptr, index * IntPtr.Size).ToPointer());
if (p == IntPtr.Zero)
return null;
if (_cache[index]?.Ptr == p)
return _cache[index]!;
if (PointerWrapper.TryResolvePointer(p, out T? t))
return t!;
return null;
}
if (PointerWrapper.TryResolvePointer(p, out T? t))
return t!;
return null;
}
public T? this[int index]
{
get
{
unsafe
var p = GetPtr(index);
if (p == IntPtr.Zero)
return null;
lock (_cache)
{
if (index >= Count)
throw new IndexOutOfRangeException();
// Where's your god now?
// (We add the offset of the index to the pointer, then dereference the pointer pointer to get the actual pointer to the object we want.)
var p = new IntPtr(*(void**)IntPtr.Add(_ptr, index * IntPtr.Size).ToPointer());
if (p == IntPtr.Zero)
return null;
if (_cache[index]?.Ptr == p)
return _cache[index]!;
if (PointerWrapper.TryResolvePointer(p, out T? t))