libquickjspp-sys 0.1.0

QuickJSpp Javascript Engine FFI bindings
Documentation
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
//-< DATABASE.CPP >--------------------------------------------------*--------*
// GigaBASE                  Version 1.0         (c) 1999  GARRET    *     ?  *
// (Post Relational Database Management System)                      *   /\|  *
//                                                                   *  /  \  *
//                          Created:     20-Nov-1998  K.A. Knizhnik  * / [] \ *
//                          Last update: 23-Nov-2001  K.A. Knizhnik  * GARRET *
//-------------------------------------------------------------------*--------*
// Database memory management, query execution, scheme evaluation
//-------------------------------------------------------------------*--------*

#include "stdtp.h"
#include "database.h"
#include "btree.h"

void dbDatabase::handleError(int error, char const *msg) {
  if (errorHandler != NULL) {
    (*errorHandler)(error, msg);
  } else {
    fprintf(stderr, "Error %d: %s\n", error, msg);
  }
}

void dbDatabase::throwException(int error, char const *msg) {
  handleError(error, msg);
  throw dbException(error, msg);
}

//bool dbDatabase::open(const wchar_t *name, int openAttr) {
  //return open(cvt::w2a(name), openAttr);
//}

bool dbDatabase::open(const char *name, int openAttr) {
  dbCriticalSection cs(mutex);
  int               rc;
  opened = false;

  length_t indexSize =
      initIndexSize < dbFirstUserId ? length_t(dbFirstUserId) : initIndexSize;
  indexSize = DOALIGN(indexSize, dbHandlesPerPage);

  memset(dirtyPagesMap, 0, dbDirtyPageBitmapSize + 4);

  for (int i = dbBitmapId + dbBitmapPages; --i >= 0;) {
    bitmapPageAvailableSpace[i] = INT_MAX;
  }
  currRBitmapPage = currPBitmapPage = dbBitmapId;
  currRBitmapOffs = currPBitmapOffs = 0;
  reservedChain                     = NULL;
  classDescList                     = NULL;
  gcThreshold                       = 0;
  allocatedDelta                    = 0;
  gcDone                            = false;
  modified                          = false;

  if (accessType == dbReadOnly) { openAttr |= dbFile::read_only; }
  if (*name == L'@') {
    // char fn[1024];
    // wcstombs ( fn, name+1, 1024 );
    FILE *f = fopen(name, "r");
    if (f == NULL) {
      handleError(dybase_open_error,
                  "Failed to open database configuration file");
      return false;
    }
    dbMultiFile::dbSegment segment[dbMaxFileSegments];
    const int              maxFileNameLen = 1024;
    char                   fileName[maxFileNameLen];
    int                    i, n;
    db_int8                size;
    bool                   raid          = false;
    length_t               raidBlockSize = dbDefaultRaidBlockSize;
    for (i = 0; (n = fscanf(f, "%s" INT8_FORMAT, fileName, &size)) >= 1; i++) {
      if (i == dbMaxFileSegments) {
        while (--i >= 0)
          delete[] segment[i].name;
        fclose(f);
        handleError(dybase_open_error, "Too much segments");
        return false;
      }

      if (n == 1) {
        if (i == 0) {
          raid = true;
        } else if (!raid && segment[i - 1].size == 0) {
          while (--i >= 0)
            delete[] segment[i].name;
          fclose(f);
          handleError(dybase_open_error, "Segment size was not specified");
          return false;
        }
        size = 0;
      } else if (size == 0 || raid) {
        while (--i >= 0)
          delete[] segment[i].name;
        fclose(f);
        handleError(dybase_open_error,
                    size == 0
                        ? "Invalid segment size"
                        : "segment size should not be specified for raid");
        return false;
      }

      if (strcmp(fileName, ".RaidBlockSize") == 0) {
        raidBlockSize = (length_t)size;
        raid          = true;
        i -= 1;
        continue;
      }
      segment[i].size = offs_t(size);
      char *  suffix  = strchr(fileName, '[');
      db_int8 offs    = 0;
      if (suffix != NULL) {
        *suffix = '\0';
        sscanf(suffix + 1, INT8_FORMAT, &offs);
      }
      segment[i].name = new char[strlen(fileName) + 1];
      strcpy(segment[i].name, fileName);
      segment[i].offs = offs_t(offs);
    }
    fclose(f);
    if (i == 0) {
      // fclose(f);
      handleError(dybase_open_error, "File should have at least one segment");
      return false;
    }
    if (i == 1 && raid) { raid = false; }
    dbMultiFile *mfile;
    if (raid) {
      mfile = new dbRaidFile(raidBlockSize);
    } else {
      mfile = new dbMultiFile;
    }
    rc = mfile->open(i, segment, openAttr);
    while (--i >= 0)
      delete[] segment[i].name;
    if (rc != dbFile::ok) {
      delete mfile;
      handleError(dybase_open_error, "Failed to create database file");
      return false;
    }
    file = mfile;
  } else {
    file = new dbFile;
    if (file->open(name, openAttr) != dbFile::ok) {
      delete file;
      handleError(dybase_open_error, "Failed to create database file");
      return false;
    }
  }
  memset(header, 0, sizeof(dbHeader));
  rc = file->read(0, header, dbPageSize);
  if (rc != dbFile::ok && rc != dbFile::eof) {
    delete file;
    handleError(dybase_open_error, "Failed to read file header");
    return false;
  }

  if ((unsigned)header->curr > 1) {
    delete file;
    handleError(dybase_open_error,
                "Database file was corrupted: invalid root index");
    return false;
  }
  if (!header->isInitialized()) {
    if (accessType == dbReadOnly) {
      delete file;
      handleError(dybase_open_error,
                  "Can not open uninitialized file in read only mode");
      return false;
    }
    curr = header->curr           = 0;
    length_t used                 = dbPageSize;
    header->root[0].index         = used;
    header->root[0].indexSize     = indexSize;
    header->root[0].indexUsed     = dbFirstUserId;
    header->root[0].freeList      = 0;
    header->root[0].classDescList = 0;
    header->root[0].rootObject    = 0;
    used += indexSize * sizeof(offs_t);
    header->root[1].index         = used;
    header->root[1].indexSize     = indexSize;
    header->root[1].indexUsed     = dbFirstUserId;
    header->root[1].freeList      = 0;
    header->root[1].classDescList = 0;
    header->root[1].rootObject    = 0;
    used += indexSize * sizeof(offs_t);

    header->root[0].shadowIndex     = header->root[1].index;
    header->root[1].shadowIndex     = header->root[0].index;
    header->root[0].shadowIndexSize = indexSize;
    header->root[1].shadowIndexSize = indexSize;

    length_t bitmapPages =
        (used + dbPageSize * (dbAllocationQuantum * 8 - 1) - 1) /
        (dbPageSize * (dbAllocationQuantum * 8 - 1));
    length_t bitmapSize     = bitmapPages * dbPageSize;
    length_t usedBitmapSize = (used + bitmapSize) / (dbAllocationQuantum * 8);
    byte *   bitmap         = (byte *)dbFile::allocateBuffer(bitmapSize);
    memset(bitmap, 0xFF, usedBitmapSize);
    memset(bitmap + usedBitmapSize, 0, bitmapSize - usedBitmapSize);
    rc = file->write(used, bitmap, bitmapSize);
    dbFile::deallocateBuffer(bitmap);
    if (rc != dbFile::ok) {
      delete file;
      handleError(dybase_open_error, "Failed to write to the file");
      return false;
    }
    length_t bitmapIndexSize =
        DOALIGN((dbBitmapId + dbBitmapPages) * sizeof(offs_t), dbPageSize);
    offs_t *index      = (offs_t *)dbFile::allocateBuffer(bitmapIndexSize);
    index[dbInvalidId] = dbFreeHandleFlag;
    length_t i;
    for (i = 0; i < bitmapPages; i++) {
      index[dbBitmapId + i] = used | dbPageObjectFlag | dbModifiedFlag;
      used += dbPageSize;
    }
    header->root[0].bitmapEnd = dbBitmapId + i;
    header->root[1].bitmapEnd = dbBitmapId + i;
    while (i < dbBitmapPages) {
      index[dbBitmapId + i] = dbFreeHandleFlag;
      i += 1;
    }
    rc = file->write(header->root[1].index, index, bitmapIndexSize);
    dbFile::deallocateBuffer(index);
    if (rc != dbFile::ok) {
      delete file;
      handleError(dybase_open_error, "Failed to write index to the file");
      return false;
    }
    header->root[0].size = used;
    header->root[1].size = used;
    currIndexSize        = dbFirstUserId;
    if (!pool.open(file, used)) {
      delete file;
      handleError(dybase_open_error, "Failed to allocate page pool");
      return false;
    }
    if (dbFileExtensionQuantum != 0) {
      file->setSize(DOALIGN(used, dbFileExtensionQuantum));
    }
    offs_t indexPage = header->root[1].index;
    offs_t lastIndexPage =
        indexPage + header->root[1].bitmapEnd * sizeof(offs_t);
    while (indexPage < lastIndexPage) {
      offs_t *p = (offs_t *)pool.put(indexPage);
      for (i = 0; i < dbHandlesPerPage; i++) {
        p[i] &= ~dbModifiedFlag;
      }
      pool.unfix(p);
      indexPage += dbPageSize;
    }
    pool.copy(header->root[0].index, header->root[1].index,
              currIndexSize * sizeof(offs_t));
    header->dirty        = true;
    header->root[0].size = header->root[1].size;
    if (file->write(0, header, dbPageSize) != dbFile::ok) {
      pool.close();
      delete file;
      handleError(dybase_open_error, "Failed to write to the file");
      return false;
    }
    pool.flush();
    header->initialized = true;
    if (file->write(0, header, dbPageSize) != dbFile::ok ||
        file->flush() != dbFile::ok) {
      pool.close();
      delete file;
      handleError(dybase_open_error, "Failed to complete file initialization");
      return false;
    }
  } else {
    int curr   = header->curr;
    this->curr = curr;
    if (header->root[curr].indexSize != header->root[curr].shadowIndexSize) {
      delete file;
      handleError(dybase_open_error, "Header of database file is corrupted");
      return false;
    }

    if (rc != dbFile::ok) {
      delete file;
      handleError(dybase_open_error, "Failed to read object index");
      return false;
    }
    pool.open(file, header->root[curr].size);
    if (header->dirty) {
      TRACE_MSG(("Database was not normally closed: start recovery\n"));
      if (accessType == dbReadOnly) {
        pool.close();
        delete file;
        handleError(dybase_open_error,
                    "Can not open dirty file in read only mode");
        return false;
      }
      header->root[1 - curr].size        = header->root[curr].size;
      header->root[1 - curr].indexUsed   = header->root[curr].indexUsed;
      header->root[1 - curr].freeList    = header->root[curr].freeList;
      header->root[1 - curr].index       = header->root[curr].shadowIndex;
      header->root[1 - curr].indexSize   = header->root[curr].shadowIndexSize;
      header->root[1 - curr].shadowIndex = header->root[curr].index;
      header->root[1 - curr].shadowIndexSize = header->root[curr].indexSize;
      header->root[1 - curr].bitmapEnd       = header->root[curr].bitmapEnd;
      header->root[1 - curr].rootObject      = header->root[curr].rootObject;
      header->root[1 - curr].classDescList   = header->root[curr].classDescList;

      pool.copy(
          header->root[1 - curr].index, header->root[curr].index,
          DOALIGN(header->root[curr].indexUsed * sizeof(offs_t), dbPageSize));
      TRACE_MSG(("Recovery completed\n"));
    }
    currIndexSize = header->root[1 - curr].indexUsed;
  }
  committedIndexSize = currIndexSize;

  loadScheme();
  opened = true;
  return true;
}

void dbDatabase::loadScheme() {
  dbGetTie            tie;
  dbClassDescriptor **cpp = &classDescList;
  int                 cid = header->root[1 - curr].classDescList;
  while (cid != 0) {
    dbClass *          cls  = ((dbClass *)getObject(tie, cid))->clone();
    dbClassDescriptor *desc = new dbClassDescriptor(cls, cid);
    classOidHash.put(&desc->oid, sizeof(desc->oid), desc);
    classSignatureHash.put(cls->signature, desc->signatureSize, desc);
    *cpp = desc;
    cpp  = &desc->next;
    cid  = cls->next;
  }
  *cpp = NULL;
}

void dbDatabase::close() {
  dbCriticalSection cs(mutex);
  if (!opened) {
    handleError(dybase_not_opened, "Database not opened");
    return;
  }
  if (modified) { commitTransaction(); }
  dbClassDescriptor *desc, *next;
  for (desc = classDescList; desc != NULL; desc = next) {
    next = desc->next;
    delete desc;
  }
  classDescList = NULL;
  classOidHash.clear();
  classSignatureHash.clear();

  opened = false;
  if (header->dirty) {
    int rc = file->write(0, header, dbPageSize);
    if (rc != dbFile::ok) {
      throwException(dybase_file_error, "Failed to write header to the disk");
    }
    pool.flush();
    header->dirty = false;
    rc            = file->write(0, header, dbPageSize);
    if (rc != dbFile::ok) {
      throwException(dybase_file_error, "Failed to write header to the disk");
    }
  }
  pool.close();
  file->close();
  delete file;
}

dbObject *dbDatabase::putObject(dbPutTie &tie, oid_t oid) {
  offs_t    pos  = getPos(oid);
  int       offs = (int)pos & (dbPageSize - 1);
  byte *    p    = pool.get(pos - offs);
  dbObject *obj  = (dbObject *)(p + (offs & ~dbFlagsMask));
  if (!(offs & dbModifiedFlag)) {
    dirtyPagesMap[length_t(oid / dbHandlesPerPage / 32)] |=
        1 << int(oid / dbHandlesPerPage & 31);
    cloneBitmap(pos & ~dbFlagsMask, obj->size);
    allocate(obj->size, oid);
    pos = getPos(oid);
  }
  tie.set(pool, oid, pos & ~dbFlagsMask, obj->size);
  pool.unfix(p);
  return (dbObject *)tie.get();
}

byte *dbDatabase::put(dbPutTie &tie, oid_t oid) {
  offs_t pos = getPos(oid);
  if (!(pos & dbModifiedFlag)) {
    dirtyPagesMap[length_t(oid / dbHandlesPerPage / 32)] |=
        1 << int(oid / dbHandlesPerPage & 31);
    allocate(dbPageSize, oid);
    cloneBitmap(pos & ~dbFlagsMask, dbPageSize);
    pos = getPos(oid);
  }
  tie.set(pool, oid, pos & ~dbFlagsMask, dbPageSize);
  return tie.get();
}

oid_t dbDatabase::getRoot() { return header->root[1 - curr].rootObject; }

void dbDatabase::setRoot(oid_t oid) {
  header->root[1 - curr].rootObject = oid;
  modified                          = true;
}

dbLoadHandle *dbDatabase::getLoadHandle(oid_t oid) {
  dbCriticalSection cs(mutex);
  if (!opened) {
    handleError(dybase_not_opened, "Database not opened");
    return NULL;
  }
  dbLoadHandle *hnd = new dbLoadHandle();
  dbObject *    obj = getObject(hnd->tie, oid);
  hnd->curr         = (byte *)(obj + 1);
  hnd->end          = (byte *)obj + obj->size;
  hnd->desc =
      (dbClassDescriptor *)classOidHash.get(&obj->cid, sizeof(obj->cid));
  // assert(hnd->desc != NULL);
  if (hnd->desc == NULL)
    handleError(dybase_bad_key_type, "Bad object descriptor");
  return hnd;
}

void dbDatabase::storeObject(dbStoreHandle *handle) {
  dbCriticalSection cs(mutex);
  if (!opened) {
    handleError(dybase_not_opened, "Database not opened");
    return;
  }
  dbObject *         obj  = (dbObject *)handle->body.base();
  dbClassDescriptor *desc = (dbClassDescriptor *)classSignatureHash.get(
      handle->signature.base(), handle->signature.size());
  if (desc == NULL) {
    dbClass *cls =
        dbClass::create(handle->signature.base(), handle->signature.size());
    cls->next = header->root[1 - curr].classDescList;
    desc      = new dbClassDescriptor(cls, allocateObject(cls));
    header->root[1 - curr].classDescList = desc->oid;
    classOidHash.put(&desc->oid, sizeof(desc->oid), desc);
    classSignatureHash.put(cls->signature, desc->signatureSize, desc);
    desc->next    = classDescList;
    classDescList = desc;
  }
  obj->size  = handle->body.size();
  obj->cid   = desc->oid;
  oid_t  oid = handle->oid;
  offs_t pos = getPos(oid);
  if (pos == 0) {
    pos = allocate(obj->size);
    setPos(oid, pos | dbModifiedFlag);
  } else {
    int      offs    = (int)pos & (dbPageSize - 1);
    byte *   p       = pool.get(pos - offs);
    length_t oldSize = ((dbObject *)(p + (offs & ~dbFlagsMask)))->size;
    pool.unfix(p);
    if (!(offs & dbModifiedFlag)) {
      dirtyPagesMap[length_t(oid / dbHandlesPerPage / 32)] |=
          1 << int(oid / dbHandlesPerPage & 31);
      cloneBitmap(pos, oldSize);
      pos = allocate(obj->size);
      setPos(oid, pos | dbModifiedFlag);
    } else {
      if (DOALIGN(oldSize, dbAllocationQuantum) !=
          DOALIGN(obj->size, dbAllocationQuantum)) {
        offs_t newPos = allocate(obj->size);
        cloneBitmap(pos & ~dbFlagsMask, oldSize);
        free(pos & ~dbFlagsMask, oldSize);
        pos = newPos;
        setPos(oid, pos | dbModifiedFlag);
      }
    }
  }
  pool.put(pos & ~dbFlagsMask, (byte *)obj, obj->size);
}

oid_t dbDatabase::allocateObject(dbObject *obj) {
  if (!opened) {
    handleError(dybase_not_opened, "Database not opened");
    return 0;
  }
  oid_t  oid = allocateId();
  offs_t pos = allocate(obj->size);
  setPos(oid, pos | dbModifiedFlag);
  pool.put(pos, (byte *)obj, obj->size);
  return oid;
}

void dbDatabase::freeObject(oid_t oid) {
  dbCriticalSection cs(mutex);
  if (!opened) {
    handleError(dybase_not_opened, "Database not opened");
    return;
  }
  dbObject hdr;
  getHeader(hdr, oid);
  offs_t pos = getPos(oid);
  if (pos & dbModifiedFlag) {
    free(pos & ~dbFlagsMask, hdr.size);
  } else {
    cloneBitmap(pos, hdr.size);
  }
  freeId(oid);
}

void dbDatabase::freePage(oid_t oid) {
  offs_t pos = getPos(oid);
  if (pos & dbModifiedFlag) {
    free(pos & ~dbFlagsMask, dbPageSize);
  } else {
    cloneBitmap(pos & ~dbFlagsMask, dbPageSize);
  }
  freeId(oid);
}

inline void dbDatabase::extend(offs_t size) {
  if (size > header->root[1 - curr].size) {
    if (dbFileExtensionQuantum != 0 &&
        DOALIGN(size, dbFileExtensionQuantum) !=
            DOALIGN(header->root[1 - curr].size, dbFileExtensionQuantum)) {
      file->setSize(DOALIGN(size, dbFileExtensionQuantum));
    }
    header->root[1 - curr].size = size;
  }
}

inline bool dbDatabase::wasReserved(offs_t pos, length_t size) {
  for (dbLocation *location = reservedChain; location != NULL;
       location             = location->next) {
    if (pos - location->pos < location->size || location->pos - pos < size) {
      return true;
    }
  }
  return false;
}

inline void dbDatabase::reserveLocation(dbLocation &location, offs_t pos,
                                        length_t size) {
  location.pos  = pos;
  location.size = size;
  location.next = reservedChain;
  reservedChain = &location;
}

inline void dbDatabase::commitLocation() {
  reservedChain = reservedChain->next;
}

void dbDatabase::setDirty() {
  modified = true;
  if (!header->dirty) {
    header->dirty = true;
    if (file->write(0, header, dbPageSize) != dbFile::ok) {
      throwException(dybase_file_error, "Failed to write header to the file");
    }
    pool.flush();
  }
}

offs_t dbDatabase::allocate(length_t size, oid_t oid) {
  static byte const firstHoleSize[] = {
      8, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0,
      3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
      4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0,
      3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
      5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0,
      3, 0, 1, 0, 2, 0, 1, 0, 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
      4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0,
      3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
      6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0,
      3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
      4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0};
  static byte const lastHoleSize[] = {
      8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3,
      3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
      2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1,
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
      1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  static byte const maxHoleSize[] = {
      8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3,
      3, 3, 3, 3, 3, 3, 3, 3, 5, 4, 3, 3, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2,
      4, 3, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 6, 5, 4, 4, 3, 3, 3, 3,
      3, 2, 2, 2, 2, 2, 2, 2, 4, 3, 2, 2, 2, 1, 1, 1, 3, 2, 1, 1, 2, 1, 1, 1,
      5, 4, 3, 3, 2, 2, 2, 2, 3, 2, 1, 1, 2, 1, 1, 1, 4, 3, 2, 2, 2, 1, 1, 1,
      3, 2, 1, 1, 2, 1, 1, 1, 7, 6, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3,
      4, 3, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 5, 4, 3, 3, 2, 2, 2, 2,
      3, 2, 1, 1, 2, 1, 1, 1, 4, 3, 2, 2, 2, 1, 1, 1, 3, 2, 1, 1, 2, 1, 1, 1,
      6, 5, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 4, 3, 2, 2, 2, 1, 1, 1,
      3, 2, 1, 1, 2, 1, 1, 1, 5, 4, 3, 3, 2, 2, 2, 2, 3, 2, 1, 1, 2, 1, 1, 1,
      4, 3, 2, 2, 2, 1, 1, 1, 3, 2, 1, 1, 2, 1, 1, 0};
  static byte const maxHoleOffset[] = {
      0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 0, 1, 5, 5, 5, 5, 5, 5,
      0, 5, 5, 5, 5, 5, 5, 5, 0, 1, 2, 2, 0, 3, 3, 3, 0, 1, 6, 6, 0, 6, 6, 6,
      0, 1, 2, 2, 0, 6, 6, 6, 0, 1, 6, 6, 0, 6, 6, 6, 0, 1, 2, 2, 3, 3, 3, 3,
      0, 1, 4, 4, 0, 4, 4, 4, 0, 1, 2, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
      0, 1, 2, 2, 0, 3, 3, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 2, 2, 0, 1, 0, 3,
      0, 1, 0, 2, 0, 1, 0, 7, 0, 1, 2, 2, 3, 3, 3, 3, 0, 4, 4, 4, 4, 4, 4, 4,
      0, 1, 2, 2, 0, 5, 5, 5, 0, 1, 5, 5, 0, 5, 5, 5, 0, 1, 2, 2, 0, 3, 3, 3,
      0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 2, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6,
      0, 1, 2, 2, 3, 3, 3, 3, 0, 1, 4, 4, 0, 4, 4, 4, 0, 1, 2, 2, 0, 1, 0, 3,
      0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 2, 2, 0, 3, 3, 3, 0, 1, 0, 2, 0, 1, 0, 4,
      0, 1, 2, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 0};

  setDirty();
  size = DOALIGN(size, dbAllocationQuantum);
  allocatedDelta += size;
  if (gcThreshold != 0 && allocatedDelta > gcThreshold && !gcDone) {
    startGC();
  }

  int            objBitSize = size >> dbAllocationQuantumBits;
  offs_t         pos;
  int            holeBitSize = 0;
  int            alignment   = size & (dbPageSize - 1);
  length_t       offs;
  const int      pageBits = dbPageSize * 8;
  oid_t          firstPage, lastPage;
  int            holeBeforeFreePage = 0;
  oid_t          freeBitmapPage     = 0;
  dbLocation     location;
  dbPutTie       tie;
  oid_t          i;
  const length_t inc = dbPageSize / dbAllocationQuantum / 8;

  lastPage = header->root[1 - curr].bitmapEnd;
  if (alignment == 0) {
    firstPage = currPBitmapPage;
    offs      = DOALIGN(currPBitmapOffs, inc);
  } else {
    firstPage = currRBitmapPage;
    offs      = currRBitmapOffs;
  }

  for (;;) {
    if (alignment == 0) {
      // allocate page object
      for (i = firstPage; i < lastPage; i++) {
        int spaceNeeded = objBitSize - holeBitSize < pageBits
                              ? objBitSize - holeBitSize
                              : pageBits;
        if (bitmapPageAvailableSpace[i] <= spaceNeeded) {
          holeBitSize = 0;
          offs        = 0;
          continue;
        }
        byte *   begin     = get(i);
        length_t startOffs = offs;
        while (offs < dbPageSize) {
          if (begin[offs++] != 0) {
            offs        = DOALIGN(offs, inc);
            holeBitSize = 0;
          } else if ((holeBitSize += 8) == objBitSize) {
            pos =
                ((offs_t(i - dbBitmapId) * dbPageSize + offs) * 8 - holeBitSize)
                << dbAllocationQuantumBits;
            if (wasReserved(pos, size)) {
              offs += objBitSize >> 3;
              startOffs = offs = DOALIGN(offs, inc);
              holeBitSize      = 0;
              continue;
            }
            reserveLocation(location, pos, size);
            currPBitmapPage = i;
            currPBitmapOffs = offs;
            extend(pos + size);
            if (oid != 0) {
              offs_t prev   = getPos(oid);
              int    marker = (int)prev & dbFlagsMask;
              pool.copy(pos, prev - marker, size);
              setPos(oid, pos | marker | dbModifiedFlag);
            }
            pool.unfix(begin);
            begin              = put(tie, i);
            length_t holeBytes = holeBitSize >> 3;
            if (holeBytes > offs) {
              memset(begin, 0xFF, offs);
              holeBytes -= offs;
              begin = put(tie, --i);
              offs  = dbPageSize;
            }
            while (holeBytes > dbPageSize) {
              memset(begin, 0xFF, dbPageSize);
              holeBytes -= dbPageSize;
              bitmapPageAvailableSpace[i] = 0;
              begin                       = put(tie, --i);
            }
            memset(&begin[offs - holeBytes], 0xFF, holeBytes);
            commitLocation();
            return pos;
          }
        }
        if (startOffs == 0 && holeBitSize == 0 &&
            spaceNeeded < bitmapPageAvailableSpace[i]) {
          bitmapPageAvailableSpace[i] = spaceNeeded;
        }
        offs = 0;
        pool.unfix(begin);
      }
    } else {
      for (i = firstPage; i < lastPage; i++) {
        int spaceNeeded = objBitSize - holeBitSize < pageBits
                              ? objBitSize - holeBitSize
                              : pageBits;
        if (bitmapPageAvailableSpace[i] <= spaceNeeded) {
          holeBitSize = 0;
          offs        = 0;
          continue;
        }
        byte *   begin     = get(i);
        length_t startOffs = offs;

        while (offs < dbPageSize) {
          int mask = begin[offs];
          if (holeBitSize + firstHoleSize[mask] >= objBitSize) {
            pos =
                ((offs_t(i - dbBitmapId) * dbPageSize + offs) * 8 - holeBitSize)
                << dbAllocationQuantumBits;
            if (wasReserved(pos, size)) {
              startOffs   = offs += (objBitSize + 7) >> 3;
              holeBitSize = 0;
              continue;
            }
            reserveLocation(location, pos, size);
            currRBitmapPage = i;
            currRBitmapOffs = offs;
            extend(pos + size);
            if (oid != 0) {
              offs_t prev   = getPos(oid);
              int    marker = (int)prev & dbFlagsMask;
              pool.copy(pos, prev - marker, size);
              setPos(oid, pos | marker | dbModifiedFlag);
            }
            pool.unfix(begin);
            begin = put(tie, i);
            begin[offs] |= (1 << (objBitSize - holeBitSize)) - 1;
            if (holeBitSize != 0) {
              if (length_t(holeBitSize) > offs * 8) {
                memset(begin, 0xFF, offs);
                holeBitSize -= offs * 8;
                begin = put(tie, --i);
                offs  = dbPageSize;
              }
              while (holeBitSize > pageBits) {
                memset(begin, 0xFF, dbPageSize);
                holeBitSize -= pageBits;
                bitmapPageAvailableSpace[i] = 0;
                begin                       = put(tie, --i);
              }
              while ((holeBitSize -= 8) > 0) {
                begin[--offs] = 0xFF;
              }
              begin[offs - 1] |= ~((1 << -holeBitSize) - 1);
            }
            commitLocation();
            return pos;
          } else if (maxHoleSize[mask] >= objBitSize) {
            int holeBitOffset = maxHoleOffset[mask];
            pos = ((offs_t(i - dbBitmapId) * dbPageSize + offs) * 8 +
                   holeBitOffset)
                  << dbAllocationQuantumBits;
            if (wasReserved(pos, size)) {
              startOffs   = offs += (objBitSize + 7) >> 3;
              holeBitSize = 0;
              continue;
            }
            reserveLocation(location, pos, size);
            currRBitmapPage = i;
            currRBitmapOffs = offs;
            extend(pos + size);
            if (oid != 0) {
              offs_t prev   = getPos(oid);
              int    marker = (int)prev & dbFlagsMask;
              pool.copy(pos, prev - marker, size);
              setPos(oid, pos | marker | dbModifiedFlag);
            }
            pool.unfix(begin);
            begin = put(tie, i);
            begin[offs] |= ((1 << objBitSize) - 1) << holeBitOffset;
            commitLocation();
            return pos;
          }
          offs += 1;
          if (lastHoleSize[mask] == 8) {
            holeBitSize += 8;
          } else {
            holeBitSize = lastHoleSize[mask];
          }
        }
        if (startOffs == 0 && holeBitSize == 0 &&
            spaceNeeded < bitmapPageAvailableSpace[i]) {
          bitmapPageAvailableSpace[i] = spaceNeeded;
        }
        offs = 0;
        pool.unfix(begin);
      }
    }
    if (firstPage == dbBitmapId) {
      if (freeBitmapPage > i) {
        i           = freeBitmapPage;
        holeBitSize = holeBeforeFreePage;
      }
      if (i == dbBitmapId + dbBitmapPages) {
        throwException(dybase_out_of_memory_error, "Out of memory");
      }
      length_t extension = (size > extensionQuantum) ? size : extensionQuantum;
      int      morePages =
          (extension + dbPageSize * (dbAllocationQuantum * 8 - 1) - 1) /
          (dbPageSize * (dbAllocationQuantum * 8 - 1));

      if (length_t(i + morePages) > dbBitmapId + dbBitmapPages) {
        morePages = (size + dbPageSize * (dbAllocationQuantum * 8 - 1) - 1) /
                    (dbPageSize * (dbAllocationQuantum * 8 - 1));
        if (length_t(i + morePages) > dbBitmapId + dbBitmapPages) {
          throwException(dybase_out_of_memory_error, "Out of memory");
        }
      }
      objBitSize -= holeBitSize;
      int skip = DOALIGN(objBitSize, dbPageSize / dbAllocationQuantum);
      pos      = (offs_t(i - dbBitmapId)
             << (dbPageBits + dbAllocationQuantumBits + 3)) +
            (skip << dbAllocationQuantumBits);
      extend(pos + morePages * dbPageSize);
      length_t len = objBitSize >> 3;
      offs_t   adr = pos;
      byte *   p;
      while (len >= dbPageSize) {
        p = pool.put(adr);
        memset(p, 0xFF, dbPageSize);
        pool.unfix(p);
        adr += dbPageSize;
        len -= dbPageSize;
      }
      p = pool.put(adr);
      memset(p, 0xFF, len);
      p[len] = (1 << (objBitSize & 7)) - 1;
      pool.unfix(p);
      adr = pos + (skip >> 3);
      len = morePages * (dbPageSize / dbAllocationQuantum / 8);
      for (;;) {
        int off = (int)adr & (dbPageSize - 1);
        p       = pool.put(adr - off);
        if (dbPageSize - off >= len) {
          memset(p + off, 0xFF, len);
          pool.unfix(p);
          break;
        } else {
          memset(p + off, 0xFF, dbPageSize - off);
          pool.unfix(p);
          adr += dbPageSize - off;
          len -= dbPageSize - off;
        }
      }
      oid_t j = i;
      while (--morePages >= 0) {
        dirtyPagesMap[length_t(j / dbHandlesPerPage / 32)] |=
            1 << int(j / dbHandlesPerPage & 31);
        setPos(j++, pos | dbPageObjectFlag | dbModifiedFlag);
        pos += dbPageSize;
      }
      freeBitmapPage = header->root[1 - curr].bitmapEnd = j;
      j = i + objBitSize / pageBits;
      if (alignment != 0) {
        currRBitmapPage = j;
        currRBitmapOffs = 0;
      } else {
        currPBitmapPage = j;
        currPBitmapOffs = 0;
      }
      while (j > i) {
        bitmapPageAvailableSpace[length_t(--j)] = 0;
      }

      pos = (offs_t(i - dbBitmapId) * dbPageSize * 8 - holeBitSize)
            << dbAllocationQuantumBits;
      if (oid != 0) {
        offs_t prev   = getPos(oid);
        int    marker = (int)prev & dbFlagsMask;
        pool.copy(pos, prev - marker, size);
        setPos(oid, pos | marker | dbModifiedFlag);
      }
      if (holeBitSize != 0) {
        reserveLocation(location, pos, size);
        while (holeBitSize > pageBits) {
          holeBitSize -= pageBits;
          byte *p = put(tie, --i);
          memset(p, 0xFF, dbPageSize);
          bitmapPageAvailableSpace[i] = 0;
        }
        byte *cur = (byte *)put(tie, --i) + dbPageSize;
        while ((holeBitSize -= 8) > 0) {
          *--cur = 0xFF;
        }
        *(cur - 1) |= ~((1 << -holeBitSize) - 1);
        commitLocation();
      }
      return pos;
    }
    if (gcThreshold != 0 && !gcDone) {
      allocatedDelta -= size;
      startGC();
      currRBitmapPage = currPBitmapPage = dbBitmapId;
      currRBitmapOffs = currPBitmapOffs = 0;
      return allocate(size, oid);
    }
    freeBitmapPage     = i;
    holeBeforeFreePage = holeBitSize;
    holeBitSize        = 0;
    lastPage           = firstPage + 1;
    firstPage          = dbBitmapId;
    offs               = 0;
  }
}

void dbDatabase::free(offs_t pos, length_t size) {
  assert(pos != 0 && (pos & (dbAllocationQuantum - 1)) == 0);
  dbPutTie tie;
  offs_t   quantNo    = pos / dbAllocationQuantum;
  int      objBitSize = (size + dbAllocationQuantum - 1) / dbAllocationQuantum;
  oid_t    pageId     = dbBitmapId + oid_t(quantNo / (dbPageSize * 8));
  length_t offs       = (length_t(quantNo) & (dbPageSize * 8 - 1)) >> 3;
  byte *   p          = put(tie, pageId) + offs;
  int      bitOffs    = int(quantNo) & 7;

  allocatedDelta -= objBitSize * dbAllocationQuantum;

  if ((length_t(pos) & (dbPageSize - 1)) == 0 && size >= dbPageSize) {
    if (pageId == currPBitmapPage && offs < currPBitmapOffs) {
      currPBitmapOffs = offs;
    }
  } else {
    if (pageId == currRBitmapPage && offs < currRBitmapOffs) {
      currRBitmapOffs = offs;
    }
  }

  bitmapPageAvailableSpace[pageId] = INT_MAX;

  if (objBitSize > 8 - bitOffs) {
    objBitSize -= 8 - bitOffs;
    *p++ &= (1 << bitOffs) - 1;
    offs += 1;
    while (objBitSize + offs * 8 > dbPageSize * 8) {
      memset(p, 0, dbPageSize - offs);
      p                                = put(tie, ++pageId);
      bitmapPageAvailableSpace[pageId] = INT_MAX;
      objBitSize -= (dbPageSize - offs) * 8;
      offs = 0;
    }
    while ((objBitSize -= 8) > 0) {
      *p++ = 0;
    }
    *p &= ~((1 << (objBitSize + 8)) - 1);
  } else {
    *p &= ~(((1 << objBitSize) - 1) << bitOffs);
  }
}

void dbDatabase::gc() {
  dbCriticalSection cs(mutex);
  if (gcDone) { return; }
  startGC();
}

void dbDatabase::startGC() {
  int bitmapSize =
      (int)(header->root[curr].size >> (dbAllocationQuantumBits + 5)) + 1;
  bool   existsNotMarkedObjects;
  offs_t pos;
  int    i, j;

  // mark
  greyBitmap  = new db_int4[bitmapSize];
  blackBitmap = new db_int4[bitmapSize];
  memset(greyBitmap, 0, bitmapSize * sizeof(db_int4));
  memset(blackBitmap, 0, bitmapSize * sizeof(db_int4));
  int rootOid = header->root[curr].rootObject;
  if (rootOid != 0) {
    dbGetTie tie;
    markOid(rootOid);
    do {
      existsNotMarkedObjects = false;
      for (i = 0; i < bitmapSize; i++) {
        if (greyBitmap[i] != 0) {
          existsNotMarkedObjects = true;
          for (j = 0; j < 32; j++) {
            if ((greyBitmap[i] & (1 << j)) != 0) {
              pos = (((offs_t)i << 5) + j) << dbAllocationQuantumBits;
              greyBitmap[i] &= ~(1 << j);
              blackBitmap[i] |= 1 << j;
              int       offs = (int)pos & (dbPageSize - 1);
              byte *    pg   = pool.get(pos - offs);
              dbObject *obj  = (dbObject *)(pg + offs);
              if (obj->cid == dbBtreeId) {
                ((dbBtree *)obj)->markTree(this);
              } else if (obj->cid >= dbFirstUserId) {
                markOid(obj->cid);
                tie.set(pool, pos);
                markObject((dbObject *)tie.get());
              }
              pool.unfix(pg);
            }
          }
        }
      }
    } while (existsNotMarkedObjects);
  }

  // sweep
  gcDone = true;
  for (i = dbFirstUserId, j = committedIndexSize; i < j; i++) {
    pos = getGCPos(i);
    if (((int)pos & (dbPageObjectFlag | dbFreeHandleFlag)) == 0) {
      unsigned bit = (unsigned)(pos >> dbAllocationQuantumBits);
      if ((blackBitmap[bit >> 5] & (1 << (bit & 31))) == 0) {
        // object is not accessible
        assert(getPos(i) == pos);
        int       offs = (int)pos & (dbPageSize - 1);
        byte *    pg   = pool.get(pos - offs);
        dbObject *obj  = (dbObject *)(pg + offs);
        if (obj->cid == dbBtreeId) {
          dbBtree::_drop(this, i);
        } else if (obj->cid >= dbFirstUserId) {
          freeId(i);
          cloneBitmap(pos, obj->size);
        }
        pool.unfix(pg);
      }
    }
  }

  delete[] greyBitmap;
  delete[] blackBitmap;
  allocatedDelta = 0;
}

void dbDatabase::markObject(dbObject *obj) {
  byte *p   = (byte *)(obj + 1);
  byte *end = (byte *)obj + obj->size;
  while (p < end) {
    p = markField(p);
  }
}

byte *dbDatabase::markField(byte *p) {
  int     type = *p++;
  db_int4 len;
  oid_t   oid;
  int     i;

  switch (type & 0xF) {
  case dybase_object_ref_type:
  case dybase_array_ref_type:
  case dybase_index_ref_type:
    memcpy(&oid, p, sizeof(oid_t));
    markOid(oid);
    p += sizeof(oid_t);
    break;
  case dybase_bool_type: p += 1; break;
  case dybase_int_type: p += sizeof(db_int4); break;
  case dybase_date_type:
  case dybase_long_type:
  case dybase_real_type: p += sizeof(db_int8); break;
  case dybase_chars_type:
    if (type != dybase_chars_type) {
      // small string
      p += type >> 4;
    } else {
      memcpy(&len, p, sizeof(db_int4));
      p += sizeof(db_int4) + len;
    }
    break;
  case dybase_bytes_type:
    if (type != dybase_bytes_type) {
      // small blob
      p += type >> 4;
    }
    else {
      memcpy(&len, p, sizeof(db_int4));
      p += sizeof(db_int4) + len;
    }
    break;
  case dybase_array_type:
    if (type != dybase_array_type) {
      // small array
      for (i = type >> 4; --i >= 0;) {
        p = markField(p);
      }
    } else {
      memcpy(&len, p, sizeof(db_int4));
      p += sizeof(db_int4);
      for (i = len; --i >= 0;) {
        p = markField(p);
      }
    }
    break;
  case dybase_map_type:
    if (type != dybase_map_type) {
      // small map
      for (i = (type >> 4) << 1; --i >= 0;) {
        p = markField(p);
      }
    } else {
      memcpy(&len, p, sizeof(db_int4));
      p += sizeof(db_int4);
      for (i = len << 1; --i >= 0;) {
        p = markField(p);
      }
    }
  }
  return p;
}

void dbDatabase::cloneBitmap(offs_t pos, length_t size) {
  offs_t   quantNo    = pos / dbAllocationQuantum;
  int      objBitSize = (size + dbAllocationQuantum - 1) / dbAllocationQuantum;
  oid_t    pageId     = dbBitmapId + oid_t(quantNo / (dbPageSize * 8));
  length_t offs       = (length_t(quantNo) & (dbPageSize * 8 - 1)) >> 3;
  int      bitOffs    = int(quantNo) & 7;
  oid_t    oid        = pageId;
  pos                 = getPos(oid);
  if (!(pos & dbModifiedFlag)) {
    dirtyPagesMap[length_t(oid / dbHandlesPerPage / 32)] |=
        1 << (int(oid / dbHandlesPerPage) & 31);
    allocate(dbPageSize, oid);
    cloneBitmap(pos & ~dbFlagsMask, dbPageSize);
  }

  if (objBitSize > 8 - bitOffs) {
    objBitSize -= 8 - bitOffs;
    offs += 1;
    while (objBitSize + offs * 8 > dbPageSize * 8) {
      oid = ++pageId;
      pos = getPos(oid);
      if (!(pos & dbModifiedFlag)) {
        dirtyPagesMap[length_t(oid / dbHandlesPerPage / 32)] |=
            1 << (int(oid / dbHandlesPerPage) & 31);
        allocate(dbPageSize, oid);
        cloneBitmap(pos & ~dbFlagsMask, dbPageSize);
      }
      objBitSize -= (dbPageSize - offs) * 8;
      offs = 0;
    }
  }
}

oid_t dbDatabase::allocate() {
  dbCriticalSection cs(mutex);
  return allocateId();
}

oid_t dbDatabase::allocateId() {
  oid_t oid;
  int   curr = 1 - this->curr;
  setDirty();
  if ((oid = header->root[curr].freeList) != 0) {
    header->root[curr].freeList = oid_t(getPos(oid) >> dbFlagsBits);
    dirtyPagesMap[length_t(oid / dbHandlesPerPage / 32)] |=
        1 << (int(oid / dbHandlesPerPage) & 31);
  } else {
    if (currIndexSize + 1 > header->root[curr].indexSize) {
      length_t oldIndexSize = header->root[curr].indexSize;
      length_t newIndexSize = oldIndexSize * 2;
      while (newIndexSize < oldIndexSize + 1) {
        newIndexSize = newIndexSize * 2;
      }
      TRACE_MSG(
          ("Extend index size from %ld to %ld\n", oldIndexSize, newIndexSize));
      offs_t newIndex = allocate(newIndexSize * sizeof(offs_t));
      offs_t oldIndex = header->root[curr].index;
      pool.copy(newIndex, oldIndex, currIndexSize * sizeof(offs_t));
      header->root[curr].index     = newIndex;
      header->root[curr].indexSize = newIndexSize;
      free(oldIndex, oldIndexSize * sizeof(offs_t));
    }
    oid                          = currIndexSize;
    header->root[curr].indexUsed = ++currIndexSize;
  }
  setPos(oid, 0);
  return oid;
}

void dbDatabase::freeId(oid_t oid) {
  dirtyPagesMap[length_t(oid / dbHandlesPerPage / 32)] |=
      1 << (int(oid / dbHandlesPerPage) & 31);
  setPos(oid, (offs_t(header->root[1 - curr].freeList) << dbFlagsBits) |
                  dbFreeHandleFlag);
  header->root[1 - curr].freeList = oid;
}

void dbDatabase::commit() {
  dbCriticalSection cs(mutex);
  commitTransaction();
}

void dbDatabase::commitTransaction() {
  if (!opened) {
    handleError(dybase_not_opened, "Database not opened");
    return;
  }
  if (!modified) { return; }
  //
  // Commit transaction
  //
  int      rc;
  int      n, curr = header->curr;
  oid_t    i;
  db_int4 *map                = dirtyPagesMap;
  length_t currIndexSize      = this->currIndexSize;
  length_t committedIndexSize = this->committedIndexSize;
  length_t oldIndexSize       = header->root[curr].indexSize;
  length_t newIndexSize       = header->root[1 - curr].indexSize;
  length_t nPages             = committedIndexSize / dbHandlesPerPage;
  if (newIndexSize > oldIndexSize) {
    offs_t newIndex = allocate(newIndexSize * sizeof(offs_t));
    header->root[1 - curr].shadowIndex     = newIndex;
    header->root[1 - curr].shadowIndexSize = newIndexSize;
    cloneBitmap(header->root[curr].index, oldIndexSize * sizeof(offs_t));
    free(header->root[curr].index, oldIndexSize * sizeof(offs_t));
  }

  for (i = 0; i < nPages; i++) {
    if (map[length_t(i >> 5)] & (1 << int(i & 31))) {
      offs_t *srcIndex =
          (offs_t *)pool.get(header->root[1 - curr].index + i * dbPageSize);
      offs_t *dstIndex =
          (offs_t *)pool.get(header->root[curr].index + i * dbPageSize);
      for (length_t j = 0; j < dbHandlesPerPage; j++) {
        offs_t pos = dstIndex[j];
        if (srcIndex[j] != pos) {
          if (!(pos & dbFreeHandleFlag)) {
            if (pos & dbPageObjectFlag) {
              free(pos & ~dbFlagsMask, dbPageSize);
            } else {
              int       offs = (int)pos & (dbPageSize - 1);
              dbObject *rec =
                  (dbObject *)(pool.get(pos - offs) + (offs & ~dbFlagsMask));
              free(pos, rec->size);
              pool.unfix(rec);
            }
          }
        }
      }
      pool.unfix(srcIndex);
      pool.unfix(dstIndex);
    }
  }
  if ((committedIndexSize % dbHandlesPerPage) != 0 &&
      (map[length_t(i >> 5)] & (1 << int(i & 31)))) {
    offs_t *srcIndex =
        (offs_t *)pool.get(header->root[1 - curr].index + i * dbPageSize);
    offs_t *dstIndex =
        (offs_t *)pool.get(header->root[curr].index + i * dbPageSize);
    n = committedIndexSize % dbHandlesPerPage;
    do {
      offs_t pos = *dstIndex;
      if (*srcIndex != pos) {
        if (!(pos & dbFreeHandleFlag)) {
          if (pos & dbPageObjectFlag) {
            free(pos & ~dbFlagsMask, dbPageSize);
          } else {
            int       offs = (int)pos & (dbPageSize - 1);
            dbObject *rec =
                (dbObject *)(pool.get(pos - offs) + (offs & ~dbFlagsMask));
            free(pos, rec->size);
            pool.unfix(rec);
          }
        }
      }
      dstIndex += 1;
      srcIndex += 1;
    } while (--n != 0);

    pool.unfix(srcIndex);
    pool.unfix(dstIndex);
  }

  for (i = 0; i <= nPages; i++) {
    if (map[length_t(i >> 5)] & (1 << int(i & 31))) {
      offs_t *p =
          (offs_t *)pool.put(header->root[1 - curr].index + i * dbPageSize);
      for (length_t j = 0; j < dbHandlesPerPage; j++) {
        p[j] &= ~dbModifiedFlag;
      }
      pool.unfix(p);
    }
  }
  if (currIndexSize > committedIndexSize) {
    offs_t page =
        (header->root[1 - curr].index + committedIndexSize * sizeof(offs_t)) &
        ~((offs_t)dbPageSize - 1);
    offs_t end = (header->root[1 - curr].index + dbPageSize - 1 +
                  currIndexSize * sizeof(offs_t)) &
                 ~((offs_t)dbPageSize - 1);
    while (page < end) {
      offs_t *p = (offs_t *)pool.put(page);
      for (length_t h = 0; h < dbHandlesPerPage; h++) {
        p[h] &= ~dbModifiedFlag;
      }
      pool.unfix(p);
      page += dbPageSize;
    }
  }

  if ((rc = file->write(0, header, dbPageSize)) != dbFile::ok) {
    throwException(dybase_file_error, "Failed to write header");
  }

  pool.flush();

  header->curr = curr ^= 1;

  if ((rc = file->write(0, header, dbPageSize)) != dbFile::ok ||
      (rc = file->flush()) != dbFile::ok) {
    throwException(dybase_file_error, "Failed to flush changes to the disk");
  }

  header->root[1 - curr].size          = header->root[curr].size;
  header->root[1 - curr].indexUsed     = currIndexSize;
  header->root[1 - curr].freeList      = header->root[curr].freeList;
  header->root[1 - curr].bitmapEnd     = header->root[curr].bitmapEnd;
  header->root[1 - curr].rootObject    = header->root[curr].rootObject;
  header->root[1 - curr].classDescList = header->root[curr].classDescList;

  if (newIndexSize != oldIndexSize) {
    header->root[1 - curr].index           = header->root[curr].shadowIndex;
    header->root[1 - curr].indexSize       = header->root[curr].shadowIndexSize;
    header->root[1 - curr].shadowIndex     = header->root[curr].index;
    header->root[1 - curr].shadowIndexSize = header->root[curr].indexSize;
    pool.copy(header->root[1 - curr].index, header->root[curr].index,
              currIndexSize * sizeof(offs_t));
    memset(map, 0,
           4 * ((currIndexSize + dbHandlesPerPage * 32 - 1) /
                (dbHandlesPerPage * 32)));
  } else {
    for (i = 0; i < nPages; i++) {
      if (map[length_t(i >> 5)] & (1 << int(i & 31))) {
        map[length_t(i >> 5)] -= (1 << int(i & 31));
        pool.copy(header->root[1 - curr].index + i * dbPageSize,
                  header->root[curr].index + i * dbPageSize, dbPageSize);
      }
    }
    if (currIndexSize > i * dbHandlesPerPage &&
        ((map[length_t(i >> 5)] & (1 << int(i & 31))) != 0 ||
         currIndexSize != committedIndexSize)) {
      pool.copy(header->root[1 - curr].index + i * dbPageSize,
                header->root[curr].index + i * dbPageSize,
                length_t(sizeof(offs_t) * currIndexSize - i * dbPageSize));
      memset(map + length_t(i >> 5), 0,
             length_t(((currIndexSize + dbHandlesPerPage * 32 - 1) /
                           (dbHandlesPerPage * 32) -
                       (i >> 5)) *
                      4));
    }
  }
  this->curr               = curr;
  this->committedIndexSize = currIndexSize;
  modified                 = false;
  gcDone                   = false;
}

void dbDatabase::rollback() {
  dbCriticalSection cs(mutex);
  if (!opened) {
    handleError(dybase_not_opened, "Database not opened");
    return;
  }
  if (!modified) { return; }
  int      curr = header->curr;
  length_t nPages =
      (committedIndexSize + dbHandlesPerPage - 1) / dbHandlesPerPage;
  db_int4 *map = dirtyPagesMap;
  if (header->root[1 - curr].index != header->root[curr].shadowIndex) {
    pool.copy(header->root[curr].shadowIndex, header->root[curr].index,
              dbPageSize * nPages);
  } else {
    for (oid_t i = 0; i < nPages; i++) {
      if (map[length_t(i >> 5)] & (1 << int(i & 31))) {
        pool.copy(header->root[curr].shadowIndex + i * dbPageSize,
                  header->root[curr].index + i * dbPageSize, dbPageSize);
      }
    }
  }
  memset(map, 0,
         length_t((currIndexSize + dbHandlesPerPage * 32 - 1) /
                  (dbHandlesPerPage * 32)) *
             4);
  header->root[1 - curr].indexSize     = header->root[curr].shadowIndexSize;
  header->root[1 - curr].indexUsed     = header->root[curr].indexUsed;
  header->root[1 - curr].freeList      = header->root[curr].freeList;
  header->root[1 - curr].index         = header->root[curr].shadowIndex;
  header->root[1 - curr].bitmapEnd     = header->root[curr].bitmapEnd;
  header->root[1 - curr].size          = header->root[curr].size;
  header->root[1 - curr].rootObject    = header->root[curr].rootObject;
  header->root[1 - curr].classDescList = header->root[curr].classDescList;

  currRBitmapPage = currPBitmapPage = dbBitmapId;
  currRBitmapOffs = currPBitmapOffs = 0;
  currIndexSize                     = committedIndexSize;

  modified = false;

  oid_t              cid  = header->root[curr].classDescList;
  dbClassDescriptor *desc = classDescList;
  while (desc->oid != cid) {
    classOidHash.remove(&desc->oid, sizeof(desc->oid));
    classSignatureHash.remove(desc->cls->signature, desc->signatureSize);
    dbClassDescriptor *next = desc->next;
    delete desc;
    desc = next;
  }
  classDescList = desc;
}

dbDatabase::dbDatabase(dbAccessType type, dbErrorHandler hnd, length_t poolSize,
                       length_t dbExtensionQuantum, length_t dbInitIndexSize)
    : accessType(type), extensionQuantum(dbExtensionQuantum),
      initIndexSize(dbInitIndexSize), pool(this, poolSize), errorHandler(hnd) {
  dirtyPagesMap            = new db_int4[dbDirtyPageBitmapSize / 4 + 1];
  bitmapPageAvailableSpace = new int[dbBitmapId + dbBitmapPages];
  classDescList            = NULL;
  opened                   = false;
  header                   = (dbHeader *)dbFile::allocateBuffer(dbPageSize);
  dbFileExtensionQuantum   = 0;
  dbFileSizeLimit          = 0;
}

dbDatabase::~dbDatabase() {
  delete[] dirtyPagesMap;
  delete[] bitmapPageAvailableSpace;
  dbFile::deallocateBuffer(header);
}

void dbTrace(char *message, ...) {
  va_list args;
  va_start(args, message);
  vfprintf(stderr, message, args);
  va_end(args);
}