forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnsafeRingQueue.cs
More file actions
80 lines (68 loc) · 1.75 KB
/
Copy pathUnsafeRingQueue.cs
File metadata and controls
80 lines (68 loc) · 1.75 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
using System;
using Unity.Burst;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;
// To be replaced by official versions in Unity.Collections when available.
public unsafe struct UnsafeRingQueue<T> : IDisposable
where T : unmanaged
{
[NativeDisableUnsafePtrRestriction]
T* m_Buffer;
Allocator m_Allocator;
int m_Capacity;
int m_StartIndex;
int m_EndIndex;
public int Count
{
get
{
var dist = m_EndIndex - m_StartIndex;
return (dist < 0) ? ((m_Capacity - 1) + dist) : dist;
}
}
public UnsafeRingQueue(int capacity, Allocator allocator)
{
m_Buffer = (T*)UnsafeUtility.Malloc(capacity * UnsafeUtility.SizeOf<T>(), 16, allocator);
m_Allocator = allocator;
m_StartIndex = 0;
m_EndIndex = 0;
m_Capacity = capacity;
}
public void Enqueue(T value)
{
m_Buffer[m_EndIndex] = value;
m_EndIndex = (m_EndIndex+1) % m_Capacity;
}
public T Dequeue()
{
T value = m_Buffer[m_StartIndex];
m_StartIndex = (m_StartIndex+1) % m_Capacity;
return value;
}
void Deallocate()
{
if (m_Buffer != null)
UnsafeUtility.Free(m_Buffer, m_Allocator);
}
public void Dispose()
{
Deallocate();
m_Buffer = null;
}
public JobHandle Dispose(JobHandle inputDeps)
{
var jobHandle = new DisposeJob { Container = this }.Schedule(inputDeps);
m_Buffer = null;
return jobHandle;
}
[BurstCompile]
struct DisposeJob : IJob
{
public UnsafeRingQueue<T> Container;
public void Execute()
{
Container.Deallocate();
}
}
}