Skip to content

Commit

Permalink
initial mmap support for windows
Browse files Browse the repository at this point in the history
write appears to work, but read does not
  • Loading branch information
cyrush committed Sep 28, 2016
1 parent 5efce01 commit 4814724
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 20 deletions.
64 changes: 47 additions & 17 deletions src/libs/conduit/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
//
#include <sys/mman.h>
#include <unistd.h>
#else
#include "Windows.h"
#endif

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -12811,7 +12813,10 @@ class Node::MMap
#if !defined(CONDUIT_PLATFORM_WINDOWS)
// memory-map file descriptor
int m_mmap_fd;

#else
// handles for windows mmap
HANDLE m_file_hnd;
HANDLE m_map_hnd;
#endif

};
Expand All @@ -12824,6 +12829,8 @@ Node::MMap::MMap()
m_mmap_fd(-1)
#else
// windows
m_file_hnd(INVALID_HANDLE_VALUE),
m_map_hnd(INVALID_HANDLE_VALUE)
#endif
{
// empty
Expand Down Expand Up @@ -12864,14 +12871,40 @@ Node::MMap::open(const std::string &path,
if (m_data == MAP_FAILED)
CONDUIT_ERROR("<Node::mmap> mmap data = MAP_FAILED" << path);
#else
///
/// TODO: mmap isn't supported on windows, we need to use a
/// a windows specific API.
/// See: https://lc.llnl.gov/jira/browse/CON-38
///
/// For now, we simply throw an error
///
CONDUIT_ERROR("<Node::mmap> conduit does not yet support mmap on Windows");
m_file_hnd = CreateFile(path.c_str(),
(GENERIC_READ | GENERIC_WRITE),
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_RANDOM_ACCESS,
NULL);

if (m_file_hnd == INVALID_HANDLE_VALUE)
{
CONDUIT_ERROR("<Node::mmap> CreateFile() Failed ");
}

m_map_hnd = CreateFileMapping(m_file_hnd,
NULL,
PAGE_READWRITE,
0, 0, 0);

if (m_map_hnd == NULL)
{
CloseHandle(m_file_hnd);
CONDUIT_ERROR("<Node::mmap> CreateFileMapping() failed with error" << GetLastError());
}

m_data = MapViewOfFile(m_map_hnd,
FILE_MAP_ALL_ACCESS,
0, 0, 0);

if (m_data == NULL)
{
CloseHandle(m_map_hnd);
CloseHandle(m_file_hnd);
CONDUIT_ERROR("<Node::mmap> MapViewOfFile() failed with error" << GetLastError());
}
#endif
}

Expand All @@ -12898,14 +12931,11 @@ Node::MMap::close()
m_mmap_fd = -1;

#else
///
/// TODO: mmap isn't supported on windows, we need to use a
/// a windows specific API.
/// See: https://lc.llnl.gov/jira/browse/CON-38
///
/// For now, we simply throw an error
///
CONDUIT_ERROR("<Node::mmap> conduit does not yet support mmap on Windows");
UnmapViewOfFile(m_data);
CloseHandle(m_map_hnd);
CloseHandle(m_file_hnd);
m_file_hnd = INVALID_HANDLE_VALUE;
m_map_hnd = INVALID_HANDLE_VALUE;
#endif

// clear data pointer and size member
Expand Down
9 changes: 7 additions & 2 deletions src/tests/conduit/t_conduit_node_binary_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,13 @@ TEST(conduit_node_binary_io, mmap_simple)
// change mmap
nmmap[0]["a"] = 100;
nmmap[0]["b"] = 200;



#if defined(CONDUIT_PLATFORM_WINDOWS)
// need to close the mmap on windows in order
// to read it for the next test
nmmap.reset();
#endif

// standard read

Node ntest;
Expand Down
7 changes: 6 additions & 1 deletion src/tests/conduit/t_conduit_node_save_load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,12 @@ TEST(conduit_node_save_load, mmap_simple_file)
nmmap[0]["a"] = 100;
nmmap[0]["b"] = 200;


#if defined(CONDUIT_PLATFORM_WINDOWS)
// need to close the mmap on windows in order
// to read it for the next test
nmmap.reset();
#endif

// standard read

Node ntest;
Expand Down

0 comments on commit 4814724

Please sign in to comment.