1 /*
2 * Copyright © 2014 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
17 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 */
26
27 /**
28 ****************************************************************************************************
29 * @file addrobject.cpp
30 * @brief Contains the Object base class implementation.
31 ****************************************************************************************************
32 */
33
34 #include "addrinterface.h"
35 #include "addrobject.h"
36
37 namespace Addr
38 {
39
40 /**
41 ****************************************************************************************************
42 * Object::Object
43 *
44 * @brief
45 * Constructor for the Object class.
46 ****************************************************************************************************
47 */
Object()48 Object::Object()
49 {
50 m_client.handle = NULL;
51 m_client.callbacks.allocSysMem = NULL;
52 m_client.callbacks.freeSysMem = NULL;
53 m_client.callbacks.debugPrint = NULL;
54 }
55
56 /**
57 ****************************************************************************************************
58 * Object::Object
59 *
60 * @brief
61 * Constructor for the Object class.
62 ****************************************************************************************************
63 */
Object(const Client * pClient)64 Object::Object(const Client* pClient)
65 {
66 m_client = *pClient;
67 }
68
69 /**
70 ****************************************************************************************************
71 * Object::~Object
72 *
73 * @brief
74 * Destructor for the Object class.
75 ****************************************************************************************************
76 */
~Object()77 Object::~Object()
78 {
79 }
80
81 /**
82 ****************************************************************************************************
83 * Object::ClientAlloc
84 *
85 * @brief
86 * Calls instanced allocSysMem inside Client
87 ****************************************************************************************************
88 */
ClientAlloc(size_t objSize,const Client * pClient)89 VOID* Object::ClientAlloc(
90 size_t objSize, ///< [in] Size to allocate
91 const Client* pClient) ///< [in] Client pointer
92 {
93 VOID* pObjMem = NULL;
94
95 if (pClient->callbacks.allocSysMem != NULL)
96 {
97 ADDR_ALLOCSYSMEM_INPUT allocInput = {0};
98
99 allocInput.size = sizeof(ADDR_ALLOCSYSMEM_INPUT);
100 allocInput.flags.value = 0;
101 allocInput.sizeInBytes = static_cast<UINT_32>(objSize);
102 allocInput.hClient = pClient->handle;
103
104 pObjMem = pClient->callbacks.allocSysMem(&allocInput);
105 }
106
107 return pObjMem;
108 }
109
110 /**
111 ****************************************************************************************************
112 * Object::Alloc
113 *
114 * @brief
115 * A wrapper of ClientAlloc
116 ****************************************************************************************************
117 */
Alloc(size_t objSize) const118 VOID* Object::Alloc(
119 size_t objSize ///< [in] Size to allocate
120 ) const
121 {
122 return ClientAlloc(objSize, &m_client);
123 }
124
125 /**
126 ****************************************************************************************************
127 * Object::ClientFree
128 *
129 * @brief
130 * Calls freeSysMem inside Client
131 ****************************************************************************************************
132 */
ClientFree(VOID * pObjMem,const Client * pClient)133 VOID Object::ClientFree(
134 VOID* pObjMem, ///< [in] User virtual address to free.
135 const Client* pClient) ///< [in] Client pointer
136 {
137 if (pClient->callbacks.freeSysMem != NULL)
138 {
139 if (pObjMem != NULL)
140 {
141 ADDR_FREESYSMEM_INPUT freeInput = {0};
142
143 freeInput.size = sizeof(ADDR_FREESYSMEM_INPUT);
144 freeInput.hClient = pClient->handle;
145 freeInput.pVirtAddr = pObjMem;
146
147 pClient->callbacks.freeSysMem(&freeInput);
148 }
149 }
150 }
151
152 /**
153 ****************************************************************************************************
154 * Object::Free
155 *
156 * @brief
157 * A wrapper of ClientFree
158 ****************************************************************************************************
159 */
Free(VOID * pObjMem) const160 VOID Object::Free(
161 VOID* pObjMem ///< [in] User virtual address to free.
162 ) const
163 {
164 ClientFree(pObjMem, &m_client);
165 }
166
167 /**
168 ****************************************************************************************************
169 * Object::operator new
170 *
171 * @brief
172 * Placement new operator. (with pre-allocated memory pointer)
173 *
174 * @return
175 * Returns pre-allocated memory pointer.
176 ****************************************************************************************************
177 */
operator new(size_t objSize,VOID * pMem)178 VOID* Object::operator new(
179 size_t objSize, ///< [in] Size to allocate
180 VOID* pMem) ///< [in] Pre-allocated pointer
181 {
182 return pMem;
183 }
184
185 /**
186 ****************************************************************************************************
187 * Object::operator delete
188 *
189 * @brief
190 * Frees Object object memory.
191 ****************************************************************************************************
192 */
operator delete(VOID * pObjMem)193 VOID Object::operator delete(
194 VOID* pObjMem) ///< [in] User virtual address to free.
195 {
196 Object* pObj = static_cast<Object*>(pObjMem);
197 ClientFree(pObjMem, &pObj->m_client);
198 }
199
200 /**
201 ****************************************************************************************************
202 * Object::DebugPrint
203 *
204 * @brief
205 * Print debug message
206 *
207 * @return
208 * N/A
209 ****************************************************************************************************
210 */
DebugPrint(const CHAR * pDebugString,...) const211 VOID Object::DebugPrint(
212 const CHAR* pDebugString, ///< [in] Debug string
213 ...
214 ) const
215 {
216 #if DEBUG
217 if (m_client.callbacks.debugPrint != NULL)
218 {
219 ADDR_DEBUGPRINT_INPUT debugPrintInput = {0};
220
221 debugPrintInput.size = sizeof(ADDR_DEBUGPRINT_INPUT);
222 debugPrintInput.pDebugString = const_cast<CHAR*>(pDebugString);
223 debugPrintInput.hClient = m_client.handle;
224 va_start(debugPrintInput.ap, pDebugString);
225
226 m_client.callbacks.debugPrint(&debugPrintInput);
227
228 va_end(debugPrintInput.ap);
229 }
230 #endif
231 }
232
233 } // Addr
234