MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
mlTrace.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2007, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef ML_TRACE_H
14#define ML_TRACE_H
15
23
24#include "mlTypeDefs.h"
25#include "mlUtilsSystem.h"
26#include <cstring>
27
29
30//-------------------------------------------------------------------------
52//-------------------------------------------------------------------------
53template <typename INDEXTYPE> class TraceBuffer
54{
55public:
56 //-----------------------------------------------------------
59 //-----------------------------------------------------------
60 inline TraceBuffer()
61 {
62 // Initialize both buffers with NULL pointers.
63 std::memset(_traceStack, 0, _numBufBytes);
64 std::memset(_traceList, 0, _numBufBytes);
65
66 // Reset indices into stack and list.
67 _traceStackTop = 0;
68 _traceListEnd = 0;
69 }
70
71 //-----------------------------------------------------------
78 //-----------------------------------------------------------
79 inline void add(const char* const traceInString)
80 {
81 // Push string address onto the stack. Be sure that
82 // _stackTop is incremented after storing the pointer to
83 // make memory access and incrementation of index independent.
84 // This way, the CPU can execute both statements in parallel.
85 // Also make sure to increment the index \e not within [ and ]
86 // to avoid that indexing works on 32-bit integers, which would
87 // access areas outside buffers
88 _traceStack[_traceStackTop] = traceInString;
89
90 // Do not increment inside [ and ] because that could be evaluated
91 // to an integer, which would lead to illegal out of range accesses.
92 ++_traceStackTop;
93
94 // Append string address to end of trace list. For optimizing
95 // performance, also see _traceStack push above.
96 _traceList[_traceListEnd] = traceInString;
97
98 // Do not increment inside [ and ] because that could be evaluated
99 // to an integer, which would lead to illegal out of range accesses.
100 ++_traceListEnd;
101 }
102
103 //-----------------------------------------------------------
107 //-----------------------------------------------------------
108 inline void remove()
109 {
110 // Decrement trace stack top. Be sure that integer index
111 // is really limited to size of INDEXTYPE since passing
112 // decremented _traceStackTop without masking it could
113 // lead to usage of 32-bit index in index [ ] and
114 // accesses outside buffers. Note that mask calculation
115 // is constant and probably will be evaluated on compile
116 // time.
117 _traceStackTop = static_cast<INDEXTYPE>((_traceStackTop - 1) & _bufferIndexMask);
118
119 // Reset stack entry. This way, unused entries do not use
120 // invalid information.
121 _traceStack[_traceStackTop] = NULL;
122 }
123
124 //-----------------------------------------------------------
127 //-----------------------------------------------------------
129 {
130 return _traceStackTop;
131 }
132
133 //-----------------------------------------------------------
136 //-----------------------------------------------------------
138 {
139 return _traceListEnd;
140 }
141
142 //-----------------------------------------------------------
144 //-----------------------------------------------------------
145 inline const char* const* getTraceStack() const
146 {
147 return _traceStack;
148 }
149
150 //-----------------------------------------------------------
152 //-----------------------------------------------------------
153 inline const char* const* getTraceList() const
154 {
155 return _traceList;
156 }
157
158private:
163 enum {
164 _numBufEntries = (1L << (sizeof(INDEXTYPE) << 3)),
165 _numBufBytes = _numBufEntries * sizeof(const char*),
166 _bufferIndexMask = _numBufEntries - 1
167 };
168
170 INDEXTYPE _traceStackTop;
171
173 INDEXTYPE _traceListEnd;
174
176 const char* _traceStack[_numBufEntries];
177
179 const char* _traceList[_numBufEntries];
180};
181
182//-------------------------------------------------------------------------
195//-------------------------------------------------------------------------
196template <typename INDEXTYPE> class Trace
197{
198public:
199 //-----------------------------------------------------------
210 //-----------------------------------------------------------
212 : _traceBuf(traceBuf)
213 {
214 // Push string address on stack and at end of trace list.
216 }
217
218 //-----------------------------------------------------------
225 //-----------------------------------------------------------
226 inline ~Trace()
227 {
228 _traceBuf.remove();
229 }
230
231private:
235 TraceBuffer<INDEXTYPE>& _traceBuf;
236
237
240 inline Trace(const Trace& trace)
241 : _traceBuf(trace._traceBuf)
242 {}
243
247 inline Trace<INDEXTYPE>& operator=(const Trace& /* trace */)
248 {
249 return *this;
250 }
251};
252
253
254//---------------------------------------------------------------------------
256//---------------------------------------------------------------------------
258
259//---------------------------------------------------------------------------
263//---------------------------------------------------------------------------
265
266
268
269
270#endif // __mlTrace_H
This class manages a list and a stack of pointers to permanent strings.
Definition mlTrace.h:54
TraceBuffer()
Initializing constructor.
Definition mlTrace.h:60
const char *const * getTraceList() const
Returns the pointer to the first element of the trace list.
Definition mlTrace.h:153
INDEXTYPE getTraceListEnd() const
Returns the index into the trace list buffer.
Definition mlTrace.h:137
void add(const char *const traceInString)
Adds an element.
Definition mlTrace.h:79
void remove()
Destructor.
Definition mlTrace.h:108
INDEXTYPE getTraceStackTop() const
Returns the index into the trace stack buffer.
Definition mlTrace.h:128
const char *const * getTraceStack() const
Returns the pointer to the first element of the trace stack.
Definition mlTrace.h:145
This class simply implements a constructor and a destructor.
Definition mlTrace.h:197
Trace(const char *const traceInString, TraceBuffer< INDEXTYPE > &traceBuf)
Constructor.
Definition mlTrace.h:211
~Trace()
Destructor.
Definition mlTrace.h:226
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
unsigned short MLuint16
Definition mlTypeDefs.h:142
#define ML_UTILS_EXPORT
Definition mlUtilities.h:18
ML_UTILS_EXPORT TraceBuffer< MLGlobalTraceBufferType > MLGlobalTraceBuffer
This is a global singleton of the TraceBuffer class.
MLuint16 MLGlobalTraceBufferType
The type used in the MLGlobalTraceBuffer.
Definition mlTrace.h:257