Added tracing logs to darray when creating/destroying/copying/moving/(re)allocating

This commit is contained in:
2025-06-26 13:14:55 +10:00
parent 2cd9844d07
commit 18062cd3eb
2 changed files with 29 additions and 13 deletions

1
.gitignore vendored
View File

@@ -6,3 +6,4 @@ thirdparty/*
*.ilk *.ilk
*.spv *.spv
*.lib *.lib
/bin/basic.exp

View File

@@ -64,11 +64,12 @@ namespace basalt
inline darray<T>::darray(const darray<T>& src) inline darray<T>::darray(const darray<T>& src)
{ {
this->m_pdata = mem::allocT<T>(src.m_nelements, src.m_tag); this->m_pdata = mem::allocT<T>(src.m_nelements, src.m_tag);
BASSERT_ERROR(this->m_pdata != nullptr, return, "Assertion %s failed at %s:%d\n\tMemory allocation failed for allocation size %u\n\t%s | %s | %u"); BASSERT_ERROR(this->m_pdata != nullptr, return; , "Assertion %s failed at %s:%d\n\tMemory allocation failed for allocation size %u\n\t%s | %s | %u");
this->m_tag = src.m_tag; this->m_tag = src.m_tag;
this->m_ncapacity = src.m_nelements; this->m_ncapacity = src.m_nelements;
this->m_nelements = src.m_nelements; this->m_nelements = src.m_nelements;
memcpy(this->m_pdata, src.m_pdata, sizeof(T) * this->m_nelements); memcpy(this->m_pdata, src.m_pdata, sizeof(T) * this->m_nelements);
BTRACE("darray<%s> %p was copied to %p (%p)\n", typeid(T).name(), &src, this, this->m_pdata);
} }
template<typename T> template<typename T>
inline basalt::darray<T>& darray<T>::operator=(const darray<T>& src) inline basalt::darray<T>& darray<T>::operator=(const darray<T>& src)
@@ -77,11 +78,12 @@ namespace basalt
return *this; return *this;
this->~darray(); this->~darray();
this->m_pdata = mem::allocT<T>(src.m_nelements, src.m_tag); this->m_pdata = mem::allocT<T>(src.m_nelements, src.m_tag);
BASSERT_ERROR(this->m_pdata != nullptr, return, "Assertion %s failed at %s:%d\n\tMemory allocation failed for allocation size %u\n\t%s | %s | %u"); BASSERT_ERROR(this->m_pdata != nullptr, return; , "Assertion %s failed at %s:%d\n\tMemory allocation failed for allocation size %u\n\t%s | %s | %u");
this->m_tag = src.m_tag; this->m_tag = src.m_tag;
this->m_ncapacity = src.m_nelements; this->m_ncapacity = src.m_nelements;
this->m_nelements = src.m_nelements; this->m_nelements = src.m_nelements;
memcpy(this->m_pdata, src.m_pdata, sizeof(T)*this->m_nelements); memcpy(this->m_pdata, src.m_pdata, sizeof(T)*this->m_nelements);
BTRACE("darray<%s> %p was copied to %p (%p)\n", typeid(T).name(), &src, this, this->m_pdata);
} }
template<typename T> template<typename T>
@@ -95,6 +97,7 @@ namespace basalt
src.m_ncapacity = 0; src.m_ncapacity = 0;
src.m_nelements = 0; src.m_nelements = 0;
src.m_tag = 0; src.m_tag = 0;
BTRACE("darray<%s> %p was moved to %p\n", typeid(T).name(), &src, this);
} }
template<typename T> template<typename T>
@@ -111,6 +114,7 @@ namespace basalt
src.m_ncapacity = 0; src.m_ncapacity = 0;
src.m_nelements = 0; src.m_nelements = 0;
src.m_tag = 0; src.m_tag = 0;
BTRACE("darray<%s> %p was moved to %p\n", typeid(T).name(), &src, this);
return *this; return *this;
} }
@@ -134,6 +138,8 @@ namespace basalt
new (this->m_pdata + i) T(*(vals.begin() + i)); new (this->m_pdata + i) T(*(vals.begin() + i));
else BASSERT_FATAL(false, "Assertion %s failed at %s:%d\n\tFailed to copy objects from initializer_list<%s> into darray<%s>\n\t\t%s was not copyable\n", else BASSERT_FATAL(false, "Assertion %s failed at %s:%d\n\tFailed to copy objects from initializer_list<%s> into darray<%s>\n\t\t%s was not copyable\n",
typeid(T).name(), typeid(T).name(), typeid(T).name()); typeid(T).name(), typeid(T).name(), typeid(T).name());
BTRACE("darray<%s> %p created: initalizer ctor %p\n", typeid(T).name(), this, this->m_pdata);
} }
template<typename T> template<typename T>
@@ -143,6 +149,7 @@ namespace basalt
this->m_ncapacity = 0; this->m_ncapacity = 0;
this->m_nelements = 0; this->m_nelements = 0;
this->m_tag = tag; this->m_tag = tag;
BTRACE("darray<%s> %p created: null ctor %p\n", typeid(T).name(), this, this->m_pdata);
} }
template<typename T> template<typename T>
@@ -154,26 +161,31 @@ namespace basalt
this->m_ncapacity = n_capacity; this->m_ncapacity = n_capacity;
this->m_nelements = 0; this->m_nelements = 0;
this->m_tag = tag; this->m_tag = tag;
BTRACE("darray<%s> %p created: capacity ctor %p\n", typeid(T).name(), this, this->m_pdata);
} }
template<typename T> template<typename T>
inline darray<T>::darray(size_t n_elements, const T& fill_value, MEMORY_TAG tag) inline darray<T>::darray(size_t n_elements, const T& fill_value, MEMORY_TAG tag)
{ {
this->m_pdata = mem::allocT<T>(n_capacity, tag); this->m_pdata = mem::allocT<T>(n_elements, tag);
if (this->m_pdata == nullptr) if (this->m_pdata == nullptr)
return; return;
this->m_ncapacity = n_capacity; this->m_ncapacity = n_elements;
this->m_nelements = n_elements; this->m_nelements = n_elements;
for (size_t i = 0; i < n_elements; ++i) for (size_t i = 0; i < n_elements; ++i)
this->m_pdata[i] = fill_value; this->m_pdata[i] = fill_value;
this->m_tag = tag; this->m_tag = tag;
BTRACE("darray<%s> %p created: fill ctor %p\n", typeid(T).name(), this, this->m_pdata);
} }
template<typename T> template<typename T>
inline darray<T>::~darray() inline darray<T>::~darray()
{ {
if (this->m_pdata != nullptr) if (this->m_pdata != nullptr)
mem::dealloc(this->m_pdata, this->m_nelements * sizeof(T), this->m_tag); {
mem::dealloc(this->m_pdata, this->m_ncapacity * sizeof(T), this->m_tag);
BTRACE("darray<%s> %p destroyed %p\n", typeid(T).name(), this, this->m_pdata);
}
} }
template<typename T> template<typename T>
inline void darray<T>::swap(darray<T>& other) inline void darray<T>::swap(darray<T>& other)
@@ -190,9 +202,10 @@ namespace basalt
other.m_tag = this->m_tag; other.m_tag = this->m_tag;
this->m_tag = tmp2; this->m_tag = tmp2;
T* tmp = other.m_pdata; T* tmp3 = other.m_pdata;
other.m_pdata = this->m_pdata; other.m_pdata = this->m_pdata;
this->m_pdata = tmp; this->m_pdata = tmp3;
BTRACE("darray<%s> %p (%p) swapped with darray<%s> %p (%p)\n", typeid(T).name(), this, this->m_pdata, typeid(T).name(), &other, this->m_pdata);
} }
template<typename T> template<typename T>
inline void darray<T>::changetag(MEMORY_TAG new_tag) inline void darray<T>::changetag(MEMORY_TAG new_tag)
@@ -239,7 +252,7 @@ namespace basalt
// Call destructor for objects outside of the new size // Call destructor for objects outside of the new size
if (!std::is_trivially_destructible_v<T>) if (!std::is_trivially_destructible_v<T>)
if (new_size < m_nelements) if (new_size < m_nelements)
for (size_t i = new_size; i < this->m_nelements) for (size_t i = new_size; i < this->m_nelements; ++i)
this->m_pdata[i].~T(); this->m_pdata[i].~T();
this->m_nelements = new_size; this->m_nelements = new_size;
@@ -270,14 +283,15 @@ namespace basalt
else else
{ {
BERROR("Can not resize object dynarray<%s>\n\tIt is not trivially copyable, copy assignable, copy constructible, move assignable or move constructable\n", typeid(T).name()); BERROR("Can not resize object dynarray<%s>\n\tIt is not trivially copyable, copy assignable, copy constructible, move assignable or move constructable\n", typeid(T).name());
mem::dealloc(tmp, this->m_ncapacity*sizeof(T), this->m_tag); mem::dealloc(tmp, new_capacity*sizeof(T), this->m_tag);
return; return;
} }
if (!std::is_trivially_destructible_v<T>) if (!std::is_trivially_destructible_v<T>)
for (size_t i = 0; i < this->m_nelements; ++i) for (size_t i = 0; i < this->m_nelements; ++i)
this->m_pdata[i].~T(); this->m_pdata[i].~T();
mem::dealloc(this->m_pdata, this->m_nelements*sizeof(T), this->m_tag); mem::dealloc(this->m_pdata, this->m_ncapacity*sizeof(T), this->m_tag);
BTRACE("darray<%s> %p Resized from %llu (%p) to %llu (%p)\n", typeid(T).name(), this, this->m_ncapacity, this->m_pdata, new_capacity, tmp);
this->m_ncapacity = new_capacity; this->m_ncapacity = new_capacity;
this->m_pdata = tmp; this->m_pdata = tmp;
} }
@@ -304,14 +318,15 @@ namespace basalt
else else
{ {
BERROR("Can not resize object dynarray<%s>\n\tIt is not trivially copyable, copy assignable, copy constructible, move assignable or move constructable\n", typeid(T).name()); BERROR("Can not resize object dynarray<%s>\n\tIt is not trivially copyable, copy assignable, copy constructible, move assignable or move constructable\n", typeid(T).name());
mem::dealloc(tmp); mem::dealloc(tmp, this->m_nelements*sizeof(T), this->m_tag);
return; return;
} }
if (!std::is_trivially_destructible_v<T>) if (!std::is_trivially_destructible_v<T>)
for (size_t i = 0; i < this->m_nelements; ++i) for (size_t i = 0; i < this->m_nelements; ++i)
this->m_pdata[i].~T(); this->m_pdata[i].~T();
mem::dealloc(this->m_pdata, this->m_nelements * sizeof(T), this->m_tag); mem::dealloc(this->m_pdata, this->m_ncapacity * sizeof(T), this->m_tag);
BTRACE("darray<%s> %p (%p) Shrink fit to %llu (%p)\n", typeid(T).name(), this, this->m_pdata, this->m_nelements, tmp);
this->m_ncapacity = this->m_nelements; this->m_ncapacity = this->m_nelements;
this->m_pdata = tmp; this->m_pdata = tmp;
} }
@@ -398,7 +413,7 @@ namespace basalt
BASSERT_FATAL(this->m_nelements > 0, "Assertion %s failed at %s:%d\n\tAttempted to pop an empty darray<%s>\n", typeid(T).name()); BASSERT_FATAL(this->m_nelements > 0, "Assertion %s failed at %s:%d\n\tAttempted to pop an empty darray<%s>\n", typeid(T).name());
if (idx > this->m_nelements) if (idx > this->m_nelements)
idx = 0; idx = 0;
return *this->m_pdata[idx]; return this->m_pdata[idx];
} }
template<typename T> template<typename T>