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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
#if !VALIDATE
using System.Diagnostics;
#endif
namespace Vulkan
{
public unsafe class NativeList<T> : IEnumerable<T>, IDisposable where T : struct
{
private byte* _dataPtr;
private uint _elementCapacity;
private uint _count;
public const int DefaultCapacity = 4;
private const float GrowthFactor = 2f;
private static readonly uint s_elementByteSize = InitializeTypeSize();
public NativeList() : this(DefaultCapacity) { }
public NativeList(uint capacity)
{
Allocate(capacity);
}
public NativeList(uint capacity, uint count)
{
Allocate(capacity);
Count = count;
}
public NativeList(NativeList<T> existingList)
{
Allocate(existingList._elementCapacity);
long size = existingList._count * s_elementByteSize;
System.Buffer.MemoryCopy (_dataPtr, existingList._dataPtr, size, size);
//Unsafe.CopyBlock(_dataPtr, existingList._dataPtr, existingList._count * s_elementByteSize);
}
public IntPtr Data
{
get
{
ThrowIfDisposed();
return new IntPtr(_dataPtr);
}
}
public uint Count
{
get
{
ThrowIfDisposed();
return _count;
}
set
{
ThrowIfDisposed();
if (value > _elementCapacity)
{
uint newLements = value - Count;
CoreResize(value);
Unsafe.InitBlock(_dataPtr + _count * s_elementByteSize, 0, newLements * s_elementByteSize);
}
_count = value;
}
}
public ref T this[uint index]
{
get
{
ThrowIfDisposed();
#if VALIDATE
if (index >= _count)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
#else
Debug.Assert(index < _count);
#endif
return ref Unsafe.AsRef<T>(_dataPtr + index * s_elementByteSize);
}
}
public ref T this[int index]
{
get
{
ThrowIfDisposed();
#if VALIDATE
if (index < 0 || index >= _count)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
#else
Debug.Assert(index >= 0 && index < _count);
#endif
return ref Unsafe.AsRef<T>(_dataPtr + index * s_elementByteSize);
}
}
public ReadOnlyNativeListView<T> GetReadOnlyView()
{
ThrowIfDisposed();
return new ReadOnlyNativeListView<T>(this, 0, _count);
}
public ReadOnlyNativeListView<T> GetReadOnlyView(uint start, uint count)
{
ThrowIfDisposed();
#if VALIDATE
if (start + count > _count)
{
throw new ArgumentOutOfRangeException();
}
#else
Debug.Assert(start + count <= _count);
#endif
return new ReadOnlyNativeListView<T>(this, start, count);
}
public View<ViewType> GetView<ViewType>() where ViewType : struct
{
ThrowIfDisposed();
return new View<ViewType>(this);
}
public bool IsDisposed => _dataPtr == null;
public void Add(ref T item)
{
ThrowIfDisposed();
if (_count == _elementCapacity)
{
CoreResize((uint)(_elementCapacity * GrowthFactor));
}
Unsafe.Copy(_dataPtr + _count * s_elementByteSize, ref item);
_count += 1;
}
public void Add(T item)
{
ThrowIfDisposed();
if (_count == _elementCapacity)
{
CoreResize((uint)(_elementCapacity * GrowthFactor));
}
Unsafe.Write(_dataPtr + _count * s_elementByteSize, item);
_count += 1;
}
public void Add(void* data, uint numElements)
{
ThrowIfDisposed();
uint needed = _count + numElements;
if (numElements > _elementCapacity)
{
CoreResize((uint)(needed * GrowthFactor));
}
Unsafe.CopyBlock(_dataPtr + _count * s_elementByteSize, data, numElements * s_elementByteSize);
_count += numElements;
}
public bool Remove(ref T item)
{
ThrowIfDisposed();
bool result = IndexOf(ref item, out uint index);
if (result)
{
CoreRemoveAt(index);
}
return result;
}
public bool Remove(T item) => Remove(ref item);
public void RemoveAt(uint index)
{
ThrowIfDisposed();
#if VALIDATE
if (index >= _count)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
#else
Debug.Assert(index < _count);
#endif
CoreRemoveAt(index);
}
public void Clear()
{
ThrowIfDisposed();
_count = 0;
}
public bool IndexOf(ref T item, out uint index)
{
ThrowIfDisposed();
byte* itemPtr = (byte*)Unsafe.AsPointer(ref item);
for (index = 0; index < _count; index++)
{
byte* ptr = _dataPtr + index * s_elementByteSize;
if (Equals(ptr, itemPtr, s_elementByteSize))
{
return true;
}
}
return false;
}
public bool IndexOf(T item, out uint index)
{
ThrowIfDisposed();
byte* itemPtr = (byte*)Unsafe.AsPointer(ref item);
for (index = 0; index < _count; index++)
{
byte* ptr = _dataPtr + index * s_elementByteSize;
if (Equals(ptr, itemPtr, s_elementByteSize))
{
return true;
}
}
return false;
}
public IntPtr GetAddress(uint index)
{
ThrowIfDisposed();
#if VALIDATE
if (index >= _count)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
#else
Debug.Assert(index < _count);
#endif
return new IntPtr(_dataPtr + (index * s_elementByteSize));
}
public void Resize(uint elementCount)
{
ThrowIfDisposed();
CoreResize(elementCount);
if (_elementCapacity < _count)
{
_count = _elementCapacity;
}
}
private static uint InitializeTypeSize()
{
#if VALIDATE
// TODO: DHetermine if the structure type contains references and throw if it does.
// https://github.com/dotnet/corefx/issues/14047
#endif
return (uint)Unsafe.SizeOf<T>();
}
private void CoreResize(uint elementCount)
{
_dataPtr = (byte*)Marshal.ReAllocHGlobal(new IntPtr(_dataPtr), (IntPtr)(elementCount * s_elementByteSize));
_elementCapacity = elementCount;
}
private void Allocate(uint elementCount)
{
_dataPtr = (byte*)Marshal.AllocHGlobal((int)(elementCount * s_elementByteSize));
_elementCapacity = elementCount;
}
private bool Equals(byte* ptr, byte* itemPtr, uint s_elementByteSize)
{
for (int i = 0; i < s_elementByteSize; i++)
{
if (ptr[i] != itemPtr[i])
{
return false;
}
}
return true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void CoreRemoveAt(uint index)
{
Unsafe.CopyBlock(_dataPtr + index * s_elementByteSize, _dataPtr + (_count - 1) * s_elementByteSize, s_elementByteSize);
_count -= 1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#if !VALIDATE
[Conditional("DEBUG")]
#endif
private void ThrowIfDisposed()
{
#if VALIDATE
if (_dataPtr == null)
{
throw new ObjectDisposedException(nameof(Data));
}
#else
Debug.Assert(_dataPtr != null, "NativeList is disposed.");
#endif
}
public void Dispose()
{
ThrowIfDisposed();
Marshal.FreeHGlobal(new IntPtr(_dataPtr));
_dataPtr = null;
}
#if DEBUG
~NativeList()
{
if (_dataPtr != null)
{
Debug.WriteLine($"A NativeList<{typeof(T).Name}> was not properly disposed.");
Dispose();
}
}
#endif
public Enumerator GetEnumerator()
{
ThrowIfDisposed();
return new Enumerator(_dataPtr, _count);
}
IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public struct Enumerator : IEnumerator<T>
{
private byte* _basePtr;
private uint _count;
private uint _currentIndex;
private T _current;
public Enumerator(byte* basePtr, uint count)
{
_basePtr = basePtr;
_count = count;
_currentIndex = 0;
_current = default(T);
}
public T Current => _current;
object IEnumerator.Current => Current;
public bool MoveNext()
{
if (_currentIndex != _count)
{
_current = Unsafe.Read<T>(_basePtr + _currentIndex * s_elementByteSize);
_currentIndex += 1;
return true;
}
return false;
}
public void Reset()
{
_current = default(T);
_currentIndex = 0;
}
public void Dispose() { }
}
public struct View<ViewType> : IEnumerable<ViewType> where ViewType : struct
{
private static readonly uint s_elementByteSize = (uint)Unsafe.SizeOf<ViewType>();
private readonly NativeList<T> _parent;
public View(NativeList<T> parent)
{
_parent = parent;
}
public uint Count => (_parent.Count * NativeList<T>.s_elementByteSize) / s_elementByteSize;
public ViewType this[uint index]
{
get
{
#if VALIDATE
if (index >= Count)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
#else
Debug.Assert(index < Count);
#endif
return Unsafe.Read<ViewType>(_parent._dataPtr + index * s_elementByteSize);
}
}
public Enumerator GetEnumerator() => new Enumerator(this);
IEnumerator<ViewType> IEnumerable<ViewType>.GetEnumerator() => GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public struct Enumerator : IEnumerator<ViewType>
{
private View<ViewType> _view;
private uint _currentIndex;
private ViewType _current;
public Enumerator(View<ViewType> view)
{
_view = view;
_currentIndex = 0;
_current = default(ViewType);
}
public ViewType Current => _view[_currentIndex];
object IEnumerator.Current => Current;
public bool MoveNext()
{
if (_currentIndex != _view.Count)
{
_current = _view[_currentIndex];
_currentIndex += 1;
return true;
}
return false;
}
public void Reset()
{
_currentIndex = 0;
_current = default(ViewType);
}
public void Dispose() { }
}
}
}
public struct ReadOnlyNativeListView<T> : IEnumerable<T> where T : struct
{
private readonly NativeList<T> _list;
private readonly uint _start;
public readonly uint Count;
public ReadOnlyNativeListView(NativeList<T> list, uint start, uint count)
{
_list = list;
_start = start;
Count = count;
}
public T this[uint index]
{
get
{
#if VALIDATE
if (index >= Count)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
#else
Debug.Assert(index < Count);
#endif
return _list[index + _start];
}
}
public T this[int index]
{
get
{
#if VALIDATE
if (index < 0 || index >= Count)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
#else
Debug.Assert(index >= 0 && index < Count);
#endif
return _list[(uint)index + _start];
}
}
public Enumerator GetEnumerator() => new Enumerator(this);
IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public struct Enumerator : IEnumerator<T>
{
private ReadOnlyNativeListView<T> _view;
private uint _currentIndex;
private T _current;
public Enumerator(ReadOnlyNativeListView<T> view)
{
_view = view;
_currentIndex = view._start;
_current = default(T);
}
public T Current => _view[_currentIndex];
object IEnumerator.Current => Current;
public bool MoveNext()
{
if (_currentIndex != _view._start + _view.Count)
{
_current = _view[_currentIndex];
_currentIndex += 1;
return true;
}
return false;
}
public void Reset()
{
_currentIndex = _view._start;
_current = default(T);
}
public void Dispose() { }
}
}
}
|