Skip to content

Commit ece1ceb

Browse files
some last smaller touchups.
1 parent d9cbb3f commit ece1ceb

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

ExtendedSystemObjects/IntArray.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void InsertAt(int index, int value, int count = 1)
9090
Buffer.MemoryCopy(
9191
_ptr + index,
9292
_ptr + index + count,
93-
(_capacity - (index + count)) * sizeof(int),
93+
(_capacity - (index)) * sizeof(int), // size of dest from index onward
9494
shiftCount * sizeof(int));
9595
}
9696

@@ -177,14 +177,28 @@ public void Resize(int newSize)
177177
{
178178
if (newSize < 0) throw new ArgumentOutOfRangeException(nameof(newSize));
179179

180-
_buffer = Marshal.ReAllocHGlobal(_buffer, (IntPtr)(newSize * sizeof(int)));
181-
_ptr = (int*)_buffer;
180+
if (newSize == _capacity)
181+
return;
182+
183+
IntPtr newBuffer = Marshal.ReAllocHGlobal(_buffer, (IntPtr)(newSize * sizeof(int)));
184+
int* newPtr = (int*)newBuffer;
185+
186+
// If growing, zero out the newly allocated portion
187+
if (newSize > _capacity)
188+
{
189+
Span<int> newRegion = new Span<int>(newPtr + _capacity, newSize - _capacity);
190+
newRegion.Clear();
191+
}
192+
193+
_buffer = newBuffer;
194+
_ptr = newPtr;
182195
_capacity = newSize;
183196

184197
if (Length > newSize)
185198
Length = newSize;
186199
}
187200

201+
188202
/// <summary>
189203
/// Clears all elements to zero.
190204
/// </summary>

ExtendedSystemObjects/UnmanagedArray.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
using System;
1+
// ReSharper disable MemberCanBeInternal
2+
3+
using System;
24
using System.Runtime.InteropServices;
35

46
namespace ExtendedSystemObjects
57
{
8+
/// <inheritdoc cref="IDisposable" />
69
/// <summary>
710
/// Unsafe array
811
/// </summary>
912
/// <typeparam name="T">Generic Type, must be unmanaged</typeparam>
10-
/// <seealso cref="System.IDisposable" />
11-
public unsafe class UnmanagedArray<T> : IUnmanagedArray<T>, IDisposable where T : unmanaged
13+
/// <seealso cref="T:System.IDisposable" />
14+
public sealed unsafe class UnmanagedArray<T> : IUnmanagedArray<T>, IDisposable where T : unmanaged
1215
{
1316
private IntPtr _buffer;
1417
private T* _ptr;

0 commit comments

Comments
 (0)