Skip to content

Commit

Permalink
[C++] Initial Windows Build. Compiles but does not run yet. more work…
Browse files Browse the repository at this point in the history
… required....
  • Loading branch information
0xcafed00d committed Oct 24, 2014
1 parent 1795d1c commit 1d39327
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 28 deletions.
19 changes: 18 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ build-local.properties
.gradle
build

# cpp build
# cpp build linux
cppbuild/CMakeCache.txt
cppbuild/CMakeFiles/
cppbuild/CTestTestfile.cmake
Expand All @@ -38,6 +38,23 @@ cppbuild/aeron-examples/
cppbuild/binaries/
cppbuild/cmake_install.cmake

# cpp build windows
cppbuild/*.opensdf
cppbuild/*.sdf
cppbuild/*.sln
cppbuild/*.suo
cppbuild/*.vcxproj
cppbuild/*.filters
cppbuild/*.lastbuildstate
cppbuild/*.tlog
cppbuild/*.log
cppbuild/*.cache
cppbuild/Debug
cppbuild/Release
cppbuild/Win32



# Benchmark timings
timings.log

Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ if(APPLE)
add_definitions(-DDarwin)
elseif(WIN32)
add_definitions(-DWIN32)

set(CMAKE_CXX_FLAGS_DEBUG "/MTd")
set(CMAKE_CXX_FLAGS_RELEASE "/MT")

# TODO: add MSVC flags
endif()

Expand Down
3 changes: 2 additions & 1 deletion aeron-common/src/main/cpp/concurrent/AtomicBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <mintomic/mintomic.h>

#include <util/Exceptions.h>
#include <util/StringUtil.h>

namespace aeron { namespace common { namespace concurrent {

Expand Down Expand Up @@ -199,7 +200,7 @@ class AtomicBuffer
inline void boundsCheck(size_t index, size_t length)
{
if (index + length > m_length)
throw aeron::common::util::OutOfBoundsException("Index Out of Bounds", SOURCEINFO);
throw aeron::common::util::OutOfBoundsException(std::string("Index Out of Bounds. Index: ") + util::toString(index + length) + " Capacity: " + util::toString(m_length), SOURCEINFO);
}
};

Expand Down
13 changes: 10 additions & 3 deletions aeron-common/src/main/cpp/util/Exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@

namespace aeron { namespace common { namespace util {

#define SOURCEINFO __PRETTY_FUNCTION__, " : " __FILE__ " : " TOSTRING(__LINE__)

#ifdef _WIN32
#define SOURCEINFO __FUNCTION__, " : " __FILE__ " : " TOSTRING(__LINE__)
#define AERON_NOEXCEPT
#else
#define SOURCEINFO __PRETTY_FUNCTION__, " : " __FILE__ " : " TOSTRING(__LINE__)
#define AERON_NOEXCEPT noexcept
#endif

class SourcedException : public std::exception
{
Expand All @@ -25,12 +32,12 @@ class SourcedException : public std::exception
{
}

virtual const char *what() const noexcept
virtual const char *what() const AERON_NOEXCEPT
{
return m_what.c_str();
}

const char *where() const noexcept
const char *where() const AERON_NOEXCEPT
{
return m_where.c_str();
}
Expand Down
110 changes: 91 additions & 19 deletions aeron-common/src/main/cpp/util/MemoryMappedFile.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@

#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#ifndef _WIN32
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#endif

#include <string>
#include <string.h>

#include "MemoryMappedFile.h"
#include "Exceptions.h"
#include "ScopeUtils.h"
#include "StringUtil.h"

namespace aeron { namespace common { namespace util {

Expand All @@ -24,6 +27,83 @@ MemoryMappedFile::ptr_t MemoryMappedFile::mapExisting(const char *filename)
return std::shared_ptr<MemoryMappedFile>(new MemoryMappedFile(filename));
}

uint8_t* MemoryMappedFile::getMemoryPtr() const
{
return m_memory;
}

size_t MemoryMappedFile::getMemorySize() const
{
return m_memorySize;
}

#ifdef _WIN32
MemoryMappedFile::MemoryMappedFile(const char *filename, size_t size)
{
}

MemoryMappedFile::MemoryMappedFile(const char *filename)
: m_file(NULL), m_mapping(NULL)
{
FileHandle fd;
fd.handle = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

if (fd.handle == INVALID_HANDLE_VALUE)
{
DWORD err = GetLastError();
throw IOException(std::string("Failed to open existing file: ") + filename + " " + toString(err) , SOURCEINFO);
}

LARGE_INTEGER fileSize;
GetFileSizeEx(m_file, &fileSize);

m_memorySize = (size_t)fileSize.QuadPart;

m_memory = doMapping(m_memorySize, fd);


}

MemoryMappedFile::~MemoryMappedFile()
{
CloseHandle(m_file);
UnmapViewOfFile(m_memory);
CloseHandle(m_mapping);
}

uint8_t* MemoryMappedFile::doMapping(size_t size, FileHandle fd)
{
m_mapping = CreateFileMapping(fd.handle, NULL, PAGE_READWRITE, 0, size, NULL);
if (m_mapping == NULL)
throw IOException("Failed to Memory Map File", SOURCEINFO);

void* memory = (LPTSTR)MapViewOfFile(m_mapping, FILE_MAP_ALL_ACCESS, 0, 0, size);
if (memory == NULL)
throw IOException("Failed to Memory Map File", SOURCEINFO);

return static_cast<uint8_t*>(memory);
}

void MemoryMappedFile::fill(FileHandle fd, size_t size, uint8_t value)
{
// uint8_t buffer[PAGE_SIZE];
// memset(buffer, value, PAGE_SIZE);
//
// while (size >= PAGE_SIZE)
// {
// if (static_cast<size_t>(write(fd, buffer, PAGE_SIZE)) != PAGE_SIZE)
// throw IOException("Failed Writing to File", SOURCEINFO);
// size -= PAGE_SIZE;
// }
//
// if (size)
// {
// if (static_cast<size_t>(write(fd, buffer, size)) != size)
// throw IOException("Failed Writing to File", SOURCEINFO);
// }
}

#else
MemoryMappedFile::MemoryMappedFile(const char *filename, size_t size)
{
int fd = open(filename, O_RDWR|O_CREAT, 0666);
Expand Down Expand Up @@ -66,17 +146,7 @@ MemoryMappedFile::~MemoryMappedFile()
munmap(m_memory, m_memorySize);
}

uint8_t* MemoryMappedFile::getMemoryPtr() const
{
return m_memory;
}

size_t MemoryMappedFile::getMemorySize() const
{
return m_memorySize;
}

uint8_t* MemoryMappedFile::doMapping(size_t size, int fd)
uint8_t* MemoryMappedFile::doMapping(size_t size, FileHandle fd)
{
void* memory = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (memory == MAP_FAILED)
Expand All @@ -85,23 +155,25 @@ uint8_t* MemoryMappedFile::doMapping(size_t size, int fd)
return static_cast<uint8_t*>(memory);
}

void MemoryMappedFile::fill(int fd, size_t size, uint8_t value)
void MemoryMappedFile::fill(FileHandle fd, size_t size, uint8_t value)
{
uint8_t buffer[PAGE_SIZE];
memset(buffer, value, PAGE_SIZE);

while (size >= PAGE_SIZE)
{
if (static_cast<size_t>(write(fd, buffer, PAGE_SIZE)) != PAGE_SIZE)
if (static_cast<size_t>(write(fd.handle, buffer, PAGE_SIZE)) != PAGE_SIZE)
throw IOException("Failed Writing to File", SOURCEINFO);
size -= PAGE_SIZE;
}

if (size)
{
if (static_cast<size_t>(write(fd, buffer, size)) != size)
if (static_cast<size_t>(write(fd.handle, buffer, size)) != size)
throw IOException("Failed Writing to File", SOURCEINFO);
}
}

#endif

}}}
26 changes: 23 additions & 3 deletions aeron-common/src/main/cpp/util/MemoryMappedFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
#include <cstdint>
#include <memory>

#ifdef _WIN32
#include <windows.h>
#endif


namespace aeron { namespace common { namespace util {

class MemoryMappedFile
Expand All @@ -26,12 +31,27 @@ class MemoryMappedFile
MemoryMappedFile (const char* filename, size_t size);
MemoryMappedFile (const char* filename);

void fill (int fd, size_t sz, std::uint8_t);
uint8_t* doMapping(size_t size, int fd);
struct FileHandle
{
#ifdef _WIN32
HANDLE handle;
#else
int handle;
#endif
};

void fill (FileHandle fd, size_t sz, std::uint8_t);
uint8_t* doMapping(size_t size, FileHandle fd);

std::uint8_t* m_memory = 0;
size_t m_memorySize = 0;
const size_t PAGE_SIZE = 4096; // TODO: Get this programaticaly?
const static size_t PAGE_SIZE = 4096; // TODO: Get this programaticaly?

#ifdef _WIN32
HANDLE m_file;
HANDLE m_mapping;
#endif

};

}}}
Expand Down
2 changes: 1 addition & 1 deletion aeron-examples/src/main/cpp/AeronStat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using namespace aeron::common::concurrent;
using namespace std::chrono;


std::atomic_bool running (true);
std::atomic<bool> running (true);

void sigIntHandler (int param)
{
Expand Down

0 comments on commit 1d39327

Please sign in to comment.