Skip to content

Commit 0a39dc1

Browse files
just some renaming
1 parent d014722 commit 0a39dc1

31 files changed

+661
-1126
lines changed

Infinity.Core/Memory/Chunk.cs

Lines changed: 0 additions & 81 deletions
This file was deleted.

Infinity.Core/Memory/ChunkAllocator.cs

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,67 @@
22
using System.Collections.Generic;
33
using System.Runtime.InteropServices;
44

5-
public unsafe class ChunkedByteAllocator : IDisposable
5+
namespace Infinity.Core
66
{
7-
private readonly int _slabSize;
8-
private readonly Stack<IntPtr> _freeSlabs = new Stack<IntPtr>();
9-
10-
public ChunkedByteAllocator(int slabSize)
7+
public unsafe class ChunkAllocator : IDisposable
118
{
12-
if (slabSize <= 0) throw new ArgumentOutOfRangeException(nameof(slabSize));
13-
_slabSize = slabSize;
14-
}
9+
private readonly int _slabSize;
10+
private readonly Stack<IntPtr> _freeSlabs = new Stack<IntPtr>();
1511

16-
/// <summary>
17-
/// Allocates a block of memory. If size <= slabSize, reuses slab; otherwise allocates exact size.
18-
/// </summary>
19-
public byte* Allocate(int size)
20-
{
21-
if (size <= 0) throw new ArgumentOutOfRangeException(nameof(size));
12+
public ChunkAllocator(int slabSize)
13+
{
14+
if (slabSize <= 0) throw new ArgumentOutOfRangeException(nameof(slabSize));
15+
_slabSize = slabSize;
16+
}
2217

23-
if (size <= _slabSize)
18+
/// <summary>
19+
/// Allocates a block of memory. If size <= slabSize, reuses slab; otherwise allocates exact size.
20+
/// </summary>
21+
public byte* Allocate(int size)
2422
{
25-
if (_freeSlabs.Count > 0)
23+
if (size <= 0) throw new ArgumentOutOfRangeException(nameof(size));
24+
25+
if (size <= _slabSize)
2626
{
27-
return (byte*)_freeSlabs.Pop();
27+
if (_freeSlabs.Count > 0)
28+
{
29+
return (byte*)_freeSlabs.Pop();
30+
}
31+
return (byte*)Marshal.AllocHGlobal(_slabSize);
32+
}
33+
else
34+
{
35+
return (byte*)Marshal.AllocHGlobal(size);
2836
}
29-
return (byte*)Marshal.AllocHGlobal(_slabSize);
30-
}
31-
else
32-
{
33-
return (byte*)Marshal.AllocHGlobal(size);
3437
}
35-
}
36-
37-
/// <summary>
38-
/// Frees a previously allocated block.
39-
/// </summary>
40-
/// <param name="ptr">Pointer to free.</param>
41-
/// <param name="size">Original allocation size. Determines if slab or large allocation.</param>
42-
public void Free(byte* ptr, int size = -1)
43-
{
44-
if (ptr == null) return;
4538

46-
if (size <= _slabSize || size < 0)
47-
{
48-
// Return to slab pool
49-
_freeSlabs.Push((IntPtr)ptr);
50-
}
51-
else
39+
/// <summary>
40+
/// Frees a previously allocated block.
41+
/// </summary>
42+
/// <param name="ptr">Pointer to free.</param>
43+
/// <param name="size">Original allocation size. Determines if slab or large allocation.</param>
44+
public void Free(byte* ptr, int size = -1)
5245
{
53-
// Free large allocation
54-
Marshal.FreeHGlobal((IntPtr)ptr);
46+
if (ptr == null) return;
47+
48+
if (size <= _slabSize || size < 0)
49+
{
50+
// Return to slab pool
51+
_freeSlabs.Push((IntPtr)ptr);
52+
}
53+
else
54+
{
55+
// Free large allocation
56+
Marshal.FreeHGlobal((IntPtr)ptr);
57+
}
5558
}
56-
}
5759

58-
public void Dispose()
59-
{
60-
while (_freeSlabs.Count > 0)
60+
public void Dispose()
6161
{
62-
Marshal.FreeHGlobal(_freeSlabs.Pop());
62+
while (_freeSlabs.Count > 0)
63+
{
64+
Marshal.FreeHGlobal(_freeSlabs.Pop());
65+
}
6366
}
6467
}
6568
}

Infinity.Core/Memory/Manager.cs

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
namespace Infinity.Core
2+
{
3+
public unsafe class MemoryChunk : IDisposable
4+
{
5+
private byte* _ptr;
6+
private int _capacity;
7+
private int _length;
8+
private readonly ChunkAllocator _allocator;
9+
10+
public byte* Ptr => _ptr;
11+
public int Length => _length;
12+
public int Capacity => _capacity;
13+
14+
public MemoryChunk(ChunkAllocator allocator, int initialCapacity = 1024)
15+
{
16+
_allocator = allocator;
17+
_capacity = initialCapacity;
18+
_ptr = _allocator.Allocate(_capacity);
19+
_length = 0;
20+
}
21+
22+
/// <summary>
23+
/// Ensures buffer has space for additional bytes. Grows automatically if needed.
24+
/// </summary>
25+
public void EnsureCapacity(int additional)
26+
{
27+
int required = _length + additional;
28+
if (required <= _capacity) return;
29+
30+
int newCapacity = _capacity;
31+
while (newCapacity < required) newCapacity *= 2;
32+
33+
byte* newPtr = _allocator.Allocate(newCapacity);
34+
Buffer.MemoryCopy(_ptr, newPtr, newCapacity, _length);
35+
_allocator.Free(_ptr, _capacity);
36+
37+
_ptr = newPtr;
38+
_capacity = newCapacity;
39+
}
40+
41+
/// <summary>
42+
/// Increases the length after writing bytes.
43+
/// </summary>
44+
public void Advance(int count)
45+
{
46+
_length += count;
47+
}
48+
49+
public void SetLength(int length)
50+
{
51+
if (length < 0 || length > _capacity)
52+
throw new ArgumentOutOfRangeException(nameof(length), "Length must be between 0 and Capacity.");
53+
_length = length;
54+
}
55+
56+
public void Dispose()
57+
{
58+
Free();
59+
GC.SuppressFinalize(this); // prevent finalizer from running twice
60+
}
61+
62+
private void Free()
63+
{
64+
if (_ptr != null)
65+
{
66+
_allocator.Free(_ptr);
67+
_ptr = null;
68+
_capacity = 0;
69+
_length = 0;
70+
}
71+
}
72+
73+
~MemoryChunk()
74+
{
75+
// This will run if Dispose was not called
76+
Free();
77+
}
78+
79+
/// <summary>
80+
/// Returns a read-only span of written bytes.
81+
/// </summary>
82+
public ReadOnlySpan<byte> AsSpan => new ReadOnlySpan<byte>(_ptr, _length);
83+
}
84+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Buffers;
2+
using System.Runtime.InteropServices;
3+
4+
namespace Infinity.Core
5+
{
6+
public unsafe class UnmanagedMemoryManager : MemoryManager<byte>
7+
{
8+
private readonly byte* _ptr;
9+
private readonly int _length;
10+
11+
public UnmanagedMemoryManager(byte* ptr, int length)
12+
{
13+
if (ptr == null)
14+
throw new ArgumentNullException(nameof(ptr));
15+
if (length < 0)
16+
throw new ArgumentOutOfRangeException(nameof(length));
17+
18+
_ptr = ptr;
19+
_length = length;
20+
}
21+
22+
public override Span<byte> GetSpan() => new Span<byte>(_ptr, _length);
23+
24+
public override MemoryHandle Pin(int elementIndex = 0)
25+
{
26+
if (elementIndex < 0 || elementIndex > _length)
27+
throw new ArgumentOutOfRangeException(nameof(elementIndex));
28+
29+
// No GCHandle needed because memory is unmanaged
30+
return new MemoryHandle(_ptr + elementIndex);
31+
}
32+
33+
public override void Unpin()
34+
{
35+
// Nothing to do for unmanaged memory
36+
}
37+
38+
protected override void Dispose(bool disposing)
39+
{
40+
// Nothing to free: memory is externally owned
41+
}
42+
43+
public void Dispose()
44+
{
45+
Dispose(true);
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)