mapped_region.hpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_INTERPROCESS_MAPPED_REGION_HPP
  11. #define BOOST_INTERPROCESS_MAPPED_REGION_HPP
  12. #include <boost/interprocess/detail/config_begin.hpp>
  13. #include <boost/interprocess/detail/workaround.hpp>
  14. #include <boost/interprocess/interprocess_fwd.hpp>
  15. #include <boost/interprocess/exceptions.hpp>
  16. #include <boost/move/move.hpp>
  17. #include <boost/interprocess/detail/utilities.hpp>
  18. #include <boost/interprocess/detail/os_file_functions.hpp>
  19. #include <string>
  20. #include <boost/cstdint.hpp>
  21. #include <boost/assert.hpp>
  22. //Some Unixes use caddr_t instead of void * in madvise
  23. // SunOS Tru64 HP-UX AIX
  24. #if defined(sun) || defined(__sun) || defined(__osf__) || defined(__osf) || defined(_hpux) || defined(hpux) || defined(_AIX)
  25. #define BOOST_INTERPROCESS_MADVISE_USES_CADDR_T
  26. #include <sys/types.h>
  27. #endif
  28. //A lot of UNIXes have destructive semantics for MADV_DONTNEED, so
  29. //we need to be careful to allow it.
  30. #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  31. #define BOOST_INTERPROCESS_MADV_DONTNEED_HAS_NONDESTRUCTIVE_SEMANTICS
  32. #endif
  33. #if defined (BOOST_INTERPROCESS_WINDOWS)
  34. # include <boost/interprocess/detail/win32_api.hpp>
  35. # include <boost/interprocess/sync/windows/sync_utils.hpp>
  36. #else
  37. # ifdef BOOST_HAS_UNISTD_H
  38. # include <fcntl.h>
  39. # include <sys/mman.h> //mmap
  40. # include <unistd.h>
  41. # include <sys/stat.h>
  42. # include <sys/types.h>
  43. # if defined(BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS)
  44. # include <sys/shm.h> //System V shared memory...
  45. # endif
  46. # include <boost/assert.hpp>
  47. # else
  48. # error Unknown platform
  49. # endif
  50. #endif //#if (defined BOOST_INTERPROCESS_WINDOWS)
  51. //!\file
  52. //!Describes mapped region class
  53. namespace boost {
  54. namespace interprocess {
  55. /// @cond
  56. //Solaris declares madvise only in some configurations but defines MADV_XXX, a bit confusing.
  57. //Predeclare it here to avoid any compilation error
  58. #if (defined(sun) || defined(__sun)) && defined(MADV_NORMAL)
  59. extern "C" int madvise(caddr_t, size_t, int);
  60. #endif
  61. namespace ipcdetail{ class interprocess_tester; }
  62. namespace ipcdetail{ class raw_mapped_region_creator; }
  63. /// @endcond
  64. //!The mapped_region class represents a portion or region created from a
  65. //!memory_mappable object.
  66. //!
  67. //!The OS can map a region bigger than the requested one, as region must
  68. //!be multiple of the page size, but mapped_region will always refer to
  69. //!the region specified by the user.
  70. class mapped_region
  71. {
  72. /// @cond
  73. //Non-copyable
  74. BOOST_MOVABLE_BUT_NOT_COPYABLE(mapped_region)
  75. /// @endcond
  76. public:
  77. //!Creates a mapping region of the mapped memory "mapping", starting in
  78. //!offset "offset", and the mapping's size will be "size". The mapping
  79. //!can be opened for read only, read-write or copy-on-write.
  80. //!
  81. //!If an address is specified, both the offset and the address must be
  82. //!multiples of the page size.
  83. //!
  84. //!The map is created using "default_map_options". This flag is OS
  85. //!dependant and it should not be changed unless the user needs to
  86. //!specify special options.
  87. //!
  88. //!In Windows systems "map_options" is a DWORD value passed as
  89. //!"dwDesiredAccess" to "MapViewOfFileEx". If "default_map_options" is passed
  90. //!it's initialized to zero. "map_options" is XORed with FILE_MAP_[COPY|READ|WRITE].
  91. //!
  92. //!In UNIX systems and POSIX mappings "map_options" is an int value passed as "flags"
  93. //!to "mmap". If "default_map_options" is specified it's initialized to MAP_NOSYNC
  94. //!if that option exists and to zero otherwise. "map_options" XORed with MAP_PRIVATE or MAP_SHARED.
  95. //!
  96. //!In UNIX systems and XSI mappings "map_options" is an int value passed as "shmflg"
  97. //!to "shmat". If "default_map_options" is specified it's initialized to zero.
  98. //!"map_options" is XORed with SHM_RDONLY if needed.
  99. //!
  100. //!The OS could allocate more pages than size/page_size(), but get_address()
  101. //!will always return the address passed in this function (if not null) and
  102. //!get_size() will return the specified size.
  103. template<class MemoryMappable>
  104. mapped_region(const MemoryMappable& mapping
  105. ,mode_t mode
  106. ,offset_t offset = 0
  107. ,std::size_t size = 0
  108. ,const void *address = 0
  109. ,map_options_t map_options = default_map_options);
  110. //!Default constructor. Address will be 0 (nullptr).
  111. //!Size will be 0.
  112. //!Does not throw
  113. mapped_region();
  114. //!Move constructor. *this will be constructed taking ownership of "other"'s
  115. //!region and "other" will be left in default constructor state.
  116. mapped_region(BOOST_RV_REF(mapped_region) other)
  117. #if defined (BOOST_INTERPROCESS_WINDOWS)
  118. : m_base(0), m_size(0)
  119. , m_page_offset(0)
  120. , m_mode(read_only)
  121. , m_file_or_mapping_hnd(ipcdetail::invalid_file())
  122. #else
  123. : m_base(0), m_size(0), m_page_offset(0), m_mode(read_only), m_is_xsi(false)
  124. #endif
  125. { this->swap(other); }
  126. //!Destroys the mapped region.
  127. //!Does not throw
  128. ~mapped_region();
  129. //!Move assignment. If *this owns a memory mapped region, it will be
  130. //!destroyed and it will take ownership of "other"'s memory mapped region.
  131. mapped_region &operator=(BOOST_RV_REF(mapped_region) other)
  132. {
  133. mapped_region tmp(boost::move(other));
  134. this->swap(tmp);
  135. return *this;
  136. }
  137. //!Swaps the mapped_region with another
  138. //!mapped region
  139. void swap(mapped_region &other);
  140. //!Returns the size of the mapping. Never throws.
  141. std::size_t get_size() const;
  142. //!Returns the base address of the mapping.
  143. //!Never throws.
  144. void* get_address() const;
  145. //!Returns the mode of the mapping used to construct the mapped region.
  146. //!Never throws.
  147. mode_t get_mode() const;
  148. //!Flushes to the disk a byte range within the mapped memory.
  149. //!If 'async' is true, the function will return before flushing operation is completed
  150. //!If 'async' is false, function will return once data has been written into the underlying
  151. //!device (i.e., in mapped files OS cached information is written to disk).
  152. //!Never throws. Returns false if operation could not be performed.
  153. bool flush(std::size_t mapping_offset = 0, std::size_t numbytes = 0, bool async = true);
  154. //!Shrinks current mapped region. If after shrinking there is no longer need for a previously
  155. //!mapped memory page, accessing that page can trigger a segmentation fault.
  156. //!Depending on the OS, this operation might fail (XSI shared memory), it can decommit storage
  157. //!and free a portion of the virtual address space (e.g.POSIX) or this
  158. //!function can release some physical memory wihout freeing any virtual address space(Windows).
  159. //!Returns true on success. Never throws.
  160. bool shrink_by(std::size_t bytes, bool from_back = true);
  161. //!This enum specifies region usage behaviors that an application can specify
  162. //!to the mapped region implementation.
  163. enum advice_types{
  164. //!Specifies that the application has no advice to give on its behavior with respect to
  165. //!the region. It is the default characteristic if no advice is given for a range of memory.
  166. advice_normal,
  167. //!Specifies that the application expects to access the region sequentially from
  168. //!lower addresses to higher addresses. The implementation can lower the priority of
  169. //!preceding pages within the region once a page have been accessed.
  170. advice_sequential,
  171. //!Specifies that the application expects to access the region in a random order,
  172. //!and prefetching is likely not advantageous.
  173. advice_random,
  174. //!Specifies that the application expects to access the region in the near future.
  175. //!The implementation can prefetch pages of the region.
  176. advice_willneed,
  177. //!Specifies that the application expects that it will not access the region in the near future.
  178. //!The implementation can unload pages within the range to save system resources.
  179. advice_dontneed
  180. };
  181. //!Advises the implementation on the expected behavior of the application with respect to the data
  182. //!in the region. The implementation may use this information to optimize handling of the region data.
  183. //!This function has no effect on the semantics of access to memory in the region, although it may affect
  184. //!the performance of access.
  185. //!If the advise type is not known to the implementation, the function returns false. True otherwise.
  186. bool advise(advice_types advise);
  187. //!Returns the size of the page. This size is the minimum memory that
  188. //!will be used by the system when mapping a memory mappable source and
  189. //!will restrict the address and the offset to map.
  190. static std::size_t get_page_size();
  191. /// @cond
  192. private:
  193. //!Closes a previously opened memory mapping. Never throws
  194. void priv_close();
  195. void* priv_map_address() const;
  196. std::size_t priv_map_size() const;
  197. bool priv_flush_param_check(std::size_t mapping_offset, void *&addr, std::size_t &numbytes) const;
  198. bool priv_shrink_param_check(std::size_t bytes, bool from_back, void *&shrink_page_start, std::size_t &shrink_page_bytes);
  199. static void priv_size_from_mapping_size
  200. (offset_t mapping_size, offset_t offset, offset_t page_offset, std::size_t &size);
  201. static offset_t priv_page_offset_addr_fixup(offset_t page_offset, const void *&addr);
  202. template<int dummy>
  203. struct page_size_holder
  204. {
  205. static const std::size_t PageSize;
  206. static std::size_t get_page_size();
  207. };
  208. void* m_base;
  209. std::size_t m_size;
  210. std::size_t m_page_offset;
  211. mode_t m_mode;
  212. #if defined(BOOST_INTERPROCESS_WINDOWS)
  213. file_handle_t m_file_or_mapping_hnd;
  214. #else
  215. bool m_is_xsi;
  216. #endif
  217. friend class ipcdetail::interprocess_tester;
  218. friend class ipcdetail::raw_mapped_region_creator;
  219. void dont_close_on_destruction();
  220. #if defined(BOOST_INTERPROCESS_WINDOWS) && !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION)
  221. template<int Dummy>
  222. static void destroy_syncs_in_range(const void *addr, std::size_t size);
  223. #endif
  224. /// @endcond
  225. };
  226. ///@cond
  227. inline void swap(mapped_region &x, mapped_region &y)
  228. { x.swap(y); }
  229. inline mapped_region::~mapped_region()
  230. { this->priv_close(); }
  231. inline std::size_t mapped_region::get_size() const
  232. { return m_size; }
  233. inline mode_t mapped_region::get_mode() const
  234. { return m_mode; }
  235. inline void* mapped_region::get_address() const
  236. { return m_base; }
  237. inline void* mapped_region::priv_map_address() const
  238. { return static_cast<char*>(m_base) - m_page_offset; }
  239. inline std::size_t mapped_region::priv_map_size() const
  240. { return m_size + m_page_offset; }
  241. inline bool mapped_region::priv_flush_param_check
  242. (std::size_t mapping_offset, void *&addr, std::size_t &numbytes) const
  243. {
  244. //Check some errors
  245. if(m_base == 0)
  246. return false;
  247. if(mapping_offset >= m_size || (mapping_offset + numbytes) > m_size){
  248. return false;
  249. }
  250. //Update flush size if the user does not provide it
  251. if(numbytes == 0){
  252. numbytes = m_size - mapping_offset;
  253. }
  254. addr = (char*)this->priv_map_address() + mapping_offset;
  255. numbytes += m_page_offset;
  256. return true;
  257. }
  258. inline bool mapped_region::priv_shrink_param_check
  259. (std::size_t bytes, bool from_back, void *&shrink_page_start, std::size_t &shrink_page_bytes)
  260. {
  261. //Check some errors
  262. if(m_base == 0 || bytes > m_size){
  263. return false;
  264. }
  265. else if(bytes == m_size){
  266. this->priv_close();
  267. return true;
  268. }
  269. else{
  270. const std::size_t page_size = mapped_region::get_page_size();
  271. if(from_back){
  272. const std::size_t new_pages = (m_size + m_page_offset - bytes - 1)/page_size + 1;
  273. shrink_page_start = static_cast<char*>(this->priv_map_address()) + new_pages*page_size;
  274. shrink_page_bytes = m_page_offset + m_size - new_pages*page_size;
  275. m_size -= bytes;
  276. }
  277. else{
  278. shrink_page_start = this->priv_map_address();
  279. m_page_offset += bytes;
  280. shrink_page_bytes = (m_page_offset/page_size)*page_size;
  281. m_page_offset = m_page_offset % page_size;
  282. m_size -= bytes;
  283. m_base = static_cast<char *>(m_base) + bytes;
  284. BOOST_ASSERT(shrink_page_bytes%page_size == 0);
  285. }
  286. return true;
  287. }
  288. }
  289. inline void mapped_region::priv_size_from_mapping_size
  290. (offset_t mapping_size, offset_t offset, offset_t page_offset, std::size_t &size)
  291. {
  292. //Check if mapping size fits in the user address space
  293. //as offset_t is the maximum file size and its signed.
  294. if(mapping_size < offset ||
  295. boost::uintmax_t(mapping_size - (offset - page_offset)) >
  296. boost::uintmax_t(std::size_t(-1))){
  297. error_info err(size_error);
  298. throw interprocess_exception(err);
  299. }
  300. size = static_cast<std::size_t>(mapping_size - (offset - page_offset));
  301. }
  302. inline offset_t mapped_region::priv_page_offset_addr_fixup(offset_t offset, const void *&address)
  303. {
  304. //We can't map any offset so we have to obtain system's
  305. //memory granularity
  306. const std::size_t page_size = mapped_region::get_page_size();
  307. //We calculate the difference between demanded and valid offset
  308. //(always less than a page in std::size_t, thus, representable by std::size_t)
  309. const std::size_t page_offset =
  310. static_cast<std::size_t>(offset - (offset / page_size) * page_size);
  311. //Update the mapping address
  312. if(address){
  313. address = static_cast<const char*>(address) - page_offset;
  314. }
  315. return page_offset;
  316. }
  317. #if defined (BOOST_INTERPROCESS_WINDOWS)
  318. inline mapped_region::mapped_region()
  319. : m_base(0), m_size(0), m_page_offset(0), m_mode(read_only)
  320. , m_file_or_mapping_hnd(ipcdetail::invalid_file())
  321. {}
  322. template<int dummy>
  323. inline std::size_t mapped_region::page_size_holder<dummy>::get_page_size()
  324. {
  325. winapi::system_info info;
  326. get_system_info(&info);
  327. return std::size_t(info.dwAllocationGranularity);
  328. }
  329. template<class MemoryMappable>
  330. inline mapped_region::mapped_region
  331. (const MemoryMappable &mapping
  332. ,mode_t mode
  333. ,offset_t offset
  334. ,std::size_t size
  335. ,const void *address
  336. ,map_options_t map_options)
  337. : m_base(0), m_size(0), m_page_offset(0), m_mode(mode)
  338. , m_file_or_mapping_hnd(ipcdetail::invalid_file())
  339. {
  340. mapping_handle_t mhandle = mapping.get_mapping_handle();
  341. {
  342. file_handle_t native_mapping_handle = 0;
  343. //Set accesses
  344. //For "create_file_mapping"
  345. unsigned long protection = 0;
  346. //For "mapviewoffile"
  347. unsigned long map_access = map_options == default_map_options ? 0 : map_options;
  348. switch(mode)
  349. {
  350. case read_only:
  351. case read_private:
  352. protection |= winapi::page_readonly;
  353. map_access |= winapi::file_map_read;
  354. break;
  355. case read_write:
  356. protection |= winapi::page_readwrite;
  357. map_access |= winapi::file_map_write;
  358. break;
  359. case copy_on_write:
  360. protection |= winapi::page_writecopy;
  361. map_access |= winapi::file_map_copy;
  362. break;
  363. default:
  364. {
  365. error_info err(mode_error);
  366. throw interprocess_exception(err);
  367. }
  368. break;
  369. }
  370. //For file mapping (including emulated shared memory through temporary files),
  371. //the device is a file handle so we need to obtain file's size and call create_file_mapping
  372. //to obtain the mapping handle.
  373. //For files we don't need the file mapping after mapping the memory, as the file is there
  374. //so we'll program the handle close
  375. void * handle_to_close = winapi::invalid_handle_value;
  376. if(!mhandle.is_shm){
  377. //Create mapping handle
  378. native_mapping_handle = winapi::create_file_mapping
  379. ( ipcdetail::file_handle_from_mapping_handle(mapping.get_mapping_handle())
  380. , protection, 0, 0, 0);
  381. //Check if all is correct
  382. if(!native_mapping_handle){
  383. error_info err = winapi::get_last_error();
  384. throw interprocess_exception(err);
  385. }
  386. handle_to_close = native_mapping_handle;
  387. }
  388. else{
  389. //For windows_shared_memory the device handle is already a mapping handle
  390. //and we need to maintain it
  391. native_mapping_handle = mhandle.handle;
  392. }
  393. //RAII handle close on scope exit
  394. const winapi::handle_closer close_handle(handle_to_close);
  395. (void)close_handle;
  396. const offset_t page_offset = priv_page_offset_addr_fixup(offset, address);
  397. //Obtain mapping size if user provides 0 size
  398. if(size == 0){
  399. offset_t mapping_size;
  400. if(!winapi::get_file_mapping_size(native_mapping_handle, mapping_size)){
  401. error_info err = winapi::get_last_error();
  402. throw interprocess_exception(err);
  403. }
  404. //This can throw
  405. priv_size_from_mapping_size(mapping_size, offset, page_offset, size);
  406. }
  407. //Map with new offsets and size
  408. void *base = winapi::map_view_of_file_ex
  409. (native_mapping_handle,
  410. map_access,
  411. offset - page_offset,
  412. static_cast<std::size_t>(page_offset + size),
  413. const_cast<void*>(address));
  414. //Check error
  415. if(!base){
  416. error_info err = winapi::get_last_error();
  417. throw interprocess_exception(err);
  418. }
  419. //Calculate new base for the user
  420. m_base = static_cast<char*>(base) + page_offset;
  421. m_page_offset = page_offset;
  422. m_size = size;
  423. }
  424. //Windows shared memory needs the duplication of the handle if we want to
  425. //make mapped_region independent from the mappable device
  426. //
  427. //For mapped files, we duplicate the file handle to be able to FlushFileBuffers
  428. if(!winapi::duplicate_current_process_handle(mhandle.handle, &m_file_or_mapping_hnd)){
  429. error_info err = winapi::get_last_error();
  430. this->priv_close();
  431. throw interprocess_exception(err);
  432. }
  433. }
  434. inline bool mapped_region::flush(std::size_t mapping_offset, std::size_t numbytes, bool async)
  435. {
  436. void *addr;
  437. if(!this->priv_flush_param_check(mapping_offset, addr, numbytes)){
  438. return false;
  439. }
  440. //Flush it all
  441. if(!winapi::flush_view_of_file(addr, numbytes)){
  442. return false;
  443. }
  444. //m_file_or_mapping_hnd can be a file handle or a mapping handle.
  445. //so flushing file buffers has only sense for files...
  446. else if(!async && m_file_or_mapping_hnd != winapi::invalid_handle_value &&
  447. winapi::get_file_type(m_file_or_mapping_hnd) == winapi::file_type_disk){
  448. return winapi::flush_file_buffers(m_file_or_mapping_hnd);
  449. }
  450. return true;
  451. }
  452. inline bool mapped_region::shrink_by(std::size_t bytes, bool from_back)
  453. {
  454. void *shrink_page_start;
  455. std::size_t shrink_page_bytes;
  456. if(!this->priv_shrink_param_check(bytes, from_back, shrink_page_start, shrink_page_bytes)){
  457. return false;
  458. }
  459. else if(shrink_page_bytes){
  460. //In Windows, we can't decommit the storage or release the virtual address space,
  461. //the best we can do is try to remove some memory from the process working set.
  462. //With a bit of luck we can free some physical memory.
  463. unsigned long old_protect_ignored;
  464. bool b_ret = winapi::virtual_unlock(shrink_page_start, shrink_page_bytes)
  465. || (winapi::get_last_error() == winapi::error_not_locked);
  466. (void)old_protect_ignored;
  467. //Change page protection to forbid any further access
  468. b_ret = b_ret && winapi::virtual_protect
  469. (shrink_page_start, shrink_page_bytes, winapi::page_noaccess, old_protect_ignored);
  470. return b_ret;
  471. }
  472. else{
  473. return true;
  474. }
  475. }
  476. inline bool mapped_region::advise(advice_types)
  477. {
  478. //Windows has no madvise/posix_madvise equivalent
  479. return false;
  480. }
  481. inline void mapped_region::priv_close()
  482. {
  483. if(m_base){
  484. void *addr = this->priv_map_address();
  485. #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION)
  486. mapped_region::destroy_syncs_in_range<0>(addr, m_size);
  487. #endif
  488. winapi::unmap_view_of_file(addr);
  489. m_base = 0;
  490. }
  491. if(m_file_or_mapping_hnd != ipcdetail::invalid_file()){
  492. winapi::close_handle(m_file_or_mapping_hnd);
  493. m_file_or_mapping_hnd = ipcdetail::invalid_file();
  494. }
  495. }
  496. inline void mapped_region::dont_close_on_destruction()
  497. {}
  498. #else //#if (defined BOOST_INTERPROCESS_WINDOWS)
  499. inline mapped_region::mapped_region()
  500. : m_base(0), m_size(0), m_page_offset(0), m_mode(read_only), m_is_xsi(false)
  501. {}
  502. template<int dummy>
  503. inline std::size_t mapped_region::page_size_holder<dummy>::get_page_size()
  504. { return std::size_t(sysconf(_SC_PAGESIZE)); }
  505. template<class MemoryMappable>
  506. inline mapped_region::mapped_region
  507. ( const MemoryMappable &mapping
  508. , mode_t mode
  509. , offset_t offset
  510. , std::size_t size
  511. , const void *address
  512. , map_options_t map_options)
  513. : m_base(0), m_size(0), m_page_offset(0), m_mode(mode), m_is_xsi(false)
  514. {
  515. mapping_handle_t map_hnd = mapping.get_mapping_handle();
  516. //Some systems dont' support XSI shared memory
  517. #ifdef BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS
  518. if(map_hnd.is_xsi){
  519. //Get the size
  520. ::shmid_ds xsi_ds;
  521. int ret = ::shmctl(map_hnd.handle, IPC_STAT, &xsi_ds);
  522. if(ret == -1){
  523. error_info err(system_error_code());
  524. throw interprocess_exception(err);
  525. }
  526. //Compare sizess
  527. if(size == 0){
  528. size = (std::size_t)xsi_ds.shm_segsz;
  529. }
  530. else if(size != (std::size_t)xsi_ds.shm_segsz){
  531. error_info err(size_error);
  532. throw interprocess_exception(err);
  533. }
  534. //Calculate flag
  535. int flag = map_options == default_map_options ? 0 : map_options;
  536. if(m_mode == read_only){
  537. flag |= SHM_RDONLY;
  538. }
  539. else if(m_mode != read_write){
  540. error_info err(mode_error);
  541. throw interprocess_exception(err);
  542. }
  543. //Attach memory
  544. void *base = ::shmat(map_hnd.handle, (void*)address, flag);
  545. if(base == (void*)-1){
  546. error_info err(system_error_code());
  547. throw interprocess_exception(err);
  548. }
  549. //Update members
  550. m_base = base;
  551. m_size = size;
  552. m_mode = mode;
  553. m_page_offset = 0;
  554. m_is_xsi = true;
  555. return;
  556. }
  557. #endif //ifdef BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS
  558. //We calculate the difference between demanded and valid offset
  559. const offset_t page_offset = priv_page_offset_addr_fixup(offset, address);
  560. if(size == 0){
  561. struct ::stat buf;
  562. if(0 != fstat(map_hnd.handle, &buf)){
  563. error_info err(system_error_code());
  564. throw interprocess_exception(err);
  565. }
  566. //This can throw
  567. priv_size_from_mapping_size(buf.st_size, offset, page_offset, size);
  568. }
  569. #ifdef MAP_NOSYNC
  570. #define BOOST_INTERPROCESS_MAP_NOSYNC MAP_NOSYNC
  571. #else
  572. #define BOOST_INTERPROCESS_MAP_NOSYNC 0
  573. #endif //MAP_NOSYNC
  574. //Create new mapping
  575. int prot = 0;
  576. int flags = map_options == default_map_options ? BOOST_INTERPROCESS_MAP_NOSYNC : map_options;
  577. #undef BOOST_INTERPROCESS_MAP_NOSYNC
  578. switch(mode)
  579. {
  580. case read_only:
  581. prot |= PROT_READ;
  582. flags |= MAP_SHARED;
  583. break;
  584. case read_private:
  585. prot |= (PROT_READ);
  586. flags |= MAP_PRIVATE;
  587. break;
  588. case read_write:
  589. prot |= (PROT_WRITE | PROT_READ);
  590. flags |= MAP_SHARED;
  591. break;
  592. case copy_on_write:
  593. prot |= (PROT_WRITE | PROT_READ);
  594. flags |= MAP_PRIVATE;
  595. break;
  596. default:
  597. {
  598. error_info err(mode_error);
  599. throw interprocess_exception(err);
  600. }
  601. break;
  602. }
  603. //Map it to the address space
  604. void* base = mmap ( const_cast<void*>(address)
  605. , static_cast<std::size_t>(page_offset + size)
  606. , prot
  607. , flags
  608. , mapping.get_mapping_handle().handle
  609. , offset - page_offset);
  610. //Check if mapping was successful
  611. if(base == MAP_FAILED){
  612. error_info err = system_error_code();
  613. throw interprocess_exception(err);
  614. }
  615. //Calculate new base for the user
  616. m_base = static_cast<char*>(base) + page_offset;
  617. m_page_offset = page_offset;
  618. m_size = size;
  619. //Check for fixed mapping error
  620. if(address && (base != address)){
  621. error_info err(busy_error);
  622. this->priv_close();
  623. throw interprocess_exception(err);
  624. }
  625. }
  626. inline bool mapped_region::shrink_by(std::size_t bytes, bool from_back)
  627. {
  628. void *shrink_page_start = 0;
  629. std::size_t shrink_page_bytes = 0;
  630. if(m_is_xsi || !this->priv_shrink_param_check(bytes, from_back, shrink_page_start, shrink_page_bytes)){
  631. return false;
  632. }
  633. else if(shrink_page_bytes){
  634. //In UNIX we can decommit and free virtual address space.
  635. return 0 == munmap(shrink_page_start, shrink_page_bytes);
  636. }
  637. else{
  638. return true;
  639. }
  640. }
  641. inline bool mapped_region::flush(std::size_t mapping_offset, std::size_t numbytes, bool async)
  642. {
  643. void *addr;
  644. if(m_is_xsi || !this->priv_flush_param_check(mapping_offset, addr, numbytes)){
  645. return false;
  646. }
  647. //Flush it all
  648. return msync(addr, numbytes, async ? MS_ASYNC : MS_SYNC) == 0;
  649. }
  650. inline bool mapped_region::advise(advice_types advice)
  651. {
  652. int unix_advice = 0;
  653. //Modes; 0: none, 2: posix, 1: madvise
  654. const unsigned int mode_none = 0;
  655. const unsigned int mode_padv = 1;
  656. const unsigned int mode_madv = 2;
  657. unsigned int mode = mode_none;
  658. //Choose advice either from POSIX (preferred) or native Unix
  659. switch(advice){
  660. case advice_normal:
  661. #if defined(POSIX_MADV_NORMAL)
  662. unix_advice = POSIX_MADV_NORMAL;
  663. mode = mode_padv;
  664. #elif defined(MADV_NORMAL)
  665. unix_advice = MADV_NORMAL;
  666. mode = mode_madv;
  667. #endif
  668. break;
  669. case advice_sequential:
  670. #if defined(POSIX_MADV_SEQUENTIAL)
  671. unix_advice = POSIX_MADV_SEQUENTIAL;
  672. mode = mode_padv;
  673. #elif defined(MADV_SEQUENTIAL)
  674. unix_advice = MADV_SEQUENTIAL;
  675. mode = mode_madv;
  676. #endif
  677. break;
  678. case advice_random:
  679. #if defined(POSIX_MADV_RANDOM)
  680. unix_advice = POSIX_MADV_RANDOM;
  681. mode = mode_padv;
  682. #elif defined(MADV_RANDOM)
  683. unix_advice = MADV_RANDOM;
  684. mode = mode_madv;
  685. #endif
  686. break;
  687. case advice_willneed:
  688. #if defined(POSIX_MADV_WILLNEED)
  689. unix_advice = POSIX_MADV_WILLNEED;
  690. mode = mode_padv;
  691. #elif defined(MADV_WILLNEED)
  692. unix_advice = MADV_WILLNEED;
  693. mode = mode_madv;
  694. #endif
  695. break;
  696. case advice_dontneed:
  697. #if defined(POSIX_MADV_DONTNEED)
  698. unix_advice = POSIX_MADV_DONTNEED;
  699. mode = mode_padv;
  700. #elif defined(MADV_DONTNEED) && defined(BOOST_INTERPROCESS_MADV_DONTNEED_HAS_NONDESTRUCTIVE_SEMANTICS)
  701. unix_advice = MADV_DONTNEED;
  702. mode = mode_madv;
  703. #endif
  704. break;
  705. default:
  706. return false;
  707. }
  708. switch(mode){
  709. #if defined(POSIX_MADV_NORMAL)
  710. case mode_padv:
  711. return 0 == posix_madvise(this->priv_map_address(), this->priv_map_size(), unix_advice);
  712. #endif
  713. #if defined(MADV_NORMAL)
  714. case mode_madv:
  715. return 0 == madvise(
  716. #if defined(BOOST_INTERPROCESS_MADVISE_USES_CADDR_T)
  717. (caddr_t)
  718. #endif
  719. this->priv_map_address(), this->priv_map_size(), unix_advice);
  720. #endif
  721. default:
  722. return false;
  723. }
  724. }
  725. inline void mapped_region::priv_close()
  726. {
  727. if(m_base != 0){
  728. #ifdef BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS
  729. if(m_is_xsi){
  730. int ret = ::shmdt(m_base);
  731. BOOST_ASSERT(ret == 0);
  732. (void)ret;
  733. return;
  734. }
  735. #endif //#ifdef BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS
  736. munmap(this->priv_map_address(), this->priv_map_size());
  737. m_base = 0;
  738. }
  739. }
  740. inline void mapped_region::dont_close_on_destruction()
  741. { m_base = 0; }
  742. #endif //##if (defined BOOST_INTERPROCESS_WINDOWS)
  743. template<int dummy>
  744. const std::size_t mapped_region::page_size_holder<dummy>::PageSize
  745. = mapped_region::page_size_holder<dummy>::get_page_size();
  746. inline std::size_t mapped_region::get_page_size()
  747. {
  748. if(!page_size_holder<0>::PageSize)
  749. return page_size_holder<0>::get_page_size();
  750. else
  751. return page_size_holder<0>::PageSize;
  752. }
  753. inline void mapped_region::swap(mapped_region &other)
  754. {
  755. ipcdetail::do_swap(this->m_base, other.m_base);
  756. ipcdetail::do_swap(this->m_size, other.m_size);
  757. ipcdetail::do_swap(this->m_page_offset, other.m_page_offset);
  758. ipcdetail::do_swap(this->m_mode, other.m_mode);
  759. #if (defined BOOST_INTERPROCESS_WINDOWS)
  760. ipcdetail::do_swap(this->m_file_or_mapping_hnd, other.m_file_or_mapping_hnd);
  761. #else
  762. ipcdetail::do_swap(this->m_is_xsi, other.m_is_xsi);
  763. #endif
  764. }
  765. //!No-op functor
  766. struct null_mapped_region_function
  767. {
  768. bool operator()(void *, std::size_t , bool) const
  769. { return true; }
  770. std::size_t get_min_size() const
  771. { return 0; }
  772. };
  773. /// @endcond
  774. } //namespace interprocess {
  775. } //namespace boost {
  776. #include <boost/interprocess/detail/config_end.hpp>
  777. #endif //BOOST_INTERPROCESS_MAPPED_REGION_HPP
  778. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  779. #ifndef BOOST_INTERPROCESS_MAPPED_REGION_EXT_HPP
  780. #define BOOST_INTERPROCESS_MAPPED_REGION_EXT_HPP
  781. #if defined(BOOST_INTERPROCESS_WINDOWS) && !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION)
  782. # include <boost/interprocess/sync/windows/sync_utils.hpp>
  783. # include <boost/interprocess/detail/windows_intermodule_singleton.hpp>
  784. namespace boost {
  785. namespace interprocess {
  786. template<int Dummy>
  787. inline void mapped_region::destroy_syncs_in_range(const void *addr, std::size_t size)
  788. {
  789. ipcdetail::sync_handles &handles =
  790. ipcdetail::windows_intermodule_singleton<ipcdetail::sync_handles>::get();
  791. handles.destroy_syncs_in_range(addr, size);
  792. }
  793. } //namespace interprocess {
  794. } //namespace boost {
  795. #endif //defined(BOOST_INTERPROCESS_WINDOWS) && !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION)
  796. #endif //#ifdef BOOST_INTERPROCESS_MAPPED_REGION_EXT_HPP
  797. #endif //#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)