1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
|
// ==========================================================================
// SeqAn - The Library for Sequence Analysis
// ==========================================================================
// Copyright (c) 2006-2026, Knut Reinert, FU Berlin
// Copyright (c) 2013 NVIDIA Corporation
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Knut Reinert or the FU Berlin nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
//
// ==========================================================================
// Author: David Weese <david.weese@fu-berlin.de>
// ==========================================================================
// Provides a platform independent access to memory mapping of files.
// ==========================================================================
#ifndef SEQAN_INCLUDE_SEQAN_FILE_MAPPING_H_
#define SEQAN_INCLUDE_SEQAN_FILE_MAPPING_H_
namespace seqan2 {
// ============================================================================
// Forwards
// ============================================================================
// ============================================================================
// Tags, Classes, Enums
// ============================================================================
/*!
* @enum FileMappingMode
* @headerfile <seqan/file.h>
* @brief Flags to determine the mapping mode of mapFileSegment.
*
* @signature enum FileMappingMode;
*
* The mapping mode must be compatible to the open mode of a @link FileMapping @endlink, e.g. <tt>MAP_RDWR</tt> is not
* allowed if the file mapping was opened with <tt>OPEN_RDONLY</tt>.
*
* @val FileMappingMode MAP_RDONLY = 1;
* @brief Map the segment in read-only mode.
*
* @val FileMappingMode MAP_WRONLY = 2;
* @brief Map the segment in write-only mode.
*
* @val FileMappingMode MAP_RDWR = 3;
* @brief Map the segment for reading and writing.
*
* @val FileMappingMode MAP_COPYONWRITE = 4;
* @brief Write accesses are not written back to file and not shared among different mappings.
*/
enum FileMappingMode {
MAP_RDONLY = 1,
MAP_WRONLY = 2,
MAP_RDWR = 3,
MAP_COPYONWRITE = 4
};
/*!
* @enum FileMappingAdvise
* @headerfile <seqan/file.h>
* @brief Enum with MMAP advise values.
*
* @signature enum FileMappingAdvise;
*
* @val FileMappingAdvise MAP_NORMAL;
* @brief There is no advise on the given address range.
*
* @val FileMappingAdvise MAP_RANDOM;
* @brief The address range will be accessed with random access memory pattern.
*
* @val FileMappingAdvise MAP_SEQUENTIAL;
* @brief The address range will be accessed sequentially.
*
* @val FileMappingAdvise MAP_WILLNEED;
* @brief The address range in the advise will be needed in the future.
*
* @val FileMappingAdvise MAP_DONTNEED;
* @brief The address range in the advise will not be needed any more.
*/
#ifdef STDLIB_VS
enum FileMappingAdvise {
MAP_NORMAL = 0,
MAP_RANDOM = 0,
MAP_SEQUENTIAL = 0,
MAP_WILLNEED = 0,
MAP_DONTNEED = 0
};
#else
enum FileMappingAdvise {
MAP_NORMAL = POSIX_MADV_NORMAL,
MAP_RANDOM = POSIX_MADV_RANDOM,
MAP_SEQUENTIAL = POSIX_MADV_SEQUENTIAL,
MAP_WILLNEED = POSIX_MADV_WILLNEED,
MAP_DONTNEED = POSIX_MADV_DONTNEED
};
#endif
/*!
* @class FileMapping
* @headerfile <seqan/file.h>
* @brief A structure to memory-map a file.
*
* @signature template <[typename TSpec]>
* struct FileMapping;
*
* @tparam TSpec The specializing type. Default: <tt>void</tt>.
*
* This structure represents both a file and its memory mapping.
*/
#ifdef STDLIB_VS
static SECURITY_ATTRIBUTES FileMappingDefaultAttributes =
{
sizeof(SECURITY_ATTRIBUTES),
NULL,
true
};
#endif
template <typename TSpec = void>
struct FileMapping
{
// -----------------------------------------------------------------------
// Typedefs
// -----------------------------------------------------------------------
typedef File<Async<> > TFile;
typedef typename Size<TFile>::Type TFileSize;
// -----------------------------------------------------------------------
// Members
// -----------------------------------------------------------------------
#ifdef STDLIB_VS
HANDLE handle;
#endif
TFileSize fileSize;
TFile file;
int openMode;
bool ownFile;
bool temporary;
FileMapping()
{
_initialize(*this);
}
//____________________________________________________________________________
inline operator bool()
{
return file;
}
//____________________________________________________________________________
};
// ----------------------------------------------------------------------------
// Metafunction Size
// ----------------------------------------------------------------------------
/*!
* @mfn FileMapping#Size
* @brief Return the size type of the FileMapping.
*
* @signature Size<TFileMapping>::Type;
*
* @tparam TFileMapping FileMapping to query.
*
* @return Type The size type of the FileMapping.
*/
template <typename TSpec>
struct Size<FileMapping<TSpec> >:
public Size<typename FileMapping<TSpec>::TFile> {};
// ----------------------------------------------------------------------------
// Metafunction DefaultOpenMode
// ----------------------------------------------------------------------------
template <typename TDirection>
struct DefaultMMapOpenMode_
{
enum { VALUE = OPEN_RDWR | OPEN_CREATE | OPEN_APPEND };
};
template <>
struct DefaultMMapOpenMode_<Input>
{
enum { VALUE = OPEN_RDWR | OPEN_APPEND };
};
template <>
struct DefaultMMapOpenMode_<Output>
{
enum { VALUE = OPEN_RDWR | OPEN_CREATE };
};
template <typename TSpec, typename TDirection>
struct DefaultOpenMode<FileMapping<TSpec>, TDirection>:
DefaultMMapOpenMode_<TDirection> {};
// ============================================================================
// Functions
// ============================================================================
// ----------------------------------------------------------------------------
// Function _initialize()
// ----------------------------------------------------------------------------
template <typename TSpec>
inline void
_initialize(FileMapping<TSpec> &mapping)
{
#ifdef STDLIB_VS
mapping.handle = NULL;
#endif
mapping.fileSize = 0;
mapping.openMode = OPEN_RDWR;
mapping.ownFile = false;
mapping.temporary = true;
}
// ----------------------------------------------------------------------------
// Function _mapFile()
// ----------------------------------------------------------------------------
template <typename TSpec, typename TSize>
inline bool
_mapFile(FileMapping<TSpec> &mapping, TSize mappingSize)
{
ignoreUnusedVariableWarning(mapping);
ignoreUnusedVariableWarning(mappingSize);
bool result = true;
#ifdef STDLIB_VS
if (mappingSize == 0)
{
mapping.handle = NULL;
return true;
}
DWORD prot = ((mapping.openMode & OPEN_MASK) == OPEN_RDONLY) ? PAGE_READONLY : PAGE_READWRITE;
LARGE_INTEGER largeSize;
largeSize.QuadPart = mappingSize; // 0 = map the whole file
mapping.handle = CreateFileMapping(
mapping.file.handle, // _In_ HANDLE hFile,
&FileMappingDefaultAttributes, // _In_opt_ LPSECURITY_ATTRIBUTES lpAttributes,
prot, // _In_ DWORD flProtect,
largeSize.HighPart, // _In_ DWORD dwMaximumSizeHigh,
largeSize.LowPart, // _In_ DWORD dwMaximumSizeLow,
NULL // _In_opt_ LPCTSTR lpName
);
result &= (mapping.handle != NULL);
if (mapping.handle == NULL)
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL);
SEQAN_FAIL("CreateFileMapping failed in resize: \"%s\"", lpMsgBuf /*strerror(GetLastError())*/);
LocalFree(lpMsgBuf);
}
#endif
return result;
}
// ----------------------------------------------------------------------------
// Function _unmapFile()
// ----------------------------------------------------------------------------
template <typename TSpec>
inline bool
_unmapFile(FileMapping<TSpec> &mapping)
{
ignoreUnusedVariableWarning(mapping);
bool result = true;
#ifdef STDLIB_VS
if (mapping.handle != NULL)
{
result &= (CloseHandle(mapping.handle) != 0);
if (!result)
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL);
SEQAN_FAIL("CloseHandle failed in unmap: \"%s\"", lpMsgBuf /*strerror(GetLastError())*/);
LocalFree(lpMsgBuf);
}
mapping.handle = NULL;
}
#endif
return result;
}
/*!
* @fn FileMapping#open
* @brief Open a file to be mapped into memory.
*
* @signature bool open(fileMapping, fileName[, openMode]);
*
* @param[in,out] fileMapping The FileMapping to open.
* @param[in] fileName The path to the fie.
* @param[in] openMode The mode to open the file in, flags from @link FileOpenMode @endlink to combine
* using OR. Write-only mode is not supported, use <tt>OPEN_RDWR</tt> if you need
* write access. If you omit the <tt>OPEN_APPEND</tt> flag in write mode, the file
* will be cleared when opened. Default: <tt>OPEN_RDWR | OPEN_CREATE | OPEN_APPEND</tt>.
*
* @return bool <tt>true</tt> if the opening was successful, <tt>false</tt> otherwise.
*/
// ----------------------------------------------------------------------------
// Function open()
// ----------------------------------------------------------------------------
template <typename TSpec>
inline bool
open(FileMapping<TSpec> &mapping, const char *filename, int openMode)
{
_initialize(mapping);
bool result = open(mapping.file, filename, openMode);
mapping.openMode = openMode;
mapping.ownFile = true;
mapping.temporary = false;
mapping.fileSize = (result)? length(mapping.file) : 0ul;
result &= _mapFile(mapping, mapping.fileSize);
return result;
}
template <typename TSpec, typename TFile>
inline bool
open(FileMapping<TSpec> &mapping, TFile const &file)
{
_initialize(mapping);
mapping.file = file;
mapping.openMode = OPEN_RDWR;
mapping.ownFile = false;
mapping.temporary = false;
if (mapping.file)
{
mapping.fileSize = length(mapping.file);
return _mapFile(mapping, mapping.fileSize);
}
return false;
}
/*!
* @fn FileMapping#openTemp
* @brief Open a temporary file to be mapped into memory.
*
* @signature bool openTemp(fileMapping);
*
* @param[in,out] fileMapping The FileMapping to open using a temporary file.
*
* @return bool <tt>true</tt> on success, <tt>false</tt> otherwise.
*/
// ----------------------------------------------------------------------------
// Function openTemp()
// ----------------------------------------------------------------------------
template <typename TSpec>
inline bool
openTemp(FileMapping<TSpec> &mapping)
{
_initialize(mapping);
bool result = openTemp(mapping.file);
mapping.openMode = OPEN_RDWR;
mapping.ownFile = true;
mapping.temporary = true;
return result;
}
/*!
* @fn FileMapping#close
* @brief Close a file and its memory mapping.
*
* @signature bool close(fileMapping);
*
* @param[in,out] fileMapping The FileMapping to close
*
* @return bool <tt>true</tt> on success, <tt>false</tt> otherwise.
*/
// ----------------------------------------------------------------------------
// Function close()
// ----------------------------------------------------------------------------
template <typename TSpec>
inline bool
close(FileMapping<TSpec> &mapping)
{
bool result = _unmapFile(mapping);
if (mapping.ownFile)
result &= close(mapping.file);
_initialize(mapping);
return result;
}
/*!
* @fn FileMapping#closeAndResize
* @brief Close a memory mapping and resize and close the underlying file.
*
* @signature bool closeAndResize(fileMapping, newFileSize);
*
* @param[in,out] fileMapping The FileMapping to close.
* @param[in] newFileSize The size the file should have after closing.
*
* @return bool <tt>true</tt> indicating success, <tt>false</tt> failure.
*/
// ----------------------------------------------------------------------------
// Function closeAndResize()
// ----------------------------------------------------------------------------
template <typename TSpec, typename TSize>
inline bool
closeAndResize(FileMapping<TSpec> &mapping, TSize newFileSize)
{
bool result = _unmapFile(mapping);
if (newFileSize != mapping.fileSize)
resize(mapping.file, newFileSize);
if (mapping.ownFile)
result &= close(mapping.file);
_initialize(mapping);
return result;
}
/*!
* @fn FileMapping#length
* @brief Return the file size of a memory mapping.
*
* @signature TSize length(fileMapping);
*
* @param[in] fileMapping The FileMapping to return the length for.
*
* @return TSize The file size (Metafunction: @link FileMapping#Size @endlink).
*/
// ----------------------------------------------------------------------------
// Function length()
// ----------------------------------------------------------------------------
template <typename TSpec>
inline typename Size<FileMapping<TSpec> >::Type
length(FileMapping<TSpec> &mapping)
{
return mapping.fileSize;
}
template <typename TSpec>
inline typename Size<FileMapping<TSpec> >::Type
length(FileMapping<TSpec> const &mapping)
{
return mapping.fileSize;
}
/*!
* @fn FileMapping#resize
* @brief Resize the underlying file.
*
* @signature bool resize(fileMapping, newFileSize);
*
* @param[in,out] fileMapping The FileMapping to resize.
* @param[in] newFileSize The new file size to set.
*
* @return bool <tt>true</tt> on success, <tt>false</tt> otherwise.
*
* @section Remarks
*
* On Windows, all existing file mappings must be unmapped via @link FileMapping#unmapFileSegment @endlink before
* claling this function.
*/
template <typename TSpec, typename TSize>
inline bool
resize(FileMapping<TSpec> &mapping, TSize newFileSize)
{
typedef typename Size<FileMapping<TSpec> >::Type TFileSize;
if ((TFileSize)newFileSize == mapping.fileSize)
return true;
bool result = _unmapFile(mapping);
resize(mapping.file, newFileSize);
mapping.fileSize = newFileSize;
result &= _mapFile(mapping, newFileSize);
return result;
}
/*!
* @fn FileMapping#flushFileSegment
* @brief Wait for all outstanding transactions of a memory-mapped file segment.
*
* @signature bool flushFileSegment(fileMapping, addr, beginPos, size);
*
* @param[in,out] fileMapping A FileMapping object.
* @param[in] addr A pointer to the beginning of memory-mapped segment in memory (returned by a prior
* call of @link FileMapping#mapFileSegment @endlink.
* @param[in] beginPos The absolute start address of the segment in bytes.
* @param[in] size The segment length in bytes.
*
* @return bool <tt>true</tt> on success, <tt>false</tt> on failure.
*
* @section Remarks
*
* This function has no effect under Windows. On all other platforms it calls <tt>msync</tt>. This function is only
* needed to synchronize file accesses in non-shared-memory environments.
*/
template <typename TSpec, typename TPos, typename TSize>
inline bool
flushFileSegment(FileMapping<TSpec> &, void *addr, TPos beginPos, TSize size)
{
#ifdef STDLIB_VS
ignoreUnusedVariableWarning(addr);
ignoreUnusedVariableWarning(beginPos);
ignoreUnusedVariableWarning(size);
return true;
#else
return (msync(static_cast<char*>(addr) + beginPos, size, MS_SYNC) == 0);
#endif
}
/*!
* @fn FileMapping#cancelFileSegment
* @brief Cancel all outstanding transactions of a memory-mapped file segment.
*
* @signature bool cancelFileSegment(fileMapping, addr, beginPos, size);
*
* @param[in,out] fileMapping The FileMapping object.
* @param[in] addr A pointer to the beginning of the memory-mapped segment in memory (returned by a prior
* call of FileMapping#mapFileSegment).
* @param[in] beginPos the absolute start address of the segment in bytes.
* @param[in] size The segment length in bytes.
*
* @return bool <tt>true</tt> on success, <tt>false</tt> on failure.
*/
template <typename TSpec, typename TPos, typename TSize>
inline bool
cancelFileSegment(FileMapping<TSpec> &, void *addr, TPos fileOfs, TSize size)
{
#ifdef STDLIB_VS
ignoreUnusedVariableWarning(addr);
ignoreUnusedVariableWarning(fileOfs);
ignoreUnusedVariableWarning(size);
return true;
#else
return (msync(addr, size + fileOfs, MS_INVALIDATE) == 0);
#endif
}
/*!
* @fn FileMapping#adviseFileSegment
* @brief Give advise about use of a memory-mapped file segment.
*
* @signature bool adviseFileSegment(fileMapping, advise, addr, fileOfs, size);
*
* @param[in,out] fileMapping The FileMapping object to advise segment in.
* @param[in] advise The advise type. Type: @link FileMappingAdvise @endlink.
* @param[in] addr A pointer to the beginning of the memory-mapped segment in memory (returned by a
* prior call of @link FileMapping#mapFileSegment @endlink).
* @param[in] fileOfs The absolute start address of the segment in bytes.
* @param[in] size The segment length in bytes.
*
* @return bool <tt>true</tt> on success, <tt>false</tt> on failure.
*
* @section Remarks
*
* This function has no effect on Windows. On all other platforms it calls <tt>posix_madvise</tt>.
*/
template <typename TSpec, typename TPos, typename TSize>
inline bool
adviseFileSegment(FileMapping<TSpec> &, FileMappingAdvise advise, void *addr, TPos fileOfs, TSize size)
{
#ifdef STDLIB_VS
ignoreUnusedVariableWarning(advise);
ignoreUnusedVariableWarning(addr);
ignoreUnusedVariableWarning(fileOfs);
ignoreUnusedVariableWarning(size);
return true;
#else
// posix_fadvise(mapping.file.handle, beginPos, size, advise);
return (posix_madvise(static_cast<char*>(addr) + fileOfs, size, advise) == 0);
#endif
}
/*!
* @fn FileMapping#mapFileSegment
* @brief Map a segment of a file into memory.
*
* @signature TPtr mapFileSegment(fileMapping, fileOfs[, size[, mode]]);
*
* @param[in,out] fileMapping A FileMapping object.
* @param[in] fileOfs The absolute start address of the segment in bytes.
* @param[in] size The segment length in bytes.
* @param[in] mode The mapping access mode. Default read/write open mode of the underlying file.
* Type: @link FileMappingMode @endlink.
*
* @return TPtr A pointer to the beginning of the memory-mapped segment in memory or <tt>NULL</tt> on error. TPtr is
* <tt>void *</tt>.
*/
template <typename TSpec, typename TPos, typename TSize, typename TFileMappingMode>
inline void *
mapFileSegment(FileMapping<TSpec> &mapping, TPos fileOfs, TSize size, TFileMappingMode mode)
{
SEQAN_ASSERT_EQ((int)OPEN_RDONLY, (int)MAP_RDONLY);
SEQAN_ASSERT_EQ((int)OPEN_WRONLY, (int)MAP_WRONLY);
void *addr;
if (size == 0)
return NULL;
mode = (FileMappingMode)(mode & (mapping.openMode & OPEN_MASK));
#ifdef STDLIB_VS
DWORD access = ((mode & OPEN_MASK) == OPEN_RDONLY) ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS;
LARGE_INTEGER largeOfs;
largeOfs.QuadPart = fileOfs;
addr = MapViewOfFile(
mapping.handle, // _In_ HANDLE hFileMappingObject,
access, // _In_ DWORD dwDesiredAccess,
largeOfs.HighPart, // _In_ DWORD dwFileOffsetHigh,
largeOfs.LowPart, // _In_ DWORD dwFileOffsetLow,
size); // _In_ SIZE_T dwNumberOfBytesToMap (0 = map the whole file)
#else
int prot = 0;
if (mode & MAP_RDONLY) prot |= PROT_READ;
if (mode & MAP_WRONLY) prot |= PROT_WRITE;
int flags = 0;
if ((mode & MAP_COPYONWRITE) != 0)
flags |= MAP_PRIVATE;
else
flags |= MAP_SHARED;
addr = mmap(
NULL,
size,
prot,
flags,
mapping.file.handle,
fileOfs);
// std::cerr << "mmap(0,"<<size<<','<<prot<<','<<flags<<','<<mapping.file.handle<<','<<fileOfs<<")="<<addr<<std::endl;
if (addr == MAP_FAILED)
addr = NULL;
#endif
if (addr == NULL)
{
SEQAN_FAIL("mapFileSegment(%i,%i,%i) failed (filesize=%i): \"%s\"", fileOfs, size, mode, length(mapping.file), strerror(errno));
}
return addr;
}
template <typename TSpec, typename TPos, typename TSize>
inline void *
mapFileSegment(FileMapping<TSpec> &mapping, TPos fileOfs, TSize size)
{
return mapFileSegment(mapping, fileOfs, size, MAP_RDWR);
}
template <typename TSpec, typename TPos, typename TSize>
inline void *
mapFileSegment(FileMapping<TSpec> &mapping, TPos fileOfs = 0)
{
return mapFileSegment(mapping, fileOfs, length(mapping) - fileOfs);
}
/*!
* @fn FileMapping#unmapFileSegment
* @brief Unmap a memory-mapped file segment.
*
* @signature bool unmappedFileSegment(fileMapping, addr, size);
*
* @param[in,out] fileMapping A FileMapping object.
* @param[in] addr The pointer to the beginning of the memory-mapped segment in memory. Type: <tt>void*</tt>.
* @param[in] size The segment length in bytes.
*
* @return bool <tt>true</tt> on success, <tt>false</tt> on failure.
*/
template <typename TSpec, typename TSize>
inline bool
unmapFileSegment(FileMapping<TSpec> &, void *addr, TSize size)
{
bool result;
#ifdef STDLIB_VS
ignoreUnusedVariableWarning(size);
result = (UnmapViewOfFile(addr) != 0);
#else
result = (munmap(addr, size) == 0);
// std::cerr << "munmap("<<addr<<','<<size<<")="<<result<<std::endl;
#endif
if (!result)
SEQAN_FAIL("unmapFileSegment(%x,%i) failed: \"%s\"", (uint64_t)addr, size, strerror(errno));
return result;
}
/*!
* @fn FileMapping#remapFileSegment
* @brief Change the size of a memory-mapped file segment.
*
* @signature TPtr remapFileSegment(fileMapping, oldAddr, oldFileOfs, oldSize, newSize);
*
* @param[in,out] fileMapping The FileMapping object.
* @param[in] oldAddr The address returned by @link FileMapping#mapFileSegment @endlink.
* @param[in] oldFileOfs The <tt>fileOfs</tt> parameter used in @link FileMapping#mapFileSegment @endlink.
* @param[in] oldSize The <tt>size</tt> parameter used in @link FileMapping#mapFileSegment @endlink.
* @param[in] newSize the new segment length in bytes.
*
* @return TPtr A pointer to the beginning of the memory-mapped segment in memory or <tt>NULL</tt> on error. Type:
* <tt>void*</tt>.
*/
template <typename TSpec, typename TPos, typename TSize>
inline void *
remapFileSegment(FileMapping<TSpec> &mapping, void *oldAddr, TPos oldFileOfs, TSize oldSize, TSize newSize)
{
void *addr;
#if !defined(STDLIB_VS) && defined(MREMAP_MAYMOVE)
ignoreUnusedVariableWarning(mapping);
ignoreUnusedVariableWarning(oldFileOfs);
addr = mremap(oldAddr, oldSize, newSize, MREMAP_MAYMOVE);
// std::cerr << "mremap("<<oldAddr<<','<<oldSize<<','<<newSize<<','<<MREMAP_MAYMOVE<<")="<<addr<<std::endl;
if (addr == MAP_FAILED)
addr = NULL;
#else
// for BSD systems without mremap(..) like Mac OS X ...
unmapFileSegment(mapping, oldAddr, oldSize);
addr = mapFileSegment(mapping, oldFileOfs, newSize);
#endif
if (addr == NULL)
SEQAN_FAIL("remapFileSegment failed: \"%s\"", strerror(errno));
return addr;
}
} // namespace seqan2
#endif // #ifndef SEQAN_INCLUDE_SEQAN_FILE_MAPPING_H_
|