-
Notifications
You must be signed in to change notification settings - Fork 0
/
swapfile.cpp
1061 lines (935 loc) · 33.5 KB
/
swapfile.cpp
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
/*
Copyright 2019 Vlad Meșco
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <vector>
#include <list>
#include <memory>
#include <cstdio>
#include <string>
#include <sstream>
#include <algorithm>
#include <type_traits>
#include "swapfile.h"
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#define NOMINMAX
#include <windows.h>
#include <io.h>
#include "cprintf.h"
class FileImpl;
class FileLine : public ILine
{
friend class FileImpl;
FILE* const m_file;
// technically, this is not supported; we'll regroup if/when these asserts fail
static_assert(std::is_convertible<fpos_t, int64_t>::value, "fpos_t is not convertible to int64_t");
static_assert(std::is_convertible<int64_t, fpos_t>::value, "fpos_t is not convertible to int64_t");
static_assert(sizeof(fpos_t) <= sizeof(int64_t), "fpos_t is not convertible to int64_t");
int64_t m_pos;
public:
# pragma pack(push, 1)
// I don't like counting, this lets me use offsetof()
struct LineFormat
{
int64_t next;
uint16_t sz;
char* text;
};
# pragma pack(pop)
bool operator==(ILine const& other) const
{
auto pp = dynamic_cast<FileLine const*>(&other);
if(!pp) return false;
cprintf<CPK::swap>("FileLine== %zd %zd\n", m_pos, pp->m_pos);
if(m_file != pp->m_file) return false;
if(m_pos != pp->m_pos) return false;
return true;
}
FileLine(FILE* file, fpos_t pos)
: m_file(file)
, m_pos(pos)
{}
size_t length() override
{
fpos_t offset = m_pos + offsetof(LineFormat, sz);
fsetpos(m_file, &offset);
uint16_t len = 0;
fread(&len, 2, 1, m_file);
cprintf<CPK::swap>("[%p] Read length %u from %I64d\n", m_file, len, m_pos);
return len;
}
Text text() override
{
fpos_t offset = m_pos + offsetof(LineFormat, sz);
fsetpos(m_file, &offset);
uint16_t len = 0;
fread(&len, 2, 1, m_file);
if(len == RefMagic) throw std::runtime_error("Invalid indirection");
char* rval = (char*)malloc(len + 1);
if(!rval) std::terminate();
rval[len] = '\0';
offset = m_pos + offsetof(LineFormat, text);
fsetpos(m_file, &offset);
cprintf<CPK::swap>("[%p] Read %u chars from %I64d\n", m_file, len, m_pos);
fread(rval, 1, len, m_file);
return Text(len, (uint8_t*)rval, std::function<void(void*)>([](void*p) { free(p); }));
}
LinePtr next() override
{
fpos_t offset = m_pos + offsetof(LineFormat, next);
fsetpos(m_file, &offset);
int64_t nextPos = 0;
fread(&nextPos, sizeof(int64_t), 1, m_file);
cprintf<CPK::swap>("[%p] next: %I64d -> %I64d\n", m_file, m_pos, nextPos);
if(nextPos == 0) return {};
return std::make_shared<FileLine>(m_file, nextPos);
}
void link(LinePtr const& p) override
{
auto pp = p.DownCast<FileLine>(); //std::dynamic_pointer_cast<FileLine>(p);
fpos_t offset = m_pos + offsetof(LineFormat, next);
fsetpos(m_file, &offset);
int64_t nextPos = (pp) ? pp->m_pos : 0;
cprintf<CPK::swap>("[%p] link: %I64d -> %I64d\n", m_file, m_pos, nextPos);
fwrite(&nextPos, sizeof(int64_t), 1, m_file);
}
LinePtr Copy() override
{
return std::make_shared<FileLine>(m_file, m_pos);
}
Text ref() override
{
static_assert(sizeof(fpos_t) <= sizeof(uint8_t*), "fpos_t is smaller than a pointer");
return Text(RefMagic, (uint8_t*)(m_pos));
}
int64_t derefi()
{
fpos_t offset = m_pos + offsetof(LineFormat, sz);
fsetpos(m_file, &offset);
uint16_t len = 0;
fread(&len, 2, 1, m_file);
if(len != RefMagic) throw std::runtime_error("Invalid indirection");
offset = m_pos + offsetof(LineFormat, text);
fsetpos(m_file, &offset);
cprintf<CPK::swap>("[%p] Read %u chars from %I64d\n", m_file, len, m_pos);
fpos_t target = 0;
fread(&target, sizeof(target), 1, m_file);
return target;
}
LinePtr deref() override
{
fpos_t target = (fpos_t)derefi();
return std::make_shared<FileLine>(m_file, target);
}
}; // FileLine
class FileImpl : public ISwapImpl
{
// I've heard online that one must fseek(0, CUR) for ftell
// to work when switching between R&W in update mode; I even
// saw it written in some man 3 page somewhere. I could not
// find it in the last publicly published C standard draft,
// so IDK, based on pure observation, the first statement is
// correct. Maybe I haven't read the standard fully
FILE* m_file;
std::string m_name;
FileImpl(FILE* f, std::string name) : m_file(f), m_name(name) {}
static std::string GetTEMPFileName()
{
static char tempDir[4096] = {'\0'};
if(*tempDir == '\0') {
DWORD length = GetTempPathA(sizeof(tempDir), tempDir);
if(length <= 0 || length >= 4096) return "";
tempDir[length] = '\0';
}
char lpTempFileName[MAX_PATH + 1] = {'\0'};
LPCSTR lpPath = tempDir, lpPrefix = "jed";
DWORD uUnique = 0;
if(GetTempFileNameA(lpPath, lpPrefix, uUnique, lpTempFileName)) {
cprintf<CPK::swap>("Computed temp file: %s\n", lpTempFileName);
return lpTempFileName;
}
return "";
}
static std::string GetCWDFileName()
{
char lpTempFileName[MAX_PATH + 1] = {'\0'};
LPCSTR lpPath = ".", lpPrefix = "jed";
DWORD uUnique = 0;
if(GetTempFileNameA(lpPath, lpPrefix, uUnique, lpTempFileName)) {
cprintf<CPK::swap>("Computed temp file: %s\n", lpTempFileName);
return lpTempFileName;
}
return "";
}
public:
# pragma pack(push, 1)
// I don't like counting, this let's me use offsetof()
struct Header
{
int64_t head;
uint16_t padding;
int64_t cut;
int64_t undo;
};
# pragma pack(pop)
static ISwapImpl* Create()
{
Header head;
memset(&head, 0, sizeof(head));
// try tmpfile
FILE *f = tmpfile();
if(f) {
fseek(f, 0, SEEK_SET);
if(1 == fwrite(&head, sizeof(Header), 1, f)) {
cprintf<CPK::swap>("Will use tmpfile() [%p]\n", f);
return new FileImpl(f, "");
}
}
std::string (*tempFunctions[])() = {
GetTEMPFileName,
GetCWDFileName,
};
for(auto&& tempFunction : tempFunctions) {
auto name = tempFunction().c_str();
f = fopen(name, "wb");
if(f) {
fseek(f, 0, SEEK_SET);
if(1 == fwrite(&head, sizeof(Header), 1, f)) {
cprintf<CPK::swap>("--> will use this one [%p]\n", f);
return new FileImpl(f, name);
}
}
}
cprintf<CPK::swap>("Failed to create any temp file\n");
return nullptr;
}
~FileImpl()
{
fclose(m_file);
if(m_name != "") remove(m_name.c_str());
}
LinePtr head() override
{
return std::make_shared<FileLine>(m_file, offsetof(Header, head));
}
LinePtr cut() override
{
fseek(m_file, 0, SEEK_SET);
Header head;
memset(&head, 0, sizeof(Header));
fread(&head, sizeof(Header), 1, m_file);
if(head.cut == 0) {
cprintf<CPK::swap>("[%p] No cut buffer\n", m_file);
return {};
}
cprintf<CPK::swap>("[%p] Cut buffer points to %I64d\n", m_file, head.cut);
return std::make_shared<FileLine>(m_file, head.cut);
}
LinePtr undo() override
{
fseek(m_file, 0, SEEK_SET);
Header head;
memset(&head, 0, sizeof(Header));
fread(&head, sizeof(Header), 1, m_file);
if(head.undo == 0) {
cprintf<CPK::swap>("[%p] No undo buffer\n", m_file);
return {};
}
cprintf<CPK::swap>("[%p] Undo buffer points to %I64d\n", m_file, head.undo);
return std::make_shared<FileLine>(m_file, head.undo);
}
int64_t undoi(bool pop) override
{
fseek(m_file, 0, SEEK_SET);
Header head;
memset(&head, 0, sizeof(Header));
fread(&head, sizeof(Header), 1, m_file);
if(head.undo == 0) {
cprintf<CPK::swap>("[%p] No undo buffer\n", m_file);
return 0;
}
cprintf<CPK::swap>("[%p] Undo buffer points to %I64d\n", m_file, head.undo);
if(pop) {
auto line = std::make_shared<FileLine>(m_file, head.undo);
return line->derefi();
} else {
return head.undo;
}
}
LinePtr line(Text const& s) override
{
fseek(m_file, 0, SEEK_END);
fpos_t fp;
memset(&fp, 0, sizeof(fp));
fgetpos(m_file, &fp);
cprintf<CPK::swap>("[%p] Adding line to %I64d\n", m_file, fp);
int64_t zero = 0;
fwrite(&zero, sizeof(int64_t), 1, m_file);
uint16_t len = s.size();
fwrite(&len, sizeof(uint16_t), 1, m_file);
if(len == RefMagic) {
fwrite(s.data(), sizeof(fpos_t), 1, m_file);
cprintf<CPK::swap>("--> wrote indirect handle to %zd\n", (fpos_t)s.data());
} else {
fwrite(s.data(), 1, len, m_file);
cprintf<CPK::swap>("--> wrote %u chars\n", len);
}
return std::make_shared<FileLine>(m_file, fp);
}
LinePtr cut(LinePtr const& p) override
{
auto pp = p.DownCast<FileLine>(); //std::dynamic_pointer_cast<FileLine>(p);
fseek(m_file, 0, SEEK_SET);
Header head;
memset(&head, 0, sizeof(Header));
fread(&head, sizeof(Header), 1, m_file);
cprintf<CPK::swap>("[%p] Cut buffer was %I64d\n", m_file, head.cut);
head.cut = (pp) ? pp->m_pos : 0;
fseek(m_file, 0, SEEK_SET);
fwrite(&head, sizeof(Header), 1, m_file);
cprintf<CPK::swap>("--> set cut buffer to %I64d\n", head.cut);
return p;
}
LinePtr undo(LinePtr const& p)
{
cprintf<CPK::swap>("Entered undo(p)\n");
fseek(m_file, 0, SEEK_SET);
Header head;
memset(&head, 0, sizeof(Header));
fread(&head, sizeof(Header), 1, m_file);
cprintf<CPK::swap>("[%p] Undo buffer was %I64d\n", m_file, head.undo);
auto pp = p.DownCast<FileLine>(); //std::dynamic_pointer_cast<FileLine>(p);
LinePtr undoHead;
if(pp && pp->m_pos > 0) {
cprintf<CPK::swap>("[%p] Asked to save undo command at %I64d\n", m_file, pp->m_pos);
static_assert(sizeof(fpos_t) <= sizeof(uint8_t*), "fpos_t is smaller than a pointer");
auto undoHead = line(Text(RefMagic, (uint8_t*)head.undo));
cprintf<CPK::swap>("[%p] Adding double link %I64d\n", m_file, undoHead.DownCast<FileLine>()->m_pos);
undoHead->link(p);
} else {
cprintf<CPK::swap>("Asked to hide undo mark\n");
}
head.undo = (undoHead) ? undoHead.DownCast<FileLine>()->m_pos : 0;
fseek(m_file, 0, SEEK_SET);
fwrite(&head, sizeof(Header), 1, m_file);
cprintf<CPK::swap>("--> set undo buffer to %I64d\n", head.undo);
return p;
}
void undoi(int64_t pos)
{
fseek(m_file, 0, SEEK_SET);
Header head;
memset(&head, 0, sizeof(Header));
fread(&head, sizeof(Header), 1, m_file);
cprintf<CPK::swap>("[%p] Undo buffer was %I64d\n", m_file, head.undo);
head.undo = pos;
fseek(m_file, 0, SEEK_SET);
fwrite(&head, sizeof(Header), 1, m_file);
cprintf<CPK::swap>("--> set undo buffer to %I64d\n", head.undo);
}
#if 0
void gc() override
{
cprintf<CPK::swap>("[%p] gcing swap file\n", m_file);
auto cutBuffer = this->cut();
auto undoBuffer = this->undo();
auto l = this->head();
std::unique_ptr<FileImpl> temp((FileImpl*)FileImpl::Create());
cprintf<CPK::swap>("Relinking text\n");
LinePtr prev = temp->head();
for(l = l->next(); l; l = l->next()) {
auto inserted = temp->line(l->text());
prev->link(inserted);
prev = inserted;
}
bool overlapTempCut = false;
prev.reset();
cprintf<CPK::swap>("Writing undo buffer\n");
for(; undoBuffer; undoBuffer = undoBuffer->next()) {
auto inserted = temp->line(undoBuffer->text());
if(undoBuffer == cutBuffer) {
temp->cut(inserted);
overlapTempCut = true;
cprintf<CPK::swap>("Detected overlap between undo and cut buffers\n");
}
if(prev) prev->link(inserted);
else {
prev = inserted;
temp->undo(inserted);
}
}
if(!overlapTempCut) {
cprintf<CPK::swap>("Writing cut buffer\n");
prev.reset();
for(; cutBuffer; cutBuffer = cutBuffer->next()) {
auto inserted = temp->line(cutBuffer->text());
if(prev) prev->link(inserted);
else {
prev = inserted;
temp->cut(inserted);
}
}
}
std::swap(m_file, temp->m_file);
std::swap(m_name, temp->m_name);
}
#endif
};
class MappedLine : public ILine
{
friend class MappedImpl;
MappedImpl& file;
SIZE_T offset;
public:
# pragma pack(push, 1)
// I don't like counting, this let's me use offsetof()
struct LineFormat
{
int64_t next;
uint16_t sz;
uint8_t text[16];
};
# pragma pack(pop)
bool operator==(ILine const& other) const
{
auto pp = dynamic_cast<MappedLine const*>(&other);
if(!pp) return false;
cprintf<CPK::swap>("FileLine== %zd %zd\n", offset, pp->offset);
if(&file != &pp->file) return false;
if(offset != pp->offset) return false;
return true;
}
MappedLine(MappedImpl& mimpl, SIZE_T zOffset)
: file(mimpl)
, offset(zOffset)
{}
size_t length() override;
Text text() override;
LinePtr next() override;
void link(LinePtr const& p) override;
LinePtr Copy() override
{
return std::make_shared<MappedLine>(file, offset);
}
Text ref() override;
int64_t derefi();
LinePtr deref() override;
};
class MappedImpl : public ISwapImpl
{
public:
# pragma pack(push, 1)
// I don't like counting, this let's me use offsetof()
struct Header
{
int64_t head;
uint16_t padding;
int64_t cut;
int64_t undo;
};
# pragma pack(pop)
static constexpr SIZE_T LineBaseSize = sizeof(MappedLine::LineFormat) - sizeof(MappedLine::LineFormat::text);
friend class MappedLine;
private:
static constexpr SIZE_T zBaseSize = 1024;
HANDLE m_file, m_map;
SIZE_T m_baseSize, m_increment;
SIZE_T m_size;
uint8_t* m_view;
std::string m_name;
MappedImpl(HANDLE hFile, std::string name, HANDLE hMap, uint8_t* mView, SIZE_T sz)
: m_file(hFile), m_name(name), m_map(hMap),
m_increment(1), m_size(sizeof(Header)), m_baseSize(sz), m_view(mView)
{
cprintf<CPK::swap>("[%p] MappedImpl created\n", this);
static_assert(zBaseSize > sizeof(Header), "Incorrect base size");
}
static std::string GetTEMPFileName()
{
static char tempDir[4096] = {'\0'};
if(*tempDir == '\0') {
DWORD length = GetTempPathA(sizeof(tempDir), tempDir);
if(length <= 0 || length >= 4096) return "";
tempDir[length] = '\0';
}
char lpTempFileName[MAX_PATH + 1] = {'\0'};
LPCSTR lpPath = tempDir, lpPrefix = "jed";
DWORD uUnique = 0;
if(GetTempFileNameA(lpPath, lpPrefix, uUnique, lpTempFileName)) {
cprintf<CPK::swap>("Computed temp file: %s\n", lpTempFileName);
return lpTempFileName;
}
return "";
}
static std::string GetCWDFileName()
{
char lpTempFileName[MAX_PATH + 1] = {'\0'};
LPCSTR lpPath = ".", lpPrefix = "jed";
DWORD uUnique = 0;
if(GetTempFileNameA(lpPath, lpPrefix, uUnique, lpTempFileName)) {
cprintf<CPK::swap>("Computed temp file: %s\n", lpTempFileName);
return lpTempFileName;
}
return "";
}
static constexpr SIZE_T GetNextIncrement(SIZE_T increment)
{
if(increment < 1) increment = 1;
SIZE_T newSize = zBaseSize;
while(--increment && newSize < (1 << 24)) { // 16MB
newSize = newSize << 1;
}
if(increment > 0) return newSize * increment;
return newSize;
}
void EnsureSize(SIZE_T growBy = 0)
{
// I don't even know what null pointer gets dereferenced in the expression
// m_size + growBy > m_baseSize
#pragma warning(push)
#pragma warning(disable:6011)
if(m_size + growBy > m_baseSize) {
cprintf<CPK::swap>("[%p] Swap file must grow, because %zd is smaller than %zd\n", this, m_baseSize, m_size + growBy);
UnmapViewOfFile(m_view);
CloseHandle(m_map);
while(m_size + growBy > m_baseSize) {
m_increment++;
m_baseSize = GetNextIncrement(m_increment);
cprintf<CPK::swap>("[%p] Swapfile increment computed to be %zd\n", this, m_baseSize);
}
m_map = CreateFileMapping(
m_file,
NULL,
PAGE_READWRITE,
(m_baseSize >> 32) & 0xFFFFFFFF,
m_baseSize & 0xFFFFFFFF,
NULL);
if(!m_map) {
// FIXME more graciousness?
fprintf(stderr, "[%p] MappedImpl: I have forgotten. I have failed...\n", this);
std::terminate();
}
m_view = nullptr;
}
if(!m_view) {
cprintf<CPK::swap>("[%p] Creating view\n", this);
m_view = (uint8_t*)MapViewOfFile(m_map, FILE_MAP_ALL_ACCESS, 0, 0, m_baseSize);
Header* head = (Header*)m_view;
}
#pragma warning(pop)
}
public:
static ISwapImpl* Create()
{
Header head;
memset(&head, 0, sizeof(head));
HANDLE hFile = (HANDLE)0, hMap = (HANDLE)0;
uint8_t* mView = nullptr;
SIZE_T sz = 0;
std::string (*tempFunctions[])() = {
GetTEMPFileName,
GetCWDFileName,
};
for(auto&& tempFunction : tempFunctions) {
auto tempName = tempFunction();
auto* name = tempName.c_str();
hFile = CreateFileA(
name,
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
/*FILE_ATTRIBUTE_TEMPORARY|*/FILE_FLAG_RANDOM_ACCESS, // but no DELETE_ON_CLOSE
NULL);
if(hFile) {
sz = GetNextIncrement(1);
hMap = CreateFileMapping(
hFile,
NULL,
PAGE_READWRITE,
(sz >> 32) & 0xFFFFFFFF,
sz & 0xFFFFFFFF,
NULL);
if(hMap) {
mView = (uint8_t*)MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, sz);
if(!mView) {
CloseHandle(hMap);
CloseHandle(hFile);
hMap = (HANDLE)0;
hFile = (HANDLE)0;
mView = nullptr;
continue;
}
memcpy(mView, &head, sizeof(Header));
cprintf<CPK::swap>("--> will use this one %s [%p]\n", name, hFile);
return new MappedImpl(hFile, name, hMap, mView, sz);
} else {
CloseHandle(hFile);
}
}
}
cprintf<CPK::swap>("Failed to create any temp file\n");
return nullptr;
}
static ISwapImpl* Recover(std::string const& path)
{
#if 0
// get basesize from file size, and traverse each line to recompute size, and determine increment from basesize; no need to do shennanigans and store that info in the swapfile itself
Header head;
memset(&head, 0, sizeof(head));
HANDLE hFile = (HANDLE)0, hMap = (HANDLE)0;
uint8_t* mView = nullptr;
SIZE_T sz = 0;
std::string (*tempFunctions[])() = {
GetTEMPFileName,
GetCWDFileName,
};
auto* name = path.c_str();
hFile = CreateFileA( // TODO CreateFileW
name,
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_ALWAYS,
/*FILE_ATTRIBUTE_TEMPORARY|*/FILE_FLAG_RANDOM_ACCESS, // but no DELETE_ON_CLOSE
NULL);
if(hFile) {
hMap = CreateFileMapping(
hFile,
NULL,
PAGE_READWRITE,
(sz >> 32) & 0xFFFFFFFF,
sz & 0xFFFFFFFF,
NULL);
sz = GetNextIncrement(1);
if(hMap) {
mView = (uint8_t*)MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, sz);
if(!mView) {
CloseHandle(hMap);
CloseHandle(hFile);
hMap = (HANDLE)0;
hFile = (HANDLE)0;
mView = nullptr;
return nullptr;
}
Header* thishead = (Header*)mView;
auto oldSize = thishead->sz;
auto rval = new MappedImpl(hFile, name, hMap, mView, thishead->basesz);
thishead = nullptr; // clobbered by EnsureSize
mView = nullptr; // clobbered by EnsureSize
rval->EnsureSize(); // sets increment and recreates mapping and view to be what they should be
rval->m_size = oldSize; // set the correct size
cprintf<CPK::swap>("--> will use this one [%p]\n", hFile);
return rval;
} else {
CloseHandle(hFile);
}
}
cprintf<CPK::swap>("Failed to create any temp file\n");
#endif
return nullptr;
}
~MappedImpl()
{
if(m_view) UnmapViewOfFile(m_view);
if(m_map) CloseHandle(m_map);
if(m_file) CloseHandle(m_file);
if(!m_name.empty()) DeleteFileA(m_name.c_str()); // TODO use DeleteFileW
cprintf<CPK::swap>("Deleted %s maybe\n", m_name.c_str());
}
LinePtr head() override
{
return std::make_shared<MappedLine>(*this, offsetof(Header, head));
}
LinePtr cut() override
{
EnsureSize();
Header* head = (Header*)(m_view);
if(head->cut == 0) {
cprintf<CPK::swap>("[%p] No cut buffer\n", m_file);
return {};
}
cprintf<CPK::swap>("[%p] Cut buffer points to %I64d\n", m_file, head->cut);
return std::make_shared<MappedLine>(*this, head->cut);
}
LinePtr undo() override
{
EnsureSize();
Header* head = (Header*)(m_view);
if(head->undo == 0) {
cprintf<CPK::swap>("[%p] No undo buffer\n", m_file);
return {};
}
cprintf<CPK::swap>("[%p] Undo buffer points to %I64d\n", this, head->undo);
return std::make_shared<MappedLine>(*this, head->undo);
}
int64_t undoi(bool pop) override
{
EnsureSize();
Header* head = (Header*)(m_view);
if(head->undo == 0) {
cprintf<CPK::swap>("[%p] No undo buffer\n", m_file);
return {};
}
cprintf<CPK::swap>("[%p] Undo buffer points to %I64d\n", this, head->undo);
if(pop) {
auto line = std::make_shared<MappedLine>(*this, head->undo);
return line->derefi();
} else {
return head->undo;
}
}
LinePtr line(Text const& s) override
{
EnsureSize(LineBaseSize
+ (s.size() == RefMagic ? (SIZE_T)sizeof(SIZE_T) : (SIZE_T)s.size()));
Header* head = (Header*)(m_view);
cprintf<CPK::swap>("[%p] Adding line to %zd\n", m_file, m_size);
MappedLine::LineFormat* newLine = (MappedLine::LineFormat*)(m_view + m_size);
newLine->next = 0;
newLine->sz = s.size();
if(newLine->sz == RefMagic) {
memcpy(&newLine->text[0], s.data(), sizeof(SIZE_T));
m_size += LineBaseSize + sizeof(SIZE_T);
} else {
memcpy(&newLine->text[0], s.data(), s.size());
m_size += LineBaseSize + newLine->sz;
}
return std::make_shared<MappedLine>(*this, ((uint8_t*)newLine) - m_view);
}
LinePtr cut(LinePtr const& p) override
{
auto pp = p.DownCast<MappedLine>(); //std::dynamic_pointer_cast<FileLine>(p);
EnsureSize();
Header* head = (Header*)(m_view);
cprintf<CPK::swap>("[%p] Cut buffer was %I64d\n", m_file, head->cut);
head->cut = (pp) ? pp->offset : 0;
cprintf<CPK::swap>("--> set cut buffer to %I64d\n", head->cut);
return p;
}
LinePtr undo(LinePtr const& p)
{
cprintf<CPK::swap>("Entered undo(p)\n");
EnsureSize();
Header* head = (Header*)(m_view);
auto pp = p.DownCast<MappedLine>(); //std::dynamic_pointer_cast<FileLine>(p);
LinePtr undoHead;
if(pp && pp->offset > 0) {
cprintf<CPK::swap>("[%p] Asked to save undo command at %I64d\n", m_file, pp->offset);
uint8_t* tOffset = new uint8_t[sizeof(decltype(head->undo))];
for(int i = 0; i < sizeof(decltype(head->undo)); ++i) {
tOffset[i] = (head->undo >> (8 * i)) & 0xFF;
}
auto magicText = Text(RefMagic, tOffset, std::function<void(void*)>([](void* p) {
uint8_t* pp = reinterpret_cast<uint8_t*>(p);
delete[] pp;
}));
undoHead = line(magicText);
cprintf<CPK::swap>("[%p] Adding double link %I64d\n", m_file, undoHead.DownCast<MappedLine>()->offset);
undoHead->link(p);
} else {
cprintf<CPK::swap>("Asked to hide undo mark\n");
}
cprintf<CPK::swap>("[%p] Undo buffer was %I64d\n", m_file, head->undo);
//head->undo = (pp) ? pp->offset : 0;
head->undo = (undoHead) ? undoHead.DownCast<MappedLine>()->offset : 0;
cprintf<CPK::swap>("--> set undo buffer to %I64d\n", head->undo);
return p;
}
void undoi(int64_t offset)
{
EnsureSize();
Header* head = (Header*)(m_view);
cprintf<CPK::swap>("[%p] Undo buffer was %I64d\n", m_file, head->undo);
//head->undo = (pp) ? pp->offset : 0;
head->undo = offset;
cprintf<CPK::swap>("--> set undo buffer to %I64d\n", head->undo);
}
#if 0
void gc() override
{
cprintf<CPK::swap>("[%p] gcing swap file\n", m_file);
auto cutBuffer = this->cut();
auto undoBuffer = this->undo();
auto l = this->head();
std::unique_ptr<MappedImpl> temp((MappedImpl*)MappedImpl::Create());
cprintf<CPK::swap>("Relinking text\n");
LinePtr prev = temp->head();
for(l = l->next(); l; l = l->next()) {
auto inserted = temp->line(l->text());
prev->link(inserted);
prev = inserted;
}
bool overlapTempCut = false;
prev.reset();
cprintf<CPK::swap>("Writing undo buffer\n");
for(; undoBuffer; undoBuffer = undoBuffer->next()) {
auto inserted = temp->line(undoBuffer->text());
if(undoBuffer == cutBuffer) {
temp->cut(inserted);
overlapTempCut = true;
cprintf<CPK::swap>("Detected overlap between undo and cut buffers\n");
}
if(prev) prev->link(inserted);
else {
prev = inserted;
temp->undo(inserted);
}
}
if(!overlapTempCut) {
cprintf<CPK::swap>("Writing cut buffer\n");
prev.reset();
for(; cutBuffer; cutBuffer = cutBuffer->next()) {
auto inserted = temp->line(cutBuffer->text());
if(prev) prev->link(inserted);
else {
prev = inserted;
temp->cut(inserted);
}
}
}
std::swap(m_file, temp->m_file);
std::swap(m_map, temp->m_map);
std::swap(m_baseSize, temp->m_baseSize);
std::swap(m_increment, temp->m_increment);
std::swap(m_view, temp->m_view);
std::swap(m_size, temp->m_size);
std::swap(m_name, temp->m_name);
}
#endif
};
inline size_t MappedLine::length()
{
LineFormat* me = (LineFormat*)(file.m_view + offset);
return me->sz;
}
inline Text MappedLine::text()
{
LineFormat* me = (LineFormat*)(file.m_view + offset);
if(me->sz == RefMagic) throw std::runtime_error("Invalid indirection");
return Text(me->sz, &me->text[0]);
}
inline LinePtr MappedLine::next()
{
LineFormat* me = (LineFormat*)(file.m_view + offset);
if(me->next) return std::make_shared<MappedLine>(file, me->next);
return {};
}
inline void MappedLine::link(LinePtr const& p)
{
auto pp = p.DownCast<MappedLine>();
LineFormat* me = (LineFormat*)(file.m_view + offset);
if(!pp) {
me->next = 0;
cprintf<CPK::swap>("[%p] link: %zd -> %zd\n", &file, offset, 0ull);
} else {
me->next = pp->offset;
cprintf<CPK::swap>("[%p] link: %zd -> %zd\n", &file, offset, pp->offset);
}
}
inline Text MappedLine::ref()
{
LineFormat* me = (LineFormat*)(file.m_view + offset);
uint8_t* tOffset = new uint8_t[sizeof(decltype(offset))];
for(int i = 0; i < sizeof(decltype(offset)); ++i) {
tOffset[i] = (offset >> (8 * i)) & 0xFF;
}
return Text(RefMagic, tOffset, std::function<void(void*)>([](void* p) {
uint8_t* pp = reinterpret_cast<uint8_t*>(p);
delete[] pp;
}));
}
inline int64_t MappedLine::derefi()
{
LineFormat* me = (LineFormat*)(file.m_view + offset);
std::make_unsigned<decltype(offset)>::type refd = 0u;
for(int i = 0; i < sizeof(decltype(offset)); ++i) {
refd |= static_cast<decltype(refd)>(me->text[i]) << (8 * i);
}
return (int64_t)(refd);
}
inline LinePtr MappedLine::deref()
{
auto refd = derefi();
return std::make_shared<MappedLine>(file, (decltype(offset))(refd));
}
class MemoryImpl : public FileImpl
{
// TODO
};
struct NullImpl: public FileImpl
{
};
Swapfile::Swapfile(int type) : m_pImpl(nullptr)
{
ISwapImpl* (*factories[])() = {
&MappedImpl::Create,
&FileImpl::Create,
&MemoryImpl::Create,
&NullImpl::Create,
nullptr
};
for(auto f = factories; f; ++f) {
m_pImpl = (*f)();
if(m_pImpl) return;
}
}