1 #include "hmbdc/Copyright.hpp" 4 #include "hmbdc/os/Allocators.hpp" 5 #include "hmbdc/Compile.hpp" 6 #include "hmbdc/Config.hpp" 7 #include "hmbdc/Exception.hpp" 20 namespace hmbdc {
namespace pattern {
25 : std::runtime_error(what_arg){}
28 template <
typename Seq>
30 template<
typename Allocator = os::DefaultAllocator>
31 chunk_base_ptr(uint32_t valueTypeSizePower2Num, uint32_t ringSizePower2Num
32 , Allocator& allocator = os::DefaultAllocator::instance())
33 : space_((
char*)allocator.memalign(SMP_CACHE_BYTES, (1ul << valueTypeSizePower2Num) * (1ul << ringSizePower2Num))
35 , MASK((1ul << ringSizePower2Num) - 1)
36 , valueTypeSizePower2Num_(valueTypeSizePower2Num)
37 , freer_([
this, &allocator](){allocator.free(((
char*
const)
this) + space_);}) {
38 if (valueTypeSizePower2Num + ringSizePower2Num > 32) {
39 HMBDC_THROW(std::out_of_range,
"failed to allocated space");
43 static size_t footprint(uint32_t valueTypeSizePower2Num, uint32_t ringSizePower2Num) {
45 + (1u << valueTypeSizePower2Num) * (1u << ringSizePower2Num);
52 void* operator + (
size_t index)
const HMBDC_RESTRICT {
53 return (((
char*
const)
this) + space_) + (index << valueTypeSizePower2Num_) +
sizeof(Seq);
56 Seq* getSeq(
size_t index)
const HMBDC_RESTRICT {
57 return reinterpret_cast<Seq*
>((((
char*
const)
this) + space_) + (index << valueTypeSizePower2Num_));
61 size_t operator - (
void const* HMBDC_RESTRICT from,
chunk_base_ptr<Seq> const& HMBDC_RESTRICT start) {
62 return (static_cast<char const*>(from) -
sizeof(Seq) - (((
char*
const)&start) + start.space_))
63 >> start.valueTypeSizePower2Num_;
65 std::ptrdiff_t
const space_;
67 uint32_t
const valueTypeSizePower2Num_;
68 std::function<void()> freer_;
88 template <
typename Seq>
97 : start_(&start), seq_(seq){}
98 iterator() : start_(
nullptr), seq_(0){}
99 Seq seq()
const {
return seq_;}
100 void clear() {start_ =
nullptr;}
102 iterator& operator ++() HMBDC_RESTRICT {
107 iterator operator ++(
int) HMBDC_RESTRICT {
113 iterator operator + (
size_t dis)
const HMBDC_RESTRICT {
119 iterator& operator += (
size_t dis) HMBDC_RESTRICT {
124 size_t operator - (
iterator const& other)
const HMBDC_RESTRICT {
125 return seq_ - other.seq_;
128 explicit operator bool()
const HMBDC_RESTRICT {
132 bool operator < (
iterator const& other)
const HMBDC_RESTRICT {
133 return seq_ < other.seq_;
136 bool operator == (
iterator const& other)
const HMBDC_RESTRICT {
return seq_ == other.seq_;}
137 bool operator != (
iterator const& other)
const HMBDC_RESTRICT {
return seq_ != other.seq_;}
138 void* operator*()
const HMBDC_RESTRICT {
return *start_ + (seq_ & start_->MASK);}
139 template <
typename T> T&
get()
const HMBDC_RESTRICT {
return *
static_cast<T*
>(**this); }
140 template <
typename T>
141 T* operator->() HMBDC_RESTRICT {
return static_cast<T*
>(*start_ + (seq_ & start_->MASK));}
Definition: LockFreeBufferMisc.hpp:29
Definition: LockFreeBufferMisc.hpp:23
Definition: LockFreeBufferMisc.hpp:89