Lock cache while using it in ReadOnlyNativeArray
This commit is contained in:
parent
d5bbc18b1a
commit
d38338ddc3
|
@ -71,38 +71,39 @@ namespace PkmnLibSharp.Utilities
|
|||
return -1;
|
||||
}
|
||||
|
||||
internal T? GetDontInitialise(int index)
|
||||
{
|
||||
unsafe
|
||||
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.)
|
||||
var p = new IntPtr(*(void**)IntPtr.Add(_ptr, index * IntPtr.Size).ToPointer());
|
||||
return new IntPtr(*(void**)IntPtr.Add(_ptr, index * IntPtr.Size).ToPointer());
|
||||
}
|
||||
|
||||
internal T? GetDontInitialise(int index)
|
||||
{
|
||||
var p = GetPtr(index);
|
||||
if (p == IntPtr.Zero)
|
||||
return null;
|
||||
lock (_cache)
|
||||
{
|
||||
if (_cache[index]?.Ptr == p)
|
||||
return _cache[index]!;
|
||||
}
|
||||
if (PointerWrapper.TryResolvePointer(p, out T? t))
|
||||
return t!;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public T? this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
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());
|
||||
var p = GetPtr(index);
|
||||
if (p == IntPtr.Zero)
|
||||
return null;
|
||||
lock (_cache)
|
||||
{
|
||||
if (_cache[index]?.Ptr == p)
|
||||
return _cache[index]!;
|
||||
if (PointerWrapper.TryResolvePointer(p, out T? t))
|
||||
|
|
Loading…
Reference in New Issue