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 #if DEBUG
21 #include <stdio.h>
22 #endif
23
24 namespace Addr
25 {
26
27 #if DEBUG && ADDR_ALLOW_TLS
28 thread_local ADDR_CLIENT_HANDLE g_clientHandle = nullptr;
29 thread_local ADDR_DEBUGPRINT g_pfnDebugPrint = nullptr;
30 #endif
31
32 /**
33 ****************************************************************************************************
34 * Object::Object
35 *
36 * @brief
37 * Constructor for the Object class.
38 ****************************************************************************************************
39 */
Object()40 Object::Object()
41 {
42 m_client.handle = NULL;
43 m_client.callbacks.allocSysMem = NULL;
44 m_client.callbacks.freeSysMem = NULL;
45 m_client.callbacks.debugPrint = NULL;
46 }
47
48 /**
49 ****************************************************************************************************
50 * Object::Object
51 *
52 * @brief
53 * Constructor for the Object class.
54 ****************************************************************************************************
55 */
Object(const Client * pClient)56 Object::Object(const Client* pClient)
57 {
58 m_client = *pClient;
59 }
60
61 /**
62 ****************************************************************************************************
63 * Object::~Object
64 *
65 * @brief
66 * Destructor for the Object class.
67 ****************************************************************************************************
68 */
~Object()69 Object::~Object()
70 {
71 }
72
73 /**
74 ****************************************************************************************************
75 * Object::ClientAlloc
76 *
77 * @brief
78 * Calls instanced allocSysMem inside Client
79 ****************************************************************************************************
80 */
ClientAlloc(size_t objSize,const Client * pClient)81 VOID* Object::ClientAlloc(
82 size_t objSize, ///< [in] Size to allocate
83 const Client* pClient) ///< [in] Client pointer
84 {
85 VOID* pObjMem = NULL;
86
87 if (pClient->callbacks.allocSysMem != NULL)
88 {
89 ADDR_ALLOCSYSMEM_INPUT allocInput = {0};
90
91 allocInput.size = sizeof(ADDR_ALLOCSYSMEM_INPUT);
92 allocInput.flags.value = 0;
93 allocInput.sizeInBytes = static_cast<UINT_32>(objSize);
94 allocInput.hClient = pClient->handle;
95
96 pObjMem = pClient->callbacks.allocSysMem(&allocInput);
97 }
98
99 return pObjMem;
100 }
101
102 /**
103 ****************************************************************************************************
104 * Object::Alloc
105 *
106 * @brief
107 * A wrapper of ClientAlloc
108 ****************************************************************************************************
109 */
Alloc(size_t objSize) const110 VOID* Object::Alloc(
111 size_t objSize ///< [in] Size to allocate
112 ) const
113 {
114 return ClientAlloc(objSize, &m_client);;
115 }
116
117 /**
118 ****************************************************************************************************
119 * Object::ClientFree
120 *
121 * @brief
122 * Calls freeSysMem inside Client
123 ****************************************************************************************************
124 */
ClientFree(VOID * pObjMem,const Client * pClient)125 VOID Object::ClientFree(
126 VOID* pObjMem, ///< [in] User virtual address to free.
127 const Client* pClient) ///< [in] Client pointer
128 {
129 if (pClient->callbacks.freeSysMem != NULL)
130 {
131 if (pObjMem != NULL)
132 {
133 ADDR_FREESYSMEM_INPUT freeInput = {0};
134
135 freeInput.size = sizeof(ADDR_FREESYSMEM_INPUT);
136 freeInput.hClient = pClient->handle;
137 freeInput.pVirtAddr = pObjMem;
138
139 pClient->callbacks.freeSysMem(&freeInput);
140 }
141 }
142 }
143
144 /**
145 ****************************************************************************************************
146 * Object::Free
147 *
148 * @brief
149 * A wrapper of ClientFree
150 ****************************************************************************************************
151 */
Free(VOID * pObjMem) const152 VOID Object::Free(
153 VOID* pObjMem ///< [in] User virtual address to free.
154 ) const
155 {
156 ClientFree(pObjMem, &m_client);
157 }
158
159 /**
160 ****************************************************************************************************
161 * Object::operator new
162 *
163 * @brief
164 * Placement new operator. (with pre-allocated memory pointer)
165 *
166 * @return
167 * Returns pre-allocated memory pointer.
168 ****************************************************************************************************
169 */
operator new(size_t objSize,VOID * pMem)170 VOID* Object::operator new(
171 size_t objSize, ///< [in] Size to allocate
172 VOID* pMem ///< [in] Pre-allocated pointer
173 ) noexcept
174 {
175 return pMem;
176 }
177
178 /**
179 ****************************************************************************************************
180 * Object::operator delete
181 *
182 * @brief
183 * Frees Object object memory.
184 ****************************************************************************************************
185 */
operator delete(VOID * pObjMem)186 VOID Object::operator delete(
187 VOID* pObjMem) ///< [in] User virtual address to free.
188 {
189 Object* pObj = static_cast<Object*>(pObjMem);
190 ClientFree(pObjMem, &pObj->m_client);
191 }
192
193
194 #if DEBUG
195 /**
196 ****************************************************************************************************
197 * ApplyDebugPrinters
198 *
199 * @brief
200 * Sets the debug printers via TLS
201 *
202 * @return
203 * N/A
204 ****************************************************************************************************
205 */
ApplyDebugPrinters(ADDR_DEBUGPRINT pfnDebugPrint,ADDR_CLIENT_HANDLE pClientHandle)206 VOID ApplyDebugPrinters(
207 ADDR_DEBUGPRINT pfnDebugPrint,
208 ADDR_CLIENT_HANDLE pClientHandle)
209 {
210 #if ADDR_ALLOW_TLS
211 g_clientHandle = pClientHandle;
212 g_pfnDebugPrint = pfnDebugPrint;
213 #endif
214 }
215
216 /**
217 ****************************************************************************************************
218 * DebugPrint
219 *
220 * @brief
221 * Print debug message
222 *
223 * @return
224 * N/A
225 ****************************************************************************************************
226 */
DebugPrint(const CHAR * pDebugString,...)227 VOID DebugPrint(
228 const CHAR* pDebugString, ///< [in] Debug string
229 ...
230 )
231 {
232 va_list ap;
233 va_start(ap, pDebugString);
234
235 #if ADDR_ALLOW_TLS
236 if (g_pfnDebugPrint != NULL)
237 {
238 ADDR_DEBUGPRINT_INPUT debugPrintInput = {0};
239
240 debugPrintInput.size = sizeof(ADDR_DEBUGPRINT_INPUT);
241 debugPrintInput.pDebugString = const_cast<CHAR*>(pDebugString);
242 debugPrintInput.hClient = g_clientHandle;
243 va_copy(debugPrintInput.ap, ap);
244
245 g_pfnDebugPrint(&debugPrintInput);
246
247 va_end(debugPrintInput.ap);
248 }
249 else
250 #endif
251 {
252 #if ADDR_ALLOW_STDIO
253 fprintf(stderr, "Warning: Addrlib assert function called without corresponding 'ApplyDebugPrinters'\n");
254 vfprintf(stderr, pDebugString, ap);
255 #endif
256 }
257 va_end(ap);
258 }
259 #endif
260
261 } // Addr
262