forked from avin2/SensorKinect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXnBufferPool.cpp
More file actions
243 lines (192 loc) · 6.94 KB
/
XnBufferPool.cpp
File metadata and controls
243 lines (192 loc) · 6.94 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
/****************************************************************************
* *
* PrimeSense Sensor 5.x Alpha *
* Copyright (C) 2011 PrimeSense Ltd. *
* *
* This file is part of PrimeSense Sensor. *
* *
* PrimeSense Sensor is free software: you can redistribute it and/or modify*
* it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* PrimeSense Sensor is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with PrimeSense Sensor. If not, see <http://www.gnu.org/licenses/>.*
* *
****************************************************************************/
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include "XnBufferPool.h"
//---------------------------------------------------------------------------
// Code
//---------------------------------------------------------------------------
XnBufferPool::XnBufferPool() :
m_nBufferSize(0),
m_nNextBufferID(0),
m_hLock(NULL),
m_dump(NULL)
{}
XnBufferPool::~XnBufferPool()
{
XnBufferPool::Free();
}
XnStatus XnBufferPool::Init(XnUInt32 nBufferSize)
{
XnStatus nRetVal = XN_STATUS_OK;
m_dump = xnDumpFileOpen("BufferPool", "bufferpool_%x.txt", this);
nRetVal = xnOSCreateCriticalSection(&m_hLock);
XN_IS_STATUS_OK(nRetVal);
// allocate buffers
xnDumpFileWriteString(m_dump, "Initializing with size %u\n", nBufferSize);
nRetVal = ChangeBufferSize(nBufferSize);
XN_IS_STATUS_OK(nRetVal);
return (XN_STATUS_OK);
}
void XnBufferPool::Free()
{
if (m_hLock != NULL)
{
xnOSCloseCriticalSection(&m_hLock);
m_hLock = NULL;
}
}
XnStatus XnBufferPool::ChangeBufferSize(XnUInt32 nBufferSize)
{
XnStatus nRetVal = XN_STATUS_OK;
xnDumpFileWriteString(m_dump, "Changing buffer size to %d\n", nBufferSize);
xnOSEnterCriticalSection(&m_hLock);
m_nBufferSize = nBufferSize;
// first free old ones
FreeAll(FALSE);
nRetVal = AllocateBuffers(nBufferSize);
if (nRetVal != XN_STATUS_OK)
{
xnOSLeaveCriticalSection(&m_hLock);
return (nRetVal);
}
xnDumpFileWriteString(m_dump, "Buffers were allocated\n");
xnOSLeaveCriticalSection(&m_hLock);
return (XN_STATUS_OK);
}
void XnBufferPool::FreeAll(XnBool bForceDestroyOfLockedBuffers)
{
// free existing buffers
xnDumpFileWriteString(m_dump, "freeing existing buffers...\n");
XnBuffersList::Iterator it = m_AllBuffers.Begin();
while (it != m_AllBuffers.End())
{
XnBuffersList::Iterator currIt = it;
// first advance (we might remove this item)
++it;
// now check current
XnBufferInPool* pBuffer = *currIt;
// check if item is in free list (or we're forcing deletion)
if (bForceDestroyOfLockedBuffers || pBuffer->m_nRefCount == 0)
{
xnDumpFileWriteString(m_dump, "\tdestroying buffer %u\n", pBuffer->m_nID);
DestroyBuffer((void*)pBuffer->GetData());
XN_DELETE(pBuffer);
m_AllBuffers.Remove(currIt);
}
else
{
// we can't free it, cause it's still locked. instead, mark it for deletion
xnDumpFileWriteString(m_dump, "\tBuffer %u can't be destroyed right now (locked). Just mark it for destruction.\n", pBuffer->m_nID);
pBuffer->m_bDestroy = TRUE;
}
}
m_FreeBuffers.Clear();
xnDumpFileWriteString(m_dump, "Buffers were freed\n");
}
XnStatus XnBufferPool::AddNewBuffer(void* pBuffer, XnUInt32 nSize)
{
XnBufferInPool* pBufferInPool;
XN_VALIDATE_NEW(pBufferInPool, XnBufferInPool);
xnOSEnterCriticalSection(&m_hLock);
pBufferInPool->m_nID = m_nNextBufferID++;
pBufferInPool->SetExternalBuffer((XnUChar*)pBuffer, nSize);
xnDumpFileWriteString(Dump(), "\tAdd new buffer %u with size %u at 0x%p\n", pBufferInPool->m_nID, nSize, pBuffer);
// add it to free list
m_AllBuffers.AddLast(pBufferInPool);
m_FreeBuffers.AddLast(pBufferInPool);
xnOSLeaveCriticalSection(&m_hLock);
return XN_STATUS_OK;
}
XnStatus XnBufferPool::GetBuffer(XnBuffer** ppBuffer)
{
XnStatus nRetVal = XN_STATUS_OK;
xnOSEnterCriticalSection(&m_hLock);
XnBuffersList::Iterator it = m_FreeBuffers.Begin();
if (it == m_FreeBuffers.End())
{
xnOSLeaveCriticalSection(&m_hLock);
return XN_STATUS_ALLOC_FAILED;
}
XnBufferInPool* pBuffer = *it;
// remove from list
nRetVal = m_FreeBuffers.Remove(it);
if (nRetVal != XN_STATUS_OK)
{
xnOSLeaveCriticalSection(&m_hLock);
return XN_STATUS_ALLOC_FAILED;
}
pBuffer->m_nRefCount = 1;
xnDumpFileWriteString(m_dump, "%u taken from pool\n", pBuffer->m_nID);
xnOSLeaveCriticalSection(&m_hLock);
*ppBuffer = pBuffer;
return (XN_STATUS_OK);
}
void XnBufferPool::AddRef(XnBuffer* pBuffer)
{
if (pBuffer == NULL)
{
XN_ASSERT(FALSE);
return;
}
xnOSEnterCriticalSection(&m_hLock);
XnBufferInPool* pBufferInPool = (XnBufferInPool*)pBuffer;
++pBufferInPool->m_nRefCount;
xnDumpFileWriteString(m_dump, "%u add ref (%d)\n", pBufferInPool->m_nID, pBufferInPool->m_nRefCount);
xnOSLeaveCriticalSection(&m_hLock);
}
void XnBufferPool::DecRef(XnBuffer* pBuffer)
{
if (pBuffer == NULL)
{
XN_ASSERT(FALSE);
return;
}
XnBufferInPool* pBufInPool = (XnBufferInPool*)pBuffer;
xnOSEnterCriticalSection(&m_hLock);
xnDumpFileWriteString(m_dump, "%u dec ref (%d)", pBufInPool->m_nID, pBufInPool->m_nRefCount-1);
if (--pBufInPool->m_nRefCount == 0)
{
if (pBufInPool->m_bDestroy)
{
// remove it from all buffers pool
XnBuffersList::ConstIterator it = m_AllBuffers.Find(pBufInPool);
XN_ASSERT(it != m_AllBuffers.End());
m_AllBuffers.Remove(it);
// and free it
DestroyBuffer((void*)pBufInPool->GetData());
xnDumpFileWriteString(m_dump, "destroy!\n");
}
else
{
// return it to free buffers list
m_FreeBuffers.AddLast(pBufInPool);
xnDumpFileWriteString(m_dump, "return to pool!\n");
}
}
else
{
xnDumpFileWriteString(m_dump, "\n");
}
xnOSLeaveCriticalSection(&m_hLock);
}