Skip to content

Commit 73d71d4

Browse files
author
LoneWandererProductions
committed
Add a SlabLane
1 parent e437321 commit 73d71d4

3 files changed

Lines changed: 476 additions & 2 deletions

File tree

MemoryManager.Core/MemoryManagerConfig.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
* PROGRAMMER: Peter Geinitz (Wayfarer)
77
*/
88

9-
using System;
10-
119
namespace MemoryManager.Core
1210
{
1311
/// <summary>

MemoryManager.Core/SlabBin.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* COPYRIGHT: See COPYING in the top level directory
3+
* PROJECT: MemoryManager.Core
4+
* FILE: SlabBin.cs
5+
* PURPOSE: Contains the SlabBin struct, which manages fixed-size buckets for the SlabLane. It tracks free slots and their offsets for efficient allocation and deallocation.
6+
* PROGRAMMER: Peter Geinitz (Wayfarer)
7+
*/
8+
9+
using System.Runtime.CompilerServices;
10+
11+
namespace MemoryManager.Core
12+
{
13+
/// <summary>
14+
/// The SlabBin struct manages fixed-size buckets for the SlabLane. It tracks free slots and their offsets for efficient allocation and deallocation.
15+
/// </summary>
16+
public struct SlabBin
17+
{
18+
/// <summary>
19+
/// The size class
20+
/// </summary>
21+
public readonly int SizeClass;
22+
23+
/// <summary>
24+
/// The physical slot size
25+
/// </summary>
26+
public readonly int PhysicalSlotSize;
27+
28+
/// <summary>
29+
/// The free offsets
30+
/// </summary>
31+
private readonly int[] _freeOffsets;
32+
33+
/// <summary>
34+
/// The top
35+
/// </summary>
36+
private int _top;
37+
38+
/// <summary>
39+
/// Initializes a new instance of the <see cref="SlabBin"/> struct.
40+
/// </summary>
41+
/// <param name="sizeClass">The size class.</param>
42+
/// <param name="physicalSlotSize">Size of the physical slot.</param>
43+
/// <param name="baseOffset">The base offset.</param>
44+
/// <param name="totalSlots">The total slots.</param>
45+
public SlabBin(int sizeClass, int physicalSlotSize, int baseOffset, int totalSlots)
46+
{
47+
SizeClass = sizeClass;
48+
PhysicalSlotSize = physicalSlotSize;
49+
_freeOffsets = new int[totalSlots];
50+
_top = totalSlots;
51+
52+
// Populate lookup index coordinates backwards to create high-speed LIFO cache hits
53+
for (int i = 0; i < totalSlots; i++)
54+
{
55+
_freeOffsets[i] = baseOffset + i * physicalSlotSize;
56+
}
57+
}
58+
59+
/// <summary>
60+
/// Gets the free count.
61+
/// </summary>
62+
/// <value>
63+
/// The free count.
64+
/// </value>
65+
public int FreeCount => _top;
66+
67+
/// <summary>
68+
/// Pops the slot offset.
69+
/// </summary>
70+
/// <returns></returns>
71+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
72+
public int PopSlotOffset() => _freeOffsets[--_top];
73+
74+
/// <summary>
75+
/// Pushes the slot offset.
76+
/// </summary>
77+
/// <param name="offset">The offset.</param>
78+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
79+
public void PushSlotOffset(int offset) => _freeOffsets[_top++] = offset;
80+
}
81+
}

0 commit comments

Comments
 (0)