File: CQtManagedRestrictedModificationMapData.cpp

package info (click to toggle)
konclude 0.7.0%2B1137~dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 48,824 kB
  • sloc: cpp: 378,238; xml: 116,067; makefile: 38; sh: 8; ansic: 3
file content (277 lines) | stat: -rw-r--r-- 10,089 bytes parent folder | download | duplicates (2)
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
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 *		Copyright (C) 2013-2015, 2019 by the Konclude Developer Team.
 *
 *		This file is part of the reasoning system Konclude.
 *		For details and support, see <http://konclude.com/>.
 *
 *		Konclude is free software: you can redistribute it and/or modify
 *		it under the terms of version 3 of the GNU Lesser General Public
 *		License (LGPLv3) as published by the Free Software Foundation.
 *
 *		Konclude 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 Konclude. If not, see <http://www.gnu.org/licenses/>.
 *
 * 		This file incorporates work covered by the following copyright and  
 * 		permission notice (to be compatible with LGPLv2.1,
 *		this code is available/released under GPLv3, 
 * 		see https://www.gnu.org/licenses/gpl-faq.html#AllCompatibility):
 *
 *
 * 			Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
 * 			Contact: http://www.qt-project.org/legal
 *
 * 			This file is part of the QtCore module of the Qt Toolkit.
 *
 * 			$QT_BEGIN_LICENSE:LGPL$
 * 			Commercial License Usage
 * 			Licensees holding valid commercial Qt licenses may use this file in
 * 			accordance with the commercial license agreement provided with the
 * 			Software or, alternatively, in accordance with the terms contained in
 * 			a written agreement between you and Digia.  For licensing terms and
 * 			conditions see http://qt.digia.com/licensing.  For further information
 * 			use the contact form at http://qt.digia.com/contact-us.
 *
 * 			GNU Lesser General Public License Usage
 * 			Alternatively, this file may be used under the terms of the GNU Lesser
 * 			General Public License version 2.1 as published by the Free Software
 * 			Foundation and appearing in the file LICENSE.LGPL included in the
 *			packaging of this file.  Please review the following information to
 * 			ensure the GNU Lesser General Public License version 2.1 requirements
 * 			will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 *
 * 			In addition, as a special exception, Digia gives you certain additional
 * 			rights.  These rights are described in the Digia Qt LGPL Exception
 * 			version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 *
 * 			GNU General Public License Usage
 * 			Alternatively, this file may be used under the terms of the GNU
 * 			General Public License version 3.0 as published by the Free Software
 * 			Foundation and appearing in the file LICENSE.GPL included in the
 * 			packaging of this file.  Please review the following information to
 * 			ensure the GNU General Public License version 3.0 requirements will be
 * 			met: http://www.gnu.org/copyleft/gpl.html.
 *
 *
 * 			$QT_END_LICENSE$
 *
 *
 */

#include "CQtManagedRestrictedModificationMapData.h"


namespace Konclude {

	namespace Utilities {

		namespace Container {


#ifdef KONCLUDE_EMPLOY_QT_MANAGED_RESTRICTED_MODIFICATION_MAP_CONTAINER



			CQtManagedRestrictedModificationMapData CQtManagedRestrictedModificationMapData::shared_null = {
				&shared_null,
				{ &shared_null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
				/*Q_BASIC_ATOMIC_INITIALIZER(1)*/nullptr, 0, 0, 0, false, true, false, 0
			};

			CQtManagedRestrictedModificationMapData *CQtManagedRestrictedModificationMapData::createData(CQtManagedRestrictedModificationMapBase* referenceMap, CMemoryAllocationManager* memMan)
			{
				return createData(0,referenceMap,memMan);
			}

			CQtManagedRestrictedModificationMapData *CQtManagedRestrictedModificationMapData::createData(int alignment, CQtManagedRestrictedModificationMapBase* referenceMap, CMemoryAllocationManager* memMan)
			{

				CQtManagedRestrictedModificationMapData *d = CObjectAllocator<CQtManagedRestrictedModificationMapData>::allocateAndConstruct(memMan);
				
				//CMemoryAllocator::allocateMemory(memMan,sizeof(CQtManagedRestrictedModificationMapData));
				//CQtManagedRestrictedModificationMapData *d = CMemoryAllocator::allocateMemory(memMan,sizeof(CQtManagedRestrictedModificationMapData));
				//CQtManagedRestrictedModificationMapData *d = new CQtManagedRestrictedModificationMapData;
				Q_CHECK_PTR(d);
				Node *e = reinterpret_cast<Node *>(d);
				e->backward = e;
				e->forward[0] = e;
				//d->ref = 1;
				d->refMap = referenceMap;
				d->topLevel = 0;
				d->size = 0;
				d->randomBits = 0;
				d->insertInOrder = false;
				d->sharable = true;
				d->strictAlignment = alignment > 8;
				d->reserved = 0;
				return d;
			}

			void CQtManagedRestrictedModificationMapData::continueFreeData(int offset, CMemoryAllocationManager* memMan)
			{
				Node *e = reinterpret_cast<Node *>(this);
				Node *cur = e->forward[0];
				Node *prev;
				while (cur != e) {
					prev = cur;
					cur = cur->forward[0];
					if (strictAlignment)
						//qFreeAligned(reinterpret_cast<char *>(prev) - offset);
						CMemoryAllocator::releaseAlignedMemory(reinterpret_cast<char *>(prev) - offset,memMan);
					else
						//qFree(reinterpret_cast<char *>(prev) - offset);
						CMemoryAllocator::releaseMemory(reinterpret_cast<char *>(prev) - offset,memMan);
				}
				CObjectAllocator<CQtManagedRestrictedModificationMapData>::release(this,memMan);
				//delete this;
			}

			CQtManagedRestrictedModificationMapData::Node *CQtManagedRestrictedModificationMapData::node_create(Node *update[], int offset, CMemoryAllocationManager* memMan)
			{
				return node_create(update, offset, 0, memMan);
			}

			/*!
				Creates a new node inside the data structure.

				\a update is an array with pointers to the node after which the new node
				should be inserted. Because of the strange skip list data structure there
				could be several pointers to this node on different levels.
				\a offset is an amount of bytes that needs to reserved just before the
				CQtManagedRestrictedModificationMapData::Node structure.

				\a alignment dictates the alignment for the data.

				\internal
				\since 4.6
			*/
			CQtManagedRestrictedModificationMapData::Node *CQtManagedRestrictedModificationMapData::node_create(Node *update[], int offset, int alignment, CMemoryAllocationManager* memMan)
			{
				int level = 0;
				uint mask = (1 << Sparseness) - 1;

				while ((randomBits & mask) == mask && level < LastLevel) {
					++level;
					mask <<= Sparseness;
				}

				if (level > topLevel) {
					Node *e = reinterpret_cast<Node *>(this);
					level = ++topLevel;
					e->forward[level] = e;
					update[level] = e;
				}

				++randomBits;
				if (level == 3 && !insertInOrder)
					randomBits = qrand();

				//void *concreteNode = strictAlignment ?
				//					 qMallocAligned(offset + sizeof(Node) + level * sizeof(Node *), alignment) :
				//					 qMalloc(offset + sizeof(Node) + level * sizeof(Node *));
				void *concreteNode = strictAlignment ?
					CMemoryAllocator::allocateAlignedMemory(memMan,offset + sizeof(Node) + level * sizeof(Node *), alignment) :
					CMemoryAllocator::allocateMemory(memMan,offset + sizeof(Node) + level * sizeof(Node *));
				Q_CHECK_PTR(concreteNode);

				Node *abstractNode = reinterpret_cast<Node *>(reinterpret_cast<char *>(concreteNode) + offset);

				abstractNode->backward = update[0];
				update[0]->forward[0]->backward = abstractNode;

				for (int i = level; i >= 0; i--) {
					abstractNode->forward[i] = update[i]->forward[i];
					update[i]->forward[i] = abstractNode;
					update[i] = abstractNode;
				}
				++size;
				return abstractNode;
			}

			void CQtManagedRestrictedModificationMapData::node_delete(Node *update[], int offset, Node *node, CMemoryAllocationManager* memMan)
			{
				node->forward[0]->backward = node->backward;

				for (int i = 0; i <= topLevel; ++i) {
					if (update[i]->forward[i] != node)
						break;
					update[i]->forward[i] = node->forward[i];
				}
				--size;
				if (strictAlignment)
					//qFreeAligned(reinterpret_cast<char *>(node) - offset);
					CMemoryAllocator::releaseAlignedMemory(reinterpret_cast<char *>(node) - offset,memMan);
				else
					//qFree(reinterpret_cast<char *>(node) - offset);
					CMemoryAllocator::releaseMemory(reinterpret_cast<char *>(node) - offset,memMan);
			}

#ifdef KONCLUDE_CQTMANAGEDRESTRICTEDMODIFICATIONMAP_DEBUG

			uint CQtManagedRestrictedModificationMapData::adjust_ptr(Node *node)
			{
				if (node == reinterpret_cast<Node *>(this)) {
				   return (uint)0xDEADBEEF;
				} else {
					return (uint)node;
				}
			}

			void CQtManagedRestrictedModificationMapData::dump()
			{
				qDebug("Map data (ref = %d, size = %d, randomBits = %#.8x)", int(ref), size, randomBits);

				QString preOutput;
				QVector<QString> output(topLevel + 1);
				Node *e = reinterpret_cast<Node *>(this);

				QString str;
				str.sprintf("    %.8x", adjust_ptr(reinterpret_cast<Node *>(this)));
				preOutput += str;

				Node *update[LastLevel + 1];
				for (int i = 0; i <= topLevel; ++i) {
					str.sprintf("%d: [%.8x] -", i, adjust_ptr(reinterpret_cast<Node *>(forward[i])));
					output[i] += str;
					update[i] = reinterpret_cast<Node *>(forward[i]);
				}

				Node *node = reinterpret_cast<Node *>(forward[0]);
				while (node != e) {
					int level = 0;
					while (level < topLevel && update[level + 1] == node)
						++level;

					str.sprintf("       %.8x", adjust_ptr(node));
					preOutput += str;

					for (int i = 0; i <= level; ++i) {
						str.sprintf("-> [%.8x] -", adjust_ptr(node->forward[i]));
						output[i] += str;
						update[i] = node->forward[i];
					}
					for (int j = level + 1; j <= topLevel; ++j)
						output[j] += QLatin1String("---------------");
					node = node->forward[0];
				}

				qDebug("%s", preOutput.ascii());
				for (int i = 0; i <= topLevel; ++i)
					qDebug("%s", output[i].ascii());
			}
#endif


#endif // KONCLUDE_EMPLOY_QT_MANAGED_RESTRICTED_MODIFICATION_MAP_CONTAINER


		}; // end namespace Container

	}; // end namespace Utilities

}; // end namespace Konclude