1 #include "hmbdc/Copyright.hpp" 4 #include "hmbdc/os/Allocators.hpp" 5 #include "hmbdc/Compile.hpp" 18 #ifndef SMP_CACHE_BYTES 19 #define SMP_CACHE_BYTES 64 22 namespace hmbdc {
namespace pattern {
27 : std::runtime_error(what_arg){}
30 template <
typename Seq>
32 template<
typename Allocator = os::DefaultAllocator>
33 chunk_base_ptr(uint32_t valueTypeSizePower2Num, uint32_t ringSizePower2Num
34 , Allocator& allocator = os::DefaultAllocator::instance)
35 : space_((
char*)allocator.memalign(SMP_CACHE_BYTES, (1u << valueTypeSizePower2Num) * (1u << ringSizePower2Num))
37 , MASK((1u << ringSizePower2Num) - 1)
38 , valueTypeSizePower2Num_(valueTypeSizePower2Num)
39 , freer_([
this, &allocator](){allocator.free(((
char*
const)
this) + space_);}) {
42 static size_t footprint(uint32_t valueTypeSizePower2Num, uint32_t ringSizePower2Num) {
44 + (1u << valueTypeSizePower2Num) * (1u << ringSizePower2Num);
51 void* operator + (
size_t index)
const HMBDC_RESTRICT {
52 return (((
char*
const)
this) + space_) + (index << valueTypeSizePower2Num_) +
sizeof(Seq);
55 Seq* getSeq(
size_t index)
const HMBDC_RESTRICT {
56 return reinterpret_cast<Seq*
>((((
char*
const)
this) + space_) + (index << valueTypeSizePower2Num_));
60 size_t operator - (
void const* HMBDC_RESTRICT from,
chunk_base_ptr<Seq> const& HMBDC_RESTRICT start) {
61 return (static_cast<char const*>(from) -
sizeof(Seq) - (((
char*
const)&start) + start.space_))
62 >> start.valueTypeSizePower2Num_;
64 std::ptrdiff_t
const space_;
66 uint32_t
const valueTypeSizePower2Num_;
67 std::function<void()> freer_;
70 } __attribute__((__aligned__(SMP_CACHE_BYTES)));
72 template <
typename Seq>
80 : start_(&start), seq_(seq){}
81 iterator() : start_(
nullptr), seq_(0){}
82 Seq seq()
const {
return seq_;}
83 void clear() {start_ =
nullptr;}
85 iterator& operator ++() HMBDC_RESTRICT {
90 iterator operator ++(
int) HMBDC_RESTRICT {
96 iterator operator + (
size_t dis)
const HMBDC_RESTRICT {
102 size_t operator - (
iterator const& other)
const HMBDC_RESTRICT {
103 return seq_ - other.seq_;
106 explicit operator bool()
const HMBDC_RESTRICT {
110 bool operator < (
iterator const& other)
const HMBDC_RESTRICT {
111 return seq_ < other.seq_;
114 bool operator == (
iterator const& other)
const HMBDC_RESTRICT {
return seq_ == other.seq_;}
115 bool operator != (
iterator const& other)
const HMBDC_RESTRICT {
return seq_ != other.seq_;}
116 void* operator*()
const HMBDC_RESTRICT {
return *start_ + (seq_ & start_->MASK);}
117 template <
typename T> T&
get()
const HMBDC_RESTRICT {
return *
static_cast<T*
>(**this); }
118 template <
typename T>
119 T* operator->() HMBDC_RESTRICT {
return static_cast<T*
>(*start_ + (seq_ & start_->MASK));}
Definition: LockFreeBufferMisc.hpp:31
Definition: LockFreeBufferMisc.hpp:25
Definition: LockFreeBufferMisc.hpp:73