Skip to content

Commit 504e5f9

Browse files
committed
Re-imagine filesystem API interception mechanism.
Instead of using macros which end up catching too many strays especially in c++ world where member functions can be named common things like close or open, we instead use wrapper functions that are forced to be inlined.
1 parent 0e26e37 commit 504e5f9

File tree

2 files changed

+511
-116
lines changed

2 files changed

+511
-116
lines changed

Embedded/np_embed.c

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ typedef struct FDMAP_S FDMAP;
1818

1919
FDMAP np_fd2file[512] = {};
2020

21-
#ifdef _WIN32
22-
#define NP_FOREIGN_PTR *(void**)e == NULL || (((EFILE*)e)->handle_type != EHANDLE_VIRTUAL && ((EFILE*)e)->handle_type != EHANDLE_NATIVE)
23-
#else
24-
#define NP_FOREIGN_PTR ((EFILE*)e)->handle_type != EHANDLE_VIRTUAL && ((EFILE*)e)->handle_type != EHANDLE_NATIVE
25-
#endif
26-
2721
uint32_t hash(char * key){ // Hash Function: MurmurOAAT64
2822
uint32_t h = 3323198485ul;
2923
for (;*key;++key) {
@@ -190,21 +184,14 @@ NP_DECL(EFILE*) np_fopen(const char* file, const char* mode) {
190184
return e;
191185
}
192186

193-
NP_DECL(int) np_open(const char *file, int flags, ...) {
187+
NP_DECL(int) np_open(const char *file, int flags, va_list args) {
194188
#ifdef _WIN32
195189
int mode = 0;
196190
#else
197191
mode_t mode = 0;
198192
#endif
199193
if (flags & O_CREAT) {
200-
va_list args;
201-
va_start(args, flags);
202-
#ifdef _WIN32
203194
mode = va_arg(args, int);
204-
#else
205-
mode = va_arg(args, mode_t);
206-
#endif
207-
va_end(args);
208195
}
209196

210197
char absolute_path[PATH_MAX] = {};
@@ -272,13 +259,10 @@ NP_DECL(EFILE*) np_wfopen(const wchar_t *wfile, const wchar_t *mode) {
272259
return e;
273260
}
274261

275-
NP_DECL(int) np_wopen(const wchar_t *wfile, int flags, ...) {
262+
NP_DECL(int) np_wopen(const wchar_t *wfile, int flags, va_list args) {
276263
int mode = 0;
277264
if (flags & O_CREAT) {
278-
va_list args;
279-
va_start(args, flags);
280265
mode = va_arg(args, int);
281-
va_end(args);
282266
}
283267

284268
char file[PATH_MAX] = {};
@@ -591,20 +575,12 @@ NP_DECL(int) np_fseeko64(void *e, int64_t offset, int origin) {
591575
return np_fseek_priv(e, offset, origin);
592576
}
593577

594-
NP_DECL(int) np_fscanf(void *e, const char *format, ...) {
578+
NP_DECL(int) np_fscanf(void *e, const char *format, va_list args) {
595579
if (NP_FOREIGN_PTR) {
596-
va_list args;
597-
va_start(args, format);
598-
int result = vfscanf(((FILE*)e), format, args);
599-
va_end(args);
600-
return result;
580+
return vfscanf(((FILE*)e), format, args);
601581
}
602582
if (((EFILE*)e)->handle_type != EHANDLE_VIRTUAL) {
603-
va_list args;
604-
va_start(args, format);
605-
int result = vfscanf(((EFILE*)e)->f, format, args);
606-
va_end(args);
607-
return result;
583+
return vfscanf(((EFILE*)e)->f, format, args);
608584
}
609585
return 0;
610586
}

0 commit comments

Comments
 (0)