MeVisLab Toolbox Reference
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 //-------------------------------------------------------------------------
53 template <typename INDEXTYPE> class TraceBuffer {
54 public:
55 //-----------------------------------------------------------
58 //-----------------------------------------------------------
59 inline TraceBuffer()
60 {
61 // Initialize both buffers with NULL pointers.
62 std::memset(_traceStack, 0, _numBufBytes);
63 std::memset(_traceList, 0, _numBufBytes);
64
65 // Reset indices into stack and list.
66 _traceStackTop = 0;
67 _traceListEnd = 0;
68 }
69
70 //-----------------------------------------------------------
77 //-----------------------------------------------------------
78 inline void add(const char* const traceInString){
79 // Push string address onto the stack. Be sure that
80 // _stackTop is incremented after storing pointer to
81 // make memory access and incrementation of index independent.
82 // So the CPU can execute both statements in parallel.
83 // Also be sure to increment index NOT within "[" and "]"
84 // to avoid that indexing works on 32bit integers which would
85 // access areas outside buffers
86 _traceStack[_traceStackTop] = traceInString;
87
88 // Do not increment inside [ and ] because that could be evaluated
89 // to an integer which would lead to illegal out of range accesses.
90 ++_traceStackTop;
91
92 // Append string address to end of trace list. For optimizing
93 // performance also see _traceStack push above.
94 _traceList[_traceListEnd] = traceInString;
95
96 // Do not increment inside [ and ] because that could be evaluated
97 // to an integer which would lead to illegal out of range accesses.
98 ++_traceListEnd;
99 }
100
101 //-----------------------------------------------------------
105 //-----------------------------------------------------------
106 inline void remove(){
107 // Decrement trace stack top. Be sure that integer index
108 // is really limited to size of INDEXTYPE since passing
109 // decremented _traceStackTop without masking it could
110 // lead to usage of 32 bit index in index "[" "]" and
111 // accesses outside buffers. Note that mask calculation
112 // is constant and probably will be evaluated on compile
113 // time.
114 _traceStackTop = static_cast<INDEXTYPE>((_traceStackTop - 1) & _bufferIndexMask);
115
116 // Reset stack entry. So unused entries do not use
117 // invalid information.
118 _traceStack[_traceStackTop] = NULL;
119 }
120
121 //-----------------------------------------------------------
124 //-----------------------------------------------------------
125 inline INDEXTYPE getTraceStackTop() const { return _traceStackTop; }
126
127 //-----------------------------------------------------------
130 //-----------------------------------------------------------
131 inline INDEXTYPE getTraceListEnd() const { return _traceListEnd; }
132
133 //-----------------------------------------------------------
135 //-----------------------------------------------------------
136 inline const char* const *getTraceStack() const { return _traceStack; }
137
138 //-----------------------------------------------------------
140 //-----------------------------------------------------------
141 inline const char* const *getTraceList() const { return _traceList; }
142
143 private:
144
149 enum { _numBufEntries = (1L << (sizeof(INDEXTYPE) << 3)) ,
150 _numBufBytes = _numBufEntries * sizeof(const char *),
151 _bufferIndexMask = _numBufEntries - 1
152 };
153
155 INDEXTYPE _traceStackTop;
156
158 INDEXTYPE _traceListEnd;
159
161 const char *_traceStack[_numBufEntries];
162
164 const char *_traceList[_numBufEntries];
165 };
166
167 //-------------------------------------------------------------------------
180 //-------------------------------------------------------------------------
181 template <typename INDEXTYPE> class Trace {
182 public:
183
184 //-----------------------------------------------------------
195 //-----------------------------------------------------------
196 inline Trace(const char * const traceInString, TraceBuffer<INDEXTYPE> &traceBuf) :
197 _traceBuf(traceBuf)
198 {
199 // Push string address on stack and at end of trace list.
201 }
202
203 //-----------------------------------------------------------
210 //-----------------------------------------------------------
211 inline ~Trace()
212 {
213 _traceBuf.remove();
214 }
215
216 private:
217
221 TraceBuffer<INDEXTYPE> &_traceBuf;
222
223
226 inline Trace(const Trace &trace) : _traceBuf(trace._traceBuf){ }
227
231 inline Trace<INDEXTYPE> &operator=(const Trace & /* trace */){ return *this; }
232 };
233
234
235 //---------------------------------------------------------------------------
237 //---------------------------------------------------------------------------
239
240 //---------------------------------------------------------------------------
244 //---------------------------------------------------------------------------
246
247
249
250
251#endif // __mlTrace_H
252
This class manages a list and a stack of pointers to permanent strings.
Definition mlTrace.h:53
TraceBuffer()
Initializing constructor.
Definition mlTrace.h:59
const char *const * getTraceList() const
Returns the pointer to the first element of the trace list.
Definition mlTrace.h:141
INDEXTYPE getTraceListEnd() const
Returns index into trace list buffer.
Definition mlTrace.h:131
void add(const char *const traceInString)
Adds an element.
Definition mlTrace.h:78
void remove()
Destructor.
Definition mlTrace.h:106
INDEXTYPE getTraceStackTop() const
Returns index into trace stack buffer.
Definition mlTrace.h:125
const char *const * getTraceStack() const
Returns the pointer to the first element of the trace stack.
Definition mlTrace.h:136
This class simply implements a constructor and a destructor.
Definition mlTrace.h:181
Trace(const char *const traceInString, TraceBuffer< INDEXTYPE > &traceBuf)
Constructor.
Definition mlTrace.h:196
~Trace()
Destructor.
Definition mlTrace.h:211
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
unsigned short MLuint16
Definition mlTypeDefs.h:142
#define ML_UTILS_EXPORT
Defines platform dependent DLL export macro for mlUtils.
Definition mlUtilities.h:20
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:238