refactor using pimpl and fix test

This commit is contained in:
AndreaRigoni
2026-03-25 16:18:07 +00:00
parent a467b7385b
commit 7d4acaef6d
17 changed files with 479 additions and 412 deletions

View File

@@ -52,6 +52,7 @@ public:
else
m_RamData = static_cast<T *>(::operator new(m_Size * sizeof(T)));
}
std::cout << "DataAllocator Constructor: ptr=" << m_RamData << " size=" << m_Size << " own=" << m_OwnsObjects << std::endl;
}
DataAllocator(const DataAllocator<T> &other)
@@ -63,7 +64,12 @@ public:
m_RamData = new T[m_Size];
else
m_RamData = static_cast<T *>(::operator new(m_Size * sizeof(T)));
std::memcpy(m_RamData, other.m_RamData, m_Size * sizeof(T));
if (m_OwnsObjects) {
std::copy(other.m_RamData, other.m_RamData + m_Size, m_RamData);
} else {
std::memcpy(m_RamData, other.m_RamData, m_Size * sizeof(T));
}
}
#ifdef USE_CUDA
if (other.m_VramData) {
@@ -73,14 +79,17 @@ public:
}
#endif
}
std::cout << "DataAllocator CopyConstructor: from=" << other.m_RamData << " to=" << m_RamData << " size=" << m_Size << " own=" << m_OwnsObjects << std::endl;
}
~DataAllocator() {
std::cout << "DataAllocator Destructor: ptr=" << m_RamData << " size=" << m_Size << " own=" << m_OwnsObjects << std::endl;
if (m_RamData) {
if (m_OwnsObjects)
delete[] m_RamData;
else
::operator delete(m_RamData);
m_RamData = nullptr;
}
#ifdef USE_CUDA
if (m_VramData) {
@@ -91,6 +100,13 @@ public:
DataAllocator &operator=(const DataAllocator &other) {
if (this != &other) {
if (m_Size == other.m_Size && m_OwnsObjects != other.m_OwnsObjects) {
// Ownership changed but size is same: we must force reallocation
// to avoid using the wrong delete operator later.
size_t oldSize = m_Size;
m_Size = 0;
resize(oldSize); // This will free the old buffer with the OLD ownership
}
m_OwnsObjects = other.m_OwnsObjects;
resize(other.m_Size);
m_Device = other.m_Device;
@@ -101,7 +117,11 @@ public:
else
m_RamData = static_cast<T *>(::operator new(m_Size * sizeof(T)));
}
std::memcpy(m_RamData, other.m_RamData, m_Size * sizeof(T));
if (m_OwnsObjects) {
std::copy(other.m_RamData, other.m_RamData + m_Size, m_RamData);
} else {
std::memcpy(m_RamData, other.m_RamData, m_Size * sizeof(T));
}
}
#ifdef USE_CUDA
if (other.m_VramData) {
@@ -112,6 +132,7 @@ public:
}
#endif
}
std::cout << "DataAllocator AssigmentOp: otherPtr=" << other.m_RamData << " thisPtr=" << m_RamData << " size=" << m_Size << " own=" << m_OwnsObjects << std::endl;
return *this;
}
@@ -152,6 +173,8 @@ public:
if (m_Size == size)
return;
std::cout << "DataAllocator Resize: from=" << m_Size << " to=" << size << " ptr=" << m_RamData << " own=" << m_OwnsObjects << std::endl;
T *newRam = nullptr;
T *newVram = nullptr;
@@ -162,7 +185,11 @@ public:
newRam = static_cast<T *>(::operator new(size * sizeof(T)));
if (m_RamData) {
std::memcpy(newRam, m_RamData, std::min(m_Size, size) * sizeof(T));
if (m_OwnsObjects) {
std::copy(m_RamData, m_RamData + std::min(m_Size, size), newRam);
} else {
std::memcpy(newRam, m_RamData, std::min(m_Size, size) * sizeof(T));
}
}
#ifdef USE_CUDA