Skip to content

Commit a5ab385

Browse files
Hide more platform specific implementation details.
1 parent 99a3cb5 commit a5ab385

7 files changed

+99
-72
lines changed
Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
1-
#ifndef __NBL_SYSTEM_C_FILE_VIEW_VIRTUAL_ALLOCATOR_POSIX_H_INCLUDED__
2-
#define __NBL_SYSTEM_C_FILE_VIEW_VIRTUAL_ALLOCATOR_POSIX_H_INCLUDED__
3-
#include "IFileViewAllocator.h"
4-
#include <sys/mman.h>
1+
#ifndef _NBL_SYSTEM_C_FILE_VIEW_VIRTUAL_ALLOCATOR_POSIX_H_INCLUDED_
2+
#define _NBL_SYSTEM_C_FILE_VIEW_VIRTUAL_ALLOCATOR_POSIX_H_INCLUDED_
53

6-
#if defined(_NBL_PLATFORM_LINUX_) || defined(_NBL_PLATFORM_ANDROID_)
74

5+
#include "nbl/system/IFileViewAllocator.h"
6+
7+
8+
#if defined(_NBL_PLATFORM_LINUX_) || defined(_NBL_PLATFORM_ANDROID_)
89
namespace nbl::system
910
{
11+
1012
class CFileViewVirtualAllocatorPOSIX : public IFileViewAllocator
1113
{
12-
public:
13-
void* alloc(size_t size) override
14-
{
15-
return mmap((caddr_t)0, size, PROT_WRITE | PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
16-
}
17-
bool dealloc(void* data, size_t size) override
18-
{
19-
auto ret = munmap(data, size);
20-
return ret != -1;
21-
}
14+
public:
15+
void* alloc(size_t size) override;
16+
bool dealloc(void* data, size_t size) override;
2217
};
23-
}
2418

19+
}
2520
#endif
2621
#endif
Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
1-
#ifndef __NBL_SYSTEM_C_FILE_VIEW_VIRTUAL_ALLOCATOR_WIN32_H_INCLUDED__
2-
#define __NBL_SYSTEM_C_FILE_VIEW_VIRTUAL_ALLOCATOR_WIN32_H_INCLUDED__
3-
#include "IFileViewAllocator.h"
1+
#ifndef _NBL_SYSTEM_C_FILE_VIEW_VIRTUAL_ALLOCATOR_WIN32_H_INCLUDED_
2+
#define _NBL_SYSTEM_C_FILE_VIEW_VIRTUAL_ALLOCATOR_WIN32_H_INCLUDED_
3+
4+
5+
#include "nbl/system/IFileViewAllocator.h"
46

5-
#ifdef _NBL_PLATFORM_WINDOWS_
6-
#include "Windows.h"
77

88
namespace nbl::system
99
{
10+
#ifdef _NBL_PLATFORM_WINDOWS_
1011
class CFileViewVirtualAllocatorWin32 : public IFileViewAllocator
1112
{
12-
public:
13-
void* alloc(size_t size) override
14-
{
15-
return VirtualAlloc(nullptr, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
16-
}
17-
bool dealloc(void* data, size_t size) override
18-
{
19-
return VirtualFree(data, 0, MEM_RELEASE);
20-
}
13+
public:
14+
void* alloc(size_t size) override;
15+
bool dealloc(void* data, size_t size) override;
2116
};
17+
#endif
2218
}
2319

24-
#endif
2520
#endif

include/nbl/system/CStdoutLoggerAndroid.h

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,21 @@
11
#ifndef _NBL_SYSTEM_C_STDOUT_LOGGER_ANDROID_H_INCLUDED_
22
#define _NBL_SYSTEM_C_STDOUT_LOGGER_ANDROID_H_INCLUDED_
3-
#ifdef _NBL_PLATFORM_ANDROID_
4-
#include "nbl/system/IThreadsafeLogger.h"
5-
#include <android/log.h>
63

4+
#include "nbl/system/IThreadsafeLogger.h"
75

6+
#ifdef _NBL_PLATFORM_ANDROID_
87
namespace nbl::system
98
{
10-
class CStdoutLoggerAndroid : public IThreadsafeLogger
11-
{
9+
10+
class CStdoutLoggerAndroid : public IThreadsafeLogger
11+
{
1212
public:
1313
CStdoutLoggerAndroid(core::bitflag<E_LOG_LEVEL> logLevelMask = ILogger::defaultLogMask()) : IThreadsafeLogger(logLevelMask) {}
1414

1515
private:
16-
auto getNativeLogLevel(E_LOG_LEVEL logLevel)
17-
{
18-
switch (logLevel)
19-
{
20-
case ELL_DEBUG:
21-
{
22-
return ANDROID_LOG_DEBUG;
23-
}
24-
case ELL_INFO:
25-
{
26-
return ANDROID_LOG_INFO;
27-
}
28-
case ELL_WARNING:
29-
{
30-
return ANDROID_LOG_WARN;
31-
}
32-
case ELL_ERROR:
33-
{
34-
return ANDROID_LOG_ERROR;
35-
}
36-
case ELL_PERFORMANCE:
37-
{
38-
return ANDROID_LOG_INFO;
39-
}
40-
default:
41-
{
42-
assert(false);
43-
return ANDROID_LOG_UNKNOWN;
44-
}
45-
}
46-
}
47-
void threadsafeLog_impl(const std::string_view& fmt, E_LOG_LEVEL logLevel, va_list args) override
48-
{
49-
(void)__android_log_print(getNativeLogLevel(logLevel), "Nabla Engine", "%s", constructLogString(fmt, logLevel, args).c_str());
50-
}
51-
};
16+
void threadsafeLog_impl(const std::string_view& fmt, E_LOG_LEVEL logLevel, va_list args) override;
17+
};
18+
5219
}
5320

5421
#endif

src/nbl/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ set(NBL_SYSTEM_SOURCES
196196
${NBL_ROOT_PATH}/src/nbl/system/CArchiveLoaderTar.cpp
197197
${NBL_ROOT_PATH}/src/nbl/system/ISystem.cpp
198198
${NBL_ROOT_PATH}/src/nbl/system/IFileArchive.cpp
199+
${NBL_ROOT_PATH}/src/nbl/system/CFileViewVirtualAllocatorWin32.cpp
200+
${NBL_ROOT_PATH}/src/nbl/system/CFileViewVirtualAllocatorPOSIX.cpp
201+
${NBL_ROOT_PATH}/src/nbl/system/CStdoutLoggerAndroid.cpp
199202
)
200203
set(NBL_UI_SOURCES
201204
${NBL_ROOT_PATH}/src/nbl/ui/CWindowWin32.cpp
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "nbl/system/CFileViewVirtualAllocatorPOSIX.h"
2+
3+
using namespace nbl::system;
4+
5+
#if defined(_NBL_PLATFORM_LINUX_) || defined(_NBL_PLATFORM_ANDROID_)
6+
#include <sys/mman.h>
7+
8+
void* CFileViewVirtualAllocatorPOSIX::alloc(size_t size) override
9+
{
10+
return mmap((caddr_t)0, size, PROT_WRITE|PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
11+
}
12+
bool CFileViewVirtualAllocatorPOSIX::dealloc(void* data, size_t size) override
13+
{
14+
const auto ret = munmap(data,size);
15+
return ret != -1;
16+
}
17+
#endif
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "nbl/system/CFileViewVirtualAllocatorWin32.h"
2+
3+
using namespace nbl::system;
4+
5+
#ifdef _NBL_PLATFORM_WINDOWS_
6+
#include "Windows.h"
7+
8+
void* CFileViewVirtualAllocatorWin32::alloc(size_t size)
9+
{
10+
return VirtualAlloc(nullptr, size, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE); // TODO: are these even the right flags? Do we want to commit everything right away?
11+
}
12+
bool CFileViewVirtualAllocatorWin32::dealloc(void* data, size_t size)
13+
{
14+
return VirtualFree(data, 0, MEM_RELEASE);
15+
}
16+
#endif
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "nbl/system/CStdoutLoggerAndroid.h"
2+
3+
#ifdef _NBL_PLATFORM_ANDROID_
4+
#include <android/log.h>
5+
6+
using namespace nbl::system;
7+
8+
void CStdoutLoggerAndroid::threadsafeLog_impl(const std::string_view& fmt, E_LOG_LEVEL logLevel, va_list args) override
9+
{
10+
auto nativeLogLevel = ANDROID_LOG_UNKNOWN;
11+
switch (logLevel)
12+
{
13+
case ELL_DEBUG:
14+
nativeLogLevel = ANDROID_LOG_DEBUG;
15+
break;
16+
case ELL_INFO:
17+
nativeLogLevel = ANDROID_LOG_INFO;
18+
break;
19+
case ELL_WARNING:
20+
nativeLogLevel = ANDROID_LOG_WARN;
21+
break;
22+
case ELL_ERROR:
23+
nativeLogLevel = ANDROID_LOG_ERROR;
24+
break;
25+
case ELL_PERFORMANCE:
26+
nativeLogLevel = ANDROID_LOG_INFO;
27+
break;
28+
default:
29+
assert(false);
30+
break;
31+
}
32+
(void)__android_log_print(nativeLogLevel, "Nabla Engine: ", "%s", constructLogString(fmt,logLevel,args).c_str());
33+
}
34+
#endif

0 commit comments

Comments
 (0)