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) {
101 template <
typename T,
typename ...Args>
102 T* allocate(
size_t alignment, Args&&... args) {
103 std::lock_guard<decltype(lock_)> g(lock_);
104 auto res = BasePtrAllocator::allocate<T>(alignment
105 , std::forward<Args>(args)...);
118 boost::interprocess::file_lock lock_;
131 template <
typename T,
typename ...Args>
132 T* allocate(
size_t alignment, Args&&... args) {
133 std::lock_guard<decltype(lock_)> g(lock_);
134 return BasePtrAllocator::allocate<T>(alignment
135 , std::forward<Args>(args)...);
146 boost::interprocess::file_lock lock_;
157 template <
typename ...NoUses>
160 template <
typename T,
typename ...Args>
161 T* allocate(
size_t alignment, Args&&... args) {
163 auto space = memalign(alignment,
sizeof(T));
164 return new (space) T(std::forward<Args>(args)...);
167 void* memalign(
size_t alignment,
size_t size) {
168 auto res = ::memalign(alignment, size);
170 throw std::bad_alloc();
175 template <
typename T>
182 void free(
void* ptr){
192 ShmBasePtrAllocator::
193 ShmBasePtrAllocator(
char const* name,
size_t offset,
size_t len,
bool& tryOwn)
199 if (name[0] !=
'/') {
200 name_ = string(
"/") + name;
205 string devName = string(
"/dev/shm") + name_;
208 oflags |= O_CREAT | O_EXCL;
211 int fd = shm_open(name, oflags, 0660);
212 if (fd < 0 && tryOwn) {
214 fd = shm_open(name, oflags, 0660);
220 if (ftruncate(fd, len)) {
221 HMBDC_THROW(std::runtime_error,
222 "error when sizing ipc transport errno=" << errno);
227 stat(devName.c_str(), &st);
228 if (len != (
size_t)st.st_size) {
229 HMBDC_THROW(std::runtime_error,
"file not expected size - unmatch?");
231 void* base = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED
233 if (base != MAP_FAILED) {
234 set(base, len, own_);
236 lock_ = boost::interprocess::file_lock(devName.c_str());
242 HMBDC_THROW(std::runtime_error
243 ,
"error when creating file errno=" << errno);
247 ShmBasePtrAllocator::
248 ~ShmBasePtrAllocator() {
251 shm_unlink(name_.c_str());
256 DevMemBasePtrAllocator::
257 DevMemBasePtrAllocator(
char const* devName,
size_t offset,
size_t len,
bool own)
262 if (offset % getpagesize() || len_ % getpagesize()) {
263 HMBDC_THROW(std::out_of_range,
"address not at page boundary devName=" 264 << devName <<
"@" << offset <<
':' << len_);
266 int oflags = O_RDWR | O_SYNC;
267 fd_ = open(devName, oflags);
268 addr_ = (uint32_t*)mmap(NULL, len_, PROT_READ | PROT_WRITE, MAP_SHARED
270 if (addr_ == MAP_FAILED) {
271 HMBDC_THROW(std::runtime_error
272 ,
"cannot map RegRange " << devName <<
'@' << offset <<
':' << len_);
275 set(addr_, len, own_);
276 lock_ = boost::interprocess::file_lock(devName);
280 DevMemBasePtrAllocator::
281 ~DevMemBasePtrAllocator() {
Definition: TypedString.hpp:84
the default vanilla allocate
Definition: Allocators.hpp:153
similar to ShmBasePtrAllocator but using dev memory
Definition: Allocators.hpp:125
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