Skip to content

Commit c623978

Browse files
Modified SwapBufferCollection to swap handles directly instead of the silly indices thing I had.
Signed-off-by: Aditya Gaddam <adityagaddam@gmail.com> (LostInCake)
1 parent b80d969 commit c623978

File tree

1 file changed

+8
-21
lines changed

1 file changed

+8
-21
lines changed

wrappers/csharp/src/lib/SwapBufferCollection.cs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,12 @@ namespace freenect
3939
///
4040
public class SwapBufferCollection<T> : IDisposable
4141
{
42-
/// <summary>
43-
/// This small array takes care of mapping
44-
/// where the data is and where their swapped indices are
45-
/// </summary>
46-
private int[] bufferIndicesMap;
47-
42+
4843
/// <summary>
4944
/// GC handles for each of the buffers in this collection
5045
/// </summary>
5146
private GCHandle[] bufferHandles;
5247

53-
/// <summary>
54-
/// Buffers for data
55-
/// </summary>
56-
private T[][] buffers;
57-
5848
/// <summary>
5949
/// Gets the buffer at the specified index
6050
/// </summary>
@@ -65,7 +55,7 @@ public T[] this[int index]
6555
{
6656
get
6757
{
68-
return this.buffers[this.bufferIndicesMap[index]];
58+
return (T[])this.bufferHandles[index].Target;
6959
}
7060
}
7161

@@ -104,14 +94,11 @@ public SwapBufferCollection(int numBuffers, int bufferSize)
10494
this.Count = numBuffers;
10595

10696
// Allocate buffers and save pinned handles to them
107-
this.bufferIndicesMap = new int[numBuffers];
10897
this.bufferHandles = new GCHandle[numBuffers];
109-
this.buffers = new T[numBuffers][];
11098
for(int i = 0; i < numBuffers; i++)
11199
{
112-
this.bufferIndicesMap[i] = i;
113-
this.buffers[i] = new T[bufferSize];
114-
this.bufferHandles[i] = GCHandle.Alloc(this.buffers[i], GCHandleType.Pinned);
100+
var buffer = new T[bufferSize];
101+
this.bufferHandles[i] = GCHandle.Alloc(buffer, GCHandleType.Pinned);
115102
}
116103
}
117104

@@ -127,7 +114,7 @@ public SwapBufferCollection(int numBuffers, int bufferSize)
127114
/// </returns>
128115
public IntPtr GetHandle(int index)
129116
{
130-
return this.bufferHandles[this.bufferIndicesMap[index]].AddrOfPinnedObject();
117+
return this.bufferHandles[index].AddrOfPinnedObject();
131118
}
132119

133120
/// <summary>
@@ -154,9 +141,9 @@ public void Swap(int index1, int index2)
154141
}
155142

156143
// Swap
157-
int tmp = this.bufferIndicesMap[index1];
158-
this.bufferIndicesMap[index1] = this.bufferIndicesMap[index2];
159-
this.bufferIndicesMap[index2] = tmp;
144+
var tmp = this.bufferHandles[index1];
145+
this.bufferHandles[index1] = this.bufferHandles[index2];
146+
this.bufferHandles[index2] = tmp;
160147
}
161148

162149
/// <summary>

0 commit comments

Comments
 (0)