• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ************************************************************************************************************************
3 *
4 *  Copyright (C) 2007-2022 Advanced Micro Devices, Inc.  All rights reserved.
5 *  SPDX-License-Identifier: MIT
6 *
7 ***********************************************************************************************************************/
8 
9 
10 /**
11 ****************************************************************************************************
12 * @file  addrobject.cpp
13 * @brief Contains the Object base class implementation.
14 ****************************************************************************************************
15 */
16 
17 #include "addrinterface.h"
18 #include "addrobject.h"
19 
20 namespace Addr
21 {
22 
23 /**
24 ****************************************************************************************************
25 *   Object::Object
26 *
27 *   @brief
28 *       Constructor for the Object class.
29 ****************************************************************************************************
30 */
Object()31 Object::Object()
32 {
33     m_client.handle = NULL;
34     m_client.callbacks.allocSysMem = NULL;
35     m_client.callbacks.freeSysMem = NULL;
36     m_client.callbacks.debugPrint = NULL;
37 }
38 
39 /**
40 ****************************************************************************************************
41 *   Object::Object
42 *
43 *   @brief
44 *       Constructor for the Object class.
45 ****************************************************************************************************
46 */
Object(const Client * pClient)47 Object::Object(const Client* pClient)
48 {
49     m_client = *pClient;
50 }
51 
52 /**
53 ****************************************************************************************************
54 *   Object::~Object
55 *
56 *   @brief
57 *       Destructor for the Object class.
58 ****************************************************************************************************
59 */
~Object()60 Object::~Object()
61 {
62 }
63 
64 /**
65 ****************************************************************************************************
66 *   Object::ClientAlloc
67 *
68 *   @brief
69 *       Calls instanced allocSysMem inside Client
70 ****************************************************************************************************
71 */
ClientAlloc(size_t objSize,const Client * pClient)72 VOID* Object::ClientAlloc(
73     size_t         objSize,    ///< [in] Size to allocate
74     const Client*  pClient)    ///< [in] Client pointer
75 {
76     VOID* pObjMem = NULL;
77 
78     if (pClient->callbacks.allocSysMem != NULL)
79     {
80         ADDR_ALLOCSYSMEM_INPUT allocInput = {0};
81 
82         allocInput.size        = sizeof(ADDR_ALLOCSYSMEM_INPUT);
83         allocInput.flags.value = 0;
84         allocInput.sizeInBytes = static_cast<UINT_32>(objSize);
85         allocInput.hClient     = pClient->handle;
86 
87         pObjMem = pClient->callbacks.allocSysMem(&allocInput);
88     }
89 
90     return pObjMem;
91 }
92 
93 /**
94 ****************************************************************************************************
95 *   Object::Alloc
96 *
97 *   @brief
98 *       A wrapper of ClientAlloc
99 ****************************************************************************************************
100 */
Alloc(size_t objSize) const101 VOID* Object::Alloc(
102     size_t objSize      ///< [in] Size to allocate
103     ) const
104 {
105     return ClientAlloc(objSize, &m_client);;
106 }
107 
108 /**
109 ****************************************************************************************************
110 *   Object::ClientFree
111 *
112 *   @brief
113 *       Calls freeSysMem inside Client
114 ****************************************************************************************************
115 */
ClientFree(VOID * pObjMem,const Client * pClient)116 VOID Object::ClientFree(
117     VOID*          pObjMem,    ///< [in] User virtual address to free.
118     const Client*  pClient)    ///< [in] Client pointer
119 {
120     if (pClient->callbacks.freeSysMem != NULL)
121     {
122         if (pObjMem != NULL)
123         {
124             ADDR_FREESYSMEM_INPUT freeInput = {0};
125 
126             freeInput.size      = sizeof(ADDR_FREESYSMEM_INPUT);
127             freeInput.hClient   = pClient->handle;
128             freeInput.pVirtAddr = pObjMem;
129 
130             pClient->callbacks.freeSysMem(&freeInput);
131         }
132     }
133 }
134 
135 /**
136 ****************************************************************************************************
137 *   Object::Free
138 *
139 *   @brief
140 *       A wrapper of ClientFree
141 ****************************************************************************************************
142 */
Free(VOID * pObjMem) const143 VOID Object::Free(
144     VOID* pObjMem       ///< [in] User virtual address to free.
145     ) const
146 {
147     ClientFree(pObjMem, &m_client);
148 }
149 
150 /**
151 ****************************************************************************************************
152 *   Object::operator new
153 *
154 *   @brief
155 *       Placement new operator. (with pre-allocated memory pointer)
156 *
157 *   @return
158 *       Returns pre-allocated memory pointer.
159 ****************************************************************************************************
160 */
operator new(size_t objSize,VOID * pMem)161 VOID* Object::operator new(
162     size_t objSize,     ///< [in] Size to allocate
163     VOID*  pMem        ///< [in] Pre-allocated pointer
164     ) noexcept
165 {
166     return pMem;
167 }
168 
169 /**
170 ****************************************************************************************************
171 *   Object::operator delete
172 *
173 *   @brief
174 *       Frees Object object memory.
175 ****************************************************************************************************
176 */
operator delete(VOID * pObjMem)177 VOID Object::operator delete(
178     VOID* pObjMem)      ///< [in] User virtual address to free.
179 {
180     Object* pObj = static_cast<Object*>(pObjMem);
181     ClientFree(pObjMem, &pObj->m_client);
182 }
183 
184 /**
185 ****************************************************************************************************
186 *   Object::DebugPrint
187 *
188 *   @brief
189 *       Print debug message
190 *
191 *   @return
192 *       N/A
193 ****************************************************************************************************
194 */
DebugPrint(const CHAR * pDebugString,...) const195 VOID Object::DebugPrint(
196     const CHAR* pDebugString,     ///< [in] Debug string
197     ...
198     ) const
199 {
200 #if DEBUG
201     if (m_client.callbacks.debugPrint != NULL)
202     {
203         va_list ap;
204 
205         va_start(ap, pDebugString);
206 
207         ADDR_DEBUGPRINT_INPUT debugPrintInput = {0};
208 
209         debugPrintInput.size         = sizeof(ADDR_DEBUGPRINT_INPUT);
210         debugPrintInput.pDebugString = const_cast<CHAR*>(pDebugString);
211         debugPrintInput.hClient      = m_client.handle;
212         va_copy(debugPrintInput.ap, ap);
213 
214         m_client.callbacks.debugPrint(&debugPrintInput);
215 
216         va_end(ap);
217         va_end(debugPrintInput.ap);
218     }
219 #endif
220 }
221 
222 } // Addr
223