@@ -33,9 +33,12 @@ public sealed unsafe class UnmanagedArray<T> : IUnmanagedArray<T>, IEnumerable<T
3333 private IntPtr _buffer ;
3434
3535 /// <summary>
36- /// The capacity
36+ /// The capacity of the current Array.
3737 /// </summary>
38- private int _capacity ;
38+ /// <value>
39+ /// The capacity.
40+ /// </value>
41+ public int Capacity { get ; private set ; }
3942
4043 /// <summary>
4144 /// The disposed
@@ -53,7 +56,7 @@ public sealed unsafe class UnmanagedArray<T> : IUnmanagedArray<T>, IEnumerable<T
5356 /// <param name="size">The size.</param>
5457 public UnmanagedArray ( int size )
5558 {
56- _capacity = size ;
59+ Capacity = size ;
5760 Length = size ;
5861 _buffer = Marshal . AllocHGlobal ( size * sizeof ( T ) ) ;
5962 _ptr = ( T * ) _buffer ;
@@ -123,7 +126,7 @@ public void RemoveAt(int index)
123126 Buffer . MemoryCopy (
124127 _ptr + index + 1 ,
125128 _ptr + index ,
126- ( _capacity - index - 1 ) * sizeof ( T ) ,
129+ ( Capacity - index - 1 ) * sizeof ( T ) ,
127130 elementsToShift * sizeof ( T ) ) ;
128131 }
129132
@@ -162,15 +165,32 @@ IEnumerator IEnumerable.GetEnumerator()
162165 /// <param name="newSize">The new size of the array.</param>
163166 public void Resize ( int newSize )
164167 {
165- _buffer = Marshal . ReAllocHGlobal ( _buffer , ( IntPtr ) ( newSize * sizeof ( T ) ) ) ;
166- _ptr = ( T * ) _buffer ;
167- _capacity = newSize ;
168+ if ( newSize < 0 )
169+ throw new ArgumentOutOfRangeException ( nameof ( newSize ) ) ;
170+
171+ if ( newSize == Capacity )
172+ return ;
173+
174+ var newBuffer = Marshal . ReAllocHGlobal ( _buffer , ( IntPtr ) ( newSize * sizeof ( T ) ) ) ;
175+ var newPtr = ( T * ) newBuffer ;
176+
177+ if ( newSize > Capacity )
178+ {
179+ var newRegion = new Span < T > ( newPtr + Capacity , newSize - Capacity ) ;
180+ newRegion . Clear ( ) ;
181+ }
182+
183+ _buffer = newBuffer ;
184+ _ptr = newPtr ;
185+ Capacity = newSize ;
186+
168187 if ( Length > newSize )
169188 {
170189 Length = newSize ;
171190 }
172191 }
173192
193+
174194 /// <inheritdoc />
175195 /// <summary>
176196 /// Clears the array by setting all elements to zero.
@@ -228,7 +248,7 @@ public void InsertAt(int index, T value, int count = 1)
228248 Buffer . MemoryCopy (
229249 _ptr + index ,
230250 _ptr + index + count ,
231- ( _capacity - index - count ) * sizeof ( T ) ,
251+ ( Capacity - index - count ) * sizeof ( T ) ,
232252 elementsToShift * sizeof ( T ) ) ;
233253 }
234254
@@ -247,12 +267,12 @@ public void InsertAt(int index, T value, int count = 1)
247267 /// <param name="minCapacity">The minimum capacity.</param>
248268 public void EnsureCapacity ( int minCapacity )
249269 {
250- if ( minCapacity <= _capacity )
270+ if ( minCapacity <= Capacity )
251271 {
252272 return ;
253273 }
254274
255- var newCapacity = _capacity == 0 ? 4 : _capacity ;
275+ var newCapacity = Capacity == 0 ? 4 : Capacity ;
256276 while ( newCapacity < minCapacity )
257277 {
258278 newCapacity *= 2 ;
@@ -299,7 +319,7 @@ private void Dispose(bool disposing)
299319 _buffer = IntPtr . Zero ;
300320 _ptr = null ;
301321 Length = 0 ;
302- _capacity = 0 ;
322+ Capacity = 0 ;
303323 }
304324
305325 _disposed = true ;
0 commit comments