29#include "NvFlowReflect.h"
30#include "NvFlowArray.h"
36 const char* debugname;
52 NvFlowUint64 luid = 0llu;
54 NvFlowUint64 version = 0llu;
56 NvFlowUint8* deepCopyData;
57 NvFlowUint64 lastUse = 0llu;
70 for (NvFlowUint64 cachedIdx = 0u; cachedIdx < ptr->cached.size; cachedIdx++)
72 NvFlowReflectDeepCopyCached_destroy(ptr->cached[cachedIdx]);
73 ptr->cached[cachedIdx] =
nullptr;
75 ptr->cached.size = 0u;
79NV_FLOW_INLINE
NvFlowReflectDeepCopyCached* NvFlowReflectDeepCopyCached_create(NvFlowUint64 luid,
const char** pathStacks, NvFlowUint64 pathStackCount)
83 ptr->pathStack.size = 0u;
84 for (NvFlowUint64 pathStackIdx = 0u; pathStackIdx < pathStackCount; pathStackIdx++)
86 ptr->pathStack.pushBack(pathStacks[pathStackIdx]);
89 ptr->deepCopy = NvFlowReflectDeepCopy_create();
95 NvFlowReflectDeepCopy_destroy(ptr->deepCopy);
96 ptr->deepCopy =
nullptr;
102 auto& currentHeap = ptr->heaps[ptr->heaps.allocateBack()];
104 NvFlowUint64 heapSize = 4096u;
105 while (heapSize < allocSize)
110 currentHeap.reserve(heapSize);
113NV_FLOW_INLINE NvFlowUint64 NvFlowReflectDeepCopy_alignment(NvFlowUint64 size)
115 return 8u * ((size + 7u) / 8u);
118NV_FLOW_INLINE NvFlowUint8* NvFlowReflectDeepCopy_allocate(
NvFlowReflectDeepCopy* ptr, NvFlowUint64 size,
const char* debugName)
120 NvFlowUint64 allocSize = NvFlowReflectDeepCopy_alignment(size);
122 if (ptr->heaps.size > 0u)
124 auto& currentHeap = ptr->heaps[ptr->heaps.size - 1u];
126 if (currentHeap.size + allocSize <= currentHeap.capacity)
128 NvFlowUint8* ret = currentHeap.data + currentHeap.size;
130 currentHeap.size += allocSize;
132 ptr->infos.pushBack(info);
137 NvFlowReflectDeepCopy_newHeap(ptr, allocSize);
139 return NvFlowReflectDeepCopy_allocate(ptr, size, debugName);
144 for (NvFlowUint64 heapIdx = 0u; heapIdx < ptr->heaps.size; heapIdx++)
146 ptr->heaps[heapIdx].size = 0u;
148 ptr->heaps.size = 0u;
149 ptr->infos.size = 0u;
150 ptr->pathStack.size = 0u;
153NV_FLOW_INLINE NvFlowUint8* NvFlowReflectDeepCopy_recursive(
NvFlowReflectDeepCopy* ptr, NvFlowUint64 luid,
const NvFlowUint8* src,
const NvFlowReflectDataType* type, NvFlowUint64 elementCount, NvFlowBool32 isPointerArray);
157 static const NvFlowUint64 cacheFreeTreshold = 8u;
159 NvFlowUint cachedIdx = 0u;
160 while (cachedIdx < ptr->cached.size)
162 ptr->cached[cachedIdx]->lastUse++;
163 if (ptr->cached[cachedIdx]->lastUse > cacheFreeTreshold)
165 NvFlowReflectDeepCopyCached_destroy(ptr->cached[cachedIdx]);
166 ptr->cached[cachedIdx] =
nullptr;
167 ptr->cached.removeSwapPointerAtIndex(cachedIdx);
176NV_FLOW_INLINE NvFlowUint8* NvFlowReflectDeepCopy_cached(
NvFlowReflectDeepCopy* ptr, NvFlowUint64 luid,
const NvFlowUint8* src,
const NvFlowReflectDataType* type, NvFlowUint64 elementCount, NvFlowUint64 version, NvFlowBool32 isPointerArray)
178 NvFlowUint64 cachedIdx = 0u;
179 for (; cachedIdx < ptr->cached.size; cachedIdx++)
181 auto& cached = ptr->cached[cachedIdx];
182 if (cached->luid == luid && ptr->pathStack.size == cached->pathStack.size)
185 bool pathStackMatches =
true;
186 for (NvFlowUint64 pathStackIdx = 0u; pathStackIdx < ptr->pathStack.size; pathStackIdx++)
188 if (NvFlowReflectStringCompare(ptr->pathStack[pathStackIdx], cached->pathStack[pathStackIdx]) != 0)
190 pathStackMatches =
false;
194 if (pathStackMatches)
200 if (cachedIdx == ptr->cached.size)
202 cachedIdx = ptr->cached.allocateBack();
203 ptr->cached[cachedIdx] = NvFlowReflectDeepCopyCached_create(luid, ptr->pathStack.data, ptr->pathStack.size);
205 auto cached = ptr->cached[cachedIdx];
206 if (ptr->cached[cachedIdx]->version != version)
208 NvFlowReflectDeepCopy_reset(cached->deepCopy);
209 NvFlowReflectDeepCopy_cleanCache(cached->deepCopy);
210 cached->deepCopyData = NvFlowReflectDeepCopy_recursive(cached->deepCopy, luid, src, type, elementCount, isPointerArray);
212 cached->version = version;
214 cached->lastUse = 0u;
215 return cached->deepCopyData;
218NV_FLOW_INLINE
void NvFlowReflectDeepCopy_structRecursive(
NvFlowReflectDeepCopy* ptr, NvFlowUint64 luid, NvFlowUint8* dst,
const NvFlowReflectDataType* type, NvFlowUint64 elementCount, NvFlowBool32 isPointerArray)
220 if (type->dataType == eNvFlowType_struct)
222 for (NvFlowUint64 elementIdx = 0u; elementIdx < elementCount; elementIdx++)
224 NvFlowUint8* dstArray = dst + type->elementSize * elementIdx;
227 for (NvFlowUint childIdx = 0u; childIdx < type->childReflectDataCount; childIdx++)
229 const auto& childReflectData = type->childReflectDatas[childIdx];
230 if (childReflectData.reflectMode == eNvFlowReflectMode_value &&
231 childReflectData.dataType->dataType == eNvFlowType_uint64)
233 if (NvFlowReflectStringCompare(childReflectData.name,
"luid") == 0)
235 luid = *((NvFlowUint64*)(dst + childReflectData.dataOffset));
242 for (NvFlowUint64 childIdx = 0u; childIdx < type->childReflectDataCount; childIdx++)
244 const auto& childReflectData = type->childReflectDatas[childIdx];
245 ptr->pathStack.pushBack(childReflectData.name);
246 if (childReflectData.dataType->dataType == eNvFlowType_struct &&
247 (childReflectData.reflectMode == eNvFlowReflectMode_value ||
248 childReflectData.reflectMode == eNvFlowReflectMode_valueVersioned))
250 NvFlowReflectDeepCopy_structRecursive(ptr, luid, dstArray + childReflectData.dataOffset, childReflectData.dataType, 1u, NV_FLOW_FALSE);
252 if (childReflectData.reflectMode & eNvFlowReflectMode_pointerArray)
255 NvFlowUint8** childPtr = (NvFlowUint8**)(dstArray + childReflectData.dataOffset);
256 NvFlowUint64 childElementCount = 1u;
257 NvFlowUint64 childVersion = 0u;
260 if (childReflectData.reflectMode & eNvFlowReflectMode_array)
262 childElementCount = *(NvFlowUint64*)(dstArray + childReflectData.arraySizeOffset);
264 if (childReflectData.reflectMode & eNvFlowReflectMode_valueVersioned)
266 childVersion = *(NvFlowUint64*)(dstArray + childReflectData.versionOffset);
268 NvFlowBool32 isPointerArray = (childReflectData.reflectMode & eNvFlowReflectMode_pointerArray) == eNvFlowReflectMode_pointerArray;
271 if (luid > 0u && childElementCount > 0u && childVersion > 0u && childReflectData.dataType->dataType != eNvFlowType_struct)
273 *childPtr = NvFlowReflectDeepCopy_cached(ptr, luid, *childPtr, childReflectData.dataType, childElementCount, childVersion, isPointerArray);
278 *childPtr = NvFlowReflectDeepCopy_recursive(ptr, luid, *childPtr, childReflectData.dataType, childElementCount, isPointerArray);
282 ptr->pathStack.size--;
288NV_FLOW_INLINE NvFlowUint8* NvFlowReflectDeepCopy_recursive(
NvFlowReflectDeepCopy* ptr, NvFlowUint64 luid,
const NvFlowUint8* src,
const NvFlowReflectDataType* type, NvFlowUint64 elementCount, NvFlowBool32 isPointerArray)
290 const char* debugName =
"root";
291 if (ptr->pathStack.size > 0u)
293 debugName = ptr->pathStack[ptr->pathStack.size - 1u];
298 NvFlowUint8* dstData = NvFlowReflectDeepCopy_allocate(ptr,
sizeof(
void*) * elementCount, debugName);
300 memcpy(dstData, src,
sizeof(
void*) * elementCount);
303 NvFlowUint8** dstArray = (NvFlowUint8**)dstData;
304 for (NvFlowUint64 elementIdx = 0u; elementIdx < elementCount; elementIdx++)
306 if (dstArray[elementIdx])
308 dstArray[elementIdx] = NvFlowReflectDeepCopy_recursive(ptr, luid, dstArray[elementIdx], type, 1u, NV_FLOW_FALSE);
314 NvFlowUint8* dstData = NvFlowReflectDeepCopy_allocate(ptr, type->elementSize * elementCount, debugName);
316 memcpy(dstData, src, type->elementSize * elementCount);
318 NvFlowReflectDeepCopy_structRecursive(ptr, luid, dstData, type, elementCount, isPointerArray);
325 const NvFlowUint8* src = (
const NvFlowUint8*)srcVoid;
326 NvFlowReflectDeepCopy_reset(ptr);
327 NvFlowReflectDeepCopy_cleanCache(ptr);
328 return NvFlowReflectDeepCopy_recursive(ptr, 0llu, src, type, 1u, NV_FLOW_FALSE);
Definition NvFlowArray.h:213
Definition NvFlowArray.h:36
Definition NvFlowReflect.h:182
Definition NvFlowDeepCopy.h:51
Definition NvFlowDeepCopy.h:35
Definition NvFlowDeepCopy.h:43