-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmapio.h
225 lines (204 loc) · 6.14 KB
/
mmapio.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* \file mmapio.h
* \brief Memory-mapped files
* \author Cody Licorish ([email protected])
*/
#ifndef hg_MMapIO_mmapIo_H_
#define hg_MMapIO_mmapIo_H_
#include <stddef.h>
#ifdef MMAPIO_WIN32_DLL
# ifdef MMAPIO_WIN32_DLL_INTERNAL
# define MMAPIO_API __declspec(dllexport)
# else
# define MMAPIO_API __declspec(dllimport)
# endif /*MMAPIO_DLL_INTERNAL*/
#else
# define MMAPIO_API
#endif /*MMAPIO_WIN32_DLL*/
#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus*/
/**
* \brief Operating system identifier.
*/
enum mmapio_os {
mmapio_os_none = 0,
mmapio_os_unix = 1,
mmapio_os_win32 = 2
};
/**
* \brief File memory access modes.
*/
enum mmapio_mode {
/**
* \brief Open for reading only.
*/
mmapio_mode_read = 0x72,
/**
* \brief Open for reading and writing.
*/
mmapio_mode_write = 0x77,
/**
* \brief Map until end of file.
* \note When this parameter is active, the open functions
* \link mmapio_open \endlink, \link mmapio_u8open \endlink and
* \link mmapio_wopen \endlink will ignore the size parameter.
*/
mmapio_mode_end = 0x65,
/**
* \brief Make a private mapping.
* \note Changes in pages remain private to the process.
*/
mmapio_mode_private = 0x70,
/**
* \brief Allow child processes to inherit this mapping.
* \note If not using bequeath, the caller of
* \link mmapio_open \endlink, \link mmapio_u8open \endlink or
* \link mmapio_wopen \endlink must give time for the function
* to return. Otherwise, the file descriptor of the mapped file
* may leak.
*/
mmapio_mode_bequeath = 0x71
};
/**
* \brief Memory-mapped input-output interface.
*/
struct mmapio_i {
/**
* \brief Destructor; closes the file and frees the space.
* \param m map instance
*/
void (*mmi_dtor)(struct mmapio_i* m);
/**
* \brief Acquire a lock to the space.
* \param m map instance
* \return pointer to locked space on success, NULL otherwise
*/
void* (*mmi_acquire)(struct mmapio_i* m);
/**
* \brief Release a lock of the space.
* \param m map instance
* \param p pointer of region to release
*/
void (*mmi_release)(struct mmapio_i* m, void* p);
/**
* \brief Check the length of the mapped area.
* \param m map instance
* \return the length of the mapped region exposed by this interface
*/
size_t (*mmi_length)(struct mmapio_i const* m);
};
/* BEGIN error handling */
/**
* \brief Get the `errno` value from this library.
* \return an error number
*/
MMAPIO_API
int mmapio_get_errno(void);
/**
* \brief Set an `errno` value to this library.
* \param x the value to set
*/
MMAPIO_API
void mmapio_set_errno(int x);
/* END error handling */
/* BEGIN configurations */
/**
* \brief Check the library's target backend.
* \return a \link mmapio_os \endlink value
*/
MMAPIO_API
int mmapio_get_os(void);
/**
* \brief Check whether the library can handle possible race conditions
* involving file bequeath prevention. Such prevention may be necessary
* when starting child processes.
* \return nonzero if file bequeath prevention is race-proof, zero
* otherwise
*/
MMAPIO_API
int mmapio_check_bequeath_stop(void);
/* END configurations */
/* BEGIN helper functions */
/**
* \brief Helper function closes the file.
* \param m map instance
*/
MMAPIO_API
void mmapio_close(struct mmapio_i* m);
/**
* \brief Helper function acquires file data.
* \param m map instance
* \return pointer to locked space on success, NULL otherwise
*/
MMAPIO_API
void* mmapio_acquire(struct mmapio_i* m);
/**
* \brief Helper function to release a lock of the space.
* \param m map instance
* \param p pointer of region to release
*/
MMAPIO_API
void mmapio_release(struct mmapio_i* m, void* p);
/**
* \brief Helper function to check the length of the space.
* \param m map instance
* \return the length of the space
*/
MMAPIO_API
size_t mmapio_length(struct mmapio_i const* m);
/* END helper functions */
/* BEGIN open functions */
/**
* \brief Open a file using a narrow character name.
* \param nm name of file to map
* \param mode one of 'r' (for readonly) or 'w' (writeable),
* optionally followed by 'e' to extend map to end of file,
* optionally followed by 'p' to make write changes private
* \param sz size in bytes of region to map
* \param off file offset of region to map
* \return an interface on success, `NULL` otherwise
* \note On Windows, this function uses `CreateFileA` directly.
* \note On Unix, this function uses the `open` system call directly.
*/
MMAPIO_API
struct mmapio_i* mmapio_open
(char const* nm, char const* mode, size_t sz, size_t off);
/**
* \brief Open a file using a UTF-8 encoded name.
* \param nm name of file to map
* \brief mode one of 'r' (for readonly) or 'w' (writeable),
* optionally followed by 'e' to extend map to end of file,
* optionally followed by 'p' to make write changes private
* \param sz size in bytes of region to map
* \param off file offset of region to map
* \return an interface on success, `NULL` otherwise
* \note On Windows, this function re-encodes the `nm` parameter from
* UTF-8 to UTF-16, then uses `CreateFileW` on the result.
* \note On Unix, this function uses the `open` system call directly.
*/
MMAPIO_API
struct mmapio_i* mmapio_u8open
(unsigned char const* nm, char const* mode, size_t sz, size_t off);
/**
* \brief Open a file using a wide character name.
* \param nm name of file to map
* \brief mode one of 'r' (for readonly) or 'w' (writeable),
* optionally followed by 'e' to extend map to end of file,
* optionally followed by 'p' to make write changes private
* \param sz size in bytes of region to map
* \param off file offset of region to map
* \return an interface on success, `NULL` otherwise
* \note On Windows, this function uses `CreateFileW` directly.
* \note On Unix, this function translates the wide string
* to a multibyte character string, then passes the result to
* the `open` system call. Use `setlocale` in advance if necessary.
*/
MMAPIO_API
struct mmapio_i* mmapio_wopen
(wchar_t const* nm, char const* mode, size_t sz, size_t off);
/* END open functions */
#ifdef __cplusplus
};
#endif /*__cplusplus*/
#endif /*hg_MMapIO_mmapIo_H_*/