From 6bc79535bb39a2960d8eaf403f5b19f910ba0fcf Mon Sep 17 00:00:00 2001 From: Graham Pentheny Date: Tue, 31 Dec 2024 12:47:46 -0500 Subject: [PATCH] Cleanup docstrings and some formatting in RecastAlloc.h --- Recast/Include/RecastAlloc.h | 138 ++++++++++++++++++++++------------- 1 file changed, 89 insertions(+), 49 deletions(-) diff --git a/Recast/Include/RecastAlloc.h b/Recast/Include/RecastAlloc.h index dcb2f51c3..c92864ee9 100644 --- a/Recast/Include/RecastAlloc.h +++ b/Recast/Include/RecastAlloc.h @@ -33,40 +33,34 @@ enum rcAllocHint }; /// A memory allocation function. -// @param[in] size The size, in bytes of memory, to allocate. -// @param[in] rcAllocHint A hint to the allocator on how long the memory is expected to be in use. -// @return A pointer to the beginning of the allocated memory block, or null if the allocation failed. -/// @see rcAllocSetCustom +/// @param[in] size The size, in bytes of memory, to allocate. +/// @param[in] hint A hint to the allocator on how long the memory is expected to be in use. +/// @return A pointer to the beginning of the allocated memory block, or null if the allocation failed. +/// @see rcAllocSetCustom typedef void* (rcAllocFunc)(size_t size, rcAllocHint hint); /// A memory deallocation function. -/// @param[in] ptr A pointer to a memory block previously allocated using #rcAllocFunc. +/// @param[in] ptr A pointer to a memory block previously allocated using #rcAllocFunc. /// @see rcAllocSetCustom typedef void (rcFreeFunc)(void* ptr); /// Sets the base custom allocation functions to be used by Recast. -/// @param[in] allocFunc The memory allocation function to be used by #rcAlloc -/// @param[in] freeFunc The memory de-allocation function to be used by #rcFree -/// +/// @param[in] allocFunc The memory allocation function to be used by #rcAlloc +/// @param[in] freeFunc The memory de-allocation function to be used by #rcFree /// @see rcAlloc, rcFree void rcAllocSetCustom(rcAllocFunc *allocFunc, rcFreeFunc *freeFunc); /// Allocates a memory block. -/// -/// @param[in] size The size, in bytes of memory, to allocate. -/// @param[in] hint A hint to the allocator on how long the memory is expected to be in use. +/// @param[in] size The size, in bytes of memory, to allocate. +/// @param[in] hint A hint to the allocator on how long the memory is expected to be in use. /// @return A pointer to the beginning of the allocated memory block, or null if the allocation failed. -/// /// @see rcFree, rcAllocSetCustom void* rcAlloc(size_t size, rcAllocHint hint); /// Deallocates a memory block. If @p ptr is NULL, this does nothing. -/// /// @warning This function leaves the value of @p ptr unchanged. So it still /// points to the same (now invalid) location, and not to null. -/// -/// @param[in] ptr A pointer to a memory block previously allocated using #rcAlloc. -/// +/// @param[in] ptr A pointer to a memory block previously allocated using #rcAlloc. /// @see rcAlloc, rcAllocSetCustom void rcFree(void* ptr); @@ -101,7 +95,8 @@ typedef intptr_t rcSizeType; /// * push_back() and resize() support adding values from the current vector. Range-based constructors and assign(begin, end) do not. /// * No specialization for bool. template -class rcVectorBase { +class rcVectorBase +{ rcSizeType m_size; rcSizeType m_cap; T* m_data; @@ -168,41 +163,54 @@ class rcVectorBase { }; template -bool rcVectorBase::reserve(rcSizeType count) { - if (count <= m_cap) { +bool rcVectorBase::reserve(rcSizeType count) +{ + if (count <= m_cap) + { return true; } + T* new_data = allocate_and_copy(count); - if (!new_data) { + if (!new_data) + { return false; } + destroy_range(0, m_size); rcFree(m_data); m_data = new_data; m_cap = count; return true; } + template -T* rcVectorBase::allocate_and_copy(rcSizeType size) { +T* rcVectorBase::allocate_and_copy(rcSizeType size) +{ rcAssert(RC_SIZE_MAX / static_cast(sizeof(T)) >= size); T* new_data = static_cast(rcAlloc(sizeof(T) * size, H)); - if (new_data) { + if (new_data) + { copy_range(new_data, m_data, m_data + m_size); } return new_data; } + template -void rcVectorBase::assign(const T* begin, const T* end) { +void rcVectorBase::assign(const T* begin, const T* end) +{ clear(); reserve(end - begin); m_size = end - begin; copy_range(m_data, begin, end); } + template -void rcVectorBase::push_back(const T& value) { +void rcVectorBase::push_back(const T& value) +{ // rcLikely increases performance by ~50% on BM_rcVector_PushPreallocated, // and by ~2-5% on BM_rcVector_Push. - if (rcLikely(m_size < m_cap)) { + if (rcLikely(m_size < m_cap)) + { construct(m_data + m_size++, value); return; } @@ -220,34 +228,50 @@ void rcVectorBase::push_back(const T& value) { } template -rcSizeType rcVectorBase::get_new_capacity(rcSizeType min_capacity) { +rcSizeType rcVectorBase::get_new_capacity(rcSizeType min_capacity) +{ rcAssert(min_capacity <= RC_SIZE_MAX); if (rcUnlikely(m_cap >= RC_SIZE_MAX / 2)) + { return RC_SIZE_MAX; + } return 2 * m_cap > min_capacity ? 2 * m_cap : min_capacity; } template -void rcVectorBase::resize_impl(rcSizeType size, const T* value) { - if (size < m_size) { +void rcVectorBase::resize_impl(rcSizeType size, const T* value) +{ + if (size < m_size) + { destroy_range(size, m_size); m_size = size; - } else if (size > m_size) { - if (size <= m_cap) { - if (value) { + } + else if (size > m_size) + { + if (size <= m_cap) + { + if (value) + { construct_range(m_data + m_size, m_data + size, *value); - } else { + } + else + { construct_range(m_data + m_size, m_data + size); } m_size = size; - } else { + } + else + { const rcSizeType new_cap = get_new_capacity(size); T* new_data = allocate_and_copy(new_cap); // We defer deconstructing/freeing old data until after constructing // new elements in case "value" is there. - if (value) { + if (value) + { construct_range(new_data + m_size, new_data + size, *value); - } else { + } + else + { construct_range(new_data + m_size, new_data + size); } destroy_range(0, m_size); @@ -258,8 +282,10 @@ void rcVectorBase::resize_impl(rcSizeType size, const T* value) { } } } + template -void rcVectorBase::swap(rcVectorBase& other) { +void rcVectorBase::swap(rcVectorBase& other) +{ // TODO: Reorganize headers so we can use rcSwap here. rcSizeType tmp_cap = other.m_cap; rcSizeType tmp_size = other.m_size; @@ -273,36 +299,49 @@ void rcVectorBase::swap(rcVectorBase& other) { m_size = tmp_size; m_data = tmp_data; } + // static template -void rcVectorBase::construct_range(T* begin, T* end) { - for (T* p = begin; p < end; p++) { +void rcVectorBase::construct_range(T* begin, T* end) +{ + for (T* p = begin; p < end; p++) + { construct(p); } } + // static template -void rcVectorBase::construct_range(T* begin, T* end, const T& value) { - for (T* p = begin; p < end; p++) { +void rcVectorBase::construct_range(T* begin, T* end, const T& value) +{ + for (T* p = begin; p < end; p++) + { construct(p, value); } } + // static template -void rcVectorBase::copy_range(T* dst, const T* begin, const T* end) { - for (rcSizeType i = 0 ; i < end - begin; i++) { +void rcVectorBase::copy_range(T* dst, const T* begin, const T* end) +{ + for (rcSizeType i = 0 ; i < end - begin; i++) + { construct(dst + i, begin[i]); } } + template -void rcVectorBase::destroy_range(rcSizeType begin, rcSizeType end) { - for (rcSizeType i = begin; i < end; i++) { +void rcVectorBase::destroy_range(rcSizeType begin, rcSizeType end) +{ + for (rcSizeType i = begin; i < end; i++) + { m_data[i].~T(); } } template -class rcTempVector : public rcVectorBase { +class rcTempVector : public rcVectorBase +{ typedef rcVectorBase Base; public: rcTempVector() : Base() {} @@ -311,8 +350,10 @@ class rcTempVector : public rcVectorBase { rcTempVector(const rcTempVector& other) : Base(other) {} rcTempVector(const T* begin, const T* end) : Base(begin, end) {} }; + template -class rcPermVector : public rcVectorBase { +class rcPermVector : public rcVectorBase +{ typedef rcVectorBase Base; public: rcPermVector() : Base() {} @@ -322,7 +363,6 @@ class rcPermVector : public rcVectorBase { rcPermVector(const T* begin, const T* end) : Base(begin, end) {} }; - /// Legacy class. Prefer rcVector. class rcIntArray { @@ -362,11 +402,11 @@ template class rcScopedDelete /// The root array pointer. /// @return The root array pointer. inline operator T*() { return ptr; } - + private: // Explicitly disabled copy constructor and copy assignment operator. rcScopedDelete(const rcScopedDelete&); rcScopedDelete& operator=(const rcScopedDelete&); }; -#endif +#endif \ No newline at end of file