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
6BUException::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
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';
23 }
24 else
25 {
27 }
28 if(stackBuffer != NULL)
29 {
30 stackBuffer[0] = '\0';
31 stackBuffer[stackSize] = '\0';
32 }
33 else
34 {
35 stackSize=0;
36 }
37
39}
40
42{
43 if(stackBuffer != NULL)
44 free(stackBuffer);
45 if(descriptionBuffer != NULL)
46 free(descriptionBuffer);
47}
48
49void 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
94void 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
120const 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}
static const char descriptionError[]
void Append(const char *buffer)
const char * Description() const
void Copy(const exBase &rh)