DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
ExceptionBase.cpp
Go to the documentation of this file.
2
3// Error string for bad allocation of description
4static const char descriptionError[] = "Description allocation failed.\n";
5
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
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';
23 } else {
25 }
26 if (stackBuffer != NULL) {
27 stackBuffer[0] = '\0';
28 stackBuffer[stackSize] = '\0';
29 } else {
30 stackSize = 0;
31 }
32
34}
35
37{
38 if (stackBuffer != NULL)
39 free(stackBuffer);
40 if (descriptionBuffer != NULL)
42}
43
44void
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
82void
83BUException::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)
104}
105
106const char*
108{
109 if (descriptionBuffer == NULL) // If there was a bad alloc, return an error string
110 return descriptionError;
111 return descriptionBuffer;
112}
static const char descriptionError[]
void Append(const char *buffer)
const char * Description() const
void Copy(const exBase &rh)