-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdb_map.c
More file actions
606 lines (490 loc) · 12.3 KB
/
db_map.c
File metadata and controls
606 lines (490 loc) · 12.3 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <intrin.h>
#else
#define _XOPEN_SOURCE 500
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/file.h>
#include <errno.h>
#include <sched.h>
#include <time.h>
#endif
#ifdef apple
#include <libkern/OSAtomic.h>
#define pause() OSMemoryBarrier()
#else
#define pause() __asm __volatile("pause\n": : : "memory")
#endif
#include "base64.h"
#include "db.h"
#ifndef PRIx64
#define PRIx64 I64x
#endif
#include "db_object.h"
#include "db_arena.h"
#include "db_map.h"
char *hndlPath;
void yield() {
#ifndef _WIN32
pause();
#else
YieldProcessor();
#endif
}
// add next name to path
uint32_t addPath(char *path, uint32_t pathLen, char *name, uint32_t nameLen, uint64_t ver) {
uint32_t len = 0, off = 12;
char buff[12];
if (pathLen)
if( path[pathLen - 1] != '/')
path[pathLen] = '.', len = 1;
path += pathLen;
if (pathLen + nameLen + len < MAX_path) {
memcpy(path + len, name, nameLen);
len += nameLen;
}
if (ver) {
do buff[--off] = ver % 10 + '0';
while (ver /= 10);
if (pathLen + len + 14 - off < MAX_path) {
path[len++] = '-';
memcpy (path + len, buff + off, 12 - off);
len += 12 - off;
}
}
path[len] = 0;
return len;
}
// assemble filename path
void getPath(DbMap *map, char *name, uint32_t nameLen, uint64_t ver) {
uint32_t len = 12, off = 0, prev;
if (map->parent && map->parent->parent)
len += prev = map->parent->pathLen, len++;
else if (hndlPath)
len += prev = (uint32_t)strlen(hndlPath), len++;
else
prev = 0;
map->arenaPath = db_malloc(nameLen + len + 1, false);
// don't propagate Catalog paths
if (map->parent && map->parent->parent)
memcpy (map->arenaPath, map->parent->arenaPath, prev);
else if (hndlPath) {
memcpy (map->arenaPath, hndlPath, prev);
map->arenaPath[prev++] = '/';
}
off = prev;
map->pathLen = off + addPath(map->arenaPath, off, name, nameLen, ver);
}
#ifdef _WIN32
HANDLE openPath(char *path, bool create) {
HANDLE hndl;
DWORD dispMode = create ? OPEN_ALWAYS : OPEN_EXISTING;
#else
int openPath(char *path, bool create) {
int hndl, flags = O_RDWR;
#endif
#ifdef _WIN32
hndl = CreateFile(path, GENERIC_READ | GENERIC_WRITE | DELETE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, dispMode, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL);
if (create && hndl == INVALID_HANDLE_VALUE) {
fprintf(stderr, "Unable to create/open %s, error = %d\n", path, (int)GetLastError());
return NULL;
}
return hndl;
#else
if (create)
flags |= O_CREAT;
hndl = open (path, flags, 0664);
if (create && hndl == -1) {
fprintf (stderr, "Unable to open/create %s, error = %d", path, errno);
return -1;
}
return hndl;
#endif
}
void waitZero(volatile uint8_t *zero) {
while (*zero)
#ifndef _WIN32
pause();
#else
YieldProcessor();
#endif
}
void waitZero32(volatile uint32_t *zero) {
while (*zero)
#ifndef _WIN32
pause();
#else
YieldProcessor();
#endif
}
void waitZero64(volatile uint64_t *zero) {
while (*zero)
#ifndef _WIN32
pause();
#else
YieldProcessor();
#endif
}
void waitNonZero(volatile uint8_t *zero) {
while (!*zero)
#ifndef _WIN32
pause();
#else
YieldProcessor();
#endif
}
void waitNonZero32(volatile uint32_t *zero) {
while (!*zero)
#ifndef _WIN32
pause();
#else
YieldProcessor();
#endif
}
void waitNonZero64(volatile uint64_t *zero) {
while (!*zero)
#ifndef _WIN32
pause();
#else
YieldProcessor();
#endif
}
void lockLatchGrp(volatile uint8_t *latch, uint8_t bitNo) {
uint8_t mask = 1 << (bitNo % 8);
#ifndef _WIN32
while (__sync_fetch_and_or(latch + bitNo / 8, mask) & mask) {
#else
while (_InterlockedOr8(latch + bitNo / 8, mask) & mask) {
#endif
do
#ifndef _WIN32
pause();
#else
YieldProcessor();
#endif
while (latch[bitNo / 8] & mask);
}
}
void unlockLatchGrp(volatile uint8_t *latch, uint8_t bitNo) {
uint8_t mask = 1 << (bitNo % 8);
#ifndef _WIN32
__sync_fetch_and_and(latch + bitNo / 8, (uint8_t)~mask);
#else
_InterlockedAnd8(latch + bitNo / 8, (uint8_t)~mask);
#endif
}
void lockAddr(volatile uint64_t* bits) {
#ifndef _WIN32
while (__sync_fetch_and_or(bits, ADDR_MUTEX_SET) & ADDR_MUTEX_SET) {
#else
while (_InterlockedOr64((volatile int64_t *)bits, ADDR_MUTEX_SET) & ADDR_MUTEX_SET) {
#endif
do
#ifndef _WIN32
pause();
#else
YieldProcessor();
#endif
while (*bits & ADDR_MUTEX_SET);
}
}
void unlockAddr(volatile uint64_t* bits) {
#ifdef _WIN32
MemoryBarrier();
#else
__sync_synchronize();
#endif
*bits &= ~ADDR_MUTEX_SET;
}
uint8_t atomicAnd8(volatile uint8_t *value, uint8_t mask) {
#ifndef _WIN32
return __sync_fetch_and_and(value, mask);
#else
return _InterlockedAnd8( value, mask);
#endif
}
// atomic install 8 bit value
bool atomicCAS8(volatile uint8_t *dest, uint8_t comp, uint8_t newValue) {
#ifdef _WIN32
return _InterlockedCompareExchange8 (dest, newValue, comp) == comp;
#else
return __sync_bool_compare_and_swap (dest, comp, newValue);
#endif
}
// atomic install 16 bit value
bool atomicCAS16(volatile uint16_t *dest, uint16_t comp, uint16_t value) {
#ifdef _WIN32
return _InterlockedCompareExchange16 (dest, value, comp) == comp;
#else
return __sync_bool_compare_and_swap (dest, comp, value);
#endif
}
// atomic install 32 bit value
bool atomicCAS32(volatile uint32_t *dest, uint32_t comp, uint32_t value) {
#ifdef _WIN32
return _InterlockedCompareExchange (dest, value, comp) == comp;
#else
return __sync_bool_compare_and_swap (dest, comp, value);
#endif
}
// atomic install 64 bit value
bool atomicCAS64(volatile uint64_t *dest, uint64_t comp, uint64_t value) {
#ifdef _WIN32
return _InterlockedCompareExchange64 (dest, value, comp) == comp;
#else
return __sync_bool_compare_and_swap (dest, comp, value);
#endif
}
uint8_t atomicOr8(volatile uint8_t *value, uint8_t mask) {
#ifndef _WIN32
return __sync_fetch_and_or(value, mask);
#else
return _InterlockedOr8( value, mask);
#endif
}
uint64_t atomicAdd64(volatile uint64_t *value, int64_t amt) {
#ifndef _WIN32
return __sync_add_and_fetch(value, amt);
#else
return _InterlockedExchangeAdd64( value, amt) + amt;
#endif
}
uint32_t atomicAdd32(volatile uint32_t *value, int32_t amt) {
#ifndef _WIN32
return __sync_add_and_fetch(value, amt);
#else
return _InterlockedExchangeAdd( (volatile long *)value, amt) + amt;
#endif
}
uint16_t atomicAdd16(volatile uint16_t *value, int16_t amt) {
#ifndef _WIN32
return __sync_add_and_fetch(value, amt);
#else
return _InterlockedExchangeAdd16( (volatile short *)value, amt) + amt;
#endif
}
uint64_t atomicOr64(volatile uint64_t *value, uint64_t amt) {
#ifndef _WIN32
return __sync_fetch_and_or (value, amt);
#else
return _InterlockedOr64( value, amt);
#endif
}
uint32_t atomicOr32(volatile uint32_t *value, uint32_t amt) {
#ifndef _WIN32
return __sync_fetch_and_or(value, amt);
#else
return _InterlockedOr( (volatile long *)value, amt);
#endif
}
void *mapMemory (DbMap *map, uint64_t offset, uint64_t size, uint32_t segNo) {
void *mem;
#ifndef _WIN32
int flags = 0;
if( map->hndl < 0 ) {
offset = 0;
#ifdef MAP_ANON
flags |= MAP_ANON;
#endif
#ifdef __MAP_ANONYMOUS
flags |= __MAP_ANONYMOUS;
#endif
#ifdef MAP_ANONYMOUS
flags |= MAP_ANONYMOUS;
#endif
if(flags == 0 )
flags = 0x20;
}
flags |= MAP_SHARED;
mem = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, map->hndl, offset);
if (mem == MAP_FAILED) {
fprintf (stderr, "Unable to mmap %s, offset = %" PRIx64 ", size = %" PRIx64 ", error = %d", map->arenaPath, offset, size, errno);
return NULL;
}
#else
if (map->hndl == INVALID_HANDLE_VALUE)
return VirtualAlloc(NULL, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (!(map->maphndl[segNo] = CreateFileMapping(map->hndl, NULL, PAGE_READWRITE, (DWORD)((offset + size) >> 32), (DWORD)(offset + size), NULL))) {
fprintf (stderr, "Unable to CreateFileMapping %s, size = %" PRIx64 ", segment = %d error = %d\n", map->arenaPath, offset + size, segNo, (int)GetLastError());
return NULL;
}
mem = MapViewOfFile(map->maphndl[segNo], FILE_MAP_WRITE, (DWORD)(offset >> 32), (DWORD)offset, size);
if (!mem) {
fprintf (stderr, "Unable to MapViewOfFile %s, offset = %" PRIx64 ", size = %" PRIx64 ", error = %d\n", map->arenaPath, offset, size, (int)GetLastError());
return NULL;
}
#endif
return mem;
}
void unmapSeg (DbMap *map, uint32_t segNo) {
#ifndef _WIN32
munmap(map->base[segNo], map->arena->segs[segNo].size);
#else
if (map->arenaDef && !map->arenaDef->params[OnDisk].boolVal) {
VirtualFree(map->base[segNo], 0, MEM_RELEASE);
return;
}
UnmapViewOfFile(map->base[segNo]);
CloseHandle(map->maphndl[segNo]);
#endif
}
uint64_t compareAndSwap(volatile uint64_t* target, uint64_t compareVal, uint64_t swapVal) {
#ifndef _WIN32
return __sync_val_compare_and_swap(target, compareVal, swapVal);
#else
return _InterlockedCompareExchange64((volatile __int64*)target, swapVal, compareVal);
#endif
}
uint64_t atomicExchange(volatile uint64_t *target, uint64_t swapVal) {
#ifndef _WIN32
return __sync_lock_test_and_set(target, swapVal);
#else
return _InterlockedExchange64((volatile int64_t*)target, swapVal);
#endif
}
uint8_t atomicExchange8(volatile uint8_t *target, uint8_t swapVal) {
#ifndef _WIN32
return __sync_lock_test_and_set(target, swapVal);
#else
return _InterlockedExchange8(target, swapVal);
#endif
}
#ifdef _WIN32
void lockArena (HANDLE hndl, char *path) {
OVERLAPPED ovl[1];
memset (ovl, 0, sizeof(ovl));
ovl->OffsetHigh = 0x80000000;
if (LockFileEx (hndl, LOCKFILE_EXCLUSIVE_LOCK, 0, sizeof(DbArena), 0, ovl))
return;
fprintf (stderr, "Unable to lock %s, error = %d", path, (int)GetLastError());
exit(1);
}
#else
void lockArena (int hndl, char *path) {
if (!flock(hndl, LOCK_EX))
return;
fprintf (stderr, "Unable to lock %s, error = %d", path, errno);
exit(1);
}
#endif
#ifdef _WIN32
void unlockArena (HANDLE hndl, char *path) {
OVERLAPPED ovl[1];
memset (ovl, 0, sizeof(ovl));
ovl->OffsetHigh = 0x80000000;
if (UnlockFileEx (hndl, 0, sizeof(DbArena), 0, ovl))
return;
fprintf (stderr, "Unable to unlock %s, error = %d", path, (int)GetLastError());
exit(1);
}
#else
void unlockArena (int hndl, char *path) {
if (!flock(hndl, LOCK_UN))
return;
fprintf (stderr, "Unable to unlock %s, error = %d", path, errno);
exit(1);
}
#endif
bool fileExists(char *path) {
#ifdef _WIN32
int attr = GetFileAttributes(path);
if( attr == 0xffffffff)
return false;
if (attr & FILE_ATTRIBUTE_DIRECTORY)
return false;
return true;
#else
return !access(path, F_OK);
#endif
}
// close a map
void closeMap(DbMap *map) {
bool killIt = *map->arena->mutex & KILL_BIT;
#ifdef _WIN32
FILE_DISPOSITION_INFO dispInfo[1];
#endif
while (map->numSeg)
unmapSeg(map, --map->numSeg);
if (map->parent)
atomicAdd32(map->parent->openCnt, -1);
if (killIt) {
#ifdef _WIN32
memset (dispInfo, 0, sizeof(dispInfo));
dispInfo->DeleteFile = true;
SetFileInformationByHandle (map->hndl, FileDispositionInfo, dispInfo, sizeof(dispInfo));
#else
unlink(map->arenaPath);
#endif
}
#ifdef _WIN32
CloseHandle(map->hndl);
#else
if( map->hndl < 0 )
close(map->hndl);
#endif
db_free(map);
}
// delete a map by name
void deleteMap(char *path) {
DbMap map[1];
#ifdef _WIN32
HANDLE hndl = openPath (path, false);
if (hndl == INVALID_HANDLE_VALUE)
return;
lockArena(hndl, path);
#else
int hndl = openPath(path, false);
if (hndl < 0)
return;
#endif
memset (map, 0, sizeof(DbMap));
map->hndl = hndl;
if ((map->base[0] = mapMemory(map, 0, sizeof(DbArena), 0))) {
map->arena = (DbArena *)map->base[0];
atomicOr8((volatile uint8_t *)map->arena->mutex, KILL_BIT);
unmapSeg (map, 0);
}
unlockArena(hndl, path);
#ifdef _WIN32
CloseHandle(hndl);
DeleteFile (path);
#else
close(hndl);
unlink(path);
#endif
}
// open arena file and read segment zero
int readSegZero(DbMap *map, DbArena *segZero) {
#ifdef _WIN32
DWORD amt;
#else
int amt;
#endif
#ifdef _WIN32
map->hndl = openPath (map->arenaPath, true);
if (map->hndl == INVALID_HANDLE_VALUE)
return -1;
lockArena(map->hndl, map->arenaPath);
if (!ReadFile(map->hndl, segZero, sizeof(DbArena), &amt, NULL)) {
unlockArena(map->hndl, map->arenaPath);
CloseHandle(map->hndl);
return -1;
}
#else
map->hndl = openPath (map->arenaPath, true);
if (map->hndl == -1)
return -1;
lockArena(map->hndl, map->arenaPath);
#ifdef DEBUG
fprintf(stderr, "lockArena %s\n", map->arenaPath);
#endif
// read first part of segment zero if it exists
amt = pread(map->hndl, segZero, sizeof(DbArena), 0LL);
if (amt < 0)
unlockArena(map->hndl, map->arenaPath);
#endif
return amt;
}