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