]> git.cworth.org Git - apitrace/blob - common/trace_model.cpp
2c0fafaabbc6e81240d95eba251ebe9a9a434abf
[apitrace] / common / trace_model.cpp
1 /**************************************************************************
2  *
3  * Copyright 2010 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 #include "trace_model.hpp"
28
29
30 namespace trace {
31
32
33 Call::~Call() {
34     for (unsigned i = 0; i < args.size(); ++i) {
35         delete args[i].value;
36     }
37
38     if (ret) {
39         delete ret;
40     }
41 }
42
43
44 String::~String() {
45     delete [] value;
46 }
47
48
49 Struct::~Struct() {
50     for (std::vector<Value *>::iterator it = members.begin(); it != members.end(); ++it) {
51         delete *it;
52     }
53 }
54
55
56 Array::~Array() {
57     for (std::vector<Value *>::iterator it = values.begin(); it != values.end(); ++it) {
58         delete *it;
59     }
60 }
61
62 Blob::~Blob() {
63     // Blobs are often bound and referred during many calls, so we can't delete
64     // them here in that case.
65     //
66     // Once bound there is no way to know when they were unbound, which
67     // effectively means we have to leak them.  A better solution would be to
68     // keep a list of bound pointers, and defer the destruction to when the
69     // trace in question has been fully processed.
70     if (!bound) {
71         delete [] buf;
72     }
73 }
74
75 StackFrame::~StackFrame() {
76     if (module != NULL) {
77         delete [] module;
78     }
79     if (function != NULL) {
80         delete [] function;
81     }
82     if (filename != NULL) {
83         delete [] filename;
84     }
85 }
86
87
88 // bool cast
89 bool Null   ::toBool(void) const { return false; }
90 bool Bool   ::toBool(void) const { return value; }
91 bool SInt   ::toBool(void) const { return value != 0; }
92 bool UInt   ::toBool(void) const { return value != 0; }
93 bool Float  ::toBool(void) const { return value != 0; }
94 bool Double ::toBool(void) const { return value != 0; }
95 bool String ::toBool(void) const { return true; }
96 bool Struct ::toBool(void) const { return true; }
97 bool Array  ::toBool(void) const { return true; }
98 bool Blob   ::toBool(void) const { return true; }
99 bool Pointer::toBool(void) const { return value != 0; }
100 bool Repr   ::toBool(void) const { return static_cast<bool>(machineValue); }
101
102
103 // signed integer cast
104 signed long long Value  ::toSInt(void) const { assert(0); return 0; }
105 signed long long Null   ::toSInt(void) const { return 0; }
106 signed long long Bool   ::toSInt(void) const { return static_cast<signed long long>(value); }
107 signed long long SInt   ::toSInt(void) const { return value; }
108 signed long long UInt   ::toSInt(void) const { assert(static_cast<signed long long>(value) >= 0); return static_cast<signed long long>(value); }
109 signed long long Float  ::toSInt(void) const { return static_cast<signed long long>(value); }
110 signed long long Double ::toSInt(void) const { return static_cast<signed long long>(value); }
111 signed long long Repr   ::toSInt(void) const { return machineValue->toSInt(); }
112
113
114 // unsigned integer cast
115 unsigned long long Value  ::toUInt(void) const { assert(0); return 0; }
116 unsigned long long Null   ::toUInt(void) const { return 0; }
117 unsigned long long Bool   ::toUInt(void) const { return static_cast<unsigned long long>(value); }
118 unsigned long long SInt   ::toUInt(void) const { assert(value >= 0); return static_cast<signed long long>(value); }
119 unsigned long long UInt   ::toUInt(void) const { return value; }
120 unsigned long long Float  ::toUInt(void) const { return static_cast<unsigned long long>(value); }
121 unsigned long long Double ::toUInt(void) const { return static_cast<unsigned long long>(value); }
122 unsigned long long Repr   ::toUInt(void) const { return machineValue->toUInt(); }
123
124
125 // floating point cast
126 float Value  ::toFloat(void) const { assert(0); return 0; }
127 float Null   ::toFloat(void) const { return 0; }
128 float Bool   ::toFloat(void) const { return static_cast<float>(value); }
129 float SInt   ::toFloat(void) const { return static_cast<float>(value); }
130 float UInt   ::toFloat(void) const { return static_cast<float>(value); }
131 float Float  ::toFloat(void) const { return value; }
132 float Double ::toFloat(void) const { return value; }
133 float Repr   ::toFloat(void) const { return machineValue->toFloat(); }
134
135
136 // floating point cast
137 double Value  ::toDouble(void) const { assert(0); return 0; }
138 double Null   ::toDouble(void) const { return 0; }
139 double Bool   ::toDouble(void) const { return static_cast<double>(value); }
140 double SInt   ::toDouble(void) const { return static_cast<double>(value); }
141 double UInt   ::toDouble(void) const { return static_cast<double>(value); }
142 double Float  ::toDouble(void) const { return value; }
143 double Double ::toDouble(void) const { return value; }
144 double Repr   ::toDouble(void) const { return machineValue->toDouble(); }
145
146
147 // pointer cast
148 void * Value  ::toPointer(void) const { assert(0); return NULL; }
149 void * Null   ::toPointer(void) const { return NULL; }
150 void * Blob   ::toPointer(void) const { return buf; }
151 void * Pointer::toPointer(void) const { return (void *)value; }
152 void * Repr   ::toPointer(void) const { return machineValue->toPointer(); }
153
154 void * Value  ::toPointer(bool bind) { assert(0); return NULL; }
155 void * Null   ::toPointer(bool bind) { return NULL; }
156 void * Blob   ::toPointer(bool bind) { if (bind) bound = true; return buf; }
157 void * Pointer::toPointer(bool bind) { return (void *)value; }
158 void * Repr   ::toPointer(bool bind) { return machineValue->toPointer(bind); }
159
160
161 // unsigned int pointer cast
162 unsigned long long Value  ::toUIntPtr(void) const { assert(0); return 0; }
163 unsigned long long Null   ::toUIntPtr(void) const { return 0; }
164 unsigned long long Pointer::toUIntPtr(void) const { return value; }
165 unsigned long long Repr   ::toUIntPtr(void) const { return machineValue->toUIntPtr(); }
166
167
168 // string cast
169 const char * Value ::toString(void) const { assert(0); return NULL; }
170 const char * Null  ::toString(void) const { return NULL; }
171 const char * String::toString(void) const { return value; }
172 const char * Repr  ::toString(void) const { return machineValue->toString(); }
173
174
175 // virtual Value::visit()
176 void Null   ::visit(Visitor &visitor) { visitor.visit(this); }
177 void Bool   ::visit(Visitor &visitor) { visitor.visit(this); }
178 void SInt   ::visit(Visitor &visitor) { visitor.visit(this); }
179 void UInt   ::visit(Visitor &visitor) { visitor.visit(this); }
180 void Float  ::visit(Visitor &visitor) { visitor.visit(this); }
181 void Double ::visit(Visitor &visitor) { visitor.visit(this); }
182 void String ::visit(Visitor &visitor) { visitor.visit(this); }
183 void Enum   ::visit(Visitor &visitor) { visitor.visit(this); }
184 void Bitmask::visit(Visitor &visitor) { visitor.visit(this); }
185 void Struct ::visit(Visitor &visitor) { visitor.visit(this); }
186 void Array  ::visit(Visitor &visitor) { visitor.visit(this); }
187 void Blob   ::visit(Visitor &visitor) { visitor.visit(this); }
188 void Pointer::visit(Visitor &visitor) { visitor.visit(this); }
189 void Repr   ::visit(Visitor &visitor) { visitor.visit(this); }
190
191
192 void Visitor::visit(Null *) { assert(0); }
193 void Visitor::visit(Bool *) { assert(0); }
194 void Visitor::visit(SInt *) { assert(0); }
195 void Visitor::visit(UInt *) { assert(0); }
196 void Visitor::visit(Float *) { assert(0); }
197 void Visitor::visit(Double *) { assert(0); }
198 void Visitor::visit(String *) { assert(0); }
199 void Visitor::visit(Enum *node) { assert(0); }
200 void Visitor::visit(Bitmask *node) { visit(static_cast<UInt *>(node)); }
201 void Visitor::visit(Struct *) { assert(0); }
202 void Visitor::visit(Array *) { assert(0); }
203 void Visitor::visit(Blob *) { assert(0); }
204 void Visitor::visit(Pointer *) { assert(0); }
205 void Visitor::visit(Repr *node) { node->machineValue->visit(*this); }
206 void Visitor::visit(Backtrace *) { assert(0); }
207 void Visitor::visit(StackFrame *) { assert(0); }
208
209
210 static Null null;
211
212 const Value & Value::operator[](size_t index) const {
213     const Array *array = dynamic_cast<const Array *>(this);
214     if (array) {
215         if (index < array->values.size()) {
216             return *array->values[index];
217         }
218     }
219     return null;
220 }
221
222 } /* namespace trace */