Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ShinyManager.h « Shiny « src - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 403e0e8782c90f9851f136013cae24390fba63e8 (plain)
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
/*
The MIT License

Copyright (c) 2007-2010 Aidin Abedi http://code.google.com/p/shinyprofiler/

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#ifndef SHINY_MANAGER_H
#define SHINY_MANAGER_H

#include "ShinyZone.h"
#include "ShinyNode.h"
#include "ShinyNodePool.h"
#include "ShinyTools.h"
#include "ShinyOutput.h"

#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

/*---------------------------------------------------------------------------*/

typedef struct {
#ifdef SHINY_HAS_ENABLED
	bool enabled;
#endif

	shinytick_t _lastTick;

	ShinyNode* _curNode;

	uint32_t _tableMask; /* = _tableSize - 1 */

	ShinyNodeTable* _nodeTable;

#ifdef SHINY_LOOKUP_RATE
	uint64_t _lookupCount;
	uint64_t _lookupSuccessCount;
#endif

	uint32_t _tableSize;

	uint32_t nodeCount;
	uint32_t zoneCount;

	ShinyZone* _lastZone;

	ShinyNodePool* _lastNodePool;
	ShinyNodePool* _firstNodePool;

	ShinyNode rootNode;
	ShinyZone rootZone;

	float damping;

	int _initialized;
	int _firstUpdate;
} ShinyManager;


/*---------------------------------------------------------------------------*/

extern ShinyNode* _ShinyManager_dummyNodeTable[];

extern ShinyManager Shiny_instance;


/*---------------------------------------------------------------------------*/

SHINY_INLINE void _ShinyManager_appendTicksToCurNode(ShinyManager *self) {
	shinytick_t curTick;
	ShinyGetTicks(&curTick);

	ShinyNode_appendTicks(self->_curNode, curTick - self->_lastTick);
	self->_lastTick = curTick;
}

SHINY_API ShinyNode* _ShinyManager_lookupNode(ShinyManager *self, ShinyNodeCache* a_cache, ShinyZone* a_zone);

SHINY_API void _ShinyManager_createNodeTable(ShinyManager *self, uint32_t a_count);
SHINY_API void _ShinyManager_resizeNodeTable(ShinyManager *self, uint32_t a_count);

SHINY_API void _ShinyManager_createNodePool(ShinyManager *self, uint32_t a_count);
SHINY_API void _ShinyManager_resizeNodePool(ShinyManager *self, uint32_t a_count);

SHINY_API ShinyNode* _ShinyManager_createNode(ShinyManager *self, ShinyNodeCache* a_cache, ShinyZone* a_pZone);
SHINY_API void _ShinyManager_insertNode(ShinyManager *self, ShinyNode* a_pNode);

SHINY_INLINE void _ShinyManager_init(ShinyManager *self) {
	self->_initialized = TRUE;

	self->rootNode._last.entryCount = 1;
	self->rootNode._last.selfTicks = 0;
	ShinyGetTicks(&self->_lastTick);
}

SHINY_INLINE void _ShinyManager_uninit(ShinyManager *self) {
	self->_initialized = FALSE;

	ShinyNode_clear(&self->rootNode);
	self->rootNode.parent = &self->rootNode;
	self->rootNode.zone = &self->rootZone;
}

#ifdef SHINY_LOOKUP_RATE
SHINY_INLINE void _ShinyManager_incLookup(ShinyManager *self) { self->_lookupCount++; }
SHINY_INLINE void _ShinyManager_incLookupSuccess(ShinyManager *self) { self->_lookupSuccessCount++; }
SHINY_INLINE float ShinyManager_lookupRate(const ShinyManager *self) { return ((float) self->_lookupSuccessCount) / ((float) self->_lookupCount); }

#else
SHINY_INLINE void _ShinyManager_incLookup(ShinyManager * self) { self = self; }
SHINY_INLINE void _ShinyManager_incLookupSuccess(ShinyManager *  self) { self = self; }
SHINY_INLINE float ShinyManager_lookupRate(const ShinyManager *  self) { self = self; return -1; }
#endif

SHINY_API void ShinyManager_resetZones(ShinyManager *self);
SHINY_API void ShinyManager_destroyNodes(ShinyManager *self);

SHINY_INLINE float ShinyManager_tableUsage(const ShinyManager *self)  {
	return ((float) self->nodeCount) / ((float) self->_tableSize);
}

SHINY_INLINE uint32_t ShinyManager_allocMemInBytes(const ShinyManager *self) {
	return self->_tableSize * sizeof(ShinyNode*)
		 + (self->_firstNodePool)? ShinyNodePool_memoryUsageChain(self->_firstNodePool) : 0;
}

SHINY_INLINE void ShinyManager_beginNode(ShinyManager *self, ShinyNode* a_node) {
	ShinyNode_beginEntry(a_node);

	_ShinyManager_appendTicksToCurNode(self);
	self->_curNode = a_node;
}

SHINY_INLINE void ShinyManager_lookupAndBeginNode(ShinyManager *self, ShinyNodeCache* a_cache, ShinyZone* a_zone) {
#ifdef SHINY_HAS_ENABLED
	if (!self->enabled) return;
#endif

	if (self->_curNode != (*a_cache)->parent)
		*a_cache = _ShinyManager_lookupNode(self, a_cache, a_zone);

	ShinyManager_beginNode(self, *a_cache);
}

SHINY_INLINE void ShinyManager_endCurNode(ShinyManager *self) {
#ifdef SHINY_HAS_ENABLED
	if (!self->enabled) return;
#endif

	_ShinyManager_appendTicksToCurNode(self);
	self->_curNode = self->_curNode->parent;
}

/**/

SHINY_API void ShinyManager_preLoad(ShinyManager *self);

SHINY_API void ShinyManager_updateClean(ShinyManager *self);
SHINY_API void ShinyManager_update(ShinyManager *self);

SHINY_API void ShinyManager_clear(ShinyManager *self);
SHINY_API void ShinyManager_destroy(ShinyManager *self);

SHINY_INLINE void ShinyManager_sortZones(ShinyManager *self) {
	if (self->rootZone.next)
		self->_lastZone = ShinyZone_sortChain(&self->rootZone.next);
}

SHINY_API const char* ShinyManager_getOutputErrorString(ShinyManager *self);

SHINY_API int ShinyManager_output(ShinyManager *self, const char *a_filename);
SHINY_API void ShinyManager_outputToStream(ShinyManager *self, FILE *stream);

#if __cplusplus
} /* end of extern "C" */

SHINY_INLINE std::string ShinyManager_outputTreeToString(ShinyManager *self) {
	const char* error = ShinyManager_getOutputErrorString(self);
	if (error) return error;
	else return ShinyNodesToString(&self->rootNode, self->nodeCount);
}

SHINY_INLINE std::string ShinyManager_outputFlatToString(ShinyManager *self) {
	const char* error = ShinyManager_getOutputErrorString(self);
	if (error) return error;

	ShinyManager_sortZones(self);
	return ShinyZonesToString(&self->rootZone, self->zoneCount);
}

extern "C" { /* end of c++ */
#endif

SHINY_INLINE int ShinyManager_isZoneSelfTimeBelow(ShinyManager *self, ShinyZone* a_zone, float a_percentage) {
	return a_percentage * (float) self->rootZone.data.childTicks.cur
		<= (float) a_zone->data.selfTicks.cur; 
}

SHINY_INLINE int ShinyManager_isZoneTotalTimeBelow(ShinyManager *self, ShinyZone* a_zone, float a_percentage) {
	return a_percentage * (float) self->rootZone.data.childTicks.cur
		<= (float) ShinyData_totalTicksCur(&a_zone->data); 
}

/**/

SHINY_INLINE void ShinyManager_enumerateNodes(ShinyManager *self, void (*a_func)(const ShinyNode*)) {
	ShinyNode_enumerateNodes(&self->rootNode, a_func);
}

SHINY_INLINE void ShinyManager_enumerateZones(ShinyManager *self, void (*a_func)(const ShinyZone*)) {
	ShinyZone_enumerateZones(&self->rootZone, a_func);
}

#if __cplusplus
} /* end of extern "C" */

template <class T> void ShinyManager_enumerateNodes(ShinyManager *self, T* a_this, void (T::*a_func)(const ShinyNode*)) {
	ShinyNode_enumerateNodes(&self->rootNode, a_this, a_func);
}

template <class T> void ShinyManager_enumerateZones(ShinyManager *self, T* a_this, void (T::*a_func)(const ShinyZone*)) {
	ShinyZone_enumerateZones(&self->rootZone, a_this, a_func);
}

extern "C" { /* end of c++ */
#endif


/*---------------------------------------------------------------------------*/

#if __cplusplus
} /* end of extern "C" */

class ShinyEndNodeOnDestruction {
public:

	SHINY_INLINE ~ShinyEndNodeOnDestruction() {
		ShinyManager_endCurNode(&Shiny_instance);
	}
};
#endif

#endif /* SHINY_MANAGER_H */