diff options
Diffstat (limited to 'source/Core/DataBufferMemoryMap.cpp')
-rw-r--r-- | source/Core/DataBufferMemoryMap.cpp | 67 |
1 files changed, 66 insertions, 1 deletions
diff --git a/source/Core/DataBufferMemoryMap.cpp b/source/Core/DataBufferMemoryMap.cpp index a4382a0c67e1..008b736ef726 100644 --- a/source/Core/DataBufferMemoryMap.cpp +++ b/source/Core/DataBufferMemoryMap.cpp @@ -12,7 +12,11 @@ #include <fcntl.h> #include <limits.h> #include <sys/stat.h> +#ifdef _WIN32 +#include "lldb/Host/windows/windows.h" +#else #include <sys/mman.h> +#endif #include "lldb/Core/DataBufferMemoryMap.h" #include "lldb/Core/Error.h" @@ -86,7 +90,11 @@ DataBufferMemoryMap::Clear() Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_MMAP)); if (log) log->Printf("DataBufferMemoryMap::Clear() m_mmap_addr = %p, m_mmap_size = %zu", m_mmap_addr, m_mmap_size); +#ifdef _WIN32 + UnmapViewOfFile(m_mmap_addr); +#else ::munmap((void *)m_mmap_addr, m_mmap_size); +#endif m_mmap_addr = NULL; m_mmap_size = 0; m_data = NULL; @@ -139,7 +147,17 @@ DataBufferMemoryMap::MemoryMapFromFileSpec (const FileSpec* filespec, Clear(); return 0; } - +
+
+#ifdef _WIN32
+static size_t win32memmapalignment = 0;
+void LoadWin32MemMapAlignment ()
+{
+ SYSTEM_INFO data;
+ GetSystemInfo(&data);
+ win32memmapalignment = data.dwAllocationGranularity;
+}
+#endif //---------------------------------------------------------------------- // The file descriptor FD is assumed to already be opened as read only @@ -166,13 +184,59 @@ DataBufferMemoryMap::MemoryMapFromFileDescriptor (int fd, Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_MMAP|LIBLLDB_LOG_VERBOSE)); if (log) { +#ifdef _WIN32 + log->Printf("DataBufferMemoryMap::MemoryMapFromFileSpec(fd=%p, offset=0x%" PRIx64 ", length=0x%" PRIx64 ", writeable=%i, fd_is_file=%i)", +#else log->Printf("DataBufferMemoryMap::MemoryMapFromFileSpec(fd=%i, offset=0x%" PRIx64 ", length=0x%" PRIx64 ", writeable=%i, fd_is_file=%i)", +#endif fd, offset, length, writeable, fd_is_file); } +#ifdef _WIN32 + HANDLE handle = (HANDLE)_get_osfhandle(fd); + DWORD file_size_low, file_size_high; + file_size_low = GetFileSize(handle, &file_size_high); + const size_t file_size = (file_size_high << 32) | file_size_low; + const size_t max_bytes_available = file_size - offset; + if (length == SIZE_MAX) + { + length = max_bytes_available; + } + else if (length > max_bytes_available) + { + // Cap the length if too much data was requested + length = max_bytes_available; + } + + if (length > 0) + { + HANDLE fileMapping = CreateFileMapping(handle, NULL, writeable ? PAGE_READWRITE : PAGE_READONLY, file_size_high, file_size_low, NULL); + if (fileMapping != NULL) + { + if (win32memmapalignment == 0) LoadWin32MemMapAlignment();
+ lldb::offset_t realoffset = offset;
+ lldb::offset_t delta = 0;
+ if (realoffset % win32memmapalignment != 0) {
+ realoffset = realoffset / win32memmapalignment * win32memmapalignment;
+ delta = offset - realoffset;
+ } +
+ LPVOID data = MapViewOfFile(fileMapping, writeable ? FILE_MAP_WRITE : FILE_MAP_READ, 0, realoffset, length + delta);
+ m_mmap_addr = (uint8_t *)data;
+ if (!data) {
+ Error error;
+ error.SetErrorToErrno ();
+ } else { + m_data = m_mmap_addr + delta;
+ m_size = length;
+ }
+ CloseHandle(fileMapping); + } + } +#else struct stat stat; if (::fstat(fd, &stat) == 0) { @@ -253,6 +317,7 @@ DataBufferMemoryMap::MemoryMapFromFileDescriptor (int fd, } } } +#endif } return GetByteSize (); } |