From 7af076e2de7036cbab3f88ef9fc06f86c2a746cd Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 28 Feb 2018 17:55:56 +1100 Subject: [PATCH] alloc: prefer std::byte representations for iterators this allows the users to more easily walk the byte ranges (or perform simply pointer arithmetic), without as much danger of using the values in an expression inadvertantly. --- alloc/raw/affix.hpp | 7 +++++-- alloc/raw/aligned.hpp | 2 ++ alloc/raw/dynamic.hpp | 21 ++++++++++++++++----- alloc/raw/linear.cpp | 32 ++++++++++++++++++++++++-------- alloc/raw/linear.hpp | 20 +++++++++++--------- alloc/raw/null.cpp | 16 ++++++++++++++-- alloc/raw/null.hpp | 12 ++++++++++-- alloc/raw/stack.cpp | 18 +++++++++--------- alloc/raw/stack.hpp | 17 ++++++++++++++--- test/alloc/linear.cpp | 2 +- test/alloc/stack.cpp | 4 ++-- 11 files changed, 108 insertions(+), 43 deletions(-) diff --git a/alloc/raw/affix.hpp b/alloc/raw/affix.hpp index ef80effc..01157902 100644 --- a/alloc/raw/affix.hpp +++ b/alloc/raw/affix.hpp @@ -35,8 +35,11 @@ namespace util::alloc::raw { void deallocate (void *ptr, size_t bytes); void deallocate (void *ptr, size_t bytes, size_t align); - void* begin (void); - const void* begin (void) const; + std::byte* begin (void); + const std::byte* begin (void) const; + + std::byte* end (void); + const std::byte* end (void) const; size_t offset (const void*) const; }; diff --git a/alloc/raw/aligned.hpp b/alloc/raw/aligned.hpp index 281510e4..c9a283cb 100644 --- a/alloc/raw/aligned.hpp +++ b/alloc/raw/aligned.hpp @@ -77,6 +77,8 @@ namespace util::alloc::raw { auto begin (void) { return m_successor.begin (); } auto begin (void) const { return m_successor.begin (); } + auto end (void) { return m_successor.end (); } + auto end (void) const { return m_successor.end (); } //--------------------------------------------------------------------- auto diff --git a/alloc/raw/dynamic.hpp b/alloc/raw/dynamic.hpp index b7bd5418..f7b30e07 100644 --- a/alloc/raw/dynamic.hpp +++ b/alloc/raw/dynamic.hpp @@ -93,9 +93,12 @@ namespace util::alloc::raw { virtual void deallocate (void *ptr, size_t bytes) = 0; virtual void deallocate (void *ptr, size_t bytes, size_t alignment) = 0; - virtual void* begin (void) = 0; - virtual const void* begin (void) const = 0; - virtual size_t offset (const void*) const = 0; + virtual std::byte* begin (void) = 0; + virtual const std::byte* begin (void) const = 0; + virtual std::byte* end (void) = 0; + virtual const std::byte* end (void) const = 0; + + virtual size_t offset (const void*) const = 0; virtual void reset (void) = 0; @@ -132,14 +135,22 @@ namespace util::alloc::raw { deallocate (void *ptr, size_t bytes, size_t alignment) override { m_target.deallocate (ptr, bytes, alignment); } - const void* + const std::byte* begin (void) const override { return m_target.begin (); } - void* + std::byte* begin (void) override { return m_target.begin (); } + std::byte* + end (void) override + { return m_target.end (); } + + const std::byte* + end (void) const override + { return m_target.end (); } + size_t offset (const void *ptr) const override { return m_target.offset (ptr); } diff --git a/alloc/raw/linear.cpp b/alloc/raw/linear.cpp index 3a5d4480..34dc4067 100644 --- a/alloc/raw/linear.cpp +++ b/alloc/raw/linear.cpp @@ -24,9 +24,9 @@ using util::alloc::raw::linear; /////////////////////////////////////////////////////////////////////////////// linear::linear (void *begin, void *end): - m_begin (reinterpret_cast (begin)), - m_end (reinterpret_cast (end)), - m_cursor (reinterpret_cast (begin)) + m_begin (reinterpret_cast (begin)), + m_end (reinterpret_cast (end)), + m_cursor (reinterpret_cast (begin)) { CHECK_NEZ (begin); CHECK_NEZ (end); @@ -76,7 +76,7 @@ linear::deallocate (void *ptr, size_t bytes, size_t alignment) //----------------------------------------------------------------------------- -void* +std::byte* linear::data (void) { return m_begin; @@ -84,7 +84,7 @@ linear::data (void) //----------------------------------------------------------------------------- -const void* +const std::byte* linear::data (void) const { return m_begin; @@ -92,7 +92,7 @@ linear::data (void) const //----------------------------------------------------------------------------- -void* +std::byte* linear::begin (void) { return m_begin; @@ -100,18 +100,34 @@ linear::begin (void) //----------------------------------------------------------------------------- -const void* +const std::byte* linear::begin (void) const { return m_begin; } +//----------------------------------------------------------------------------- +std::byte* +linear::end (void) +{ + return m_end; +} + + +//----------------------------------------------------------------------------- +const std::byte* +linear::end (void) const +{ + return m_end; +} + + //----------------------------------------------------------------------------- size_t linear::offset (const void *_ptr) const { - auto ptr = reinterpret_cast (_ptr); + auto ptr = reinterpret_cast (_ptr); CHECK_GE (ptr, m_begin); return ptr - m_begin; diff --git a/alloc/raw/linear.hpp b/alloc/raw/linear.hpp index 2d4a3ff3..fa451ccd 100644 --- a/alloc/raw/linear.hpp +++ b/alloc/raw/linear.hpp @@ -43,15 +43,15 @@ namespace util::alloc::raw { void deallocate (void *ptr, size_t bytes); void deallocate (void *ptr, size_t bytes, size_t alignment); - void* data (void); - void* begin (void); - void* end (void); - void* cursor (void); + std::byte* data (void); + std::byte* begin (void); + std::byte* end (void); + std::byte* cursor (void); - const void* data (void) const; - const void* begin (void) const; - const void* end (void) const; - const void* cursor (void) const; + const std::byte* data (void) const; + const std::byte* begin (void) const; + const std::byte* end (void) const; + const std::byte* cursor (void) const; size_t offset (const void*) const; @@ -62,7 +62,9 @@ namespace util::alloc::raw { size_t remain (void) const; protected: - char *m_begin, *m_end, *m_cursor; + std::byte *const m_begin; + std::byte *const m_end; + std::byte *m_cursor; }; } diff --git a/alloc/raw/null.cpp b/alloc/raw/null.cpp index cc3ce234..71a70aab 100644 --- a/alloc/raw/null.cpp +++ b/alloc/raw/null.cpp @@ -67,7 +67,7 @@ null::deallocate (void *ptr, size_t bytes, size_t align) /////////////////////////////////////////////////////////////////////////////// -void* +std::byte* null::begin (void) { return nullptr; @@ -75,13 +75,25 @@ null::begin (void) //----------------------------------------------------------------------------- -const void* +const std::byte* null::begin (void) const { return nullptr; } +//----------------------------------------------------------------------------- +std::byte* +null::end (void) +{ return nullptr; } + + +//----------------------------------------------------------------------------- +const std::byte* +null::end (void) const +{ return nullptr; } + + //----------------------------------------------------------------------------- size_t null::offset (const void *ptr) const diff --git a/alloc/raw/null.hpp b/alloc/raw/null.hpp index 9003c424..bb885538 100644 --- a/alloc/raw/null.hpp +++ b/alloc/raw/null.hpp @@ -17,6 +17,8 @@ #ifndef CRUFT_UTIL_ALLOC_RAW_NULL_HPP #define CRUFT_UTIL_ALLOC_RAW_NULL_HPP +#include "../../view.hpp" + #include @@ -35,8 +37,14 @@ namespace util::alloc::raw { void deallocate (void *ptr, size_t bytes); void deallocate (void *ptr, size_t bytes, size_t align); - void* begin (void); - const void* begin (void) const; + util::view data (void); + util::view data (void) const; + + std::byte* begin (void); + const std::byte* begin (void) const; + std::byte* end (void); + const std::byte* end (void) const; + size_t offset (const void*) const; void reset (void); diff --git a/alloc/raw/stack.cpp b/alloc/raw/stack.cpp index 85db14f1..7fe5b9f7 100644 --- a/alloc/raw/stack.cpp +++ b/alloc/raw/stack.cpp @@ -25,9 +25,9 @@ using util::alloc::raw::stack; /////////////////////////////////////////////////////////////////////////////// stack::stack (void *begin, void *end): - m_begin (reinterpret_cast (begin)), - m_end (reinterpret_cast (end)), - m_cursor (reinterpret_cast (begin)) + m_begin (reinterpret_cast (begin)), + m_end (reinterpret_cast (end)), + m_cursor (m_begin) { CHECK_LE (m_begin, m_end); } @@ -37,8 +37,8 @@ stack::stack (void *begin, void *end): union record { using offset_t = uint32_t; - char *as_bytes; - offset_t *as_offset; + std::byte *as_bytes; + offset_t *as_offset; }; @@ -100,7 +100,7 @@ stack::deallocate (void *_ptr, size_t bytes, size_t alignment) (void)alignment; //alignment = util::max (MIN_ALIGNMENT, alignment); - auto ptr = reinterpret_cast (_ptr); + auto ptr = reinterpret_cast (_ptr); record record; record.as_bytes = ptr - sizeof (record::offset_t); @@ -114,7 +114,7 @@ stack::deallocate (void *_ptr, size_t bytes, size_t alignment) //----------------------------------------------------------------------------- -void* +std::byte* stack::begin (void) { return m_begin; @@ -122,7 +122,7 @@ stack::begin (void) //----------------------------------------------------------------------------- -const void* +const std::byte* stack::begin (void) const { return m_begin; @@ -133,7 +133,7 @@ stack::begin (void) const size_t stack::offset (const void *_ptr) const { - auto ptr = reinterpret_cast (_ptr); + auto ptr = reinterpret_cast (_ptr); CHECK_GE (ptr, m_begin); return ptr - m_begin; diff --git a/alloc/raw/stack.hpp b/alloc/raw/stack.hpp index 11288ce9..feef7fbd 100644 --- a/alloc/raw/stack.hpp +++ b/alloc/raw/stack.hpp @@ -17,6 +17,8 @@ #ifndef CRUFT_UTIL_ALLOC_RAW_STACK_HPP #define CRUFT_UTIL_ALLOC_RAW_STACK_HPP +#include "../../view.hpp" + #include @@ -38,8 +40,15 @@ namespace util::alloc::raw { void deallocate (void *ptr, size_t bytes); void deallocate (void *ptr, size_t bytes, size_t alignment); - void* begin (void); - const void* begin (void) const; + util::view data (void); + util::view data (void) const; + + std::byte* begin (void); + const std::byte* begin (void) const; + + std::byte* end (void); + const std::byte* end (void) const; + size_t offset (const void*) const; void reset (void); @@ -49,7 +58,9 @@ namespace util::alloc::raw { size_t remain (void) const; private: - char *m_begin, *m_end, *m_cursor; + std::byte *const m_begin; + std::byte *const m_end; + std::byte *m_cursor; }; } diff --git a/test/alloc/linear.cpp b/test/alloc/linear.cpp index 7bc06717..adb6ae53 100644 --- a/test/alloc/linear.cpp +++ b/test/alloc/linear.cpp @@ -10,7 +10,7 @@ main (void) constexpr size_t BUFFER_SIZE = 1024; - alignas (std::max_align_t) char memory[BUFFER_SIZE]; + alignas (std::max_align_t) std::byte memory[BUFFER_SIZE]; util::alloc::raw::linear store (std::begin (memory), std::end (memory)); tap.expect_eq (store.begin (), std::begin (memory), "base pointers match"); diff --git a/test/alloc/stack.cpp b/test/alloc/stack.cpp index a24d750e..e698a352 100644 --- a/test/alloc/stack.cpp +++ b/test/alloc/stack.cpp @@ -29,8 +29,8 @@ main (void) // alignment is kinda important, so make it a little easier and ensure // something suitable right off the bat. - alignas (std::max_align_t) char memory[BUFFER_SIZE]; - std::fill (std::begin (memory), std::end (memory), 0); + alignas (std::max_align_t) std::byte memory[BUFFER_SIZE]; + std::fill (std::begin (memory), std::end (memory), std::byte{0}); util::alloc::raw::stack store (memory, memory + BUFFER_AVAILABLE);