xo-alloc: streamlining DArena allocation: + construct_with() helper

This commit is contained in:
Roland Conybeare 2025-12-18 19:41:47 -05:00
commit 1ec9759f19
5 changed files with 53 additions and 42 deletions

View file

@ -100,6 +100,13 @@ namespace xo {
/** @ret iterator pointing to just after the last allocation in this arena **/
DArenaIterator end() const noexcept;
/** @ret header for first allocation in this arena **/
AllocHeader * begin_header() const noexcept;
/** @ret location of header for next (not yet performed!)
* allocation in this arena
**/
AllocHeader * end_header() const noexcept;
/** get header from allocated object address **/
header_type * obj2hdr(void * obj) noexcept;
@ -208,6 +215,22 @@ namespace xo {
///@}
};
/** construct a @tparam T instance from arguments @p args
* using memory obtained from arena @p ialloc
**/
template <typename T,
typename... Args>
static T *
construct_with(DArena & ialloc, Args&&... args)
{
std::byte * mem = ialloc.alloc(sizeof(T));
if (mem)
return new (mem) T(std::forward<Args>(args)...);
return nullptr;
}
} /*namespace mm*/
} /*namespace xo*/