hmbdc
simplify-high-performance-messaging-programming
Allocators.hpp
1 #include "hmbdc/Copyright.hpp"
2 #pragma once
3 
4 #include "hmbdc/Compile.hpp"
5 
6 #include <boost/align/align.hpp>
7 #include <boost/interprocess/sync/file_lock.hpp>
8 
9 #include <iostream>
10 #include <utility>
11 #include <mutex>
12 
13 #include <fcntl.h> /* For O_* constants */
14 #include <malloc.h>
15 
16 
17 namespace hmbdc { namespace os {
18 
19 /**
20  * @brief helping allocating object and its aggregated objects in a continouse memory
21  * @details the class working with this cannot have significant dtor (dtor that does more than
22  * just freeing memory)
23  */
25  enum {
26  fromHeap = false,
27  };
29  : cur_(NULL)
30  , size_(0)
31  , runCtor_(false)
32  {}
33 
34  BasePtrAllocator(void* base, size_t size, bool runCtor /* = true */)
35  : cur_((char*)base)
36  , size_(size)
37  , runCtor_(runCtor)
38  {}
39 
40  BasePtrAllocator(BasePtrAllocator const&) = delete;
41  BasePtrAllocator& operator = (BasePtrAllocator const&) = delete;
42  virtual ~BasePtrAllocator(){}
43 
44  template <typename T, typename ...Args>
45  T* allocate(size_t alignment, Args&&... args) {
46  auto cur = boost::alignment::align(alignment, sizeof(T), cur_, size_);
47  T *res;
48  if (runCtor_) {
49  cur_ = ((char*)cur) + sizeof(T);
50  res = ::new (cur)T(std::forward<Args>(args)...);
51  } else {
52  res = static_cast<T*>(cur);
53  }
54  return res;
55  }
56 
57  void * memalign(size_t alignment, size_t size) {
58  auto res = boost::alignment::align(alignment, size, cur_, size_);
59  cur_ = ((char*)cur_) + size_;
60  return res;
61  }
62 
63  template <typename T> void unallocate(T* ptr){
64  if (runCtor_) {
65  ptr->~T();
66  }
67  }
68  void free(void*){}
69 
70 protected:
71  void set(void* base, size_t size, bool runCtor = true) {
72  cur_= base;
73  size_ = size;
74  runCtor_ = runCtor;
75  }
76 
77 private:
78  void* cur_;
79  size_t size_;
80  bool runCtor_;
81 };
82 
83 /**
84  * @brief helping allocating object and its aggregated objects in a continouse shared memory
85  * @details the class working with this cannot have significant dtor (dtor that does more than
86  * just freeing memory)
87  */
90  ShmBasePtrAllocator(char const* name, size_t offset, size_t len
91  , int oflags /* = O_RDWR | O_CREAT | O_EXCL */);
93 
94  template <typename T, typename ...Args>
95  T* allocate(size_t alignment, Args&&... args) {
96  std::lock_guard<decltype(lock_)> g(lock_);
97  return BasePtrAllocator::allocate<T>(alignment
98  , std::forward<Args>(args)...);
99  }
100 
101  auto& fileLock() {
102  return lock_;
103  }
104 
105 private:
106  std::string name_;
107  size_t len_;
108  void* addr_;
109  bool own_;
110  boost::interprocess::file_lock lock_;
111 };
112 
113 /**
114  * @brief similar to ShmBasePtrAllocator but using dev memory
115  * @details typically PCIe BAR
116  */
119  DevMemBasePtrAllocator(char const* devName, size_t offset, size_t len
120  , int oflags /* = O_RDWR | O_CREAT | O_SYNC */);
122 
123  template <typename T, typename ...Args>
124  T* allocate(size_t alignment, Args&&... args) {
125  std::lock_guard<decltype(lock_)> g(lock_);
126  return BasePtrAllocator::allocate<T>(alignment
127  , std::forward<Args>(args)...);
128  }
129 
130  auto& fileLock() {
131  return lock_;
132  }
133 
134 private:
135  size_t len_;
136  void* addr_;
137  bool own_;
138  boost::interprocess::file_lock lock_;
139  int fd_ = -1;
140 };
141 
142 /**
143  * @brief the default vanilla allocate
144  */
146  enum {
147  fromHeap = true,
148  };
149  template <typename ...NoUses>
150  DefaultAllocator(NoUses&& ...){}
151 
152  template <typename T, typename ...Args>
153  T* allocate(size_t alignment, Args&&... args) {
154  // return new T(std::forward<Args>(args)...);
155  auto space = memalign(alignment, sizeof(T));
156  return new (space) T(std::forward<Args>(args)...);
157  }
158 
159  void* memalign(size_t alignment, size_t size) {
160  auto res = ::memalign(alignment, size);
161  if (!res) {
162  throw std::bad_alloc();
163  }
164  return res;
165  }
166 
167  template <typename T>
168  void
169  unallocate(T* ptr){
170  ptr->~T();
171  this->free(ptr);
172  // delete ptr;
173  }
174  void free(void* ptr){
175  ::free(ptr);
176  }
177  static DefaultAllocator instance;
178 };
179 
180 }}
181 
the default vanilla allocate
Definition: Allocators.hpp:145
similar to ShmBasePtrAllocator but using dev memory
Definition: Allocators.hpp:117
helping allocating object and its aggregated objects in a continouse memory
Definition: Allocators.hpp:24
helping allocating object and its aggregated objects in a continouse shared memory ...
Definition: Allocators.hpp:88
Definition: Base.hpp:13