This is an automated email from the ASF dual-hosted git repository. bneradt pushed a commit to branch dev-1-2-7 in repository https://gitbox.apache.org/repos/asf/trafficserver-libswoc.git
commit 6014f24c407ef19e729e652e93fd045730f862f9 Author: Alan M. Carroll <a...@apache.org> AuthorDate: Fri Jun 19 11:19:37 2020 -0500 Add MemArena::alloc_span. --- code/include/swoc/MemArena.h | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/code/include/swoc/MemArena.h b/code/include/swoc/MemArena.h index 289d2fe..36cf131 100644 --- a/code/include/swoc/MemArena.h +++ b/code/include/swoc/MemArena.h @@ -169,14 +169,28 @@ public: */ MemSpan<void> alloc(size_t n); - /** Allocate and initialize a block of memory. + /** ALlocate a span of memory sufficient for @a n instance of @a T. + * + * @tparam T Element type. + * @param n Number of instances. + * @return A span large enough to hold @a n instances of @a T. + * + * The instances are @b not initialized / constructed. This only allocates the memory. + * This is handy for types that don't need initialization, such as built in types like @c int. + * @code + * auto vec = arena.alloc_span<int>(20); // allocate space for 20 ints + * @endcode + */ + template <typename T> MemSpan<T> alloc_span(size_t n); + + /** Allocate and initialize a block of memory as an instance of @a T The template type specifies the type to create and any arguments are forwarded to the constructor. Example: @code struct Thing { ... }; - Thing* thing = arena.make<Thing>(...constructor args...); + auto thing = arena.make<Thing>(...constructor args...); @endcode Do @b not call @c delete an object created this way - that will attempt to free the memory and @@ -407,6 +421,11 @@ inline MemSpan<void> MemArena::Block::alloc(size_t n) { return zret; } +template<typename T> +MemSpan<T> MemArena::alloc_span(size_t n) { + return this->alloc(sizeof(T) * n).rebind<T>(); +} + template<typename T, typename... Args> T *MemArena::make(Args&& ... args) { return new(this->alloc(sizeof(T)).data()) T(std::forward<Args>(args)...); }