-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhost_system.h
More file actions
80 lines (64 loc) · 1.66 KB
/
host_system.h
File metadata and controls
80 lines (64 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#ifndef HOST_SYSTEM_H_
#define HOST_SYSTEM_H_
#if _WIN32
#define DEBUGBREAK() __debugbreak()
#define PRETTY_FUNCTION __FUNCSIG__
#if _M_AMD64 == 100
#define PLATFORM_X64 1
#else
#define PLATFORM_UNKNOWN 1
#endif
#else
#define DEBUGBREAK() assert(false)
#define PRETTY_FUNCTION __PRETTY_FUNCTION__
#include <strings.h>
#define _stricmp strcasecmp
#ifdef __x86_64__
#define PLATFORM_X64 1
#else
#define PLATFORM_UNKNOWN 1
#endif
#endif
#include <memory>
#include <string>
#include <vector>
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
class NativeFile
{
public:
virtual ~NativeFile() {}
virtual std::vector<uint8_t> ReadToVector() = 0;
static std::shared_ptr<NativeFile> Open(const std::string& string);
};
// mmap() on linux, VirtualAlloc() on Windows, etc
// This is a block of contiguous virtual memory that can potentially be remapped.
class NativeMemory
{
public:
virtual ~NativeMemory() {}
virtual uint8_t* Pointer() = 0;
virtual size_t GetSize() = 0;
virtual void MakeReadonly() = 0;
virtual void MapForWrite() = 0;
virtual void MapForExecute() = 0;
static std::unique_ptr<NativeMemory> Create(size_t size);
static std::unique_ptr<NativeMemory> Create(NativeFile *file, size_t offset, size_t size);
static size_t GetNativeSize();
};
class UnownedMemory : public NativeMemory
{
public:
UnownedMemory(const void *data, size_t size)
: data(const_cast<uint8_t*>(static_cast<const uint8_t*>(data))), size(size) {}
uint8_t* Pointer() override { return data; }
size_t GetSize() override { return size; }
void MakeReadonly() override {}
void MapForWrite() override {}
void MapForExecute() override {}
private:
uint8_t *data;
size_t size;
};
#endif