-
Notifications
You must be signed in to change notification settings - Fork 1
/
native_api.h
156 lines (133 loc) · 3.86 KB
/
native_api.h
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <cstdint>
#include <cstdlib>
#include <memory>
#include <string>
#include <chrono>
namespace native_api {
enum class memory_access {
none,
read,
read_write,
read_execute,
read_write_execute
};
struct allocated_memory {
void* ptr = nullptr;
size_t size = 0;
void* detach() {
void* r = ptr;
ptr = nullptr;
return r;
}
allocated_memory() = default;
explicit allocated_memory(void* ptr, size_t size) : ptr(ptr), size(size) {}
explicit allocated_memory(void* addr, size_t size, memory_access access) : ptr(nullptr) {
allocate(addr, size, access);
}
~allocated_memory() {
deallocate();
}
explicit operator bool() {
return ptr != nullptr;
}
void allocate(void* addr, size_t size, memory_access access);
void deallocate();
};
bool set_memory_access(void* ptr, size_t size, memory_access access);
enum class file_access {
read,
read_write
};
enum class file_open_mode {
open_existing,
create_new,
create_always,
open_always,
truncate_existing
};
enum class file_set_pos_origin {
begin,
current,
end
};
struct file_io_impl;
class file_io {
std::unique_ptr<file_io_impl> impl;
public:
file_io();
file_io(file_io&& n);
~file_io();
file_io& operator=(file_io&& n);
bool open(const char* fn, file_access access, file_open_mode mode);
bool read(void* buffer, size_t size, size_t* read);
bool write(void* buffer, size_t size, size_t* written);
uint64_t set_pos(uint64_t pos, file_set_pos_origin origin);
uint64_t get_pos();
uint64_t get_size();
};
struct directory_io_impl;
struct directory_entry {
std::string file_name;
std::chrono::system_clock::time_point creation_time;
std::chrono::system_clock::time_point access_time;
std::chrono::system_clock::time_point write_time;
uint64_t file_size = 0;
bool is_directory = false;
};
struct directory_io {
std::unique_ptr<directory_io_impl> impl;
public:
directory_io();
directory_io(directory_io&& n);
~directory_io();
directory_io& operator=(directory_io&& n);
bool open(const char* fn);
directory_entry get();
bool next();
};
bool is_directory(const char* path);
bool is_file(const char* path);
bool delete_file(const char* path);
bool create_directory(const char* path);
int32_t fetch_increment(int32_t*);
int32_t fetch_decrement(int32_t*);
bool compare_exchange(int64_t* pointer, int64_t& expected, int64_t desired);
bool compare_exchange(int32_t* pointer, int32_t& expected, int32_t desired);
bool exchange(int64_t* pointer, int64_t desired);
bool exchange(int32_t* pointer, int32_t desired);
template<size_t size, size_t alignment> struct atomic_type;
template<size_t alignment>
struct atomic_type<4, alignment> {
using type = typename std::enable_if<alignment <= 4 && (alignment & (alignment - 1)) == 0, int32_t>::type;
};
template<size_t alignment>
struct atomic_type<8, alignment> {
using type = typename std::enable_if<alignment <= 8 && (alignment & (alignment - 1)) == 0, int64_t>::type;
};
template<typename T>
struct atomic_type_for {
using type = typename std::enable_if<std::is_trivially_copyable<T>::value, typename atomic_type<sizeof(T), alignof(T)>::type>::type;
static_assert(sizeof(type) == sizeof(T) && alignof(T) <= alignof(type), "type size/alignment mismatch");
};
template<typename T, typename atomic_type_for<T>::type* = nullptr>
bool compare_exchange(T* pointer, T& expected, const T& desired) {
using AT = typename atomic_type_for<T>::type;
return compare_exchange((AT*)pointer, (AT&)expected, (const AT&)desired);
}
struct shm_io_impl;
enum {
shm_io_read = 1,
shm_io_write = 2,
shm_io_copy_on_write = 4,
};
struct shm_io {
std::unique_ptr<shm_io_impl> impl;
public:
shm_io();
shm_io(shm_io&& n);
~shm_io();
shm_io& operator=(shm_io&& n);
bool open(const char* fn, uint64_t size);
void* map(void* addr, uint64_t offset, size_t size, int flags);
};
};