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