diff --git a/alloc/arena.hpp b/alloc/arena.hpp index 65a38d9e..0a69c4e4 100644 --- a/alloc/arena.hpp +++ b/alloc/arena.hpp @@ -38,8 +38,8 @@ namespace util::alloc { U* acquire (Args&&... args) { - U *data = reinterpret_cast ( - m_store.allocate (sizeof (U), alignof (U)) + U *data = util::cast::alignment ( + m_store.allocate (sizeof (U), alignof (U)).data () ); try { diff --git a/alloc/raw/affix.hpp b/alloc/raw/affix.hpp index 01157902..2ec10fd9 100644 --- a/alloc/raw/affix.hpp +++ b/alloc/raw/affix.hpp @@ -17,6 +17,8 @@ #ifndef CRUFT_UTIL_ALLOC_RAW_AFFIX_HPP #define CRUFT_UTIL_ALLOC_RAW_AFFIX_HPP +#include "../../view.hpp" + #include namespace util::alloc::raw { @@ -29,11 +31,13 @@ namespace util::alloc::raw { /// useful for sentinels, reference counts, etc. template class affix { - void* allocate (size_t bytes); - void* allocate (size_t bytes, size_t align); + util::view allocate (size_t bytes); + util::view allocate (size_t bytes, size_t align); - void deallocate (void *ptr, size_t bytes); - void deallocate (void *ptr, size_t bytes, size_t align); + void deallocate (util::view ptr) { return deallocate (ptr.data (), ptr.size ()); } + void deallocate (util::view ptr, size_t alignment) { return deallocate (ptr.data (), ptr.size (), alignment); } + void deallocate (void *ptr, size_t bytes); + void deallocate (void *ptr, size_t bytes, size_t align); std::byte* begin (void); const std::byte* begin (void) const; diff --git a/alloc/raw/aligned/direct.hpp b/alloc/raw/aligned/direct.hpp index f3b7534e..de00d653 100644 --- a/alloc/raw/aligned/direct.hpp +++ b/alloc/raw/aligned/direct.hpp @@ -18,6 +18,7 @@ #define CRUFT_UTIL_ALLOC_RAW_ALIGNED_DIRECT_HPP #include "../../../debug.hpp" +#include "../../../view.hpp" #include #include diff --git a/alloc/raw/aligned/foreign.hpp b/alloc/raw/aligned/foreign.hpp index 946863fa..3313211b 100644 --- a/alloc/raw/aligned/foreign.hpp +++ b/alloc/raw/aligned/foreign.hpp @@ -70,7 +70,7 @@ namespace util::alloc::raw::aligned { allocate (std::size_t size) { auto ptr= reinterpret_cast ( - m_successor.allocate (size) + m_successor.allocate (size).data () ); return ptr + m_offset; } diff --git a/alloc/raw/dynamic.hpp b/alloc/raw/dynamic.hpp index 4e54b98a..fa9a294c 100644 --- a/alloc/raw/dynamic.hpp +++ b/alloc/raw/dynamic.hpp @@ -98,8 +98,8 @@ namespace util::alloc::raw { virtual ~interface () { ; } // allocation management - virtual void* allocate (size_t bytes) = 0; - virtual void* allocate (size_t bytes, size_t alignment) = 0; + virtual util::view allocate (size_t bytes) = 0; + virtual util::view allocate (size_t bytes, size_t alignment) = 0; virtual void deallocate (void *ptr, size_t bytes) = 0; virtual void deallocate (void *ptr, size_t bytes, size_t alignment) = 0; @@ -132,7 +132,7 @@ namespace util::alloc::raw { { ; } // allocation management - void* + util::view allocate (size_t bytes) override { return m_target.allocate (bytes); } @@ -140,7 +140,7 @@ namespace util::alloc::raw { // we can't totally eliminate this call given the point is to // expose the common API area, but we will throw if the operation // is unsupported in the child. - void* + util::view allocate (size_t bytes, size_t alignment) override { if constexpr (has_aligned_allocate_v) { diff --git a/alloc/raw/fallback.hpp b/alloc/raw/fallback.hpp index 22ec0b32..ce2b880d 100644 --- a/alloc/raw/fallback.hpp +++ b/alloc/raw/fallback.hpp @@ -17,6 +17,8 @@ #ifndef CRUFT_UTIL_ALLOC_RAW_FALLBACK_HPP #define CRUFT_UTIL_ALLOC_RAW_FALLBACK_HPP +#include "../../view.hpp" + #include #include @@ -30,9 +32,11 @@ namespace util::alloc::raw { m_children (_children...) { ; } - void* allocate (size_t bytes); - void* allocate (size_t bytes, size_t align); + util::view allocate (size_t bytes); + util::view allocate (size_t bytes, size_t align); + void deallocate (util::view ptr) { return deallocate (ptr.data (), ptr.size ()); } + void deallocate (util::view ptr, size_t alignment) { return deallocate (ptr.data (), ptr.size (), alignment); } void deallocate (void *ptr, size_t bytes); void deallocate (void *ptr, size_t bytes, size_t align); diff --git a/alloc/raw/linear.cpp b/alloc/raw/linear.cpp index d14c9940..c34a5a06 100644 --- a/alloc/raw/linear.cpp +++ b/alloc/raw/linear.cpp @@ -31,7 +31,7 @@ linear::linear (util::view _data): /////////////////////////////////////////////////////////////////////////////// -void* +util::view linear::allocate (size_t bytes) { if (m_cursor + bytes > m_end) @@ -39,12 +39,12 @@ linear::allocate (size_t bytes) auto ptr = m_cursor; m_cursor += bytes; - return ptr; + return { ptr, bytes }; } //----------------------------------------------------------------------------- -void* +util::view linear::allocate (size_t bytes, size_t alignment) { auto ptr = align (m_cursor, alignment); @@ -53,7 +53,7 @@ linear::allocate (size_t bytes, size_t alignment) m_cursor = ptr + bytes; - return ptr; + return { ptr, bytes }; } diff --git a/alloc/raw/linear.hpp b/alloc/raw/linear.hpp index dd5f0167..c217584c 100644 --- a/alloc/raw/linear.hpp +++ b/alloc/raw/linear.hpp @@ -34,11 +34,13 @@ namespace util::alloc::raw { linear (util::view _data); - void* allocate (size_t bytes); - void* allocate (size_t bytes, size_t alignment); + util::view allocate (size_t bytes); + util::view allocate (size_t bytes, size_t alignment); - void deallocate (void *ptr, size_t bytes); - void deallocate (void *ptr, size_t bytes, size_t alignment); + void deallocate (util::view ptr) { return deallocate (ptr.data (), ptr.size ()); } + void deallocate (util::view ptr, size_t alignment) { return deallocate (ptr.data (), ptr.size (), alignment); } + void deallocate (void *ptr, size_t bytes); + void deallocate (void *ptr, size_t bytes, size_t alignment); std::byte* data (void); std::byte* begin (void); diff --git a/alloc/raw/malloc.cpp b/alloc/raw/malloc.cpp index 86eb5238..a7b03c09 100644 --- a/alloc/raw/malloc.cpp +++ b/alloc/raw/malloc.cpp @@ -24,7 +24,7 @@ using util::alloc::raw::malloc; /////////////////////////////////////////////////////////////////////////////// -void* +util::view malloc::allocate (size_t bytes) { return allocate (bytes, alignof (std::max_align_t)); @@ -32,14 +32,14 @@ malloc::allocate (size_t bytes) //----------------------------------------------------------------------------- -void* +util::view malloc::allocate (size_t bytes, size_t align) { // C malloc guarantees maximal alignment CHECK_LE (align, alignof (std::max_align_t)); (void)align; - return ::malloc (bytes); + return util::view (reinterpret_cast (::malloc (bytes)), bytes); } diff --git a/alloc/raw/malloc.hpp b/alloc/raw/malloc.hpp index cdaaf93a..dedb3027 100644 --- a/alloc/raw/malloc.hpp +++ b/alloc/raw/malloc.hpp @@ -17,17 +17,21 @@ #ifndef CRUFT_UTIL_ALLOC_RAW_MALLOC_HPP #define CRUFT_UTIL_ALLOC_RAW_MALLOC_HPP +#include "../../view.hpp" + #include namespace util::alloc::raw { class malloc { public: - void* allocate (size_t bytes); - void* allocate (size_t bytes, size_t align); + util::view allocate (size_t bytes); + util::view allocate (size_t bytes, size_t align); - void deallocate (void *ptr, size_t bytes); - void deallocate (void *ptr, size_t bytes, size_t align); + void deallocate (util::view ptr) { return deallocate (ptr.data (), ptr.size ()); } + void deallocate (util::view ptr, size_t alignment) { return deallocate (ptr.data (), ptr.size (), alignment); } + void deallocate (void *ptr, size_t bytes); + void deallocate (void *ptr, size_t bytes, size_t align); }; } diff --git a/alloc/raw/null.cpp b/alloc/raw/null.cpp index 736bc4b8..a06359cb 100644 --- a/alloc/raw/null.cpp +++ b/alloc/raw/null.cpp @@ -25,7 +25,7 @@ using util::alloc::raw::null; /////////////////////////////////////////////////////////////////////////////// -void* +util::view null::allocate (size_t bytes) { return allocate (bytes, alignof (std::max_align_t)); @@ -33,7 +33,7 @@ null::allocate (size_t bytes) //----------------------------------------------------------------------------- -void* +util::view null::allocate (size_t bytes, size_t align) { (void)bytes; diff --git a/alloc/raw/null.hpp b/alloc/raw/null.hpp index bb885538..f9e4a8e6 100644 --- a/alloc/raw/null.hpp +++ b/alloc/raw/null.hpp @@ -32,8 +32,11 @@ namespace util::alloc::raw { null (const null&) = delete; null& operator= (const null&) = delete; - void* allocate (size_t bytes); - void* allocate (size_t bytes, size_t align); + util::view allocate (size_t bytes); + util::view allocate (size_t bytes, size_t align); + + void deallocate (util::view ptr) { return deallocate (ptr.data (), ptr.size ()); } + void deallocate (util::view ptr, size_t alignment) { return deallocate (ptr.data (), ptr.size (), alignment); } void deallocate (void *ptr, size_t bytes); void deallocate (void *ptr, size_t bytes, size_t align); diff --git a/alloc/raw/stack.cpp b/alloc/raw/stack.cpp index cc08a599..8f363ec7 100644 --- a/alloc/raw/stack.cpp +++ b/alloc/raw/stack.cpp @@ -47,7 +47,7 @@ constexpr auto MIN_ALIGNMENT = sizeof (record::offset_t); /////////////////////////////////////////////////////////////////////////////// -void* +util::view stack::allocate (size_t bytes) { return allocate (bytes, alignof (std::max_align_t)); @@ -55,7 +55,7 @@ stack::allocate (size_t bytes) //----------------------------------------------------------------------------- -void* +util::view stack::allocate (size_t bytes, size_t alignment) { // reserve space at the front of the allocation to record the total @@ -78,7 +78,7 @@ stack::allocate (size_t bytes, size_t alignment) *record.as_offset = util::cast::lossless (ptr - m_cursor); m_cursor = ptr + bytes; - return ptr; + return { ptr, bytes }; } diff --git a/alloc/raw/stack.hpp b/alloc/raw/stack.hpp index c7414ea7..8921e073 100644 --- a/alloc/raw/stack.hpp +++ b/alloc/raw/stack.hpp @@ -34,9 +34,11 @@ namespace util::alloc::raw { stack (util::view _data); - void *allocate (size_t bytes, size_t alignment); - void *allocate (size_t bytes); + util::view allocate (size_t bytes, size_t alignment); + util::view allocate (size_t bytes); + void deallocate (util::view ptr) { return deallocate (ptr.data (), ptr.size ()); } + void deallocate (util::view ptr, size_t alignment) { return deallocate (ptr.data (), ptr.size (), alignment); } void deallocate (void *ptr, size_t bytes); void deallocate (void *ptr, size_t bytes, size_t alignment); diff --git a/test/alloc/aligned/direct.cpp b/test/alloc/aligned/direct.cpp index c937400d..d801b2ae 100644 --- a/test/alloc/aligned/direct.cpp +++ b/test/alloc/aligned/direct.cpp @@ -29,10 +29,10 @@ main (int, char**) // alignment to produce a likely system alignment. eg, 3 + 5 == 8 which is // a power-of-2. uintptr_t result[4] = { - reinterpret_cast(alloc.allocate (9)), // just over a power of two - reinterpret_cast(alloc.allocate (1)), // a single byte - reinterpret_cast(alloc.allocate (64)), // a cache line - reinterpret_cast(alloc.allocate (250)) // multiple cache lines, but not a power of two + reinterpret_cast(alloc.allocate (9).data ()), // just over a power of two + reinterpret_cast(alloc.allocate (1).data ()), // a single byte + reinterpret_cast(alloc.allocate (64).data ()), // a cache line + reinterpret_cast(alloc.allocate (250).data ()) // multiple cache lines, but not a power of two }; tap.expect ( diff --git a/test/alloc/stack.cpp b/test/alloc/stack.cpp index 70aa9df5..5e8d8d14 100644 --- a/test/alloc/stack.cpp +++ b/test/alloc/stack.cpp @@ -11,7 +11,7 @@ n_allocations (util::alloc::raw::stack &store, { for (unsigned i = 0; i < count; ++i) { auto ptr = store.allocate (bytes, alignment); - store.deallocate (ptr, bytes, alignment); + store.deallocate (ptr, alignment); } }