forked from andremussche/scalemm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMvalidation.pas
1574 lines (1470 loc) · 40.8 KB
/
MMvalidation.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 MMValidation;
interface
uses
Windows, SysUtils, Classes, Dialogs, Math;
type
TValidateFunction = function: Boolean of object;
TValidateProgressEvent = procedure(const CurrentValidation, Failed: string) of object;
TMMValidation = class(TComponent)
private
FOnProgress: TValidateProgressEvent;
FFailedList: string;
function Validate1: Boolean;
function Validate2: Boolean;
function Validate3: Boolean;
function Validate4: Boolean;
function Validate5: Boolean;
function Validate6: Boolean;
function Validate7: Boolean;
function Validate8: Boolean;
function Validate9: Boolean;
function Validate10: Boolean;
function Validate11: Boolean;
function Validate12: Boolean;
function Validate13: Boolean;
function Validate14: Boolean;
function Validate15: boolean;
function Validate16: boolean;
function Validate17: Boolean;
function Validate18: Boolean;
function Validate19: Boolean;
function Validate20: Boolean;
function Validate21: Boolean;
function Validate22: Boolean;
function Validate24: Boolean;
function Validate25: Boolean;
function Validate26: Boolean;
function Validate27: boolean;
function Validate28: boolean;
private
procedure DoValidate(ValidateFunction: TValidateFunction; const ValidationName: string);
public
property OnProgress: TValidateProgressEvent read FOnProgress write FOnProgress; // to show progress of validation
function Validate: string; // returns list of failed validations
function ExtraValidate: string; // returns list of failed validations
end;
TValidate28Thread = class(TThread)
public
procedure Execute; override;
end;
implementation
uses
BenchmarkForm, BenchmarkUtilities;
function TMMValidation.Validate: string;
begin
FFailedList := '';
DoValidate(Validate1, 'Validate1');
DoValidate(Validate2, 'Validate2');
DoValidate(Validate3, 'Validate3');
DoValidate(Validate4, 'Validate4');
DoValidate(Validate5, 'Validate5');
DoValidate(Validate6, 'Validate6');
DoValidate(Validate7, 'Validate7');
DoValidate(Validate8, 'Validate8');
DoValidate(Validate9, 'Validate9');
DoValidate(Validate10, 'Validate10');
DoValidate(Validate11, 'Validate11');
DoValidate(Validate12, 'Validate12');
DoValidate(Validate14, 'Validate14');
DoValidate(Validate17, 'Validate17');
DoValidate(Validate19, 'Validate19');
DoValidate(Validate21, 'Validate21');
DoValidate(Validate22, 'Validate22');
DoValidate(Validate24, 'Validate24');
DoValidate(Validate25, 'Validate25');
DoValidate(Validate26, 'Validate26');
DoValidate(Validate28, 'Validate28');
// Validate13 is really very nasty by provoking an EOutofMemory
// We execute it at the end, then give the system 10 seconds to relax...
DoValidate(Validate13, 'Validate13');
Sleep(10000);
Result := FFailedList;
end;
function TMMValidation.ExtraValidate: string;
begin
FFailedList := '';
DoValidate(Validate15, 'Validate15');
DoValidate(Validate16, 'Validate16');
DoValidate(Validate18, 'Validate18');
DoValidate(Validate20, 'Validate20');
DoValidate(Validate27, 'Validate27');
Result := FFailedList;
end;
procedure TMMValidation.DoValidate(ValidateFunction: TValidateFunction; const ValidationName: string);
var
Passed: Boolean;
begin
if Assigned(FOnProgress) then
FOnProgress(ValidationName, FFailedList);
Passed := True;
try
Passed := ValidateFunction;
except
Passed := False;
end;
if not Passed then
FFailedList := FFailedList + ValidationName + ';';
end;
type
TExportedMethod = procedure;
(**************************************************************************************************)
// all Validation functions below
(**************************************************************************************************)
function TMMValidation.Validate1 : Boolean;
var
SomeArray : array of Byte;
MemoryStatus : TMemoryStatus;
const
BYTESTOALLOCATE : Cardinal = 1;//1 byte
begin
GlobalMemoryStatus(MemoryStatus);
try
Result := True;
if MemoryStatus.dwAvailVirtual >= BYTESTOALLOCATE then
SetLength(SomeArray, BYTESTOALLOCATE);
except
Result := False;
end;
end;
function TMMValidation.Validate2 : Boolean;
var
SomeArray1, SomeArray2 : array of Byte;
MemoryStatus : TMemoryStatus;
const
BYTESTOALLOCATE : Cardinal = 1;//1 byte
begin
GlobalMemoryStatus(MemoryStatus);
try
Result := True;
if MemoryStatus.dwAvailVirtual >= BYTESTOALLOCATE then
begin
SetLength(SomeArray1, BYTESTOALLOCATE);
SetLength(SomeArray2, BYTESTOALLOCATE);
//Validate that pointers do not overlap
if @SomeArray1[0] = @SomeArray2[0] then
Result := False;
end;
except
Result := False;
end;
end;
function TMMValidation.Validate3 : Boolean;
var
SomeArray : array of Byte;
MemoryStatus : TMemoryStatus;
const
BYTESTOALLOCATEMAX : Cardinal = 2;//2 byte
begin
GlobalMemoryStatus(MemoryStatus);
try
Result := True;
if MemoryStatus.dwAvailVirtual >= BYTESTOALLOCATEMAX then
begin
//Allocate 1 byte
SetLength(SomeArray, 1);
//Fill it
SomeArray[0] := 0;
//Expand array
SetLength(SomeArray, 2);
//Validate that data in first allocation is preserved by realloc/expand
if SomeArray[0] <> 0 then
Result := False;
end;
except
Result := False;
end;
end;
function TMMValidation.Validate4 : Boolean;
var
I : Cardinal;
MemoryStatus : TMemoryStatus;
pMem : Pointer;
pChr : PAnsiChar;
const
BYTESTOALLOCATEMAX : Cardinal = 32000;//32 KB
begin
GlobalMemoryStatus(MemoryStatus);
try
Result := True;
if MemoryStatus.dwAvailVirtual >= BYTESTOALLOCATEMAX then
begin
pMem := AllocMem(BYTESTOALLOCATEMAX);
pChr := pMem;
for I := 0 to BYTESTOALLOCATEMAX-1 do
begin
if pChr^ <> #0 then
begin
Result := False;
Break;
end;
Inc(PChr);
end;
FreeMem(pMem);
end;
except
Result := False;
end;
end;
function TMMValidation.Validate5 : Boolean;
var
I : Cardinal;
SomeArray : array of Byte;
MemoryStatus : TMemoryStatus;
const
BYTESTOALLOCATE : Cardinal = 64000;//64kB byte
NOOFITERATIONS : Cardinal = 100000;
begin
GlobalMemoryStatus(MemoryStatus);
try
Result := True;
for I := 1 to NOOFITERATIONS do
begin
if MemoryStatus.dwAvailVirtual >= BYTESTOALLOCATE then
//Allocate
SetLength(SomeArray, BYTESTOALLOCATE);
//Free
SetLength(SomeArray, 0);
end;
except
Result := False;
end;
end;
function TMMValidation.Validate6 : Boolean;
var
I : Cardinal;
pMem : Pointer;
MemoryStatus : TMemoryStatus;
const
BYTESTOALLOCATE : Cardinal = 40000;//40kB byte
NOOFITERATIONS : Cardinal = 200000;
begin
GlobalMemoryStatus(MemoryStatus);
try
Result := True;
for I := 1 to NOOFITERATIONS do
begin
if MemoryStatus.dwAvailVirtual >= BYTESTOALLOCATE then
begin
//Allocate
GetMem(pMem, BYTESTOALLOCATE);
//Free
FreeMem(pMem);
end;
end;
except
Result := False;
end;
end;
function TMMValidation.Validate7 : Boolean;
var
I1, I2, I3, J1, J2 : Cardinal;
pMem : Pointer;
PointerArray : array[1..10] of Pointer;
MemoryStatus : TMemoryStatus;
const
BYTESTOALLOCATE : Cardinal = 1;//1 byte
begin
for I1 := 1 to Length(PointerArray) do
PointerArray[I1] := nil;
try
Result := True;
for I2 := 1 to Length(PointerArray) do
begin
GlobalMemoryStatus(MemoryStatus);
if MemoryStatus.dwAvailVirtual >= BYTESTOALLOCATE then
begin
//Allocate all pointers
GetMem(pMem, BYTESTOALLOCATE);
PointerArray[I2] := pMem;
end;
end;
for J1 := 1 to Length(PointerArray)-1 do
for J2 := J1+1 to Length(PointerArray) do
begin
//No pointers can overlap = be closer than BYTESTOALLOCATE
if Cardinal(Abs(Integer(PointerArray[J1]) - Integer(PointerArray[J2]))) < BYTESTOALLOCATE then
begin
Result := False;
Break;
end;
end;
//Free all pointers
for I3 := 1 to Length(PointerArray) do
begin
pMem := PointerArray[I3];
FreeMem(pMem);
PointerArray[I3] := nil;
end;
except
Result := False;
end;
end;
function TMMValidation.Validate8 : Boolean;
var
I1, I2, I3, J1, J2 : Cardinal;
pMem : Pointer;
PointerArray : array[1..10] of Pointer;
MemoryStatus : TMemoryStatus;
const
BYTESTOALLOCATE : Cardinal = 2;//2 byte
begin
for I1 := 1 to Length(PointerArray) do
PointerArray[I1] := nil;
try
Result := True;
for I2 := 1 to Length(PointerArray) do
begin
GlobalMemoryStatus(MemoryStatus);
if MemoryStatus.dwAvailVirtual >= BYTESTOALLOCATE then
begin
//Allocate all pointers
GetMem(pMem, BYTESTOALLOCATE);
PointerArray[I2] := pMem;
end;
end;
for J1 := 1 to Length(PointerArray)-1 do
for J2 := J1+1 to Length(PointerArray) do
begin
//No pointers can overlap = be closer than BYTESTOALLOCATE
if Cardinal(Abs(Integer(PointerArray[J1]) - Integer(PointerArray[J2]))) < BYTESTOALLOCATE then
begin
Result := False;
Break;
end;
end;
//Free all pointers
for I3 := 1 to Length(PointerArray) do
begin
pMem := PointerArray[I3];
FreeMem(pMem);
PointerArray[I3] := nil;
end;
except
Result := False;
end;
end;
function TMMValidation.Validate9 : Boolean;
var
I1, I2, I3, J1, J2, BytesToAllocate : Cardinal;
pMem : Pointer;
PointerArray : array[1..2000] of Pointer;
MemoryStatus : TMemoryStatus;
const
BYTESTOALLOCATEMAX : Cardinal = 3000;//3000 byte
begin
Result := True;
try
for BytesToAllocate := 1 to BYTESTOALLOCATEMAX do
begin
for I1 := 1 to Length(PointerArray) do
PointerArray[I1] := nil;
for I2 := 1 to Length(PointerArray) do
begin
GlobalMemoryStatus(MemoryStatus);
if MemoryStatus.dwAvailVirtual >= BytesToAllocate then
begin
//Allocate all pointers
GetMem(pMem, BytesToAllocate);
PointerArray[I2] := pMem;
end;
end;
for J1 := 1 to Length(PointerArray)-1 do
for J2 := J1+1 to Length(PointerArray) do
begin
//No pointers can overlap = be closer than BytesToAllocate
if Cardinal(Abs(Integer(PointerArray[J1]) - Integer(PointerArray[J2]))) < BytesToAllocate then
begin
Result := False;
Exit;
end;
end;
//Free all pointers
for I3 := 1 to Length(PointerArray) do
begin
pMem := PointerArray[I3];
FreeMem(pMem);
PointerArray[I3] := nil;
end;
end;
except
Result := False;
end;
end;
function TMMValidation.Validate10 : Boolean;
var
SomeArray : array of Byte;
MemoryStatus : TMemoryStatus;
Address: Pointer;
AddressCard : Cardinal;
const
BYTESTOALLOCATE : Cardinal = 1000000;//1 MB
begin
GlobalMemoryStatus(MemoryStatus);
try
Result := True;
if MemoryStatus.dwAvailVirtual >= BYTESTOALLOCATE then
begin
SetLength(SomeArray, BYTESTOALLOCATE);
Address := @SomeArray[0];
AddressCard := Cardinal(Address);
//Is allocation 4 byte aligned?
if AddressCard mod 4 <> 0 then
Result := False;
end;
except
Result := False;
end;
end;
//Validating small to medium upsize of one array combined with many small strings resize
function TMMValidation.Validate11 : Boolean;
var
I, J, K, L, NoOfStrings : Cardinal;
SomeArray : array of Cardinal;
StringArray : array[1..517] of Ansistring;
TempS : AnsiString;
MemoryStatus : TMemoryStatus;
const
BYTESTOALLOCATEMAX : Integer = 320001;
begin
Result := True;
try
NoOfStrings := Length(StringArray);
for I := 0 to BYTESTOALLOCATEMAX-1 do
begin
//Clear a string sometimes
if I mod 5 = 0 then
StringArray[Random(NoOfStrings) + 1] := '';
//Grow a string with an 'A'
StringArray[(I mod NoOfStrings) + 1] := StringArray[(I mod Cardinal(Length(StringArray)))+1] + 'A';
//Grow SomeArray if place for it
GlobalMemoryStatus(MemoryStatus);
if MemoryStatus.dwAvailVirtual >= I+1 then
SetLength(SomeArray, I+1);
SomeArray[I] := I;
//Validate that SomeArray data are not changed
for J := 0 to I do
begin
if SomeArray[J] <> J then
begin
Result := False;
Exit;
end;
end;
//Validate that all strings contains 'A's and nothing else
for K := 1 to Length(StringArray) do
begin
TempS := StringArray[K];
for L := 1 to Length(TempS) do
begin
if TempS[L] <> 'A' then
begin
Result := False;
Exit;
end;
end;
end;
end;
except
Result := False;
end;
end;
//Creates a square 2 dimensional array
//Grows it from 1x1 = 1 byte to BYTESTOALLOCATEMAX bytes
function TMMValidation.Validate12 : Boolean;
var
SomeArray : array of array of Cardinal;
MemoryStatus : TMemoryStatus;
I1, I2, I3, I4, I5, IMax : Cardinal;
const
BYTESTOALLOCATEMAX : Integer = 10000000;//10 MB
begin
Result := True;
GlobalMemoryStatus(MemoryStatus);
IMax := Trunc(Sqrt(BYTESTOALLOCATEMAX));
try
//Grow array
for I1 := 2 to IMax do
begin
if MemoryStatus.dwAvailVirtual >= I1*I1 then
begin
SetLength(SomeArray, I1, I1);
//Old data preserved? (0's in first iteration)
for I2 := 0 to I1-2 do
for I3 := 0 to I1-2 do
begin
if SomeArray[I2,I3] <> (I2 * I3) mod 256 then
begin
Result := False;
Exit;
end;
end;
//Fill it (again)
for I4 := 0 to I1-1 do
for I5 := 0 to I1-1 do
SomeArray[I4,I5] := (I4 * I5) mod 256;
end
else
//Stop test if no more memory available
Exit;
end;
except
Result := False;
end;
end;
// stress test... pushing the MM to an "Out of Memory" by allocating 16 kB pointers
// see whether we get an EOutOfMemory (OK) or something else (WRONG)
function TMMValidation.Validate13: Boolean;
var
Pointers : array[0..200000] of Pointer;
n : integer;
begin
n := 0;
try
repeat
//Allocate 16 kB pointer
GetMem(Pointers[n], 16384);
PAnsiChar(Pointers[n])[4] := 'A';
Inc(n);
until n > High(Pointers);
Result := True; // no exception at all, and using more than 3 GB...interesting !
except
on E: EOutOfMemory do // that's the right exception...
Result := True
else // all other exceptions are wrong...
Result := False;
end;
//Release memory
while n > 0 do
begin
Dec(n);
FreeMem(Pointers[n]);
end;
end;
function TMMValidation.Validate14 : Boolean;
var
SomeArray : array of Byte;
MemoryStatus : TMemoryStatus;
BytesToAllocate, I, StartAddress : Cardinal;
const
BYTESTOALLOCATEMIN : Cardinal = 10 * 1024;//10 Kbyte
BYTESTOALLOCATEMAX : Cardinal = 100 * 1024;//100 Kbyte
begin
GlobalMemoryStatus(MemoryStatus);
try
Result := True;
for BytesToAllocate := BYTESTOALLOCATEMIN to BYTESTOALLOCATEMAX do
begin
if MemoryStatus.dwAvailVirtual >= BytesToAllocate then
begin
//Allocate
SetLength(SomeArray, BytesToAllocate);
//4 byte alignment?
StartAddress := Cardinal(@SomeArray[8]);
if StartAddress mod 4 <> 0 then
begin
Result := False;
Exit;
end;
for I := 0 to BytesToAllocate-1 do
SomeArray[I] := 255;
//Free
SetLength(SomeArray, 0);
end;
end;
except
Result := False;
end;
end;
function TMMValidation.Validate15: boolean;
var
Ptr: Pointer;
begin
{Allocate a block}
GetMem(Ptr, 10);
{Free it}
FreeMem(Ptr);
{Try to free it again and see whether the MM catches the error}
try
FreeMem(Ptr);
Result := False;
except
Result := True;
end;
end;
function TMMValidation.Validate16: boolean;
var
i: integer;
t1: pointer;
t2: pointer;
const
TRY_TIMES = 1000;
SIZE_TO_ALLOCATE = 128;
begin
i := TRY_TIMES;
while (i <> 0) do
begin
dec(i);
try
GetMem(t1, SIZE_TO_ALLOCATE);
FreeMem(t1);
FreeMem(t1);
Result := False;
exit;
except
end;
GetMem(t1, SIZE_TO_ALLOCATE);
GetMem(t2, SIZE_TO_ALLOCATE);
if (t1 = t2) then
begin
result := false;
exit;
end;
FreeMem(t1);
FreeMem(t2);
end;
result := true;
end;
function TMMValidation.Validate17 : Boolean;
var
SomeArray : array of Byte;
MemoryStatus : TMemoryStatus;
const
BYTESTOALLOCATE : Cardinal = 256000000;//256 Mbyte
begin
GlobalMemoryStatus(MemoryStatus);
try
Result := True;
if MemoryStatus.dwAvailVirtual >= BYTESTOALLOCATE then
SetLength(SomeArray, BYTESTOALLOCATE);
except
Result := False;
end;
end;
//Grow dynamic array with STEPSIZE from MIN to MAX and validate that it is 16 byte aligned
function TMMValidation.Validate18 : Boolean;
var
SomeArray : array of Byte;
MemoryStatus : TMemoryStatus;
BytesToAllocate, I, StartAddress : Cardinal;
const
BYTESTOALLOCATESTEPSIZE : Cardinal = 11;
BYTESTOALLOCATEMIN : Cardinal = 16;//
BYTESTOALLOCATEMAX : Cardinal = 100000000;//100 Mbyte
begin
GlobalMemoryStatus(MemoryStatus);
try
Result := True;
BytesToAllocate := BYTESTOALLOCATEMIN;
while BytesToAllocate <= BYTESTOALLOCATEMAX do
begin
if MemoryStatus.dwAvailVirtual >= BytesToAllocate then
begin
//Allocate
SetLength(SomeArray, BytesToAllocate);
//16 byte alignment?
//Add 8 byte offset to compensate for refcount and size data
StartAddress := Cardinal(@SomeArray[8]);
if StartAddress mod 16 <> 0 then
begin
Result := False;
Exit;
end;
for I := 0 to BytesToAllocate-1 do
SomeArray[I] := 255;
//Free
SetLength(SomeArray, 0);
BytesToAllocate := BytesToAllocate * BYTESTOALLOCATESTEPSIZE;
end;
end;
except
Result := False;
end;
end;
//Allocates 100000 pointers up to BYTESTOALLOCATEMAX per pointer
//Validate 4 byte alignment, unique pointers, non overlapping blocks,
//read and write to block with 4 byte granularity, but does not write past the end.
//Does not write to the last 0-3 bytes of a block
function TMMValidation.Validate19 : Boolean;
var
I1, I2, I3, J1, J2, J3, BytesToAllocate, RunNo, IntegersToFill, BigBlockSize : Cardinal;
pMem : Pointer;
PointerArray : array[1..100000] of Pointer;
MemoryStatus : TMemoryStatus;
pInt : pInteger;
RefData, MemBlockSize : Integer;
const
BYTESTOALLOCATEMAX : Cardinal = 2500;//2.5 Kbyte per pointer max
RUNNOMAX : Cardinal = 125;
BIGBLOCKGROWRATE : Cardinal = 1024*1024;//1 MB extra per run
begin
Result := True;
try
for RunNo := 1 to RUNNOMAX do
begin
//Initialize pointer array - not really needed
for I1 := 1 to Length(PointerArray) do
PointerArray[I1] := nil;
for I2 := 1 to Length(PointerArray) do
begin
if I2 = 1 then
begin
BigBlockSize := RunNo * BIGBLOCKGROWRATE + 4; //Always allocate >= 4 byte
BytesToAllocate := BigBlockSize//Allocate one big block that grows linearily
end
else
begin
BytesToAllocate := Random(BYTESTOALLOCATEMAX-1)+4;//Always allocate >= 4 byte
end;
if BytesToAllocate < 4 then
raise Exception.Create('To small block allocated');
GlobalMemoryStatus(MemoryStatus);
if MemoryStatus.dwAvailVirtual >= BytesToAllocate then
begin
//Allocate all pointers
GetMem(pMem, BytesToAllocate);
if Integer(pMem) mod 4 <> 0 then
begin
//Pointer not 4 byte aligned
Result := False;
Exit;
end;
PointerArray[I2] := pMem;
//Fill allocated memory with unique data = Pointer
pInt := pMem;
IntegersToFill := (BytesToAllocate) div 4;
if IntegersToFill > 0 then
begin
//But write size in integers in first 4 bytes
pInt^ := IntegersToFill;
Inc(pInt);
//Does not write to all bytes in block if block size is not multiple of 4
for J1 := 1 to IntegersToFill-1 do
begin
//Write with 4 byte granularity
pInt^ := Integer(pMem);
Inc(pInt);
end;
end;
end;
end;
//No mem overwritten
for J2 := 1 to Length(PointerArray) do
begin
pInt := PointerArray[J2];
RefData := Integer(PointerArray[J2]);
MemBlockSize := pInt^;//Size in first 4 bytes
Inc(pInt);
for J3 := 1 to MemBlockSize-1 do
begin
if pInt^ <> RefData then
begin
//Memory block is overwritten
Result := False;
Exit;
end;
Inc(pInt);
end;
end;
//Free all pointers
for I3 := 1 to Length(PointerArray) do
begin
pMem := PointerArray[I3];
FreeMem(pMem);
PointerArray[I3] := nil;
end;
end;
except
Result := False;
end;
end;
function TMMValidation.Validate20 : Boolean;
var
SomeArray : array of Byte;
MemoryStatus : TMemoryStatus;
const
BYTESTOALLOCATE : Cardinal = 1024*1024*1024;//1G
begin
GlobalMemoryStatus(MemoryStatus);
try
Result := True;
if MemoryStatus.dwAvailVirtual >= BYTESTOALLOCATE then
SetLength(SomeArray, BYTESTOALLOCATE);
except
Result := False;
end;
end;
function TMMValidation.Validate21 : Boolean;
var
SomeArray : array of Byte;
MemoryStatus : TMemoryStatus;
I1, I2, I3, SmallAllocSize, BigAllocSize : Cardinal;
const
SMALLALLOCSIZEMIN : Cardinal = 1;//1 byte
SMALLALLOCSIZEMAX : Cardinal = 100;//100 byte
BIGALLOCSIZEMIN : Cardinal = 1024*1024;//1 Mbyte
BIGALLOCSIZEMAX : Cardinal = 1024*1024 + 100;//1 Mbyte + 100 b
FILLBYTE : Byte = 224;
begin
Result := True;
try
for SmallAllocSize := SMALLALLOCSIZEMIN to SMALLALLOCSIZEMAX do
for BigAllocSize := BIGALLOCSIZEMIN to BIGALLOCSIZEMAX do
begin
GlobalMemoryStatus(MemoryStatus);
if MemoryStatus.dwAvailVirtual >= BigAllocSize+SmallAllocSize then
begin
SetLength(SomeArray, SmallAllocSize);
for I1 := 0 to SmallAllocSize-1 do
SomeArray[I1] := FILLBYTE;
SetLength(SomeArray, BigAllocSize);
for I2 := 0 to SmallAllocSize-1 do
begin
if SomeArray[I2] <> FILLBYTE then
begin
Result := False;
Exit;
end;
end;
SetLength(SomeArray, SmallAllocSize);
for I3 := 0 to SmallAllocSize-1 do
begin
if SomeArray[I3] <> FILLBYTE then
begin
Result := False;
Exit;
end;
end;
end;
end;
except
Result := False;
end;
end;
//Grow dynamic array with STEPSIZE from MIN to MAX and validate that it is 4 byte aligned
function TMMValidation.Validate22 : Boolean;
var
SomeArray : array of Byte;
MemoryStatus : TMemoryStatus;
BytesToAllocate, StartAddress, I2, I3, OldBytesToAllocate : Cardinal;
const
BYTESTOALLOCATESTEPSIZE : Cardinal = 2;
BYTESTOALLOCATEMIN : Cardinal = 5;//5 byte
BYTESTOALLOCATEMAX : Cardinal = 50*1024*1024;//50 Mbyte
FILLBYTE : Byte = 255;
begin
try
Result := True;
OldBytesToAllocate := 1;
//Allocate
SetLength(SomeArray, 1);
SomeArray[0] := FILLBYTE;
BytesToAllocate := BYTESTOALLOCATEMIN;
while BytesToAllocate <= BYTESTOALLOCATEMAX do
begin
GlobalMemoryStatus(MemoryStatus);
if MemoryStatus.dwAvailVirtual >= BytesToAllocate then
begin
//Reallocate
SetLength(SomeArray, BytesToAllocate);
//Old data preserved?
for I3 := 0 to OldBytesToAllocate-1 do
begin
if SomeArray[I3] <> FILLBYTE then
begin
Result := False;
Exit;
end;
end;
//4 byte alignment?
//No need to add offset to compensate for refcount and size data
StartAddress := Cardinal(@SomeArray[0]);
if StartAddress mod 4 <> 0 then
begin
Result := False;
Exit;
end;
//Fill newly allocated space
for I2 := OldBytesToAllocate to BytesToAllocate-1 do
SomeArray[I2] := FILLBYTE;
OldBytesToAllocate := BytesToAllocate;
BytesToAllocate := BytesToAllocate * BYTESTOALLOCATESTEPSIZE;
end;
end;
//Free
SetLength(SomeArray, 0);
except
Result := False;
end;
end;
//Allocate NOOFPOINTERS starting at 1 byte size and growing with
//ALLOCGROWSTEPSIZE per pointer and then free's them all.
//Validates that all allocated memory is freed after SLEEPTIMEAFTERFREE seconds.
//NOT ACCEPTED BY COMMUNITY
{
function TMMValidation.Validate23 : Boolean;
var
MemoryStatus : TMemoryStatus;
PointerArray : array of Pointer;
I, IntitialFreeMemory, EndFreeMemory : Integer;
AllocSize : Integer;
AllocSizeFP : Double;
const
NOOFPOINTERS : Integer = 1000000;
ALLOCGROWSTEPSIZE : Double = 0.001;
SLEEPTIMEAFTERFREE : Integer = 10;//Seconds to free
SLACK : Integer = 32*1024*1024;
begin
try
Result := True;
GlobalMemoryStatus(MemoryStatus);
IntitialFreeMemory := MemoryStatus.dwAvailVirtual;
//Allocate
SetLength(PointerArray, NOOFPOINTERS);
AllocSizeFP := 1;
for I:= 0 to Length(PointerArray)-1 do
begin