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.
This commit is contained in:
Danny Robson 2018-02-28 17:55:56 +11:00
parent 2941a5a3e1
commit 7af076e2de
11 changed files with 108 additions and 43 deletions

View File

@ -35,8 +35,11 @@ namespace util::alloc::raw {
void deallocate (void *ptr, size_t bytes); void deallocate (void *ptr, size_t bytes);
void deallocate (void *ptr, size_t bytes, size_t align); void deallocate (void *ptr, size_t bytes, size_t align);
void* begin (void); std::byte* begin (void);
const void* begin (void) const; const std::byte* begin (void) const;
std::byte* end (void);
const std::byte* end (void) const;
size_t offset (const void*) const; size_t offset (const void*) const;
}; };

View File

@ -77,6 +77,8 @@ namespace util::alloc::raw {
auto begin (void) { return m_successor.begin (); } auto begin (void) { return m_successor.begin (); }
auto begin (void) const { 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 auto

View File

@ -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) = 0;
virtual void deallocate (void *ptr, size_t bytes, size_t alignment) = 0; virtual void deallocate (void *ptr, size_t bytes, size_t alignment) = 0;
virtual void* begin (void) = 0; virtual std::byte* begin (void) = 0;
virtual const void* begin (void) const = 0; virtual const std::byte* begin (void) const = 0;
virtual size_t offset (const 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; virtual void reset (void) = 0;
@ -132,14 +135,22 @@ namespace util::alloc::raw {
deallocate (void *ptr, size_t bytes, size_t alignment) override deallocate (void *ptr, size_t bytes, size_t alignment) override
{ m_target.deallocate (ptr, bytes, alignment); } { m_target.deallocate (ptr, bytes, alignment); }
const void* const std::byte*
begin (void) const override begin (void) const override
{ return m_target.begin (); } { return m_target.begin (); }
void* std::byte*
begin (void) override begin (void) override
{ return m_target.begin (); } { 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 size_t
offset (const void *ptr) const override offset (const void *ptr) const override
{ return m_target.offset (ptr); } { return m_target.offset (ptr); }

View File

@ -24,9 +24,9 @@ using util::alloc::raw::linear;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
linear::linear (void *begin, void *end): linear::linear (void *begin, void *end):
m_begin (reinterpret_cast<char*> (begin)), m_begin (reinterpret_cast<std::byte*> (begin)),
m_end (reinterpret_cast<char*> (end)), m_end (reinterpret_cast<std::byte*> (end)),
m_cursor (reinterpret_cast<char*> (begin)) m_cursor (reinterpret_cast<std::byte*> (begin))
{ {
CHECK_NEZ (begin); CHECK_NEZ (begin);
CHECK_NEZ (end); CHECK_NEZ (end);
@ -76,7 +76,7 @@ linear::deallocate (void *ptr, size_t bytes, size_t alignment)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void* std::byte*
linear::data (void) linear::data (void)
{ {
return m_begin; return m_begin;
@ -84,7 +84,7 @@ linear::data (void)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
const void* const std::byte*
linear::data (void) const linear::data (void) const
{ {
return m_begin; return m_begin;
@ -92,7 +92,7 @@ linear::data (void) const
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void* std::byte*
linear::begin (void) linear::begin (void)
{ {
return m_begin; return m_begin;
@ -100,18 +100,34 @@ linear::begin (void)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
const void* const std::byte*
linear::begin (void) const linear::begin (void) const
{ {
return m_begin; return m_begin;
} }
//-----------------------------------------------------------------------------
std::byte*
linear::end (void)
{
return m_end;
}
//-----------------------------------------------------------------------------
const std::byte*
linear::end (void) const
{
return m_end;
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
size_t size_t
linear::offset (const void *_ptr) const linear::offset (const void *_ptr) const
{ {
auto ptr = reinterpret_cast<const char*> (_ptr); auto ptr = reinterpret_cast<const std::byte*> (_ptr);
CHECK_GE (ptr, m_begin); CHECK_GE (ptr, m_begin);
return ptr - m_begin; return ptr - m_begin;

View File

@ -43,15 +43,15 @@ namespace util::alloc::raw {
void deallocate (void *ptr, size_t bytes); void deallocate (void *ptr, size_t bytes);
void deallocate (void *ptr, size_t bytes, size_t alignment); void deallocate (void *ptr, size_t bytes, size_t alignment);
void* data (void); std::byte* data (void);
void* begin (void); std::byte* begin (void);
void* end (void); std::byte* end (void);
void* cursor (void); std::byte* cursor (void);
const void* data (void) const; const std::byte* data (void) const;
const void* begin (void) const; const std::byte* begin (void) const;
const void* end (void) const; const std::byte* end (void) const;
const void* cursor (void) const; const std::byte* cursor (void) const;
size_t offset (const void*) const; size_t offset (const void*) const;
@ -62,7 +62,9 @@ namespace util::alloc::raw {
size_t remain (void) const; size_t remain (void) const;
protected: protected:
char *m_begin, *m_end, *m_cursor; std::byte *const m_begin;
std::byte *const m_end;
std::byte *m_cursor;
}; };
} }

View File

@ -67,7 +67,7 @@ null::deallocate (void *ptr, size_t bytes, size_t align)
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
void* std::byte*
null::begin (void) null::begin (void)
{ {
return nullptr; return nullptr;
@ -75,13 +75,25 @@ null::begin (void)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
const void* const std::byte*
null::begin (void) const null::begin (void) const
{ {
return nullptr; return nullptr;
} }
//-----------------------------------------------------------------------------
std::byte*
null::end (void)
{ return nullptr; }
//-----------------------------------------------------------------------------
const std::byte*
null::end (void) const
{ return nullptr; }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
size_t size_t
null::offset (const void *ptr) const null::offset (const void *ptr) const

View File

@ -17,6 +17,8 @@
#ifndef CRUFT_UTIL_ALLOC_RAW_NULL_HPP #ifndef CRUFT_UTIL_ALLOC_RAW_NULL_HPP
#define CRUFT_UTIL_ALLOC_RAW_NULL_HPP #define CRUFT_UTIL_ALLOC_RAW_NULL_HPP
#include "../../view.hpp"
#include <cstddef> #include <cstddef>
@ -35,8 +37,14 @@ namespace util::alloc::raw {
void deallocate (void *ptr, size_t bytes); void deallocate (void *ptr, size_t bytes);
void deallocate (void *ptr, size_t bytes, size_t align); void deallocate (void *ptr, size_t bytes, size_t align);
void* begin (void); util::view<std::byte*> data (void);
const void* begin (void) const; util::view<const std::byte*> 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; size_t offset (const void*) const;
void reset (void); void reset (void);

View File

@ -25,9 +25,9 @@ using util::alloc::raw::stack;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
stack::stack (void *begin, void *end): stack::stack (void *begin, void *end):
m_begin (reinterpret_cast<char*> (begin)), m_begin (reinterpret_cast<std::byte*> (begin)),
m_end (reinterpret_cast<char*> (end)), m_end (reinterpret_cast<std::byte*> (end)),
m_cursor (reinterpret_cast<char*> (begin)) m_cursor (m_begin)
{ {
CHECK_LE (m_begin, m_end); CHECK_LE (m_begin, m_end);
} }
@ -37,8 +37,8 @@ stack::stack (void *begin, void *end):
union record { union record {
using offset_t = uint32_t; using offset_t = uint32_t;
char *as_bytes; std::byte *as_bytes;
offset_t *as_offset; offset_t *as_offset;
}; };
@ -100,7 +100,7 @@ stack::deallocate (void *_ptr, size_t bytes, size_t alignment)
(void)alignment; (void)alignment;
//alignment = util::max (MIN_ALIGNMENT, alignment); //alignment = util::max (MIN_ALIGNMENT, alignment);
auto ptr = reinterpret_cast<char*> (_ptr); auto ptr = reinterpret_cast<std::byte*> (_ptr);
record record; record record;
record.as_bytes = ptr - sizeof (record::offset_t); 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) stack::begin (void)
{ {
return m_begin; return m_begin;
@ -122,7 +122,7 @@ stack::begin (void)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
const void* const std::byte*
stack::begin (void) const stack::begin (void) const
{ {
return m_begin; return m_begin;
@ -133,7 +133,7 @@ stack::begin (void) const
size_t size_t
stack::offset (const void *_ptr) const stack::offset (const void *_ptr) const
{ {
auto ptr = reinterpret_cast<const char*> (_ptr); auto ptr = reinterpret_cast<const std::byte*> (_ptr);
CHECK_GE (ptr, m_begin); CHECK_GE (ptr, m_begin);
return ptr - m_begin; return ptr - m_begin;

View File

@ -17,6 +17,8 @@
#ifndef CRUFT_UTIL_ALLOC_RAW_STACK_HPP #ifndef CRUFT_UTIL_ALLOC_RAW_STACK_HPP
#define CRUFT_UTIL_ALLOC_RAW_STACK_HPP #define CRUFT_UTIL_ALLOC_RAW_STACK_HPP
#include "../../view.hpp"
#include <cstddef> #include <cstddef>
@ -38,8 +40,15 @@ namespace util::alloc::raw {
void deallocate (void *ptr, size_t bytes); void deallocate (void *ptr, size_t bytes);
void deallocate (void *ptr, size_t bytes, size_t alignment); void deallocate (void *ptr, size_t bytes, size_t alignment);
void* begin (void); util::view<std::byte*> data (void);
const void* begin (void) const; util::view<const std::byte*> 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; size_t offset (const void*) const;
void reset (void); void reset (void);
@ -49,7 +58,9 @@ namespace util::alloc::raw {
size_t remain (void) const; size_t remain (void) const;
private: private:
char *m_begin, *m_end, *m_cursor; std::byte *const m_begin;
std::byte *const m_end;
std::byte *m_cursor;
}; };
} }

View File

@ -10,7 +10,7 @@ main (void)
constexpr size_t BUFFER_SIZE = 1024; 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)); util::alloc::raw::linear store (std::begin (memory), std::end (memory));
tap.expect_eq (store.begin (), std::begin (memory), "base pointers match"); tap.expect_eq (store.begin (), std::begin (memory), "base pointers match");

View File

@ -29,8 +29,8 @@ main (void)
// alignment is kinda important, so make it a little easier and ensure // alignment is kinda important, so make it a little easier and ensure
// something suitable right off the bat. // something suitable right off the bat.
alignas (std::max_align_t) char memory[BUFFER_SIZE]; alignas (std::max_align_t) std::byte memory[BUFFER_SIZE];
std::fill (std::begin (memory), std::end (memory), 0); std::fill (std::begin (memory), std::end (memory), std::byte{0});
util::alloc::raw::stack store (memory, memory + BUFFER_AVAILABLE); util::alloc::raw::stack store (memory, memory + BUFFER_AVAILABLE);