[ox/std] Cleanup MallocaPtr on heap determination

This commit is contained in:
Gary Talent 2018-09-01 00:25:32 -05:00
parent 32e2878fb1
commit e918f2e60e

View File

@ -30,14 +30,14 @@ void *operator new(std::size_t, void*) noexcept;
* @return an ox::MallocaPtr of the given type pointing to the requested size memory allocation * @return an ox::MallocaPtr of the given type pointing to the requested size memory allocation
*/ */
#if defined(OX_USE_STDLIB) #if defined(OX_USE_STDLIB)
#define ox_malloca(size, Type, ...) ox::MallocaPtr<Type>(size, new (size > MallocaStackLimit ? new uint8_t[size] : ox_alloca(size)) Type(__VA_ARGS__)) #define ox_malloca(size, Type, ...) ox::MallocaPtr<Type>(size > MallocaStackLimit, new (size > MallocaStackLimit ? new uint8_t[size] : ox_alloca(size)) Type(__VA_ARGS__))
#else #else
#define ox_malloca(size, Type, ...) ox::MallocaPtr<Type>(size, new (ox_alloca(size)) Type(__VA_ARGS__)) #define ox_malloca(size, Type, ...) ox::MallocaPtr<Type>(false, new (ox_alloca(size)) Type(__VA_ARGS__))
#endif #endif
namespace ox { namespace ox {
constexpr auto MallocaStackLimit = 1024; constexpr auto MallocaStackLimit = ox::defines::UseStdLib ? 1024 : 0;
/** /**
* MallocaPtr will automatically cleanup the pointed to address upon * MallocaPtr will automatically cleanup the pointed to address upon
@ -47,7 +47,7 @@ template<typename T>
class MallocaPtr { class MallocaPtr {
private: private:
std::size_t m_size = 0; bool m_onHeap = false;
T *m_val = nullptr; T *m_val = nullptr;
public: public:
@ -58,24 +58,22 @@ class MallocaPtr {
inline MallocaPtr(const MallocaPtr &other) = delete; inline MallocaPtr(const MallocaPtr &other) = delete;
inline MallocaPtr(MallocaPtr &&other) noexcept { inline MallocaPtr(MallocaPtr &&other) noexcept {
m_size = other.m_size; m_onHeap = other.m_onHeap;
m_val = other.m_val; m_val = other.m_val;
other.m_size = 0; other.m_onHeap = false;
other.m_val = nullptr; other.m_val = nullptr;
} }
inline MallocaPtr(std::size_t size, T *val) noexcept { inline MallocaPtr(bool onHeap, T *val) noexcept {
m_size = size; m_onHeap = onHeap;
m_val = val; m_val = val;
} }
inline ~MallocaPtr() noexcept { inline ~MallocaPtr() noexcept {
if constexpr(ox::defines::UseStdLib) { if (m_onHeap && m_val) {
if (m_size > ox::MallocaStackLimit) {
delete[] reinterpret_cast<uint8_t*>(m_val); delete[] reinterpret_cast<uint8_t*>(m_val);
} }
} }
}
inline const T *get() const noexcept { inline const T *get() const noexcept {
return reinterpret_cast<T*>(m_val); return reinterpret_cast<T*>(m_val);
@ -90,14 +88,12 @@ class MallocaPtr {
inline const T &operator=(const MallocaPtr &other) = delete; inline const T &operator=(const MallocaPtr &other) = delete;
inline const T &operator=(MallocaPtr &&other) noexcept { inline const T &operator=(MallocaPtr &&other) noexcept {
if constexpr(ox::defines::UseStdLib) { if (m_onHeap && m_val) {
if (m_size > ox::MallocaStackLimit) {
delete[] reinterpret_cast<uint8_t*>(m_val); delete[] reinterpret_cast<uint8_t*>(m_val);
} }
} m_onHeap = other.m_onHeap;
m_size = other.m_size;
m_val = other.m_val; m_val = other.m_val;
other.m_size = 0; other.m_onHeap = false;
other.m_val = nullptr; other.m_val = nullptr;
} }
@ -126,11 +122,11 @@ class MallocaPtr {
} }
inline bool operator==(const MallocaPtr<T> &other) const noexcept { inline bool operator==(const MallocaPtr<T> &other) const noexcept {
return m_val == other.m_val && m_size == other.m_size; return m_val == other.m_val && m_onHeap == other.m_onHeap;
} }
inline bool operator!=(const MallocaPtr<T> &other) const noexcept { inline bool operator!=(const MallocaPtr<T> &other) const noexcept {
return m_val != other.m_val || m_size != other.m_size; return m_val != other.m_val || m_onHeap != other.m_onHeap;
} }
}; };