Skip to content

Commit ddd711b

Browse files
authored
Tighten InstanceIndexBuffer GC-safety (#1766)
1 parent d1a9289 commit ddd711b

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

libs/client/LightEpoch.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Diagnostics;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Runtime.CompilerServices;
78
using System.Runtime.InteropServices;
89
using System.Threading;
@@ -19,7 +20,7 @@ public sealed unsafe class LightEpoch
1920
/// (1) in AssignInstance, to assign a unique instanceId to each LightEpoch instance, and
2021
/// (2) in Metadata, to track per-thread epoch table entries for each LightEpoch instance.
2122
/// </summary>
22-
[StructLayout(LayoutKind.Explicit, Size = MaxInstances * sizeof(int))]
23+
[InlineArray(MaxInstances)]
2324
private struct InstanceIndexBuffer
2425
{
2526
/// <summary>
@@ -30,17 +31,17 @@ private struct InstanceIndexBuffer
3031
/// <summary>
3132
/// Anchor field for the buffer.
3233
/// </summary>
33-
[FieldOffset(0)]
3434
int field0;
3535

3636
/// <summary>
3737
/// Reference to the entry for the given instance ID.
3838
/// </summary>
3939
[MethodImpl(MethodImplOptions.AggressiveInlining)]
40+
[UnscopedRef]
4041
internal ref int GetRef(int instanceId)
4142
{
4243
Debug.Assert(instanceId >= 0 && instanceId < MaxInstances);
43-
return ref Unsafe.AsRef<int>((int*)Unsafe.AsPointer(ref field0) + instanceId);
44+
return ref Unsafe.Add(ref field0, instanceId);
4445
}
4546
}
4647

libs/storage/Tsavorite/cs/src/core/Epochs/LightEpoch.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Diagnostics;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Runtime.CompilerServices;
78
using System.Runtime.InteropServices;
89
using System.Threading;
@@ -19,7 +20,7 @@ public sealed unsafe class LightEpoch : IEpochAccessor
1920
/// (1) in AssignInstance, to assign a unique instanceId to each LightEpoch instance, and
2021
/// (2) in Metadata, to track per-thread epoch table entries for each LightEpoch instance.
2122
/// </summary>
22-
[StructLayout(LayoutKind.Explicit, Size = MaxInstances * sizeof(int))]
23+
[InlineArray(MaxInstances)]
2324
private struct InstanceIndexBuffer
2425
{
2526
/// <summary>
@@ -30,17 +31,17 @@ private struct InstanceIndexBuffer
3031
/// <summary>
3132
/// Anchor field for the buffer.
3233
/// </summary>
33-
[FieldOffset(0)]
3434
int field0;
3535

3636
/// <summary>
3737
/// Reference to the entry for the given instance ID.
3838
/// </summary>
3939
[MethodImpl(MethodImplOptions.AggressiveInlining)]
40+
[UnscopedRef]
4041
internal ref int GetRef(int instanceId)
4142
{
4243
Debug.Assert(instanceId >= 0 && instanceId < MaxInstances);
43-
return ref Unsafe.AsRef<int>((int*)Unsafe.AsPointer(ref field0) + instanceId);
44+
return ref Unsafe.Add(ref field0, instanceId);
4445
}
4546
}
4647

0 commit comments

Comments
 (0)