hmbdc
simplify-high-performance-messaging-programming
 All Classes Namespaces Functions Variables Friends Pages
LockFreeBufferMisc.hpp
1 #include "hmbdc/Copyright.hpp"
2 #pragma once
3 
4 #include "hmbdc/os/Allocators.hpp"
5 #include "hmbdc/Compile.hpp"
6 #include "hmbdc/Config.hpp"
7 #include "hmbdc/Exception.hpp"
8 
9 #include <utility>
10 #include <stdexcept>
11 #include <cstddef>
12 #include <functional>
13 
14 #include <stdint.h>
15 #include <stddef.h>
16 #include <malloc.h>
17 #include <stdlib.h>
18 #include <memory.h>
19 
20 namespace hmbdc { namespace pattern {
21 namespace lf_misc {
22 
23 struct DeadConsumer : std::runtime_error {
24  DeadConsumer(const std::string& what_arg)
25  : std::runtime_error(what_arg){}
26 };
27 
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))
34  - (char*)this)
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");
40  }
41  }
42 
43  static size_t footprint(uint32_t valueTypeSizePower2Num, uint32_t ringSizePower2Num) {
44  return sizeof(chunk_base_ptr) + SMP_CACHE_BYTES * 2
45  + (1u << valueTypeSizePower2Num) * (1u << ringSizePower2Num);
46  }
47 
48  ~chunk_base_ptr() {
49  freer_();
50  }
51 
52  void* operator + (size_t index) const HMBDC_RESTRICT {
53  return (((char* const)this) + space_) + (index << valueTypeSizePower2Num_) + sizeof(Seq);
54  }
55 
56  Seq* getSeq(size_t index) const HMBDC_RESTRICT {
57  return reinterpret_cast<Seq*>((((char* const)this) + space_) + (index << valueTypeSizePower2Num_));
58  }
59 
60  friend
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_;
64  }
65  std::ptrdiff_t const space_;
66  Seq const MASK;
67  uint32_t const valueTypeSizePower2Num_;
68  std::function<void()> freer_;
69  chunk_base_ptr(chunk_base_ptr<Seq> const&) = delete;
70  chunk_base_ptr& operator = (chunk_base_ptr<Seq> const&) = delete;
71 
72  // template <typename BinaryOStream>
73  // friend:
74  // BinaryOStream&
75  // operator << (BinaryOStream& ofs, chunk_base_ptr const& c) {
76  // ofs.write(((char* const)&c) + c.space_, (1ul << valueTypeSizePower2Num) * (1ul << ringSizePower2Num));
77  // return ofs;
78  // }
79 
80  // template <typename BinaryIStream>
81  // BinaryIStream&
82  // operator >> (BinaryIStream& ifs, chunk_base_ptr & c) {
83  // ifs.read(((char* const)&c) + c.space_, (1ul << valueTypeSizePower2Num) * (1ul << ringSizePower2Num));
84  // return ifs;
85  // }
86 };
87 
88 template <typename Seq>
89 struct iterator {
90  using Sequence = Seq;
92  ChunkBasePtr const * HMBDC_RESTRICT start_;
93  Seq seq_;
94 
95  //this ctor is only used by LockFreeBuffer itself
96  iterator(ChunkBasePtr const& HMBDC_RESTRICT start, Seq seq)
97  : start_(&start), seq_(seq){}
98  iterator() : start_(nullptr), seq_(0){}
99  Seq seq() const {return seq_;}
100  void clear() {start_ = nullptr;}
101 
102  iterator& operator ++() HMBDC_RESTRICT {
103  ++seq_;
104  return *this;
105  }
106 
107  iterator operator ++(int) HMBDC_RESTRICT {
108  iterator tmp = *this;
109  ++*this;
110  return tmp;
111  }
112 
113  iterator operator + (size_t dis) const HMBDC_RESTRICT {
114  iterator tmp = *this;
115  tmp.seq_ += dis;
116  return tmp;
117  }
118 
119  iterator& operator += (size_t dis) HMBDC_RESTRICT {
120  seq_ += dis;
121  return *this;
122  }
123 
124  size_t operator - (iterator const& other) const HMBDC_RESTRICT {
125  return seq_ - other.seq_;
126  }
127 
128  explicit operator bool() const HMBDC_RESTRICT {
129  return start_;
130  }
131 
132  bool operator < (iterator const& other) const HMBDC_RESTRICT {
133  return seq_ < other.seq_;
134  }
135 
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));}
142 };
143 
144 } //lf_misc
145 }}
Definition: LockFreeBufferMisc.hpp:29
Definition: LockFreeBufferMisc.hpp:23
Definition: LockFreeBufferMisc.hpp:89