DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
dunedaq
sourcecode
wibmod
src
WIB1
BUException
ExceptionBase.cpp
Go to the documentation of this file.
1
#include "
wibmod/WIB1/BUException/ExceptionBase.hh
"
2
3
//Error string for bad allocation of description
4
static
const
char
descriptionError
[] =
"Description allocation failed.\n"
;
5
6
BUException::exBase::exBase
() throw() :
descriptionSize
(255),
7
stackSize
(1024),
8
PID
(-1)
9
{
10
//Allocate memory (throw if failed)
11
stackBuffer
= (
char
*) malloc(
stackSize
+1);
12
descriptionBuffer
= (
char
*) malloc(
descriptionSize
+1);
13
14
descriptionUsed
= 0;
15
stackUsed
= 0;
16
17
//Null terminate the strings to empty
18
//also make the char after the nominal end of string null terminated
19
if
(
descriptionBuffer
!= NULL)
20
{
21
descriptionBuffer
[0] =
'\0'
;
22
descriptionBuffer
[
descriptionSize
] =
'\0'
;
23
}
24
else
25
{
26
descriptionSize
= 0;
27
}
28
if
(
stackBuffer
!= NULL)
29
{
30
stackBuffer
[0] =
'\0'
;
31
stackBuffer
[
stackSize
] =
'\0'
;
32
}
33
else
34
{
35
stackSize
=0;
36
}
37
38
GenerateStackTrace
();
39
}
40
41
BUException::exBase::~exBase
() throw()
42
{
43
if
(
stackBuffer
!= NULL)
44
free(
stackBuffer
);
45
if
(
descriptionBuffer
!= NULL)
46
free(
descriptionBuffer
);
47
}
48
49
void
BUException::exBase::Copy
(
const
exBase
& rh)
throw
()
50
{
51
//Copy description buffer
52
this->
descriptionUsed
= 0;
53
this->
descriptionSize
= rh.descriptionSize;
54
//Check if the allocation had worked for rh
55
if
(rh.descriptionBuffer == NULL)
56
{
57
//It didn't work, make sure our copy is also NULL
58
this->
descriptionBuffer
= NULL;
59
}
60
else
61
{
62
//rh has a valid description, build ours
63
this->
descriptionBuffer
= (
char
*) malloc(
descriptionSize
+1);
64
if
(this->
descriptionBuffer
!= NULL)
65
{
66
memcpy(this->
descriptionBuffer
,rh.descriptionBuffer,
descriptionSize
);
67
this->
descriptionUsed
= rh.descriptionUsed;
68
}
69
}
70
71
//Copy stack trace buffer
72
this->
stackUsed
= 0;
73
this->
stackSize
= rh.stackSize;
74
//Check if the allocation worked for rh
75
if
(rh.stackBuffer == NULL)
76
{
77
//It didn't work, make sure our copy is also NULL
78
this->
stackBuffer
= NULL;
79
}
80
else
81
{
//rh was valid, so construct ours
82
this->
stackBuffer
= (
char
*) malloc(
stackSize
+1);
83
if
(this->
stackBuffer
!= NULL)
84
{
85
memcpy(this->
stackBuffer
,rh.stackBuffer,
stackSize
);
86
this->
stackUsed
= rh.stackUsed;
87
}
88
}
89
90
//Copy PID
91
this->
PID
= rh.PID;
92
}
93
94
void
BUException::exBase::Append
(
const
char
* buffer)
throw
()
95
{
96
//Compute the appended buffer's size
97
size_t
appendedSize = strlen(buffer);
98
99
//Make sure we stay in the bounds of our buffer
100
if
( appendedSize > (
descriptionSize
-
descriptionUsed
))
101
{
102
appendedSize = (
descriptionSize
-
descriptionUsed
);
103
}
104
105
//Append the new string (n keeps us safe from buffer overflow)
106
strncpy(
descriptionBuffer
+
descriptionUsed
,
107
buffer,
108
appendedSize);
109
110
//update the description size
111
descriptionUsed
+= appendedSize;
112
113
//enforce the null terminator
114
//(the buffer is one larger than the size so we always have space for a NULL)
115
//This is untrue if malloc failed initially, so we watch out for that.
116
if
(
descriptionBuffer
!= NULL)
117
descriptionBuffer
[
descriptionUsed
] =
'\0'
;
118
}
119
120
const
char
*
BUException::exBase::Description
()
const
throw()
121
{
122
if
(
descriptionBuffer
== NULL)
//If there was a bad alloc, return an error string
123
return
descriptionError
;
124
return
descriptionBuffer
;
125
}
descriptionError
static const char descriptionError[]
Definition
ExceptionBase.cpp:4
ExceptionBase.hh
BUException::exBase::~exBase
virtual ~exBase()
Definition
ExceptionBase.cpp:41
BUException::exBase::stackBuffer
char * stackBuffer
Definition
ExceptionBase.hh:89
BUException::exBase::Append
void Append(const char *buffer)
Definition
ExceptionBase.cpp:94
BUException::exBase::exBase
exBase()
Definition
ExceptionBase.cpp:6
BUException::exBase::stackSize
size_t stackSize
Definition
ExceptionBase.hh:87
BUException::exBase::GenerateStackTrace
void GenerateStackTrace()
Definition
ExceptionBase_stacktrace.cpp:40
BUException::exBase::stackUsed
size_t stackUsed
Definition
ExceptionBase.hh:88
BUException::exBase::Description
const char * Description() const
Definition
ExceptionBase.cpp:120
BUException::exBase::descriptionUsed
size_t descriptionUsed
Definition
ExceptionBase.hh:83
BUException::exBase::PID
pid_t PID
Definition
ExceptionBase.hh:91
BUException::exBase::descriptionSize
size_t descriptionSize
Definition
ExceptionBase.hh:82
BUException::exBase::descriptionBuffer
char * descriptionBuffer
Definition
ExceptionBase.hh:84
BUException::exBase::Copy
void Copy(const exBase &rh)
Definition
ExceptionBase.cpp:49
Generated on
for DUNE-DAQ by
1.16.1