/* Copyright 2015, 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see .
*/
namespace dinhelp {
namespace {
#if defined(ASSERTIONSENABLED)
const char g_guard = 0xAB;
#endif
} //unnamed namespace
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template
AutomemRawBase_heap::AutomemRawBase_heap() {
#if !defined(NDEBUG)
m_localMem = nullptr;
#endif
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template
AutomemRawBase_heap::AutomemRawBase_heap (AutomemRawBase_heap&& parOther) {
#if !defined(NDEBUG)
m_localMem = nullptr;
#endif
this->swap(parOther);
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template
T* AutomemRawBase_heap::AllocMemory() {
#if !defined(NDEBUG)
Assert(nullptr == m_localMem);
#endif
m_localMem = A().allocate(S);
#if defined(ASSERTIONSENABLED)
assert(reinterpret_cast(m_localMem) % alignof(T) == 0); //Make sure alignment is correct
std::fill(
reinterpret_cast(&m_localMem[0]),
reinterpret_cast(&m_localMem[0]) + sizeof(m_localMem),
g_guard
);
#endif
return m_localMem;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template
void AutomemRawBase_heap::FreeMemory() noexcept {
#if !defined(NDEBUG)
Assert(nullptr != m_localMem);
#endif
A().deallocate(m_localMem, S);
#if !defined(NDEBUG)
m_localMem = nullptr;
#endif
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template
template
T* AutomemRawBase_heap::GetNewT (size_t parIndex, Args&&... parArgs) {
assert(parIndex < S);
T* const location = m_localMem + parIndex;
#if defined(ASSERTIONSENABLED)
assert(reinterpret_cast(location) % alignof(T) == 0);
assert(g_guard == *reinterpret_cast(location));
#endif
return new(location) T(std::forward(parArgs)...);
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template
T* AutomemRawBase_stack::AllocMemory() {
#if defined(ASSERTIONSENABLED)
assert(reinterpret_cast(m_localMem) % alignof(T) == 0); //Make sure alignment is correct
std::fill(
reinterpret_cast(&m_localMem[0]),
reinterpret_cast(&m_localMem[0]) + sizeof(m_localMem),
g_guard
);
#endif
return reinterpret_cast(&m_localMem[0]);
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template
template
T* AutomemRawBase_stack::GetNewT (size_t parIndex, Args&&... parArgs) {
assert(parIndex < S);
auto* const location = &m_localMem[parIndex];
#if defined(ASSERTIONSENABLED)
assert(reinterpret_cast(location) % alignof(T) == 0);
assert(g_guard == *reinterpret_cast(location));
#endif
return new(location) T(std::forward(parArgs)...);
}
} //namespace dinhelp