-
Notifications
You must be signed in to change notification settings - Fork 2
/
mmdb_internal.h
233 lines (193 loc) · 4.94 KB
/
mmdb_internal.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
225
226
227
228
229
230
231
232
233
//Copyright ©2019-2020 Francisco Blas Izquierdo Riera (klondike)
#ifndef MMDB_INTERNAL_H
#define MMDB_INTERNAL_H
#define MMDB_THREADSAFE 1
//Needed to allow 64-bit seeks
#ifndef _WIN32
#define _FILE_OFFSET_BITS 64
#define _POSIX_C_SOURCE 200809L
#define _XOPEN_SOURCE 500
#endif
#include <stdint.h>
#include <stdio.h>
//TODO do some thread safety
//https://stackoverflow.com/questions/766477/are-there-equivalents-to-pread-on-different-platforms
#if defined(_WIN32)
#include <io.h>
#include <windows.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//Windows is a bit special here
#define fseeko(a,b,c) _fseeki64((a),(b),(c))
#define ftello(a) _ftelli64((a))
typedef __int64 poff64_t;
typedef int pfilehdl_t;
inline static pfilehdl_t _mm_open(const char *path) {
return _open(path,_O_RDONLY|_O_BINARY|_O_RANDOM);
}
inline static int _mm_open_err(pfilehdl_t fd) {
return ((fd) < 0);
}
inline static int _mm_close(pfilehdl_t fd) {
return _close(fd);
}
inline static size_t _mm_pread(pfilehdl_t fd, void *ret, const size_t sz, poff64_t *offset) {
HANDLE fhdl = (HANDLE)_get_osfhandle(fd);
if (fhdl == INVALID_HANDLE_VALUE)
return 0;
OVERLAPPED overlapped;
memset(&overlapped, 0, sizeof(OVERLAPPED));
overlapped.OffsetHigh = (uint32_t)(*offset >> 32);
overlapped.Offset = (uint32_t)*offset;
DWORD rdsz;
SetLastError(0);
if (!ReadFile(fhdl, ret, sz, &rdsz, &overlapped) && GetLastError() != ERROR_HANDLE_EOF) {
errno = GetLastError();
return 0;
}
*offset += rdsz;
return rdsz;
}
inline static poff64_t _mm_getfsz(pfilehdl_t fd) {
return _filelengthi64(fd);
}
#elif defined(MMDB_USE_MMAP) && defined(__unix)
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
typedef off_t poff64_t;
struct mmaphdl {
poff64_t dbsz;
char *db;
};
typedef struct mmaphdl * pfilehdl_t;
inline static pfilehdl_t _mm_open(const char *path) {
struct mmaphdl * rv = malloc(sizeof(struct mmaphdl));
if (rv == NULL)
goto close0;
int fd = open(path,O_RDONLY);
if (fd < 0)
goto close1;
struct stat stb;
if (fstat(fd, &stb) < 0)
goto close2;
rv->dbsz = stb.st_size;
rv->db=mmap(NULL,rv->dbsz,PROT_READ,MAP_SHARED,fd,0);
if(rv->db == MAP_FAILED)
goto close2;
close(fd);
return rv;
close2:
close(fd);
close1:
free(rv);
close0:
return NULL;
}
inline static int _mm_open_err(pfilehdl_t fd) {
return (fd == NULL);
}
inline static int _mm_close(pfilehdl_t fd) {
int rv = munmap(fd->db,fd->dbsz);
free(fd);
return rv;
}
inline static size_t _mm_pread(pfilehdl_t fd, void *ret, const size_t sz, poff64_t *offset) {
poff64_t rv = sz;
if (*offset >= fd->dbsz)
return 0;
if (*offset < 0)
*offset = 0;
if (fd->dbsz - *offset < rv)
rv = fd->dbsz - *offset;
memcpy(ret,fd->db+(*offset),rv);
*offset += rv;
return rv;
}
inline static poff64_t _mm_getfsz(pfilehdl_t fd) {
return fd->dbsz;
}
#elif defined(__unix)
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
typedef off_t poff64_t;
typedef int pfilehdl_t;
inline static pfilehdl_t _mm_open(const char *path) {
return open(path,O_RDONLY);
}
inline static int _mm_open_err(pfilehdl_t fd) {
return ((fd) < 0);
}
inline static int _mm_close(pfilehdl_t fd) {
return close(fd);
}
inline static size_t _mm_pread(pfilehdl_t fd, void *ret, const size_t sz, poff64_t *offset) {
ssize_t rv = pread(fd, ret, sz, *offset);
if (rv < 0)
return 0;
*offset += rv;
return rv;
}
inline static poff64_t _mm_getfsz(pfilehdl_t fd) {
struct stat stb;
if (fstat(fd, &stb) < 0)
return -1;
else
return stb.st_size;
}
#else
//These aren't thread safe!
#warning "Using non thread safe file accessors"
#undef MMDB_THREADSAFE
#define MMDB_THREADSAFE 0
typedef off_t poff64_t;
typedef FILE * pfilehdl_t;
inline static pfilehdl_t _mm_open(const char *path) {
return fopen(path,"rb");
}
inline static int _mm_open_err(pfilehdl_t fd) {
return ((fd) == NULL);
}
inline static int _mm_close(pfilehdl_t fd) {
return fclose(fd);
}
inline static size_t _mm_pread(pfilehdl_t fd, void *ret, const size_t sz, poff64_t *offset) {
size_t rv;
if (fseeko(fd, *offset, SEEK_SET) < 0)
return 0;
rv = fread(ret, 1, sz, fd);
*offset += rv;
return rv;
}
inline static poff64_t _mm_getfsz(pfilehdl_t fd) {
if (fseeko(fd, 0, SEEK_END ) < 0)
return -1;
return ftello(fd);
}
#endif
#define fread_full(fp,ret,sz,off) (_mm_pread((fp),(ret),(sz),(off)) == (sz))
#define freadc(fp,ret,off) (_mm_pread((fp),(ret),sizeof(char),(off)) == sizeof(char))
#define freadu8(fp,ret,off) (_mm_pread((fp),(ret),sizeof(uint8_t),(off)) == sizeof(uint8_t))
//TODO: mutex handling
struct mmdb_t {
poff64_t data;
poff64_t metadata;
pfilehdl_t fd;
uint32_t max_depth;
uint32_t node_count;
uint16_t record_size;
uint16_t ip_version;
};
union mmdb_length_ptr {
mmdb_length_t length;
mmdb_ptr_t ptr;
};
#endif