File: XnNodeManager.cpp

package info (click to toggle)
openni 1.5.4.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 44,844 kB
  • sloc: cpp: 116,706; ansic: 58,777; sh: 10,287; cs: 7,698; java: 7,402; python: 1,541; makefile: 492; xml: 167
file content (179 lines) | stat: -rw-r--r-- 5,837 bytes parent folder | download | duplicates (7)
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
/****************************************************************************
*                                                                           *
*  OpenNI 1.x Alpha                                                         *
*  Copyright (C) 2011 PrimeSense Ltd.                                       *
*                                                                           *
*  This file is part of OpenNI.                                             *
*                                                                           *
*  OpenNI 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.                                      *
*                                                                           *
*  OpenNI 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 OpenNI. If not, see <http://www.gnu.org/licenses/>.           *
*                                                                           *
****************************************************************************/
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include <XnOS.h>
#include <XnList.h>
#include <XnNode.h>
#include "XnNodeManager.h"

//---------------------------------------------------------------------------
// Code
//---------------------------------------------------------------------------

const XnUInt32 XnNodeManager::nInitialSize = 20;

XnNodeManager::XnNodeManager()
{
	xnOSCreateCriticalSection(&m_hCriticalSection);
	//TODO: Handle critical section creation failure

	m_nCurrentAvailability = 0;
	m_nCurrentCapacity = 0;
	m_nCurrentOccupancy = 0;
	m_pFirstAvailable = NULL;

	m_eInitializationState = XN_NM_INIT_STATE_CREATE_INTERNAL_LIST;
	m_pAllNodes = XN_NEW(XnList, this);
	if (m_pAllNodes == NULL)
	{
		// OZOZ: Allocation failed in ctor...
	}

	m_eInitializationState = XN_NM_INIT_STATE_CREATE_FIRST_LINK;
	XnStatus rc = Resize(nInitialSize);
	if (rc != XN_STATUS_OK)
	{
		// OZOZ: Allocation failed in ctor...
	}

	m_eInitializationState = XN_NM_INIT_STATE_DONE;
}

XnNodeManager::~XnNodeManager()
{
	while (m_pAllNodes->begin() != m_pAllNodes->end())
	{
		XnValue RemovedValue;
		m_pAllNodes->Remove(m_pAllNodes->rbegin(), RemovedValue);
		XnNode* pNodes = (XnNode*)RemovedValue;
		XN_DELETE_ARR(pNodes);
	}

	XN_DELETE(m_pAllNodes);

	xnOSCloseCriticalSection(&m_hCriticalSection);
}

XnNodeManager* XnNodeManager::GetInstance()
{
	// NOTE: we *never* free the NodeManager instance, as it should always exist. Global variables dtors
	// might still use lists and hashs to do their work.
	// Instead, we let the OS free its memory and resources.
	static XnNodeManager* pNM = XN_NEW(XnNodeManager);
	return pNM;
}

XnNode* XnNodeManager::Allocate()
{
	XnNode* pResult = NULL;

	xnOSEnterCriticalSection(&m_hCriticalSection);

	if (m_eInitializationState == XN_NM_INIT_STATE_CREATE_INTERNAL_LIST)
	{
		pResult = &(m_InitialNodes[0]);
	}
	else if (m_eInitializationState == XN_NM_INIT_STATE_CREATE_FIRST_LINK)
	{
		pResult = &(m_InitialNodes[1]);
	}

	if (pResult != NULL)
	{
		xnOSLeaveCriticalSection(&m_hCriticalSection);
		return pResult;
	}

	// Check if resize is in order
	if (m_nCurrentAvailability == 1 ||
		XnFloat(m_nCurrentOccupancy)/m_nCurrentCapacity > 0.75)
	{
		XnStatus rc = Resize((XnUInt32)(m_nCurrentCapacity*0.5));
		if (rc != XN_STATUS_OK && m_nCurrentAvailability == 1)
		{
			// Couldn't resize, and there is only one node available
			// We'll need that one node to add to the list, to contain all new nodes
			// So there are actually no available nodes....
			xnOSLeaveCriticalSection(&m_hCriticalSection);
			return NULL;
		}
	}

	m_nCurrentOccupancy++;
	m_nCurrentAvailability--;

	// Return the first available
	XnNode* pOut = m_pFirstAvailable;
	m_pFirstAvailable = m_pFirstAvailable->Next();
	pOut->Next() = NULL;

	xnOSLeaveCriticalSection(&m_hCriticalSection);

	return pOut;
}

void XnNodeManager::Deallocate(XnNode *pNode)
{
	xnOSEnterCriticalSection(&m_hCriticalSection);

	m_nCurrentOccupancy--;
	m_nCurrentAvailability++;

	// Add the returned node as the first available
	pNode->Next() = m_pFirstAvailable;
	pNode->Previous() = NULL;
	m_pFirstAvailable = pNode;

	xnOSLeaveCriticalSection(&m_hCriticalSection);
}

XnStatus XnNodeManager::Resize(XnUInt32 nDeltaSize)
{
	// Allocate new nodes
	XnNode* pNewNodes = XN_NEW_ARR(XnNode, nDeltaSize);
	if (pNewNodes == NULL)
	{
		return XN_STATUS_ALLOC_FAILED;
	}

	// Connect them to each other
	for (XnUInt32 i = 0; i < nDeltaSize-1; ++i)
	{
		pNewNodes[i].Next() = &(pNewNodes[i+1]);
		pNewNodes[i].Previous() = NULL;
	}

	pNewNodes[nDeltaSize-1].Previous() = NULL;

	m_nCurrentAvailability += nDeltaSize;
	m_nCurrentCapacity += nDeltaSize;

	// Add the new nodes to the list
	m_pAllNodes->AddLast(XnValue(pNewNodes));
	// Replace first available with the first from this batch
	pNewNodes[nDeltaSize-1].Next() = m_pFirstAvailable;
	m_pFirstAvailable = &(pNewNodes[0]);

	return XN_STATUS_OK;
}