• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ************************************************************************************************************************
3 *
4 *  Copyright (C) 2007-2022 Advanced Micro Devices, Inc.  All rights reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is 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
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE
23 *
24 ***********************************************************************************************************************/
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     ) noexcept
182 {
183     return pMem;
184 }
185 
186 /**
187 ****************************************************************************************************
188 *   Object::operator delete
189 *
190 *   @brief
191 *       Frees Object object memory.
192 ****************************************************************************************************
193 */
operator delete(VOID * pObjMem)194 VOID Object::operator delete(
195     VOID* pObjMem)      ///< [in] User virtual address to free.
196 {
197     Object* pObj = static_cast<Object*>(pObjMem);
198     ClientFree(pObjMem, &pObj->m_client);
199 }
200 
201 /**
202 ****************************************************************************************************
203 *   Object::DebugPrint
204 *
205 *   @brief
206 *       Print debug message
207 *
208 *   @return
209 *       N/A
210 ****************************************************************************************************
211 */
DebugPrint(const CHAR * pDebugString,...) const212 VOID Object::DebugPrint(
213     const CHAR* pDebugString,     ///< [in] Debug string
214     ...
215     ) const
216 {
217 #if DEBUG
218     if (m_client.callbacks.debugPrint != NULL)
219     {
220         va_list ap;
221 
222         va_start(ap, pDebugString);
223 
224         ADDR_DEBUGPRINT_INPUT debugPrintInput = {0};
225 
226         debugPrintInput.size         = sizeof(ADDR_DEBUGPRINT_INPUT);
227         debugPrintInput.pDebugString = const_cast<CHAR*>(pDebugString);
228         debugPrintInput.hClient      = m_client.handle;
229         va_copy(debugPrintInput.ap, ap);
230 
231         m_client.callbacks.debugPrint(&debugPrintInput);
232 
233         va_end(ap);
234         va_end(debugPrintInput.ap);
235     }
236 #endif
237 }
238 
239 } // Addr
240