annotate DEPENDENCIES/generic/include/boost/interprocess/mapped_region.hpp @ 16:2665513ce2d3

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