aboutsummaryrefslogtreecommitdiff
path: root/source/Core/DataBufferMemoryMap.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/Core/DataBufferMemoryMap.cpp')
-rw-r--r--source/Core/DataBufferMemoryMap.cpp94
1 files changed, 45 insertions, 49 deletions
diff --git a/source/Core/DataBufferMemoryMap.cpp b/source/Core/DataBufferMemoryMap.cpp
index 008b736ef726..4ca43b89eef1 100644
--- a/source/Core/DataBufferMemoryMap.cpp
+++ b/source/Core/DataBufferMemoryMap.cpp
@@ -18,11 +18,13 @@
#include <sys/mman.h>
#endif
+#include "llvm/Support/MathExtras.h"
+
#include "lldb/Core/DataBufferMemoryMap.h"
#include "lldb/Core/Error.h"
#include "lldb/Host/File.h"
#include "lldb/Host/FileSpec.h"
-#include "lldb/Host/Host.h"
+#include "lldb/Host/HostInfo.h"
#include "lldb/Core/Log.h"
#include "lldb/lldb-private-log.h"
@@ -89,7 +91,7 @@ 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);
+ log->Printf("DataBufferMemoryMap::Clear() m_mmap_addr = %p, m_mmap_size = %" PRIu64 "", m_mmap_addr, (uint64_t)m_mmap_size);
#ifdef _WIN32
UnmapViewOfFile(m_mmap_addr);
#else
@@ -113,7 +115,7 @@ DataBufferMemoryMap::Clear()
size_t
DataBufferMemoryMap::MemoryMapFromFileSpec (const FileSpec* filespec,
lldb::offset_t offset,
- lldb::offset_t length,
+ size_t length,
bool writeable)
{
if (filespec != NULL)
@@ -124,7 +126,7 @@ DataBufferMemoryMap::MemoryMapFromFileSpec (const FileSpec* filespec,
log->Printf("DataBufferMemoryMap::MemoryMapFromFileSpec(file=\"%s\", offset=0x%" PRIx64 ", length=0x%" PRIx64 ", writeable=%i",
filespec->GetPath().c_str(),
offset,
- length,
+ (uint64_t)length,
writeable);
}
char path[PATH_MAX];
@@ -147,16 +149,16 @@ 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;
-}
+
+
+#ifdef _WIN32
+static size_t win32memmapalignment = 0;
+void LoadWin32MemMapAlignment ()
+{
+ SYSTEM_INFO data;
+ GetSystemInfo(&data);
+ win32memmapalignment = data.dwAllocationGranularity;
+}
#endif
//----------------------------------------------------------------------
@@ -174,7 +176,7 @@ void LoadWin32MemMapAlignment ()
size_t
DataBufferMemoryMap::MemoryMapFromFileDescriptor (int fd,
lldb::offset_t offset,
- lldb::offset_t length,
+ size_t length,
bool writeable,
bool fd_is_file)
{
@@ -184,14 +186,10 @@ 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
+ log->Printf("DataBufferMemoryMap::MemoryMapFromFileDescriptor(fd=%i, offset=0x%" PRIx64 ", length=0x%" PRIx64 ", writeable=%i, fd_is_file=%i)",
fd,
offset,
- length,
+ (uint64_t)length,
writeable,
fd_is_file);
}
@@ -199,16 +197,13 @@ DataBufferMemoryMap::MemoryMapFromFileDescriptor (int fd,
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)
+ const lldb::offset_t file_size = llvm::Make_64(file_size_high, file_size_low);
+ const lldb::offset_t max_bytes_available = file_size - offset;
+ const size_t max_bytes_mappable = (size_t)std::min<lldb::offset_t>(SIZE_MAX, max_bytes_available);
+ if (length == SIZE_MAX || length > max_bytes_mappable)
{
// Cap the length if too much data was requested
- length = max_bytes_available;
+ length = max_bytes_mappable;
}
if (length > 0)
@@ -216,23 +211,23 @@ DataBufferMemoryMap::MemoryMapFromFileDescriptor (int fd,
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 ();
+ 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;
- }
+ m_data = m_mmap_addr + delta;
+ m_size = length;
+ }
CloseHandle(fileMapping);
}
}
@@ -240,7 +235,8 @@ DataBufferMemoryMap::MemoryMapFromFileDescriptor (int fd,
struct stat stat;
if (::fstat(fd, &stat) == 0)
{
- if (S_ISREG(stat.st_mode) && (stat.st_size > offset))
+ if (S_ISREG(stat.st_mode) &&
+ (stat.st_size > static_cast<off_t>(offset)))
{
const size_t max_bytes_available = stat.st_size - offset;
if (length == SIZE_MAX)
@@ -272,7 +268,7 @@ DataBufferMemoryMap::MemoryMapFromFileDescriptor (int fd,
if (error.GetError() == EINVAL)
{
// We may still have a shot at memory mapping if we align things correctly
- size_t page_offset = offset % Host::GetPageSize();
+ size_t page_offset = offset % HostInfo::GetPageSize();
if (page_offset != 0)
{
m_mmap_addr = (uint8_t *)::mmap(NULL, length + page_offset, prot, flags, fd, offset - page_offset);
@@ -311,8 +307,8 @@ DataBufferMemoryMap::MemoryMapFromFileDescriptor (int fd,
if (log)
{
- log->Printf("DataBufferMemoryMap::MemoryMapFromFileSpec() m_mmap_addr = %p, m_mmap_size = %zu, error = %s",
- m_mmap_addr, m_mmap_size, error.AsCString());
+ log->Printf("DataBufferMemoryMap::MemoryMapFromFileSpec() m_mmap_addr = %p, m_mmap_size = %" PRIu64 ", error = %s",
+ m_mmap_addr, (uint64_t)m_mmap_size, error.AsCString());
}
}
}