-
Notifications
You must be signed in to change notification settings - Fork 1
/
mfile.c
221 lines (191 loc) · 4.52 KB
/
mfile.c
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
/**
* mmap file wrapper
* Copyright 2016 Smx <[email protected]>
* All right reserved
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "mfile.h"
/* Branch Prediction Hints */
#define LIKELY(x) __builtin_expect (!!(x), 1)
#define UNLIKELY(x) __builtin_expect (!!(x), 0)
#define PERMS_DEFAULT (mode_t)0666
/*
* Creates a new mfile structure
*/
inline MFILE *mfile_new(){
MFILE *mem = calloc(1, sizeof(MFILE));
return mem;
}
/*
* Updates size and path to a file
*/
int _mfile_update_info(MFILE *file, const char *path){
if(path){
if(file->path)
free(file->path);
file->path = strdup(path);
}
if(stat(file->path, &(file->statBuf)) < 0)
return -1;
return 0;
}
void mfile_flush(void *mem, size_t length){
msync(mem, length, MS_INVALIDATE);
//madvise(mem, length, MADV_REMOVE);
}
/*
* Wrapper to mmap
*/
void *_mfile_map(MFILE *file, size_t mapSize, int mapFlags){
if(msize(file) < mapSize){
lseek(file->fd, mapSize-1, SEEK_SET);
uint8_t buf = 0x00;
write(file->fd, &buf, 1);
lseek(file->fd, 0, SEEK_SET);
_mfile_update_info(file, NULL);
}
file->pMem = mmap(0, mapSize, file->prot, mapFlags, file->fd, 0);
if(file->pMem == MAP_FAILED){
return NULL;
}
#ifndef _WIN32
// enable read ahead and trash previously read pages
madvise(file->pMem, mapSize, MADV_SEQUENTIAL);
#endif
return file->pMem;
}
inline void *mfile_map(MFILE *file, size_t mapSize){
return _mfile_map(file, mapSize, MAP_SHARED);
}
inline void *mfile_map_private(MFILE *file, size_t mapSize){
return _mfile_map(file, mapSize, MAP_PRIVATE);
}
/*
* Opens and maps a file with open
*/
MFILE *_mopen(const char *path, int oflags, int mapFlags){
MFILE *file = mfile_new();
file->fd = open(path, oflags, PERMS_DEFAULT);
if(file->fd < 0){
goto e0_ret;
}
if(_mfile_update_info(file, path) < 0)
goto e1_ret;
if((oflags & O_ACCMODE) == O_RDONLY) {
file->prot = PROT_READ;
} else if((oflags & O_ACCMODE) == O_WRONLY) {
file->prot = PROT_WRITE;
} else if((oflags & O_ACCMODE) == O_RDWR) {
file->prot = PROT_READ | PROT_WRITE;
}
size_t fileSz = msize(file);
if(fileSz > 0){
if(_mfile_map(file, fileSz, mapFlags) == MAP_FAILED){
goto e1_ret;
}
}
return file;
e1_ret:
close(file->fd);
e0_ret:
if(file->path)
free(file->path);
free(file);
return NULL;
}
inline MFILE *mopen(const char *path, int oflags){
return _mopen(path, oflags, MAP_SHARED);
}
inline MFILE *mopen_private(const char *path, int oflags){
return _mopen(path, oflags, MAP_PRIVATE);
}
int mgetc(MFILE *stream){
if(UNLIKELY(stream->offset >= msize(stream)))
return EOF;
return (unsigned int)(*(&((uint8_t *)(stream->pMem))[stream->offset++]));
}
int mputc(int c, MFILE *stream){
if(UNLIKELY(stream->offset >= msize(stream)))
return EOF;
((uint8_t *)(stream->pMem))[stream->offset] = (uint8_t)c;
stream->offset++;
return c;
}
inline int cgetc(cursor_t *stream){
if(UNLIKELY(stream->offset >= stream->size))
return EOF;
return (unsigned int)(
*(&(
((unsigned char *)(stream->ptr))[stream->offset++]
))
);
}
int cputc(int c, cursor_t *stream){
if(UNLIKELY(stream->offset >= stream->size))
return EOF;
((unsigned char *)(stream->ptr))[stream->offset++] = (unsigned char)c;
return c;
}
/*
* Closes an opened file and frees the structure
*/
int mclose(MFILE *mfile){
if(!mfile || mfile->fd < 0 || !mfile->pMem || mfile->statBuf.st_size <= 0)
return -1;
if(munmap(mfile->pMem, mfile->statBuf.st_size) < 0)
return -2;
free(mfile->path);
if(mfile->fh != NULL){
fclose(mfile->fh);
mfile->fd = 0;
} else {
close(mfile->fd);
}
free(mfile);
mfile = NULL;
return 0;
}
/*
* Opens and maps a file with fopen
*/
MFILE *_mfopen(const char *path, const char *mode, int mapFlags){
MFILE *file = mfile_new();
file->fh = fopen(path, mode);
if(file->fh == NULL){
goto e0_ret;
}
file->fd = fileno(file->fh);
if(_mfile_update_info(file, path) < 0)
goto e1_ret;
if(strstr(mode, "r") != NULL || strstr(mode, "+") != NULL){
file->prot |= PROT_READ;
}
if(strstr(mode, "w") != NULL){
file->prot |= PROT_WRITE;
}
size_t fileSz = msize(file);
if(fileSz > 0){
if(_mfile_map(file, fileSz, mapFlags) == MAP_FAILED){
goto e1_ret;
}
}
return file;
e1_ret:
fclose(file->fh);
e0_ret:
free(file);
return NULL;
}
inline MFILE *mfopen(const char *path, const char *mode){
return _mfopen(path, mode, MAP_SHARED);
}
inline MFILE *mfopen_private(const char *path, const char *mode){
return _mfopen(path, mode, MAP_PRIVATE);
}