-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathCachedBuffers.pas
4160 lines (3701 loc) · 108 KB
/
CachedBuffers.pas
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
unit CachedBuffers;
{******************************************************************************}
{ Copyright (c) Dmitry Mozulyov }
{ }
{ Permission is hereby granted, free of charge, to any person obtaining a copy }
{ of this software and associated documentation files (the "Software"), to deal}
{ in the Software without restriction, including without limitation the rights }
{ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell }
{ copies of the Software, and to permit persons to whom the Software is }
{ furnished to do so, subject to the following conditions: }
{ }
{ The above copyright notice and this permission notice shall be included in }
{ all copies or substantial portions of the Software. }
{ }
{ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR }
{ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, }
{ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE }
{ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER }
{ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,}
{ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN }
{ THE SOFTWARE. }
{ }
{ email: softforyou@inbox.ru }
{ skype: dimandevil }
{ repository: https://github.com/d-mozulyov/CachedBuffers }
{******************************************************************************}
// compiler directives
{$ifdef FPC}
{$MODE DELPHIUNICODE}
{$ASMMODE INTEL}
{$define INLINESUPPORT}
{$define INLINESUPPORTSIMPLE}
{$define OPERATORSUPPORT}
{$define STATICSUPPORT}
{$define GENERICSUPPORT}
{$define ANSISTRSUPPORT}
{$define SHORTSTRSUPPORT}
{$define WIDESTRSUPPORT}
{$ifdef MSWINDOWS}
{$define WIDESTRLENSHIFT}
{$endif}
{$define INTERNALCODEPAGE}
{$ifdef CPU386}
{$define CPUX86}
{$endif}
{$ifdef CPUX86_64}
{$define CPUX64}
{$endif}
{$if Defined(CPUARM) or Defined(UNIX)}
{$define POSIX}
{$ifend}
{$else}
{$if CompilerVersion >= 24}
{$LEGACYIFEND ON}
{$ifend}
{$if CompilerVersion >= 15}
{$WARN UNSAFE_CODE OFF}
{$WARN UNSAFE_TYPE OFF}
{$WARN UNSAFE_CAST OFF}
{$WARN SYMBOL_DEPRECATED OFF}
{$ifend}
{$if CompilerVersion >= 20}
{$define INLINESUPPORT}
{$ifend}
{$if CompilerVersion >= 17}
{$define INLINESUPPORTSIMPLE}
{$ifend}
{$if CompilerVersion >= 18}
{$define OPERATORSUPPORT}
{$ifend}
{$if CompilerVersion >= 18.5}
{$define STATICSUPPORT}
{$ifend}
{$if CompilerVersion >= 20}
{$define GENERICSUPPORT}
{$define SYSARRAYSUPPORT}
{$ifend}
{$if CompilerVersion < 23}
{$define CPUX86}
{$ifend}
{$if CompilerVersion >= 23}
{$define UNITSCOPENAMES}
{$define RETURNADDRESS}
{$ifend}
{$if CompilerVersion >= 21}
{$WEAKLINKRTTI ON}
{$RTTI EXPLICIT METHODS([]) PROPERTIES([]) FIELDS([])}
{$define EXTENDEDRTTI}
{$ifend}
{$if CompilerVersion >= 33}
{$define MANAGEDRECORDS}
{$ifend}
{$if (not Defined(NEXTGEN)) or (CompilerVersion >= 31)}
{$define ANSISTRSUPPORT}
{$ifend}
{$ifNdef NEXTGEN}
{$define SHORTSTRSUPPORT}
{$endif}
{$if Defined(MSWINDOWS) or (Defined(MACOS) and not Defined(IOS))}
{$define WIDESTRSUPPORT}
{$ifend}
{$if Defined(MSWINDOWS) or (Defined(WIDESTRSUPPORT) and (CompilerVersion <= 21))}
{$define WIDESTRLENSHIFT}
{$ifend}
{$if Defined(ANSISTRSUPPORT) and (CompilerVersion >= 20)}
{$define INTERNALCODEPAGE}
{$ifend}
{$if Defined(NEXTGEN)}
{$POINTERMATH ON}
{$ifend}
{$endif}
{$U-}{$V+}{$B-}{$X+}{$T+}{$P+}{$H+}{$J-}{$Z1}{$A4}
{$O+}{$R-}{$I-}{$Q-}{$W-}
{$ifdef CPUX86}
{$if not Defined(NEXTGEN)}
{$define CPUX86ASM}
{$define CPUINTELASM}
{$ifend}
{$define CPUINTEL}
{$endif}
{$ifdef CPUX64}
{$if (not Defined(POSIX)) or Defined(FPC)}
{$define CPUX64ASM}
{$define CPUINTELASM}
{$ifend}
{$define CPUINTEL}
{$endif}
{$if Defined(CPUINTEL) and Defined(POSIX)}
{$ifdef CPUX86}
{$define POSIXINTEL32}
{$else}
{$define POSIXINTEL64}
{$endif}
{$ifend}
{$if Defined(CPUX64) or Defined(CPUARM64)}
{$define LARGEINT}
{$else}
{$define SMALLINT}
{$ifend}
{$ifdef KOL_MCK}
{$define KOL}
{$endif}
{$ifdef POSIX}
{$undef CPUX86ASM}
{$undef CPUX64ASM}
{$undef CPUINTELASM}
{$endif}
interface
uses {$ifdef UNITSCOPENAMES}System.Types{$else}Types{$endif},
{$ifdef MSWINDOWS}{$ifdef UNITSCOPENAMES}Winapi.Windows{$else}Windows{$endif},{$endif}
{$ifdef POSIX}
{$ifdef FPC}
BaseUnix,
{$else}
Posix.String_, Posix.SysStat, Posix.Unistd,
{$endif}
{$endif}
{$ifdef KOL}
KOL, err
{$else}
{$ifdef UNITSCOPENAMES}System.SysUtils{$else}SysUtils{$endif}
{$endif};
type
// RTL types
{$ifdef FPC}
PUInt64 = ^UInt64;
PBoolean = ^Boolean;
PString = ^string;
{$else}
{$if CompilerVersion < 16}
UInt64 = Int64;
PUInt64 = ^UInt64;
{$ifend}
{$if CompilerVersion < 21}
NativeInt = Integer;
NativeUInt = Cardinal;
{$ifend}
{$if CompilerVersion < 22}
PNativeInt = ^NativeInt;
PNativeUInt = ^NativeUInt;
{$ifend}
PWord = ^Word;
{$endif}
{$if not Defined(FPC) and (CompilerVersion < 20)}
TDate = type TDateTime;
TTime = type TDateTime;
{$ifend}
PDate = ^TDate;
PTime = ^TTime;
{$if SizeOf(Extended) >= 10}
{$define EXTENDEDSUPPORT}
{$ifend}
TBytes = {$ifdef SYSARRAYSUPPORT}TArray<Byte>{$else}array of Byte{$endif};
PBytes = ^TBytes;
{$if Defined(NEXTGEN) and (CompilerVersion >= 31)}
AnsiChar = type System.UTF8Char;
PAnsiChar = ^AnsiChar;
AnsiString = type System.RawByteString;
PAnsiString = ^AnsiString;
{$ifend}
{ TCachedObject class }
TCachedObject = class;
TCachedObjectCallback = procedure (const ASender: TCachedObject; const AParam: Pointer);
ICachedObject = interface
function GetSelf: TCachedObject {$ifdef AUTOREFCOUNT}unsafe{$endif};
function GetPreallocated: Boolean;
function GetRefCount: Integer;
property Self: TCachedObject read GetSelf;
property Preallocated: Boolean read GetPreallocated;
property RefCount: Integer read GetRefCount;
end;
TCachedObject = class(TObject, IInterface, ICachedObject)
{$ifdef INLINESUPPORTSIMPLE}
protected
const
objDestroyingFlag = Integer($80000000);
objDisposedFlag = Integer($40000000);
objPreallocatedFlag = Integer($20000000);
REFCOUNT_MASK = not (objPreallocatedFlag or {$ifdef AUTOREFCOUNT}objDisposedFlag{$else}objDestroyingFlag{$endif});
{$endif}
protected
{$ifNdef AUTOREFCOUNT}
{$if (not Defined(FPC)) and (CompilerVersion >= 29)}[Volatile]{$ifend}
FRefCount: Integer;
{$endif}
function GetSelf: TCachedObject {$ifdef AUTOREFCOUNT}unsafe{$endif};
function GetPreallocated: Boolean; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
function GetRefCount: Integer; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
function QueryInterface({$ifdef FPC}constref{$else}const{$endif} IID: TGUID; out Obj): {$if (not Defined(FPC)) or Defined(MSWINDOWS)}HResult; stdcall{$else}Longint; cdecl{$ifend};
function _AddRef: {$if (not Defined(FPC)) or Defined(MSWINDOWS)}Integer; stdcall{$else}Longint; cdecl{$ifend};
function _Release: {$if (not Defined(FPC)) or Defined(MSWINDOWS)}Integer; stdcall{$else}Longint; cdecl{$ifend};
public
class function NewInstance: TObject {$ifdef AUTOREFCOUNT}unsafe{$ENDIF}; override;
class function PreallocatedInstance(const AMemory: Pointer; const ASize: Integer): TObject {$ifdef AUTOREFCOUNT}unsafe{$ENDIF}; virtual;
class procedure PreallocatedCall(const AParam: Pointer; const ACallback: TCachedObjectCallback); overload;
class procedure PreallocatedCall(const AParam: Pointer; const ASize: Integer; const ACallback: TCachedObjectCallback); overload;
procedure FreeInstance; override;
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
destructor Destroy; override;
{$ifdef AUTOREFCOUNT}
function __ObjAddRef: Integer; override;
function __ObjRelease: Integer; override;
{$endif}
property Preallocated: Boolean read GetPreallocated;
property RefCount: Integer read GetRefCount;
end;
TCachedObjectClass = class of TCachedObject;
{ ECachedBuffer class }
ECachedBuffer = class(Exception)
{$ifdef KOL}
constructor Create(const Msg: string);
constructor CreateFmt(const Msg: string; const Args: array of const);
constructor CreateRes(Ident: NativeUInt); overload;
constructor CreateRes(ResStringRec: PResStringRec); overload;
constructor CreateResFmt(Ident: NativeUInt; const Args: array of const); overload;
constructor CreateResFmt(ResStringRec: PResStringRec; const Args: array of const); overload;
{$endif}
end;
{ TCachedBufferMemory record }
TCachedBufferMemory = {$ifdef OPERATORSUPPORT}record{$else}object{$endif}
public
Handle: Pointer;
PreviousSize: NativeUInt;
Data: Pointer;
Size: NativeUInt;
Additional: Pointer;
AdditionalSize: NativeUInt;
private
function GetEmpty: Boolean; {$ifdef INLINESUPPORT}inline;{$endif}
function GetFixed: Boolean; {$ifdef INLINESUPPORT}inline;{$endif}
function GetPreallocated: Boolean; {$ifdef INLINESUPPORT}inline;{$endif}
public
property Empty: Boolean read GetEmpty;
property Fixed: Boolean read GetFixed;
property Preallocated: Boolean read GetPreallocated;
end;
PCachedBufferMemory = ^TCachedBufferMemory;
{ TCachedBuffer abstract class }
TCachedBufferKind = (cbReader, cbWriter);
TCachedBuffer = class;
TCachedBufferCallback = function(const ASender: TCachedBuffer; AData: PByte; ASize: NativeUInt): NativeUInt of object;
TCachedBufferProgress = procedure(const ASender: TCachedBuffer; var ACancel: Boolean) of object;
TCachedBuffer = class(TCachedObject)
protected
FMemory: TCachedBufferMemory;
FKind: TCachedBufferKind;
FFinishing: Boolean;
FEOF: Boolean;
FLimited: Boolean;
FPositionBase: Int64;
FLimit: Int64;
FStart: PByte;
FOverflow: PByte;
FHighWritten: PByte;
FCallback: TCachedBufferCallback;
FOnProgress: TCachedBufferProgress;
class function GetOptimalBufferSize(const AValue, ADefValue: NativeUInt; const ALimit: Int64 = 0): NativeUInt;
function GetMargin: NativeInt; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
function GetPosition: Int64; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
procedure SetEOF(const AValue: Boolean);
procedure SetLimit(const AValue: Int64);
function CheckLimit(const AValue: Int64): Boolean; virtual;
function DoFlush: NativeUInt;
function DoWriterFlush: Boolean;
function DoReaderFlush: Boolean;
function DoProgress: Boolean;
{$ifdef FPC}
public
{$endif}
constructor Create(const AKind: TCachedBufferKind; const ACallback: TCachedBufferCallback; const ABufferSize: NativeUInt = 0);
public
class function PreallocatedInstance(const AMemory: Pointer; const ASize: Integer): TObject; override;
class procedure PreallocatedCall(const AParam: Pointer; const ABufferSize: NativeUInt; const ACallback: TCachedObjectCallback); reintroduce;
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
destructor Destroy; override;
public
Current: PByte;
function Flush: NativeUInt;
property Kind: TCachedBufferKind read FKind;
property Overflow: PByte read FOverflow;
property Margin: NativeInt read GetMargin;
property EOF: Boolean read FEOF write SetEOF;
property Limited: Boolean read FLimited;
property Limit: Int64 read FLimit write SetLimit;
property Memory: TCachedBufferMemory read FMemory;
property Position: Int64 read GetPosition;
property OnProgress: TCachedBufferProgress read FOnProgress write FOnProgress;
end;
TCachedBufferClass = class of TCachedBuffer;
{ TCachedReader class }
TCachedWriter = class;
TCachedReader = class(TCachedBuffer)
protected
procedure OverflowRead(var ABuffer; ASize: NativeUInt);
function DoDirectPreviousRead(APosition: Int64; AData: PByte; ASize: NativeUInt): Boolean; virtual;
function DoDirectFollowingRead(APosition: Int64; AData: PByte; ASize: NativeUInt): Boolean; virtual;
procedure OverflowSkip(ASize: NativeUInt);
public
constructor Create(const ACallback: TCachedBufferCallback; const ABufferSize: NativeUInt = 0);
procedure DirectRead(const APosition: Int64; var ABuffer; const ACount: NativeUInt);
property Finishing: Boolean read FFinishing;
procedure Skip(const ACount: NativeUInt); {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
procedure Export(const AWriter: TCachedWriter; const ACount: NativeUInt = 0); {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
// TStream-like data reading
procedure Read(var ABuffer; const ACount: NativeUInt);
procedure ReadData(var AValue: Boolean); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
{$ifdef ANSISTRSUPPORT}
procedure ReadData(var AValue: AnsiChar); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
{$endif}
procedure ReadData(var AValue: WideChar); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
procedure ReadData(var AValue: ShortInt); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
procedure ReadData(var AValue: Byte); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
procedure ReadData(var AValue: SmallInt); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
procedure ReadData(var AValue: Word); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
procedure ReadData(var AValue: Integer); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
procedure ReadData(var AValue: Cardinal); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
procedure ReadData(var AValue: Int64); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
{$if Defined(FPC) or (CompilerVersion >= 16)}
procedure ReadData(var AValue: UInt64); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
{$ifend}
procedure ReadData(var AValue: Single); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
procedure ReadData(var AValue: Double); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
{$if (not Defined(FPC)) or Defined(EXTENDEDSUPPORT)}
procedure ReadData(var AValue: Extended); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
{$ifend}
{$if not Defined(FPC) and (CompilerVersion >= 23)}
procedure ReadData(var AValue: TExtended80Rec); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
{$ifend}
procedure ReadData(var AValue: Currency); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
procedure ReadData(var AValue: TPoint); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
procedure ReadData(var AValue: TRect); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
{$ifdef SHORTSTRSUPPORT}
procedure ReadData(var AValue: ShortString); overload;
{$endif}
{$ifdef ANSISTRSUPPORT}
procedure ReadData(var AValue: AnsiString{$ifdef INTERNALCODEPAGE}; ACodePage: Word = 0{$endif}); overload;
{$endif}
{$ifdef MSWINDOWS}
procedure ReadData(var AValue: WideString); overload;
{$endif}
{$ifdef UNICODE}
procedure ReadData(var AValue: UnicodeString); overload;
{$endif}
procedure ReadData(var AValue: TBytes); overload;
procedure ReadData(var AValue: Variant); overload;
end;
{ TCachedWriter class }
TCachedWriter = class(TCachedBuffer)
protected
procedure OverflowWrite(const ABuffer; ASize: NativeUInt);
function DoDirectPreviousWrite(APosition: Int64; AData: PByte; ASize: NativeUInt): Boolean; virtual;
function DoDirectFollowingWrite(APosition: Int64; AData: PByte; ASize: NativeUInt): Boolean; virtual;
public
constructor Create(const ACallback: TCachedBufferCallback; const ABufferSize: NativeUInt = 0);
procedure DirectWrite(const APosition: Int64; const ABuffer; const ACount: NativeUInt);
procedure Import(const AReader: TCachedReader; const ACount: NativeUInt = 0);
// TStream-like data writing
procedure Write(const ABuffer; const ACount: NativeUInt);
procedure WriteData(const AValue: Boolean); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
{$ifdef ANSISTRSUPPORT}
procedure WriteData(const AValue: AnsiChar); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
{$endif}
procedure WriteData(const AValue: WideChar); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
procedure WriteData(const AValue: ShortInt); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
procedure WriteData(const AValue: Byte); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
procedure WriteData(const AValue: SmallInt); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
procedure WriteData(const AValue: Word); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
procedure WriteData(const AValue: Integer); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
procedure WriteData(const AValue: Cardinal); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
procedure WriteData(const AValue: Int64); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
{$if Defined(FPC) or (CompilerVersion >= 16)}
procedure WriteData(const AValue: UInt64); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
{$ifend}
procedure WriteData(const AValue: Single); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
procedure WriteData(const AValue: Double); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
{$if (not Defined(FPC)) or Defined(EXTENDEDSUPPORT)}
procedure WriteData(const AValue: Extended); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
{$ifend}
{$if not Defined(FPC) and (CompilerVersion >= 23)}
procedure WriteData(const AValue: TExtended80Rec); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
{$ifend}
procedure WriteData(const AValue: Currency); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
procedure WriteData(const AValue: TPoint); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
procedure WriteData(const AValue: TRect); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
{$ifdef SHORTSTRSUPPORT}
procedure WriteData(const AValue: ShortString); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
{$endif}
{$ifdef ANSISTRSUPPORT}
procedure WriteData(const AValue: AnsiString); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
{$endif}
{$ifdef MSWINDOWS}
procedure WriteData(const AValue: WideString); overload;
{$endif}
{$ifdef UNICODE}
procedure WriteData(const AValue: UnicodeString); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
{$endif}
procedure WriteData(const AValue: TBytes); overload; {$ifdef INLINESUPPORTSIMPLE}inline;{$endif}
procedure WriteData(const AValue: Variant); overload;
end;
{ TCachedReReader class }
TCachedReReader = class(TCachedReader)
protected
FSource: TCachedReader;
FOwner: Boolean;
public
constructor Create(const ACallback: TCachedBufferCallback; const ASource: TCachedReader; const AOwner: Boolean = False; const ABufferSize: NativeUInt = 0);
destructor Destroy; override;
property Source: TCachedReader read FSource;
property Owner: Boolean read FOwner write FOwner;
end;
{ TCachedReWriter class }
TCachedReWriter = class(TCachedWriter)
protected
FTarget: TCachedWriter;
FOwner: Boolean;
public
constructor Create(const ACallback: TCachedBufferCallback; const ATarget: TCachedWriter; const AOwner: Boolean = False; const ABufferSize: NativeUInt = 0);
destructor Destroy; override;
property Target: TCachedWriter read FTarget;
property Owner: Boolean read FOwner write FOwner;
end;
{ TCachedFileReader class }
TCachedFileReader = class(TCachedReader)
protected
FFileName: string;
FHandle: THandle;
FHandleOwner: Boolean;
FOffset: Int64;
procedure InternalCreate(const ASize: Int64; const ASeeked: Boolean);
function CheckLimit(const AValue: Int64): Boolean; override;
function DoDirectPreviousRead(APosition: Int64; AData: PByte; ASize: NativeUInt): Boolean; override;
function DoDirectFollowingRead(APosition: Int64; AData: PByte; ASize: NativeUInt): Boolean; override;
function InternalCallback(const ASender: TCachedBuffer; AData: PByte; ASize: NativeUInt): NativeUInt;
public
constructor Create(const AFileName: string; const AOffset: Int64 = 0; const ASize: Int64 = 0);
constructor CreateHandled(const AHandle: THandle; const ASize: Int64 = 0; const AHandleOwner: Boolean = False);
destructor Destroy; override;
property FileName: string read FFileName;
property Handle: THandle read FHandle;
property HandleOwner: Boolean read FHandleOwner write FHandleOwner;
property Offset: Int64 read FOffset;
end;
{ TCachedFileWriter class }
TCachedFileWriter = class(TCachedWriter)
protected
FFileName: string;
FHandle: THandle;
FHandleOwner: Boolean;
FOffset: Int64;
function DoDirectPreviousWrite(APosition: Int64; AData: PByte; ASize: NativeUInt): Boolean; override;
function DoDirectFollowingWrite(APosition: Int64; AData: PByte; ASize: NativeUInt): Boolean; override;
function InternalCallback(const ASender: TCachedBuffer; AData: PByte; ASize: NativeUInt): NativeUInt;
public
constructor Create(const AFileName: string; const ASize: Int64 = 0);
constructor CreateHandled(const AHandle: THandle; const ASize: Int64 = 0; const AHandleOwner: Boolean = False);
destructor Destroy; override;
property FileName: string read FFileName;
property Handle: THandle read FHandle;
property HandleOwner: Boolean read FHandleOwner write FHandleOwner;
property Offset: Int64 read FOffset;
end;
{ TCachedMemoryReader class }
TCachedMemoryReader = class(TCachedReader)
protected
FPtr: Pointer;
FSize: NativeUInt;
FPtrMargin: NativeUInt;
function CheckLimit(const AValue: Int64): Boolean; override;
function InternalCallback(const ASender: TCachedBuffer; AData: PByte; ASize: NativeUInt): NativeUInt;
function FixedCallback(const ASender: TCachedBuffer; AData: PByte; ASize: NativeUInt): NativeUInt;
public
constructor Create(const APtr: Pointer; const ASize: NativeUInt; const AFixed: Boolean = False);
property Ptr: Pointer read FPtr;
property Size: NativeUInt read FSize;
end;
{ TCachedMemoryWriter class }
TCachedMemoryWriter = class(TCachedWriter)
protected
FTemporary: Boolean;
FPtr: Pointer;
FSize: NativeUInt;
FPtrMargin: NativeUInt;
function CheckLimit(const AValue: Int64): Boolean; override;
function InternalCallback(const ASender: TCachedBuffer; AData: PByte; ASize: NativeUInt): NativeUInt;
function InternalTemporaryCallback(const ASender: TCachedBuffer; AData: PByte; ASize: NativeUInt): NativeUInt;
function FixedCallback(const ASender: TCachedBuffer; AData: PByte; ASize: NativeUInt): NativeUInt;
public
constructor Create(const APtr: Pointer; const ASize: NativeUInt; const AFixed: Boolean = False);
constructor CreateTemporary;
destructor Destroy; override;
property Temporary: Boolean read FTemporary;
property Ptr: Pointer read FPtr;
property Size: NativeUInt read FSize;
end;
{ TCachedResourceReader class }
TCachedResourceReader = class(TCachedMemoryReader)
protected
HGlobal: THandle;
procedure InternalCreate(AInstance: THandle; AName, AResType: PChar; AFixed: Boolean);
public
constructor Create(const AInstance: THandle; const AResName: string; const AResType: PChar; const AFixed: Boolean = False);
constructor CreateFromID(const AInstance: THandle; const AResID: Word; const AResType: PChar; const AFixed: Boolean = False);
destructor Destroy; override;
end;
// fast non-collision Move realization
procedure NcMove(const Source; var Dest; const Size: NativeUInt); {$ifNdef CPUINTELASM}inline;{$endif}
implementation
{$ifNdef ANSISTRSUPPORT}
type
AnsiChar = type Byte;
PAnsiChar = ^AnsiChar;
{$endif}
{$ifdef FPC}
const
INVALID_HANDLE_VALUE = THandle(-1);
{$endif}
const
KB_SIZE = 1024;
MEMORY_PAGE_SIZE = 4 * KB_SIZE;
DEFAULT_CACHED_SIZE = 64 * KB_SIZE;
MAX_PREALLOCATED_SIZE = 20 * MEMORY_PAGE_SIZE; // 80KB
{ Exceptions }
procedure RaisePointers;
begin
raise ECachedBuffer.Create('Invalid current, overflow or buffer memory values');
end;
procedure RaiseEOF;
begin
raise ECachedBuffer.Create('EOF buffer data modified');
end;
procedure RaiseReading;
begin
raise ECachedBuffer.Create('Data reading error');
end;
procedure RaiseWriting;
begin
raise ECachedBuffer.Create('Data writing error');
end;
procedure RaiseLimitValue(const AValue: Int64);
begin
raise ECachedBuffer.CreateFmt('Invalid limit value %d', [AValue]);
end;
procedure RaiseVaraintType(const VType: Word);
begin
raise ECachedBuffer.CreateFmt('Invalid variant type 0x%.4x', [VType]);
end;
{ Utilitarian functions }
{$if Defined(FPC) or (CompilerVersion < 24)}
{$ifdef FPC}
function AtomicIncrement(var Target: Integer): Integer; inline;
begin
Result := InterLockedIncrement(Target);
end;
{$else .DELPHI}
function AtomicIncrement(var Target: Integer): Integer;
asm
{$ifdef CPUX86}
mov edx, 1
lock xadd [eax], edx
lea eax, [edx - 1]
{$else .CPUX64}
mov eax, 1
lock xadd [RCX], eax
dec eax
{$endif}
end;
{$endif}
{$ifdef FPC}
function AtomicDecrement(var Target: Integer): Integer; inline;
begin
Result := InterLockedDecrement(Target);
end;
{$else .DELPHI}
function AtomicDecrement(var Target: Integer): Integer;
asm
{$ifdef CPUX86}
or edx, -1
lock xadd [eax], edx
lea eax, [edx - 1]
{$else .CPUX64}
or eax, -1
lock xadd [RCX], eax
dec eax
{$endif}
end;
{$endif}
{$ifend}
function AllocateCachedBufferMemory(const APreviousSize, ABufferSize: NativeUInt): TCachedBufferMemory;
var
LOffset: NativeUInt;
begin
// detect sizes
Result.PreviousSize := (APreviousSize + MEMORY_PAGE_SIZE - 1) and -MEMORY_PAGE_SIZE;
Result.AdditionalSize := MEMORY_PAGE_SIZE;
if (ABufferSize = 0) then
begin
Result.Size := DEFAULT_CACHED_SIZE;
end else
begin
Result.Size := (ABufferSize + MEMORY_PAGE_SIZE - 1) and -MEMORY_PAGE_SIZE;
end;
// allocate
GetMem(Result.Handle, Result.PreviousSize + Result.Size + Result.AdditionalSize + MEMORY_PAGE_SIZE);
// align
LOffset := NativeUInt(Result.Handle) and (MEMORY_PAGE_SIZE - 1);
Inc(Result.PreviousSize, MEMORY_PAGE_SIZE - LOffset);
Inc(Result.AdditionalSize, LOffset);
Result.Data := Pointer(NativeUInt(Result.Handle) + Result.PreviousSize);
Result.Additional := Pointer(NativeUInt(Result.Data) + Result.Size);
end;
function GetFileSize(AHandle: THandle): Int64;
var
{$ifdef MSWINDOWS}
P: TPoint;
{$endif}
{$ifdef POSIX}
S: {$ifdef FPC}Stat{$else}_stat{$endif};
{$endif}
begin
{$ifdef MSWINDOWS}
P.X := {$ifdef UNITSCOPENAMES}Winapi.{$endif}Windows.GetFileSize(AHandle, Pointer(@P.Y));
if (P.Y = -1) then P.X := -1;
Result := PInt64(@P)^;
{$endif}
{$ifdef POSIX}
if ({$ifdef FPC}FpFStat{$else}fstat{$endif}(AHandle, S) = 0) then
Result := S.st_size
else
Result := -1;
{$endif}
end;
function DirectCachedFileMethod(const AInstance: TCachedBuffer;
const AInstanceHandle: THandle; const AInstanceOffset, APosition: Int64;
const AData: PByte; const ASize: NativeUInt): Boolean;
var
LSeekValue: Int64;
LPositionValue: Int64;
begin
LSeekValue := FileSeek(AInstanceHandle, Int64(0), 1{soFromCurrent});
try
LPositionValue := APosition + AInstanceOffset;
if (LPositionValue <> FileSeek(AInstanceHandle, LPositionValue, 0{soFromBeginning})) then
begin
Result := False;
end else
begin
Result := (ASize = AInstance.FCallback(AInstance, AData, ASize));
end;
finally
FileSeek(AInstanceHandle, LSeekValue, 0{soFromBeginning});
end;
end;
{ TCachedObject }
{$ifNdef INLINESUPPORTSIMPLE}
const
objDestroyingFlag = Integer($80000000);
objDisposedFlag = Integer($40000000);
objPreallocatedFlag = Integer($20000000);
REFCOUNT_MASK = not (objPreallocatedFlag or {$ifdef AUTOREFCOUNT}objDisposedFlag{$else}objDestroyingFlag{$endif});
{$endif}
class function TCachedObject.NewInstance: TObject;
var
LSize: Integer;
LMemory: Pointer;
begin
LSize := PInteger(NativeInt(Self) + vmtInstanceSize)^;
GetMem(LMemory, LSize);
Result := InitInstance(LMemory);
TCachedObject(Result).FRefCount := 1;
end;
class function TCachedObject.PreallocatedInstance(const AMemory: Pointer;
const ASize: Integer): TObject;
var
LSize: Integer;
LMemory: Pointer;
begin
LSize := PInteger(NativeInt(Self) + vmtInstanceSize)^;
if (ASize >= LSize) then
begin
LMemory := Pointer((NativeInt(AMemory) + 15) and -16);
if (ASize < LSize + (NativeInt(LMemory) - NativeInt(AMemory))) then
begin
LMemory := Pointer((NativeInt(AMemory) + 7) and -8);
if (ASize < LSize + (NativeInt(LMemory) - NativeInt(AMemory))) then
begin
LMemory := Pointer((NativeInt(AMemory) + 3) and -4);
if (ASize < LSize + (NativeInt(LMemory) - NativeInt(AMemory))) then
begin
LMemory := Pointer((NativeInt(AMemory) + 1) and -2);
if (ASize < LSize + (NativeInt(LMemory) - NativeInt(AMemory))) then
begin
LMemory := AMemory;
end;
end;
end;
end;
Result := InitInstance(LMemory);
TCachedObject(Result).FRefCount := 1 or objPreallocatedFlag;
end else
begin
GetMem(LMemory, LSize);
Result := InitInstance(LMemory);
TCachedObject(Result).FRefCount := 1;
end;
end;
procedure CachedPreallocatedCall_1(const AClass: TCachedObjectClass;
const AParam: Pointer; const ACallback: TCachedObjectCallback);
var
LBuffer: array[0..1 * MEMORY_PAGE_SIZE + 15] of Byte;
begin
ACallback(TCachedObject(AClass.PreallocatedInstance(@LBuffer, SizeOf(LBuffer))), AParam);
end;
procedure CachedPreallocatedCall_2(const AClass: TCachedObjectClass;
const AParam: Pointer; const ACallback: TCachedObjectCallback);
var
LBuffer: array[0..2 * MEMORY_PAGE_SIZE + 15] of Byte;
begin
ACallback(TCachedObject(AClass.PreallocatedInstance(@LBuffer, SizeOf(LBuffer))), AParam);
end;
procedure CachedPreallocatedCall_3(const AClass: TCachedObjectClass;
const AParam: Pointer; const ACallback: TCachedObjectCallback);
var
LBuffer: array[0..3 * MEMORY_PAGE_SIZE + 15] of Byte;
begin
ACallback(TCachedObject(AClass.PreallocatedInstance(@LBuffer, SizeOf(LBuffer))), AParam);
end;
procedure CachedPreallocatedCall_4(const AClass: TCachedObjectClass;
const AParam: Pointer; const ACallback: TCachedObjectCallback);
var
LBuffer: array[0..4 * MEMORY_PAGE_SIZE + 15] of Byte;
begin
ACallback(TCachedObject(AClass.PreallocatedInstance(@LBuffer, SizeOf(LBuffer))), AParam);
end;
procedure CachedPreallocatedCall_5(const AClass: TCachedObjectClass;
const AParam: Pointer; const ACallback: TCachedObjectCallback);
var
LBuffer: array[0..5 * MEMORY_PAGE_SIZE + 15] of Byte;
begin
ACallback(TCachedObject(AClass.PreallocatedInstance(@LBuffer, SizeOf(LBuffer))), AParam);
end;
procedure CachedPreallocatedCall_6(const AClass: TCachedObjectClass;
const AParam: Pointer; const ACallback: TCachedObjectCallback);
var
LBuffer: array[0..6 * MEMORY_PAGE_SIZE + 15] of Byte;
begin
ACallback(TCachedObject(AClass.PreallocatedInstance(@LBuffer, SizeOf(LBuffer))), AParam);
end;
procedure CachedPreallocatedCall_7(const AClass: TCachedObjectClass;
const AParam: Pointer; const ACallback: TCachedObjectCallback);
var
LBuffer: array[0..7 * MEMORY_PAGE_SIZE + 15] of Byte;
begin
ACallback(TCachedObject(AClass.PreallocatedInstance(@LBuffer, SizeOf(LBuffer))), AParam);
end;
procedure CachedPreallocatedCall_8(const AClass: TCachedObjectClass;
const AParam: Pointer; const ACallback: TCachedObjectCallback);
var
LBuffer: array[0..8 * MEMORY_PAGE_SIZE + 15] of Byte;
begin
ACallback(TCachedObject(AClass.PreallocatedInstance(@LBuffer, SizeOf(LBuffer))), AParam);
end;
procedure CachedPreallocatedCall_9(const AClass: TCachedObjectClass;
const AParam: Pointer; const ACallback: TCachedObjectCallback);
var
LBuffer: array[0..9 * MEMORY_PAGE_SIZE + 15] of Byte;
begin
ACallback(TCachedObject(AClass.PreallocatedInstance(@LBuffer, SizeOf(LBuffer))), AParam);
end;
procedure CachedPreallocatedCall_10(const AClass: TCachedObjectClass;
const AParam: Pointer; const ACallback: TCachedObjectCallback);
var
LBuffer: array[0..10 * MEMORY_PAGE_SIZE + 15] of Byte;
begin
ACallback(TCachedObject(AClass.PreallocatedInstance(@LBuffer, SizeOf(LBuffer))), AParam);
end;
procedure CachedPreallocatedCall_11(const AClass: TCachedObjectClass;
const AParam: Pointer; const ACallback: TCachedObjectCallback);
var
LBuffer: array[0..11 * MEMORY_PAGE_SIZE + 15] of Byte;
begin
ACallback(TCachedObject(AClass.PreallocatedInstance(@LBuffer, SizeOf(LBuffer))), AParam);
end;
procedure CachedPreallocatedCall_12(const AClass: TCachedObjectClass;
const AParam: Pointer; const ACallback: TCachedObjectCallback);
var
LBuffer: array[0..12 * MEMORY_PAGE_SIZE + 15] of Byte;
begin
ACallback(TCachedObject(AClass.PreallocatedInstance(@LBuffer, SizeOf(LBuffer))), AParam);
end;
procedure CachedPreallocatedCall_13(const AClass: TCachedObjectClass;
const AParam: Pointer; const ACallback: TCachedObjectCallback);
var
LBuffer: array[0..13 * MEMORY_PAGE_SIZE + 15] of Byte;
begin
ACallback(TCachedObject(AClass.PreallocatedInstance(@LBuffer, SizeOf(LBuffer))), AParam);
end;
procedure CachedPreallocatedCall_14(const AClass: TCachedObjectClass;
const AParam: Pointer; const ACallback: TCachedObjectCallback);
var
LBuffer: array[0..14 * MEMORY_PAGE_SIZE + 15] of Byte;
begin
ACallback(TCachedObject(AClass.PreallocatedInstance(@LBuffer, SizeOf(LBuffer))), AParam);
end;
procedure CachedPreallocatedCall_15(const AClass: TCachedObjectClass;
const AParam: Pointer; const ACallback: TCachedObjectCallback);
var
LBuffer: array[0..15 * MEMORY_PAGE_SIZE + 15] of Byte;
begin
ACallback(TCachedObject(AClass.PreallocatedInstance(@LBuffer, SizeOf(LBuffer))), AParam);
end;
procedure CachedPreallocatedCall_16(const AClass: TCachedObjectClass;
const AParam: Pointer; const ACallback: TCachedObjectCallback);
var
LBuffer: array[0..16 * MEMORY_PAGE_SIZE + 15] of Byte;
begin
ACallback(TCachedObject(AClass.PreallocatedInstance(@LBuffer, SizeOf(LBuffer))), AParam);
end;
procedure CachedPreallocatedCall_17(const AClass: TCachedObjectClass;
const AParam: Pointer; const ACallback: TCachedObjectCallback);
var
LBuffer: array[0..17 * MEMORY_PAGE_SIZE + 15] of Byte;
begin
ACallback(TCachedObject(AClass.PreallocatedInstance(@LBuffer, SizeOf(LBuffer))), AParam);
end;
procedure CachedPreallocatedCall_18(const AClass: TCachedObjectClass;
const AParam: Pointer; const ACallback: TCachedObjectCallback);
var
LBuffer: array[0..18 * MEMORY_PAGE_SIZE + 15] of Byte;
begin
ACallback(TCachedObject(AClass.PreallocatedInstance(@LBuffer, SizeOf(LBuffer))), AParam);
end;
procedure CachedPreallocatedCall_19(const AClass: TCachedObjectClass;
const AParam: Pointer; const ACallback: TCachedObjectCallback);
var
LBuffer: array[0..19 * MEMORY_PAGE_SIZE + 15] of Byte;
begin
ACallback(TCachedObject(AClass.PreallocatedInstance(@LBuffer, SizeOf(LBuffer))), AParam);
end;
procedure CachedPreallocatedCall_20(const AClass: TCachedObjectClass;
const AParam: Pointer; const ACallback: TCachedObjectCallback);
var
LBuffer: array[0..20 * MEMORY_PAGE_SIZE + 15] of Byte;
begin
ACallback(TCachedObject(AClass.PreallocatedInstance(@LBuffer, SizeOf(LBuffer))), AParam);
end;
type
TCachedPreallocatedCall = procedure(const AClass: TCachedObjectClass;