1 #include "hmbdc/Copyright.hpp" 4 #include "hmbdc/Compile.hpp" 5 #include "hmbdc/Exception.hpp" 7 #include <boost/align/align.hpp> 8 #include <boost/interprocess/sync/file_lock.hpp> 23 namespace hmbdc {
namespace os {
50 template <
typename T,
typename ...Args>
51 T* allocate(
size_t alignment, Args&&... args) {
52 auto cur = boost::alignment::align(alignment,
sizeof(T), cur_, size_);
53 cur_ = ((
char*)cur) +
sizeof(T);
56 res = ::new (cur)T(std::forward<Args>(args)...);
57 msync(cur,
sizeof(T), MS_SYNC);
59 res =
static_cast<T*
>(cur);
64 void * memalign(
size_t alignment,
size_t size) {
65 auto res = boost::alignment::align(alignment, size, cur_, size_);
66 cur_ = ((
char*)cur_) + size_;
70 template <
typename T>
void unallocate(T* ptr){
78 void set(
void* base,
size_t size,
bool runCtor =
true) {
106 template <
typename T,
typename ...Args>
107 T* allocate(
size_t alignment, Args&&... args) {
108 std::lock_guard<decltype(lock_)> g(lock_);
109 auto res = BasePtrAllocator::allocate<T>(alignment
110 , std::forward<Args>(args)...);
123 boost::interprocess::file_lock lock_;
136 template <
typename T,
typename ...Args>
137 T* allocate(
size_t alignment, Args&&... args) {
138 std::lock_guard<decltype(lock_)> g(lock_);
139 return BasePtrAllocator::allocate<T>(alignment
140 , std::forward<Args>(args)...);
151 boost::interprocess::file_lock lock_;
161 template <
typename ...NoUses>
164 template <
typename T,
typename ...Args>
165 T* allocate(
size_t alignment, Args&&... args) {
167 auto space = memalign(alignment,
sizeof(T));
168 return new (space) T(std::forward<Args>(args)...);
171 void* memalign(
size_t alignment,
size_t size) {
172 auto res = ::memalign(alignment, size);
174 throw std::bad_alloc();
179 template <
typename T>
186 void free(
void* ptr){
196 ShmBasePtrAllocator::
197 ShmBasePtrAllocator(
char const* name,
size_t offset,
size_t len,
int& ownership)
202 if (name[0] !=
'/') {
203 name_ = std::string(
"/") + name;
208 auto devName = std::string(
"/dev/shm") + name_;
216 shm_unlink(name_.c_str());
217 oflags |= O_CREAT | O_EXCL;
218 fd = shm_open(name, oflags, 0660);
220 HMBDC_THROW(std::runtime_error,
221 "error creating fresh ipc transport errno=" << errno);
223 }
else if (ownership < 0) {
224 fd = shm_open(name, oflags, 0660);
226 HMBDC_THROW(std::runtime_error,
227 "error attaching ipc transport errno=" << errno);
230 oflags |= O_CREAT | O_EXCL;
231 fd = shm_open(name, oflags, 0660);
232 if (fd < 0 && errno == EEXIST) {
234 fd = shm_open(name, oflags, 0660);
236 HMBDC_THROW(std::runtime_error,
237 "error attching ipc transport errno=" << errno);
240 }
else if (fd >= 0) {
243 HMBDC_THROW(std::runtime_error,
244 "error creating ipc transport errno=" << errno);
251 if (ftruncate(fd, len)) {
253 HMBDC_THROW(std::runtime_error,
254 "error when sizing ipc transport errno=" << errno);
259 stat(devName.c_str(), &st);
260 if (len != (
size_t)st.st_size) {
262 HMBDC_THROW(std::runtime_error,
"file not expected size - unmatch?");
264 void* base = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED
266 if (base != MAP_FAILED) {
267 set(base, len, own_);
269 lock_ = boost::interprocess::file_lock(devName.c_str());
275 HMBDC_THROW(std::runtime_error
276 ,
"error when creating file errno=" << errno);
277 }
catch (std::runtime_error
const& e) {
278 if (--retry == 0)
throw;
285 ShmBasePtrAllocator::
286 ~ShmBasePtrAllocator() {
289 shm_unlink(name_.c_str());
294 DevMemBasePtrAllocator::
295 DevMemBasePtrAllocator(
char const* devName,
size_t offset,
size_t len,
int ownership)
299 if (offset % getpagesize() || len_ % getpagesize()) {
300 HMBDC_THROW(std::out_of_range,
"address not at page boundary devName=" 301 << devName <<
"@" << offset <<
':' << len_);
303 if (ownership == 0) {
304 HMBDC_THROW(std::out_of_range,
"undefined ownership devName=" 305 << devName <<
"@" << offset <<
':' << len_);
307 int oflags = O_RDWR | O_SYNC;
308 int fd = open(devName, oflags);
309 addr_ = (uint32_t*)mmap(NULL, len_, PROT_READ | PROT_WRITE, MAP_SHARED
311 if (addr_ == MAP_FAILED) {
312 HMBDC_THROW(std::runtime_error
313 ,
"cannot map RegRange " << devName <<
'@' << offset <<
':' << len_);
315 own_ = ownership > 0;
316 set(addr_, len, own_);
317 lock_ = boost::interprocess::file_lock(devName);
322 DevMemBasePtrAllocator::
323 ~DevMemBasePtrAllocator() {
the default vanilla allocate
Definition: Allocators.hpp:157
similar to ShmBasePtrAllocator but using dev memory
Definition: Allocators.hpp:130
helping allocating object and its aggregated objects in a continouse memory
Definition: Allocators.hpp:30
helping allocating object and its aggregated objects in a continouse shared memory ...
Definition: Allocators.hpp:95