-
Notifications
You must be signed in to change notification settings - Fork 3
/
BitpackedPtr.cs
51 lines (41 loc) · 1.54 KB
/
BitpackedPtr.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace Arenas {
internal readonly struct BitpackedPtr : IEquatable<BitpackedPtr> {
internal const int LowerMask = 0b111;
internal const ulong PointerMask = ~0b111UL;
private readonly ulong value;
public BitpackedPtr(IntPtr ptr, int packedValue) {
if (packedValue < 0 || packedValue > LowerMask) {
throw new ArgumentOutOfRangeException(nameof(packedValue));
}
Debug.Assert(((ulong)ptr & LowerMask) == 0);
value = (ulong)ptr | (uint)packedValue;
}
#region Equality
public override bool Equals(object obj) {
return obj is BitpackedPtr ptr &&
value == ptr.value;
}
public bool Equals(BitpackedPtr other) {
return value == other.value;
}
public override int GetHashCode() {
return 731850958 + value.GetHashCode();
}
public static bool operator ==(BitpackedPtr left, BitpackedPtr right) {
return left.Equals(right);
}
public static bool operator !=(BitpackedPtr left, BitpackedPtr right) {
return !(left == right);
}
#endregion
public override string ToString() {
return $"{Value}:{PackedValue}";
}
public IntPtr Value { get { return (IntPtr)(value & PointerMask); } }
public int PackedValue { get { return (int)value & LowerMask; } }
}
}