From cc805b7b7437e5cefcbb6e071633f5a47b8963a3 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Wed, 12 Feb 2020 19:22:46 +0100 Subject: [PATCH] Add mmap() wrapper. --- capture/build/win32/capture.vcxproj | 3 ++ capture/build/win32/capture.vcxproj.filters | 9 +++++ .../build/win32/import-chrome.vcxproj | 3 ++ .../build/win32/import-chrome.vcxproj.filters | 9 +++++ profiler/build/win32/Tracy.vcxproj | 2 + profiler/build/win32/Tracy.vcxproj.filters | 6 +++ server/TracyMmap.cpp | 38 +++++++++++++++++++ server/TracyMmap.hpp | 19 ++++++++++ update/build/win32/update.vcxproj | 3 ++ update/build/win32/update.vcxproj.filters | 9 +++++ 10 files changed, 101 insertions(+) create mode 100644 server/TracyMmap.cpp create mode 100644 server/TracyMmap.hpp diff --git a/capture/build/win32/capture.vcxproj b/capture/build/win32/capture.vcxproj index d1ed10aa..00027ca5 100644 --- a/capture/build/win32/capture.vcxproj +++ b/capture/build/win32/capture.vcxproj @@ -134,6 +134,7 @@ + @@ -181,8 +182,10 @@ + + diff --git a/capture/build/win32/capture.vcxproj.filters b/capture/build/win32/capture.vcxproj.filters index 9804d835..c19f9c1a 100644 --- a/capture/build/win32/capture.vcxproj.filters +++ b/capture/build/win32/capture.vcxproj.filters @@ -123,6 +123,9 @@ zstd + + server + @@ -284,5 +287,11 @@ zstd + + server + + + server + \ No newline at end of file diff --git a/import-chrome/build/win32/import-chrome.vcxproj b/import-chrome/build/win32/import-chrome.vcxproj index abd755de..709ba6ad 100644 --- a/import-chrome/build/win32/import-chrome.vcxproj +++ b/import-chrome/build/win32/import-chrome.vcxproj @@ -134,6 +134,7 @@ + @@ -179,8 +180,10 @@ + + diff --git a/import-chrome/build/win32/import-chrome.vcxproj.filters b/import-chrome/build/win32/import-chrome.vcxproj.filters index 08a70582..34cc47e0 100644 --- a/import-chrome/build/win32/import-chrome.vcxproj.filters +++ b/import-chrome/build/win32/import-chrome.vcxproj.filters @@ -117,6 +117,9 @@ zstd + + server + @@ -272,5 +275,11 @@ zstd + + server + + + server + \ No newline at end of file diff --git a/profiler/build/win32/Tracy.vcxproj b/profiler/build/win32/Tracy.vcxproj index 375f20b9..22819b14 100644 --- a/profiler/build/win32/Tracy.vcxproj +++ b/profiler/build/win32/Tracy.vcxproj @@ -114,6 +114,7 @@ + @@ -193,6 +194,7 @@ + diff --git a/profiler/build/win32/Tracy.vcxproj.filters b/profiler/build/win32/Tracy.vcxproj.filters index 4f7b6608..d52a36b4 100644 --- a/profiler/build/win32/Tracy.vcxproj.filters +++ b/profiler/build/win32/Tracy.vcxproj.filters @@ -189,6 +189,9 @@ zstd + + server + @@ -473,6 +476,9 @@ zstd + + server + diff --git a/server/TracyMmap.cpp b/server/TracyMmap.cpp new file mode 100644 index 00000000..29b06e48 --- /dev/null +++ b/server/TracyMmap.cpp @@ -0,0 +1,38 @@ +#include "TracyMmap.hpp" + +#if defined _MSC_VER || defined __MINGW32__ || defined __CYGWIN__ +# include +# include + +void* mmap( void* addr, size_t length, int prot, int flags, int fd, off_t offset ) +{ + HANDLE hnd; + void* map = nullptr; + + switch( prot ) + { + case PROT_READ: + if( hnd = CreateFileMapping( HANDLE( _get_osfhandle( fd ) ), nullptr, PAGE_READONLY, 0, 0, nullptr ) ) + { + map = MapViewOfFile( hnd, FILE_MAP_READ, 0, 0, length ); + CloseHandle( hnd ); + } + break; + case PROT_WRITE: + if( hnd = CreateFileMapping( HANDLE( _get_osfhandle( fd ) ), nullptr, PAGE_READWRITE, 0, 0, nullptr ) ) + { + map = MapViewOfFile( hnd, FILE_MAP_WRITE, 0, 0, length ); + CloseHandle( hnd ); + } + break; + } + + return map ? (char*)map + offset : (void*)-1; +} + +int munmap( void* addr, size_t length ) +{ + return UnmapViewOfFile( addr ) != 0 ? 0 : -1; +} + +#endif diff --git a/server/TracyMmap.hpp b/server/TracyMmap.hpp new file mode 100644 index 00000000..017f21bd --- /dev/null +++ b/server/TracyMmap.hpp @@ -0,0 +1,19 @@ +#ifndef __TRACYMMAP_HPP__ +#define __TRACYMMAP_HPP__ + +#if !defined _MSC_VER && !defined __MINGW32__ && !defined __CYGWIN__ +# include +#else +# include +# include + +# define PROT_READ 1 +# define PROT_WRITE 2 +# define MAP_SHARED 0 + +void* mmap( void* addr, size_t length, int prot, int flags, int fd, off_t offset ); +int munmap( void* addr, size_t length ); + +#endif + +#endif diff --git a/update/build/win32/update.vcxproj b/update/build/win32/update.vcxproj index 4e1779a1..32c61369 100644 --- a/update/build/win32/update.vcxproj +++ b/update/build/win32/update.vcxproj @@ -134,6 +134,7 @@ + @@ -180,8 +181,10 @@ + + diff --git a/update/build/win32/update.vcxproj.filters b/update/build/win32/update.vcxproj.filters index 7a8556ff..61824d8e 100644 --- a/update/build/win32/update.vcxproj.filters +++ b/update/build/win32/update.vcxproj.filters @@ -120,6 +120,9 @@ zstd + + server + @@ -278,5 +281,11 @@ zstd + + server + + + server + \ No newline at end of file