annotate DEPENDENCIES/generic/include/boost/interprocess/mapped_region.hpp @ 133:4acb5d8d80b6 tip

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