forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeArray.cs
More file actions
264 lines (213 loc) · 7.1 KB
/
Copy pathNativeArray.cs
File metadata and controls
264 lines (213 loc) · 7.1 KB
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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Unity.Burst;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine.Internal;
namespace Unity.Collections
{
public enum NativeArrayOptions
{
UninitializedMemory = 0,
ClearMemory = 1
}
[StructLayout(LayoutKind.Sequential)]
[NativeContainer]
[NativeContainerSupportsMinMaxWriteRestriction]
[NativeContainerSupportsDeallocateOnJobCompletion]
[DebuggerDisplay("Length = {Length}")]
[DebuggerTypeProxy(typeof(NativeArrayDebugView < >))]
unsafe public struct NativeArray<T> : IDisposable, IEnumerable<T> where T : struct
{
[NativeDisableUnsafePtrRestriction]
internal void* m_Buffer;
internal int m_Length;
internal Allocator m_AllocatorLabel;
public NativeArray(int length, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory)
{
Allocate(length, allocator, out this);
if ((options & NativeArrayOptions.ClearMemory) == NativeArrayOptions.ClearMemory)
UnsafeUtility.MemClear(m_Buffer, (long)Length * (long)UnsafeUtility.SizeOf<T>());
}
public NativeArray(T[] array, Allocator allocator)
{
Allocate(array.Length, allocator, out this);
CopyFrom(array);
}
public NativeArray(NativeArray<T> array, Allocator allocator)
{
Allocate(array.Length, allocator, out this);
CopyFrom(array);
}
private static void Allocate(int length, Allocator allocator, out NativeArray<T> array)
{
long totalSize = UnsafeUtility.SizeOf<T>() * (long)length;
array.m_Buffer = UnsafeUtility.Malloc(totalSize, UnsafeUtility.AlignOf<T>(), allocator);
array.m_Length = length;
array.m_AllocatorLabel = allocator;
}
public int Length { get { return m_Length; } }
[BurstDiscard]
internal static void IsBlittableAndThrow()
{
if (!UnsafeUtility.IsBlittable<T>())
throw new ArgumentException(string.Format("{0} used in NativeArray<{0}> must be blittable", typeof(T)));
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
private unsafe void CheckElementReadAccess(int index)
{
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
private unsafe void CheckElementWriteAccess(int index)
{
}
public unsafe T this[int index]
{
get
{
CheckElementReadAccess(index);
return UnsafeUtility.ReadArrayElement<T>(m_Buffer, index);
}
[WriteAccessRequired]
set
{
CheckElementWriteAccess(index);
UnsafeUtility.WriteArrayElement(m_Buffer, index, value);
}
}
public bool IsCreated
{
get { return m_Buffer != null; }
}
[WriteAccessRequired]
public void Dispose()
{
UnsafeUtility.Free(m_Buffer, m_AllocatorLabel);
m_Buffer = null;
m_Length = 0;
}
[WriteAccessRequired]
public void CopyFrom(T[] array)
{
for (var i = 0; i < Length; i++)
UnsafeUtility.WriteArrayElement(m_Buffer, i, array[i]);
}
[WriteAccessRequired]
public void CopyFrom(NativeArray<T> array)
{
array.CopyTo(this);
}
public void CopyTo(T[] array)
{
for (var i = 0; i < Length; i++)
array[i] = UnsafeUtility.ReadArrayElement<T>(m_Buffer, i);
}
public void CopyTo(NativeArray<T> array)
{
UnsafeUtility.MemCpy(array.m_Buffer, m_Buffer, (long)Length * (long)UnsafeUtility.SizeOf<T>());
}
public T[] ToArray()
{
var array = new T[Length];
CopyTo(array);
return array;
}
public Enumerator GetEnumerator()
{
return new Enumerator(ref this);
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return new Enumerator(ref this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
[ExcludeFromDocs]
public struct Enumerator : IEnumerator<T>
{
private NativeArray<T> m_Array;
private int m_Index;
public Enumerator(ref NativeArray<T> array)
{
m_Array = array;
m_Index = -1;
}
public void Dispose()
{
}
public bool MoveNext()
{
m_Index++;
return m_Index < m_Array.Length;
}
public void Reset()
{
m_Index = -1;
}
public T Current
{
get
{
// Let NativeArray indexer check for out of range.
return m_Array[m_Index];
}
}
object IEnumerator.Current
{
get { return Current; }
}
}
}
/// <summary>
/// DebuggerTypeProxy for <see cref="NativeArray{T}"/>
/// </summary>
internal sealed class NativeArrayDebugView<T> where T : struct
{
private NativeArray<T> m_Array;
public NativeArrayDebugView(NativeArray<T> array)
{
m_Array = array;
}
public T[] Items
{
get { return m_Array.ToArray(); }
}
}
}
namespace Unity.Collections.LowLevel.Unsafe
{
public static class NativeArrayUnsafeUtility
{
/// Internal method used typically by other systems to provide a view on them.
/// The caller is still the owner of the data.
unsafe public static NativeArray<T> ConvertExistingDataToNativeArray<T>(void* dataPointer, int length, Allocator allocator) where T : struct
{
var newArray = new NativeArray<T>
{
m_Buffer = dataPointer,
m_Length = length,
m_AllocatorLabel = allocator,
};
return newArray;
}
public unsafe static void* GetUnsafePtr<T>(this NativeArray<T> nativeArray) where T : struct
{
return nativeArray.m_Buffer;
}
public unsafe static void* GetUnsafeReadOnlyPtr<T>(this NativeArray<T> nativeArray) where T : struct
{
return nativeArray.m_Buffer;
}
unsafe public static void* GetUnsafeBufferPointerWithoutChecks<T>(NativeArray<T> nativeArray) where T : struct
{
return nativeArray.m_Buffer;
}
}
}