File indexing completed on 2026-09-23 08:50:11
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_INTERPROCESS_MAPPED_REGION_HPP
0012 #define BOOST_INTERPROCESS_MAPPED_REGION_HPP
0013
0014 #ifndef BOOST_CONFIG_HPP
0015 # include <boost/config.hpp>
0016 #endif
0017 0018 ">#
0019 #if defined(BOOST_HAS_PRAGMA_ONCE)
0020 # pragma once
0021 #endif
0022
0023 #include <boost/interprocess/detail/config_begin.hpp>
0024 #include <boost/interprocess/detail/workaround.hpp>
0025
0026 #include <boost/interprocess/interprocess_fwd.hpp>
0027 #include <boost/interprocess/exceptions.hpp>
0028 #include <boost/move/utility_core.hpp>
0029 #include <boost/interprocess/detail/utilities.hpp>
0030 #include <boost/interprocess/detail/os_file_functions.hpp>
0031 #include <string>
0032 #include <boost/cstdint.hpp>
0033 #include <boost/assert.hpp>
0034 #include <boost/move/adl_move_swap.hpp>
0035
0036
0037
0038 #if defined(sun) || defined(__sun) || defined(__osf__) || defined(__osf) || defined(_hpux) || defined(hpux) || defined(_AIX)
0039 #define BOOST_INTERPROCESS_MADVISE_USES_CADDR_T
0040 #include <sys/types.h>
0041 #endif
0042
0043
0044
0045 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
0046 #define BOOST_INTERPROCESS_MADV_DONTNEED_HAS_NONDESTRUCTIVE_SEMANTICS
0047 #endif
0048
0049 #if defined (BOOST_INTERPROCESS_WINDOWS)
0050 # include <boost/interprocess/detail/win32_api.hpp>
0051 #else
0052 # ifdef BOOST_HAS_UNISTD_H
0053 # include <fcntl.h>
0054 # include <sys/mman.h> //mmap
0055 # include <unistd.h>
0056 # include <sys/stat.h>
0057 # include <sys/types.h>
0058 # if defined(BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS)
0059 # include <sys/shm.h> //System V shared memory...
0060 # endif
0061 # include <boost/assert.hpp>
0062 # else
0063 # error Unknown platform
0064 # endif
0065
0066 #endif
0067
0068
0069
0070
0071 namespace boost {
0072 namespace interprocess {
0073
0074 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0075
0076
0077
0078 #if (defined(sun) || defined(__sun)) && defined(MADV_NORMAL)
0079 extern "C" int madvise(caddr_t, size_t, int);
0080 #endif
0081
0082 namespace ipcdetail{ class interprocess_tester; }
0083 namespace ipcdetail{ class raw_mapped_region_creator; }
0084
0085 #endif
0086
0087
0088
0089
0090
0091
0092
0093 class mapped_region
0094 {
0095 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0096
0097 BOOST_MOVABLE_BUT_NOT_COPYABLE(mapped_region)
0098 #endif
0099
0100 public:
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128 template<class MemoryMappable>
0129 mapped_region(const MemoryMappable& mapping
0130 ,mode_t mode
0131 ,offset_t offset = 0
0132 ,std::size_t size = 0
0133 ,const void *address = 0
0134 ,map_options_t map_options = default_map_options);
0135
0136
0137
0138
0139 mapped_region() BOOST_NOEXCEPT;
0140
0141
0142
0143 mapped_region(BOOST_RV_REF(mapped_region) other) BOOST_NOEXCEPT
0144 #if defined (BOOST_INTERPROCESS_WINDOWS)
0145 : m_base(0), m_size(0)
0146 , m_page_offset(0)
0147 , m_mode(read_only)
0148 , m_file_or_mapping_hnd(ipcdetail::invalid_file())
0149 #else
0150 : m_base(0), m_size(0), m_page_offset(0), m_mode(read_only), m_is_xsi(false)
0151 #endif
0152 { this->swap(other); }
0153
0154
0155
0156 ~mapped_region();
0157
0158
0159
0160 mapped_region &operator=(BOOST_RV_REF(mapped_region) other) BOOST_NOEXCEPT
0161 {
0162 mapped_region tmp(boost::move(other));
0163 this->swap(tmp);
0164 return *this;
0165 }
0166
0167
0168
0169 void swap(mapped_region &other) BOOST_NOEXCEPT;
0170
0171
0172 std::size_t get_size() const BOOST_NOEXCEPT;
0173
0174
0175
0176 void* get_address() const BOOST_NOEXCEPT;
0177
0178
0179
0180 mode_t get_mode() const BOOST_NOEXCEPT;
0181
0182
0183
0184
0185
0186
0187 bool flush(std::size_t mapping_offset = 0, std::size_t numbytes = 0, bool async = true);
0188
0189
0190
0191
0192
0193
0194
0195 bool shrink_by(std::size_t bytes, bool from_back = true);
0196
0197
0198
0199 enum advice_types{
0200
0201
0202 advice_normal,
0203
0204
0205
0206 advice_sequential,
0207
0208
0209 advice_random,
0210
0211
0212 advice_willneed,
0213
0214
0215 advice_dontneed
0216 };
0217
0218
0219
0220
0221
0222
0223 bool advise(advice_types advise);
0224
0225
0226
0227
0228 static std::size_t get_page_size() BOOST_NOEXCEPT;
0229
0230 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0231 private:
0232
0233 void priv_close();
0234
0235 void* priv_map_address() const;
0236 std::size_t priv_map_size() const;
0237 bool priv_flush_param_check(std::size_t mapping_offset, void *&addr, std::size_t &numbytes) const;
0238 bool priv_shrink_param_check(std::size_t bytes, bool from_back, void *&shrink_page_start, std::size_t &shrink_page_bytes);
0239 static void priv_size_from_mapping_size
0240 (offset_t mapping_size, offset_t offset, offset_t page_offset, std::size_t &size);
0241 static offset_t priv_page_offset_addr_fixup(offset_t page_offset, const void *&addr);
0242
0243 template<int dummy>
0244 struct page_size_holder
0245 {
0246 static const std::size_t PageSize;
0247 static std::size_t get_page_size();
0248 };
0249
0250 void* m_base;
0251 std::size_t m_size;
0252 std::size_t m_page_offset;
0253 mode_t m_mode;
0254 #if defined(BOOST_INTERPROCESS_WINDOWS)
0255 file_handle_t m_file_or_mapping_hnd;
0256 #else
0257 bool m_is_xsi;
0258 #endif
0259
0260 friend class ipcdetail::interprocess_tester;
0261 friend class ipcdetail::raw_mapped_region_creator;
0262 void dont_close_on_destruction();
0263 #if defined(BOOST_INTERPROCESS_WINDOWS) && !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION)
0264 template<int Dummy>
0265 static void destroy_syncs_in_range(const void *addr, std::size_t size);
0266 #endif
0267 #endif
0268 };
0269
0270 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0271
0272 inline void swap(mapped_region &x, mapped_region &y) BOOST_NOEXCEPT
0273 { x.swap(y); }
0274
0275 inline mapped_region::~mapped_region()
0276 { this->priv_close(); }
0277
0278 inline std::size_t mapped_region::get_size() const BOOST_NOEXCEPT
0279 { return m_size; }
0280
0281 inline mode_t mapped_region::get_mode() const BOOST_NOEXCEPT
0282 { return m_mode; }
0283
0284 inline void* mapped_region::get_address() const BOOST_NOEXCEPT
0285 { return m_base; }
0286
0287 inline void* mapped_region::priv_map_address() const
0288 { return static_cast<char*>(m_base) - m_page_offset; }
0289
0290 inline std::size_t mapped_region::priv_map_size() const
0291 { return m_size + m_page_offset; }
0292
0293 inline bool mapped_region::priv_flush_param_check
0294 (std::size_t mapping_offset, void *&addr, std::size_t &numbytes) const
0295 {
0296
0297 if(m_base == 0)
0298 return false;
0299
0300 if(mapping_offset >= m_size || numbytes > (m_size - size_t(mapping_offset))){
0301 return false;
0302 }
0303
0304
0305 if(numbytes == 0){
0306 numbytes = m_size - mapping_offset;
0307 }
0308 addr = (char*)this->priv_map_address() + mapping_offset;
0309 numbytes += m_page_offset;
0310 return true;
0311 }
0312
0313 inline bool mapped_region::priv_shrink_param_check
0314 (std::size_t bytes, bool from_back, void *&shrink_page_start, std::size_t &shrink_page_bytes)
0315 {
0316
0317 if(m_base == 0 || bytes > m_size){
0318 return false;
0319 }
0320 else if(bytes == m_size){
0321 this->priv_close();
0322 return true;
0323 }
0324 else{
0325 const std::size_t page_size = mapped_region::get_page_size();
0326 if(from_back){
0327 const std::size_t new_pages = (m_size + m_page_offset - bytes - 1)/page_size + 1;
0328 shrink_page_start = static_cast<char*>(this->priv_map_address()) + new_pages*page_size;
0329 shrink_page_bytes = m_page_offset + m_size - new_pages*page_size;
0330 m_size -= bytes;
0331 }
0332 else{
0333 shrink_page_start = this->priv_map_address();
0334 m_page_offset += bytes;
0335 shrink_page_bytes = (m_page_offset/page_size)*page_size;
0336 m_page_offset = m_page_offset % page_size;
0337 m_size -= bytes;
0338 m_base = static_cast<char *>(m_base) + bytes;
0339 BOOST_ASSERT(shrink_page_bytes%page_size == 0);
0340 }
0341 return true;
0342 }
0343 }
0344
0345 inline void mapped_region::priv_size_from_mapping_size
0346 (offset_t mapping_size, offset_t offset, offset_t page_offset, std::size_t &size)
0347 {
0348
0349
0350 if(mapping_size < offset ||
0351 boost::uintmax_t(mapping_size - (offset - page_offset)) >
0352 boost::uintmax_t(std::size_t(-1))){
0353 error_info err(size_error);
0354 throw interprocess_exception(err);
0355 }
0356 size = static_cast<std::size_t>(mapping_size - offset);
0357 }
0358
0359 inline offset_t mapped_region::priv_page_offset_addr_fixup(offset_t offset, const void *&address)
0360 {
0361
0362
0363 const std::size_t page_size = mapped_region::get_page_size();
0364
0365
0366
0367 const std::size_t page_offset =
0368 static_cast<std::size_t>(offset - (offset / offset_t(page_size)) * offset_t(page_size));
0369
0370 if(address){
0371 address = static_cast<const char*>(address) - page_offset;
0372 }
0373 return offset_t(page_offset);
0374 }
0375
0376 #if defined (BOOST_INTERPROCESS_WINDOWS)
0377
0378 inline mapped_region::mapped_region() BOOST_NOEXCEPT
0379 : m_base(0), m_size(0), m_page_offset(0), m_mode(read_only)
0380 , m_file_or_mapping_hnd(ipcdetail::invalid_file())
0381 {}
0382
0383 template<int dummy>
0384 inline std::size_t mapped_region::page_size_holder<dummy>::get_page_size()
0385 {
0386 winapi::interprocess_system_info info;
0387 winapi::get_system_info(&info);
0388 return std::size_t(info.dwAllocationGranularity);
0389 }
0390
0391 template<class MemoryMappable>
0392 inline mapped_region::mapped_region
0393 (const MemoryMappable &mapping
0394 ,mode_t mode
0395 ,offset_t offset
0396 ,std::size_t size
0397 ,const void *address
0398 ,map_options_t map_options)
0399 : m_base(0), m_size(0), m_page_offset(0), m_mode(mode)
0400 , m_file_or_mapping_hnd(ipcdetail::invalid_file())
0401 {
0402 mapping_handle_t mhandle = mapping.get_mapping_handle();
0403 {
0404 file_handle_t native_mapping_handle = 0;
0405
0406
0407
0408 unsigned long protection = 0;
0409
0410 unsigned long map_access = map_options == default_map_options ? 0 : map_options;
0411
0412 switch(mode)
0413 {
0414 case read_only:
0415 case read_private:
0416 protection |= winapi::page_readonly;
0417 map_access |= winapi::file_map_read;
0418 break;
0419 case read_write:
0420 protection |= winapi::page_readwrite;
0421 map_access |= winapi::file_map_write;
0422 break;
0423 case copy_on_write:
0424 protection |= winapi::page_writecopy;
0425 map_access |= winapi::file_map_copy;
0426 break;
0427 default:
0428 {
0429 error_info err(mode_error);
0430 throw interprocess_exception(err);
0431 }
0432 break;
0433 }
0434
0435
0436
0437
0438
0439
0440 void * handle_to_close = winapi::invalid_handle_value;
0441 if(!mhandle.is_shm){
0442
0443 native_mapping_handle = winapi::create_file_mapping
0444 ( ipcdetail::file_handle_from_mapping_handle(mapping.get_mapping_handle())
0445 , protection, 0, (char*)0, 0);
0446
0447
0448 if(!native_mapping_handle){
0449 error_info err ((int)winapi::get_last_error());
0450 throw interprocess_exception(err);
0451 }
0452 handle_to_close = native_mapping_handle;
0453 }
0454 else{
0455
0456
0457 native_mapping_handle = mhandle.handle;
0458 }
0459
0460 const winapi::handle_closer close_handle(handle_to_close);
0461 (void)close_handle;
0462
0463 const offset_t page_offset = priv_page_offset_addr_fixup(offset, address);
0464
0465
0466 if(size == 0){
0467 offset_t mapping_size;
0468 if(!winapi::get_file_mapping_size(native_mapping_handle, mapping_size)){
0469 error_info err((int)winapi::get_last_error());
0470 throw interprocess_exception(err);
0471 }
0472
0473 priv_size_from_mapping_size(mapping_size, offset, page_offset, size);
0474 }
0475
0476
0477 void *base = winapi::map_view_of_file_ex
0478 (native_mapping_handle,
0479 map_access,
0480 ::boost::ulong_long_type(offset - page_offset),
0481 static_cast<std::size_t>(page_offset) + size,
0482 const_cast<void*>(address));
0483
0484 if(!base){
0485 error_info err((int)winapi::get_last_error());
0486 throw interprocess_exception(err);
0487 }
0488
0489
0490 m_base = static_cast<char*>(base) + page_offset;
0491 m_page_offset = static_cast<std::size_t>(page_offset);
0492 m_size = size;
0493 }
0494
0495
0496
0497
0498 if(!winapi::duplicate_current_process_handle(mhandle.handle, &m_file_or_mapping_hnd)){
0499 error_info err((int)winapi::get_last_error());
0500 this->priv_close();
0501 throw interprocess_exception(err);
0502 }
0503 }
0504
0505 inline bool mapped_region::flush(std::size_t mapping_offset, std::size_t numbytes, bool async)
0506 {
0507 void *addr;
0508 if(!this->priv_flush_param_check(mapping_offset, addr, numbytes)){
0509 return false;
0510 }
0511
0512 if(!winapi::flush_view_of_file(addr, numbytes)){
0513 return false;
0514 }
0515
0516
0517 else if(!async && m_file_or_mapping_hnd != winapi::invalid_handle_value &&
0518 winapi::get_file_type(m_file_or_mapping_hnd) == winapi::file_type_disk){
0519 return winapi::flush_file_buffers(m_file_or_mapping_hnd);
0520 }
0521 return true;
0522 }
0523
0524 inline bool mapped_region::shrink_by(std::size_t bytes, bool from_back)
0525 {
0526 void *shrink_page_start = 0;
0527 std::size_t shrink_page_bytes = 0;
0528 if(!this->priv_shrink_param_check(bytes, from_back, shrink_page_start, shrink_page_bytes)){
0529 return false;
0530 }
0531 else if(shrink_page_bytes){
0532
0533
0534
0535 unsigned long old_protect_ignored;
0536 bool b_ret = winapi::virtual_unlock(shrink_page_start, shrink_page_bytes)
0537 || (winapi::get_last_error() == winapi::error_not_locked);
0538 (void)old_protect_ignored;
0539
0540 b_ret = b_ret && winapi::virtual_protect
0541 (shrink_page_start, shrink_page_bytes, winapi::page_noaccess, old_protect_ignored);
0542 return b_ret;
0543 }
0544 else{
0545 return true;
0546 }
0547 }
0548
0549 inline bool mapped_region::advise(advice_types)
0550 {
0551
0552 return false;
0553 }
0554
0555 inline void mapped_region::priv_close()
0556 {
0557 if(m_base){
0558 void *addr = this->priv_map_address();
0559 #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION)
0560 mapped_region::destroy_syncs_in_range<0>(addr, m_size);
0561 #endif
0562 winapi::unmap_view_of_file(addr);
0563 m_base = 0;
0564 }
0565 if(m_file_or_mapping_hnd != ipcdetail::invalid_file()){
0566 winapi::close_handle(m_file_or_mapping_hnd);
0567 m_file_or_mapping_hnd = ipcdetail::invalid_file();
0568 }
0569 }
0570
0571 inline void mapped_region::dont_close_on_destruction()
0572 {}
0573
0574 #else
0575
0576 inline mapped_region::mapped_region() BOOST_NOEXCEPT
0577 : m_base(0), m_size(0), m_page_offset(0), m_mode(read_only), m_is_xsi(false)
0578 {}
0579
0580 template<int dummy>
0581 inline std::size_t mapped_region::page_size_holder<dummy>::get_page_size()
0582 { return std::size_t(sysconf(_SC_PAGESIZE)); }
0583
0584 template<class MemoryMappable>
0585 inline mapped_region::mapped_region
0586 ( const MemoryMappable &mapping
0587 , mode_t mode
0588 , offset_t offset
0589 , std::size_t size
0590 , const void *address
0591 , map_options_t map_options)
0592 : m_base(0), m_size(0), m_page_offset(0), m_mode(mode), m_is_xsi(false)
0593 {
0594 mapping_handle_t map_hnd = mapping.get_mapping_handle();
0595
0596
0597 #ifdef BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS
0598 if(map_hnd.is_xsi){
0599
0600 ::shmid_ds xsi_ds;
0601 int ret = ::shmctl(map_hnd.handle, IPC_STAT, &xsi_ds);
0602 if(ret == -1){
0603 error_info err(system_error_code());
0604 throw interprocess_exception(err);
0605 }
0606
0607 if(size == 0){
0608 size = (std::size_t)xsi_ds.shm_segsz;
0609 }
0610 else if(size != (std::size_t)xsi_ds.shm_segsz){
0611 error_info err(size_error);
0612 throw interprocess_exception(err);
0613 }
0614
0615 int flag = map_options == default_map_options ? 0 : map_options;
0616 if(m_mode == read_only){
0617 flag |= SHM_RDONLY;
0618 }
0619 else if(m_mode != read_write){
0620 error_info err(mode_error);
0621 throw interprocess_exception(err);
0622 }
0623
0624
0625
0626 void *const final_address = const_cast<void *>(address);
0627 void *base = ::shmat(map_hnd.handle, final_address, flag);
0628 if(base == (void*)-1){
0629 error_info err(system_error_code());
0630 throw interprocess_exception(err);
0631 }
0632
0633 m_base = base;
0634 m_size = size;
0635 m_mode = mode;
0636 m_page_offset = 0;
0637 m_is_xsi = true;
0638 return;
0639 }
0640 #endif
0641
0642
0643 const offset_t page_offset = priv_page_offset_addr_fixup(offset, address);
0644
0645 if(size == 0){
0646 struct ::stat buf;
0647 if(0 != fstat(map_hnd.handle, &buf)){
0648 error_info err(system_error_code());
0649 throw interprocess_exception(err);
0650 }
0651
0652 priv_size_from_mapping_size(buf.st_size, offset, page_offset, size);
0653 }
0654
0655 #ifdef MAP_NOSYNC
0656 #define BOOST_INTERPROCESS_MAP_NOSYNC MAP_NOSYNC
0657 #else
0658 #define BOOST_INTERPROCESS_MAP_NOSYNC 0
0659 #endif
0660
0661
0662 int prot = 0;
0663 int flags = map_options == default_map_options ? BOOST_INTERPROCESS_MAP_NOSYNC : map_options;
0664
0665 #undef BOOST_INTERPROCESS_MAP_NOSYNC
0666
0667 switch(mode)
0668 {
0669 case read_only:
0670 prot |= PROT_READ;
0671 flags |= MAP_SHARED;
0672 break;
0673
0674 case read_private:
0675 prot |= (PROT_READ);
0676 flags |= MAP_PRIVATE;
0677 break;
0678
0679 case read_write:
0680 prot |= (PROT_WRITE | PROT_READ);
0681 flags |= MAP_SHARED;
0682 break;
0683
0684 case copy_on_write:
0685 prot |= (PROT_WRITE | PROT_READ);
0686 flags |= MAP_PRIVATE;
0687 break;
0688
0689 default:
0690 {
0691 error_info err(mode_error);
0692 throw interprocess_exception(err);
0693 }
0694 break;
0695 }
0696
0697
0698 void* base = mmap ( const_cast<void*>(address)
0699 , static_cast<std::size_t>(page_offset) + size
0700 , prot
0701 , flags
0702 , mapping.get_mapping_handle().handle
0703 , offset - page_offset);
0704
0705
0706 if(base == MAP_FAILED){
0707 error_info err = system_error_code();
0708 throw interprocess_exception(err);
0709 }
0710
0711
0712 m_base = static_cast<char*>(base) + page_offset;
0713 m_page_offset = static_cast<std::size_t>(page_offset);
0714 m_size = size;
0715
0716
0717 if(address && (base != address)){
0718 error_info err(busy_error);
0719 this->priv_close();
0720 throw interprocess_exception(err);
0721 }
0722 }
0723
0724 inline bool mapped_region::shrink_by(std::size_t bytes, bool from_back)
0725 {
0726 void *shrink_page_start = 0;
0727 std::size_t shrink_page_bytes = 0;
0728 if(m_is_xsi || !this->priv_shrink_param_check(bytes, from_back, shrink_page_start, shrink_page_bytes)){
0729 return false;
0730 }
0731 else if(shrink_page_bytes){
0732
0733 return 0 == munmap(shrink_page_start, shrink_page_bytes);
0734 }
0735 else{
0736 return true;
0737 }
0738 }
0739
0740 inline bool mapped_region::flush(std::size_t mapping_offset, std::size_t numbytes, bool async)
0741 {
0742 void *addr;
0743 if(m_is_xsi || !this->priv_flush_param_check(mapping_offset, addr, numbytes)){
0744 return false;
0745 }
0746
0747 return msync(addr, numbytes, async ? MS_ASYNC : MS_SYNC) == 0;
0748 }
0749
0750 inline bool mapped_region::advise(advice_types advice)
0751 {
0752 int unix_advice = 0;
0753
0754 const unsigned int mode_none = 0;
0755 const unsigned int mode_padv = 1;
0756 const unsigned int mode_madv = 2;
0757
0758 (void)mode_padv;
0759 (void)mode_madv;
0760 unsigned int mode = mode_none;
0761
0762 switch(advice){
0763 case advice_normal:
0764 #if defined(POSIX_MADV_NORMAL)
0765 unix_advice = POSIX_MADV_NORMAL;
0766 mode = mode_padv;
0767 #elif defined(MADV_NORMAL)
0768 unix_advice = MADV_NORMAL;
0769 mode = mode_madv;
0770 #endif
0771 break;
0772 case advice_sequential:
0773 #if defined(POSIX_MADV_SEQUENTIAL)
0774 unix_advice = POSIX_MADV_SEQUENTIAL;
0775 mode = mode_padv;
0776 #elif defined(MADV_SEQUENTIAL)
0777 unix_advice = MADV_SEQUENTIAL;
0778 mode = mode_madv;
0779 #endif
0780 break;
0781 case advice_random:
0782 #if defined(POSIX_MADV_RANDOM)
0783 unix_advice = POSIX_MADV_RANDOM;
0784 mode = mode_padv;
0785 #elif defined(MADV_RANDOM)
0786 unix_advice = MADV_RANDOM;
0787 mode = mode_madv;
0788 #endif
0789 break;
0790 case advice_willneed:
0791 #if defined(POSIX_MADV_WILLNEED)
0792 unix_advice = POSIX_MADV_WILLNEED;
0793 mode = mode_padv;
0794 #elif defined(MADV_WILLNEED)
0795 unix_advice = MADV_WILLNEED;
0796 mode = mode_madv;
0797 #endif
0798 break;
0799 case advice_dontneed:
0800 #if defined(POSIX_MADV_DONTNEED)
0801 unix_advice = POSIX_MADV_DONTNEED;
0802 mode = mode_padv;
0803 #elif defined(MADV_DONTNEED) && defined(BOOST_INTERPROCESS_MADV_DONTNEED_HAS_NONDESTRUCTIVE_SEMANTICS)
0804 unix_advice = MADV_DONTNEED;
0805 mode = mode_madv;
0806 #endif
0807 break;
0808 default:
0809 return false;
0810 }
0811 int ret = -1;
0812 switch(mode){
0813 #if defined(POSIX_MADV_NORMAL)
0814 case mode_padv:
0815 {
0816 ret = posix_madvise(this->priv_map_address(), this->priv_map_size(), unix_advice);
0817 #ifdef __CYGWIN__
0818
0819 if (ret == EINVAL)
0820 ret = 0;
0821 #endif
0822 return 0 == ret;
0823 }
0824 #endif
0825 #if defined(MADV_NORMAL)
0826 case mode_madv:
0827 return 0 == madvise(
0828 #if defined(BOOST_INTERPROCESS_MADVISE_USES_CADDR_T)
0829 (caddr_t)
0830 #endif
0831 this->priv_map_address(), this->priv_map_size(), unix_advice);
0832 #endif
0833 default:
0834 return false;
0835 }
0836 }
0837
0838 inline void mapped_region::priv_close()
0839 {
0840 if(m_base != 0){
0841 #ifdef BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS
0842 if(m_is_xsi){
0843 int ret = ::shmdt(m_base);
0844 BOOST_ASSERT(ret == 0);
0845 (void)ret;
0846 return;
0847 }
0848 #endif
0849 munmap(this->priv_map_address(), this->priv_map_size());
0850 m_base = 0;
0851 }
0852 }
0853
0854 inline void mapped_region::dont_close_on_destruction()
0855 { m_base = 0; }
0856
0857 #endif
0858
0859 template<int dummy>
0860 const std::size_t mapped_region::page_size_holder<dummy>::PageSize
0861 = mapped_region::page_size_holder<dummy>::get_page_size();
0862
0863 inline std::size_t mapped_region::get_page_size() BOOST_NOEXCEPT
0864 {
0865 if(!page_size_holder<0>::PageSize)
0866 return page_size_holder<0>::get_page_size();
0867 else
0868 return page_size_holder<0>::PageSize;
0869 }
0870
0871 inline void mapped_region::swap(mapped_region &other) BOOST_NOEXCEPT
0872 {
0873 ::boost::adl_move_swap(this->m_base, other.m_base);
0874 ::boost::adl_move_swap(this->m_size, other.m_size);
0875 ::boost::adl_move_swap(this->m_page_offset, other.m_page_offset);
0876 ::boost::adl_move_swap(this->m_mode, other.m_mode);
0877 #if defined (BOOST_INTERPROCESS_WINDOWS)
0878 ::boost::adl_move_swap(this->m_file_or_mapping_hnd, other.m_file_or_mapping_hnd);
0879 #else
0880 ::boost::adl_move_swap(this->m_is_xsi, other.m_is_xsi);
0881 #endif
0882 }
0883
0884
0885 struct null_mapped_region_function
0886 {
0887 bool operator()(void *, std::size_t , bool) const
0888 { return true; }
0889
0890 static std::size_t get_min_size()
0891 { return 0; }
0892 };
0893
0894 #endif
0895
0896 }
0897 }
0898
0899 #include <boost/interprocess/detail/config_end.hpp>
0900
0901 #endif
0902
0903 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0904
0905 #ifndef BOOST_INTERPROCESS_MAPPED_REGION_EXT_HPP
0906 #define BOOST_INTERPROCESS_MAPPED_REGION_EXT_HPP
0907
0908 #if defined(BOOST_INTERPROCESS_WINDOWS) && !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION)
0909 # include <boost/interprocess/sync/windows/sync_utils.hpp>
0910 # include <boost/interprocess/detail/windows_intermodule_singleton.hpp>
0911
0912 namespace boost {
0913 namespace interprocess {
0914
0915 template<int Dummy>
0916 inline void mapped_region::destroy_syncs_in_range(const void *addr, std::size_t size)
0917 {
0918 ipcdetail::sync_handles &handles =
0919 ipcdetail::windows_intermodule_singleton<ipcdetail::sync_handles>::get();
0920 handles.destroy_syncs_in_range(addr, size);
0921 }
0922
0923 }
0924 }
0925
0926 #endif
0927
0928 #endif
0929
0930 #endif
0931