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 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 the default vanilla allocate
115  */
117  enum {
118  fromHeap = true,
119  };
120  template <typename ...NoUses>
121  DefaultAllocator(NoUses&& ...){}
122 
123  template <typename T, typename ...Args>
124  T* allocate(size_t alignment, Args&&... args) {
125  // return new T(std::forward<Args>(args)...);
126  auto space = memalign(alignment, sizeof(T));
127  return new (space) T(std::forward<Args>(args)...);
128  }
129 
130  void* memalign(size_t alignment, size_t size) {
131  auto res = ::memalign(alignment, size);
132  if (!res) {
133  throw std::bad_alloc();
134  }
135  return res;
136  }
137 
138  template <typename T>
139  void
140  unallocate(T* ptr){
141  ptr->~T();
142  this->free(ptr);
143  // delete ptr;
144  }
145  void free(void* ptr){
146  ::free(ptr);
147  }
148  static DefaultAllocator instance;
149 };
150 
151 }}
152 
the default vanilla allocate
Definition: Allocators.hpp:116
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:12