-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathsmmMediumMemory.pas
2123 lines (1892 loc) · 66 KB
/
smmMediumMemory.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 smmMediumMemory;
interface
{$Include smmOptions.inc}
uses
smmTypes, smmStatistics;
type
PMediumHeader = ^TMediumHeader;
PMediumHeaderExt = ^TMediumHeaderExt;
PMediumBlockMemory = ^TMediumBlockMemory;
PMediumThreadManager = ^TMediumThreadManager;
//PMediumThreadManagerOffset = ^TMediumThreadManagerOffset;
//16 bytes, single memory item
TMediumHeader = object
{$IFDEF SCALEMM_MAGICTEST}
Magic1 : NativeInt;
Magic2 : NativeInt; //8byte aligned
{$IFDEF Align16Bytes}
{$ifndef CPUX64}
Filer1: Pointer; // 16 bytes aligned for 32 bit compiler
Filer2: Pointer;
{$endif}
{$ENDIF}
{$ENDIF}
//linked items in one block
NextMem : PMediumHeader;
PrevMem : PMediumHeader;
Size : NativeUInt;
/// must be last item of header (same negative offset from mem as TBaseMemHeader)
OwnerThread : PBaseThreadManagerOffset;
procedure ThreadFree;
procedure CheckMem(aDirection: TScanDirection = sdBoth);
end;
//40 bytes, first 16 are the same, rest are use for a free block
TMediumHeaderExt = object
{$IFDEF SCALEMM_MAGICTEST}
Magic1 : NativeInt;
Magic2 : NativeInt; //8byte aligned
{$IFDEF Align16Bytes}
{$ifndef CPUX64}
Filer1: Pointer; // 16 bytes aligned for 32 bit compiler
Filer2: Pointer;
{$endif}
{$ENDIF}
{$ENDIF}
//linked items in one block
NextMem : PMediumHeader;
PrevMem : PMediumHeader;
Size : NativeUInt;
OwnerThread : PBaseThreadManagerOffset;
//Extra data of free item:---------------------------------
//linked list of free mem, freed in other thread
NextThreadFree: PMediumHeaderExt;
PrevThreadFree: PMediumHeaderExt;
//linked list of free mem of the same size of a thread
NextFreeItem : PMediumHeaderExt;
PrevFreeItem : PMediumHeaderExt;
//simple storage of size calculation:
BlockMask : NativeUInt;
ArrayPosition: NativeUInt;
procedure CheckMem(aDirection: TScanDirection = sdBoth);
function GetOwnerBlock: PMediumBlockMemory;
end;
//Block of 1Mb
TMediumBlockMemory = object //16bytes
/// Thread owner, must be first item of block (same offset as PBaseBlockMemory)
OwnerManager : PMediumThreadManager;
Size : NativeUInt;
//linked list of blocks of a thread
NextBlock,
PreviousBlock: PMediumBlockMemory;
procedure CheckMem;
procedure ChangeOwnerThread(aOwnerThread: PMediumThreadManager);
function GetFirstMem: PMediumHeader;
function GetMaxFreeSizeInBlock(): NativeUInt;
end;
TMediumThreadManagerOffset = packed
{$if CompilerVersion >= 18} //Delphi 2007 and higher?
record {$ELSE} object {$ifend}
public
//SizeType : TSizeType;
Filler1, Filler2, Filler3: Byte; //offset of 1 to distinguish of being medium block
OwnerThread: PBaseThreadManager;
FFirstBlock : PMediumBlockMemory;
end;
//Medium memory manager of a thread
TMediumThreadManager = object
public
SizeType : TSizeType;
OwnerThread : PBaseThreadManager;
FFirstBlock : PMediumBlockMemory;
private
FFreeMem : array[0..16] of PMediumHeaderExt;
FFreeMask : NativeUInt; //word, 16 bit, 65535
function AllocBlock(aMinResultSize: NativeUInt): PMediumHeaderExt;
procedure FreeBlock(aBlock: PMediumBlockMemory);
procedure PutMemToFree(aHeader: PMediumHeader; aSize: NativeUInt);
procedure FreeRemainder(aHeader: PMediumHeader; aSize: NativeUInt);
public
procedure Init;
procedure Reset;
procedure ReleaseAllFreeMem;
procedure DumpToFile(aFile: THandle; aTotalStats, aSingleStats: PThreadMemManagerStats);
procedure CheckMem(aMemory: Pointer = nil);
procedure CheckMemArray;
procedure CheckSingleSizeFreeMemList(const aMem: PMediumHeaderExt; aCheckParam: Boolean = True);
function ScanBlockForFreeItems(aBlock: PMediumBlockMemory; aMinResultSize: NativeUInt; aOnlyCheckSize: boolean): PMediumHeaderExt;
//function GetFilledMem(aSize: NativeUInt) : Pointer;
function GetMem(aSize: NativeUInt) : Pointer;
function FreeMem(aMemory: Pointer): NativeInt;
//function FreeMemNoCheck(aMemory: Pointer): NativeInt;
function FreeMemOnlyMarked(aMemory: Pointer): NativeInt;
function ReallocMem(aMemory: Pointer; aSize: NativeUInt): Pointer;
end;
const
C_MAX_MEDIUMMEM_SIZE = (1 shl 16 shl 4) - //1Mb
//(1 shl 16 shl 3) - //100kb
SizeOf(TMediumBlockMemory) -
SizeOf(TMediumHeader) - //start
SizeOf(TMediumHeader) - //header of each block
SizeOf(TMediumHeader); //end
//C_MEDIUMBLOCK_SIZE = (1 shl 16 shl 4); //1Mb
implementation
uses
smmGlobal, ScaleMM2, smmFunctions,
smmSmallMemory,
smmLargeMemory{needed for inline};
////////////////////////////////////////////////////////////////////////////////
{ TMediumThreadManager }
function TMediumThreadManager.AllocBlock(aMinResultSize: NativeUInt): PMediumHeaderExt;
var
iAllocSize: NativeUInt;
pheader: PMediumHeaderExt;
pblock, globalblock: PMediumBlockMemory;
pthreadoffset: PBaseThreadManagerOffset;
begin
pheader := nil;
globalblock := GlobalManager.GetMediumBlockMemory(@Self, aMinResultSize);
if globalblock <> nil then
repeat
pheader := ScanBlockForFreeItems(globalblock, aMinResultSize, False {process free mem});
//no mem of minimum size? fetch another block
if pheader = nil then
begin
//add block to linked list of thread blocks (replace first)
if FFirstBlock <> nil then
FFirstBlock.PreviousBlock := globalblock;
globalblock.NextBlock := FFirstBlock;
FFirstBlock := globalblock;
globalblock.PreviousBlock := nil;
globalblock := GlobalManager.GetMediumBlockMemory(@Self, aMinResultSize);
end
else
Assert(pheader.Size >= aMinResultSize);
until (pheader <> nil) or (globalblock = nil);
pblock := globalblock;
//no cached block? then alloc from large mem
if pblock = nil then
begin
iAllocSize := (1 shl 16 shl 4) {1mb}; //exact 1mb, so no gap between 2 virtual allocs
//alloc
pblock := PThreadMemManager(Self.OwnerThread).FLargeMemManager.GetMem(iAllocSize);
pblock := PMediumBlockMemory( NativeUInt(pblock));
pblock.Size := iAllocSize;
pblock.OwnerManager := @Self;
//first item
pheader := PMediumHeaderExt( NativeUInt(pblock) +
SizeOf(TMediumBlockMemory)
);
pthreadoffset := PBaseThreadManagerOffset(NativeUInt(Self.OwnerThread) or 1);
pheader.OwnerThread := pthreadoffset;
pheader.Size := iAllocSize -
SizeOf(TMediumBlockMemory) - //block
SizeOf(TMediumHeader) - //start
SizeOf(TMediumHeader) - //header of each mem
SizeOf(TMediumHeader); //end(!)
pheader.NextMem := PMediumHeader( NativeUInt(pheader) + pheader.Size );
//reset end
pheader.NextMem.Size := 0;
pheader.NextMem.OwnerThread := pthreadoffset;
pheader.NextMem.NextMem := nil;
pheader.NextMem.PrevMem := PMediumHeader(pheader);
{$IFDEF SCALEMM_MAGICTEST}
pheader.NextMem.Magic1 := 0;
pheader.Magic1 := 0;
pheader.Magic2 := 201;
{$ENDIF}
//mark as free
Assert(pheader.Size and 1 = 0);
pheader.Size := pheader.Size or 1; //8byte aligned, lowest bit = free
//init
pheader.NextFreeItem := nil;
pheader.PrevFreeItem := nil;
pheader.BlockMask := 1 shl 16;
pheader.ArrayPosition := 16;
//max size is available now
FFreeMask := FFreeMask or (1 shl 16);
FFreeMem[16] := pheader;
end;
assert(pheader <> nil);
//linked list of thread blocks (replace first)
if FFirstBlock <> nil then
FFirstBlock.PreviousBlock := pblock;
pblock.NextBlock := FFirstBlock;
FFirstBlock := pblock;
pblock.PreviousBlock := nil;
{$IFDEF SCALEMM_DEBUG}
pblock.CheckMem;
CheckMem;
{$ENDIF}
Result := pheader;
end;
procedure TMediumThreadManager.CheckMem(aMemory: Pointer = nil);
var
block: PMediumBlockMemory;
header: PMediumHeader;
headerext: PMediumHeaderExt;
i: Integer;
begin
if aMemory <> nil then
begin
header := PMediumHeader(NativeUInt(aMemory) - SizeOf(TMediumHeader));
header.CheckMem(sdNone{sdBoth});
end
else //all
begin
block := Self.FFirstBlock;
while block <> nil do
begin
block.CheckMem;
block := block.NextBlock;
end;
//check free mem
for i := Low(FFreeMem) to High(FFreeMem) do
begin
headerext := FFreeMem[i];
//check mask
if Self.OwnerThread.FThreadId > 1 then
begin
if headerext = nil then
Assert(FFreeMask and (1 shl i) = 0)
else
Assert(FFreeMask and (1 shl i) <> 0);
//check linked free items
while headerext <> nil do
begin
{$IFDEF SCALEMM_MAGICTEST}
Assert(headerext.Magic1 = 0); //must be free
{$ENDIF}
headerext.CheckMem(sdNone);
headerext := headerext.NextFreeItem;
end;
end;
end;
end;
end;
procedure TMediumThreadManager.CheckMemArray;
var
headerext: PMediumHeaderExt;
i: Integer;
begin
//check free mem
for i := Low(FFreeMem) to High(FFreeMem) do
begin
headerext := FFreeMem[i];
//check mask
if Self.OwnerThread.FThreadId > 1 then
begin
if headerext = nil then
Assert(FFreeMask and (1 shl i) = 0)
else
Assert(FFreeMask and (1 shl i) <> 0);
end;
end;
end;
procedure TMediumThreadManager.CheckSingleSizeFreeMemList(
const aMem: PMediumHeaderExt; aCheckParam: Boolean = True);
var
iMsb: NativeUInt;
pcurrent, pnext: PMediumHeaderExt;
begin
if aCheckParam then
begin
{$IFOPT C+} //assertions
if aMem.Size < C_MAX_MEDIUMMEM_SIZE then
iMsb := BitScanLast( //get highest bit
aMem.Size shr 4) //div 16 so 1mb fits in 16bit
else
iMsb := 16;
{$ELSE}
iMsb := 0;
if iMsb > 0 then ; //dummy to remove warning (unused var)
{$ENDIF}
if aMem.BlockMask <> 0 then
begin
Assert(aMem.ArrayPosition = iMsb);
Assert(aMem.BlockMask = (1 shl iMsb));
if aMem.PrevFreeItem = nil then //first
Assert(FFreeMem[aMem.ArrayPosition] = aMem); //must be first array item!
end;
end
else
begin
if aMem.BlockMask <> 0 then
begin
Assert(aMem.ArrayPosition <> 0);
Assert(aMem.BlockMask = (1 shl aMem.ArrayPosition));
end
end;
if aMem.ArrayPosition = 0 then Exit;
Assert(aMem.ArrayPosition <> 0);
pcurrent := FFreeMem[aMem.ArrayPosition];
if pcurrent <> nil then
begin
Assert(pcurrent.PrevFreeItem = nil); //first has no previous!
if aMem.BlockMask <> 0 then
Assert(pcurrent.BlockMask = aMem.BlockMask)
else
Assert(pcurrent.BlockMask = (1 shl aMem.ArrayPosition) );
end;
pnext := pcurrent; //pcurrent.NextFreeItem;
while pnext <> nil do
begin
Assert(pnext.ArrayPosition = aMem.ArrayPosition);
{$IFOPT C+} //assertions
if pnext.Size < C_MAX_MEDIUMMEM_SIZE then
iMsb := BitScanLast( //get highest bit
pnext.Size shr 4) //div 16 so 1mb fits in 16bit
else
iMsb := 16;
Assert(pnext.ArrayPosition = iMsb);
{$ENDIF}
if pnext.BlockMask <> 0 then
begin
Assert(pnext.BlockMask = pcurrent.BlockMask);
if pnext.PrevFreeItem = nil then //first
Assert(FFreeMem[pnext.ArrayPosition] = pnext) //must be first array item!
else
Assert(pnext.PrevFreeItem.NextFreeItem = pnext);
if (pnext.NextFreeItem <> nil) then
if pnext.NextFreeItem.BlockMask <> 0 then
Assert(pnext.NextFreeItem.PrevFreeItem = pnext);
end;
if (pnext.OwnerThread <> nil) and (pnext.OwnerThread.FThreadId > 1) then
Assert(pnext.OwnerThread.FThreadId = GetCurrentThreadID);
pnext := pnext.NextFreeItem;
end;
end;
procedure TMediumThreadManager.DumpToFile(aFile: THandle; aTotalStats,
aSingleStats: PThreadMemManagerStats);
var
block: PMediumBlockMemory;
mem: PMediumHeader;
iArrayPos, iCountFree, iCountAlloc, iSizeFree, iSizeAlloc: Integer;
begin
WriteToFile(aFile, 'TMediumThreadManager'#13#10);
block := Self.FFirstBlock;
while block <> nil do
begin
aTotalStats.MediumMemoryStats.BlockSize := block.Size;
aSingleStats.MediumMemoryStats.BlockSize := block.Size;
Inc(aTotalStats.MediumMemoryStats.BlockCount);
Inc(aSingleStats.MediumMemoryStats.BlockCount);
WriteToFile(aFile, '- MediumBlock @ ');
WriteNativeUIntToHexBuf(aFile, NativeUInt(block));
WriteToFile(aFile, ', size: ');
WriteNativeUIntToStrBuf(aFile, block.Size);
WriteToFile(aFile, #13#10);
iCountFree := 0;
iCountAlloc := 0;
iSizeFree := 0;
iSizeAlloc := 0;
mem := block.GetFirstMem;
//process all mem
while mem <> nil do
begin
if mem.Size <= 0 then
begin
mem := mem.NextMem;
Continue;
end;
if mem.Size < C_MAX_MEDIUMMEM_SIZE then //smaller than 1mb?
iArrayPos := BitScanLast( //get highest bit
mem.Size shr 4) //div 16 so 1mb fits in 16bit
else
iArrayPos := 16;
if aTotalStats.MediumMemoryStats.MemBySize[iArrayPos].Size = 0 then
aTotalStats.MediumMemoryStats.MemBySize[iArrayPos].Size := (1 shl iArrayPos) shl 4;
if aSingleStats.MediumMemoryStats.MemBySize[iArrayPos].Size = 0 then
aSingleStats.MediumMemoryStats.MemBySize[iArrayPos].Size := (1 shl iArrayPos) shl 4;
WriteToFile(aFile, ' - Medium item @ ');
WriteNativeUIntToHexBuf(aFile, NativeUInt(mem));
//free?
if (mem.Size and 1 <> 0) then
begin
Inc(aTotalStats.MediumMemoryStats.TotalFree, mem.Size);
Inc(aSingleStats.MediumMemoryStats.TotalFree, mem.Size);
Inc(aTotalStats.MediumMemoryStats.MemBySize[iArrayPos].Free);
Inc(aSingleStats.MediumMemoryStats.MemBySize[iArrayPos].Free);
WriteToFile(aFile, ', FREE ');
Inc(iCountFree);
Inc(iSizeFree, mem.Size);
end
else
begin
Inc(aTotalStats.MediumMemoryStats.TotalUsed, mem.Size);
Inc(aSingleStats.MediumMemoryStats.TotalUsed, mem.Size);
Inc(aTotalStats.MediumMemoryStats.MemBySize[iArrayPos].Used);
Inc(aSingleStats.MediumMemoryStats.MemBySize[iArrayPos].Used);
WriteToFile(aFile, ', Used ');
Inc(iCountAlloc);
Inc(iSizeAlloc, mem.Size);
end;
WriteToFile(aFile, ', size: ');
WriteNativeUIntToStrBuf(aFile, mem.Size);
WriteToFile(aFile, #13#10);
mem := mem.NextMem;
end;
WriteToFile(aFile, '- Used count: ');
WriteNativeUIntToStrBuf(aFile, iCountAlloc);
WriteToFile(aFile, ', used size: ');
WriteNativeUIntToStrBuf(aFile, iSizeAlloc);
WriteToFile(aFile, ' - free count: ');
WriteNativeUIntToStrBuf(aFile, iCountFree);
WriteToFile(aFile, ', free size: ');
WriteNativeUIntToStrBuf(aFile, iSizeFree);
WriteToFile(aFile, ' - total count: ');
WriteNativeUIntToStrBuf(aFile, iCountFree+iCountAlloc);
WriteToFile(aFile, ', total size: ');
WriteNativeUIntToStrBuf(aFile, iSizeFree+iSizeAlloc);
WriteToFile(aFile, #13#10);
block := block.NextBlock;
end;
end;
procedure TMediumThreadManager.FreeBlock(aBlock: PMediumBlockMemory);
begin
{$IFDEF SCALEMM_DEBUG}
aBlock.CheckMem;
Assert(Self.OwnerThread.FThreadId > 1);
{$ENDIF}
//remove from linked list
if FFirstBlock = aBlock then
begin
FFirstBlock := aBlock.NextBlock;
if FFirstBlock <> nil then
FFirstBlock.PreviousBlock := nil;
end
else
begin
if aBlock.NextBlock <> nil then
aBlock.NextBlock.PreviousBlock := aBlock.PreviousBlock;
if aBlock.PreviousBlock <> nil then
aBlock.PreviousBlock.NextBlock := aBlock.NextBlock;
end;
aBlock.NextBlock := nil;
aBlock.PreviousBlock := nil;
GlobalManager.FreeMediumBlockMemory(aBlock);
end;
function TMediumThreadManager.FreeMem(aMemory: Pointer): NativeInt;
var
header: PMediumHeader;
begin
header := PMediumHeader(NativeUInt(aMemory) - SizeOf(TMediumHeader));
{$IFDEF SCALEMM_MAGICTEST}
Assert(header.Magic1 = 123456789); //must be in use!
{$ENDIF}
Assert(header.Size and 1 = 0); //double free check
if (Self.OwnerThread.FThreadId > 1) and
not Self.OwnerThread.FThreadTerminated then
begin
{$IFDEF SCALEMM_DEBUG}
header.CheckMem(sdNone);
{$ENDIF}
PutMemToFree(header, header.Size)
end
else
begin
FreeMemOnlyMarked(aMemory);
end;
Result := 0;
{$IFDEF SCALEMM_DEBUG}
Self.CheckMem(nil);
{$ENDIF}
end;
(*
function TMediumThreadManager.FreeMemNoCheck(aMemory: Pointer): NativeInt;
var
header: PMediumHeader;
begin
header := PMediumHeader(NativeUInt(aMemory) - SizeOf(TMediumHeader));
{$IFDEF SCALEMM_MAGICTEST}
Assert(header.Magic1 = 123456789); //must be in use!
Assert(Self.OwnerThread.FThreadId > 1);
{$ENDIF}
//other thread?
if header.OwnerThread.FThreadId <> Self.OwnerThread.FThreadId then
begin
Result := PThreadMemManager(OwnerThread).FreeMem(aMemory);
//Scale_FreeMem(aMemory);
Exit;
end;
Assert(header.OwnerThread.FThreadId = GetCurrentThreadId);
{$IFDEF SCALEMM_DEBUG}
header.CheckMem(sdNone);
{$ENDIF}
Assert(header.Size and 1 = 0);
PutMemToFree(header, header.Size);
Result := 0;
// {$IFDEF SCALEMM_DEBUG}
// Self.CheckMem(nil);
// {$ENDIF}
end;
*)
function TMediumThreadManager.FreeMemOnlyMarked(aMemory: Pointer): NativeInt;
var
header: PMediumHeaderExt;
begin
header := PMediumHeaderExt(NativeUInt(aMemory) - SizeOf(TMediumHeader));
{$IFDEF SCALEMM_MAGICTEST}
Assert(header.Magic1 = 123456789); //must be in use!
{$ENDIF}
with header^ do
begin
PrevThreadFree := nil;
NextThreadFree := nil;
NextFreeItem := nil;
PrevFreeItem := nil;
BlockMask := 0; //skip checks/lookups etc
//ArrayPosition := 0;
if header.Size < C_MAX_MEDIUMMEM_SIZE then
ArrayPosition := BitScanLast( //get highest bit
header.Size shr 4) //div 16 so 1mb fits in 16bit
else
ArrayPosition := 16;
//set higest bit (mark as free)
Size := Size or 1;
{$IFDEF SCALEMM_MAGICTEST}
header.Magic1 := 0;//free
header.Magic2 := -480;
{$ENDIF}
end;
Result := 0;
end;
procedure TMediumThreadManager.FreeRemainder(aHeader: PMediumHeader;
aSize: NativeUInt);
var
pnext,
pheaderremainder: PMediumHeaderExt;
newsize: NativeUInt;
{$IFDEF SCALEMM_FILLFREEMEM}
temppointer: Pointer;
{$ENDIF}
iRemainder: NativeUInt;
begin
{$ifdef SCALEMM_DEBUG}
Assert(Self.OwnerThread.FThreadId > 1);
if (OwnerThread <> nil) and (OwnerThread.FThreadId > 1) then
Assert(OwnerThread.FThreadId = GetCurrentThreadId);
Assert(aHeader.OwnerThread.FThreadId = Self.OwnerThread.FThreadId);
{$ENDIF}
newsize := aSize;
{$IFDEF SCALEMM_FILLFREEMEM}
//reset old mem
temppointer := Pointer(NativeUInt(aHeader) + SizeOf(TMediumHeader)); //do no reset header
FillChar( temppointer^, aSize - SizeOf(TMediumHeader), $80);
{$ENDIF}
//create remainder
pheaderremainder := PMediumHeaderExt(aHeader);
//next
pnext := PMediumHeaderExt(NativeUInt(pheaderremainder) + newsize);
{$ifdef SCALEMM_MAGICTEST}
pheaderremainder.Magic1 := 0;//free
{$ENDIF}
//do a forward scan to make one block, is lowest bit set? then next is a free block
if (pnext.Size and 1 <> 0) then
begin
{$IFDEF SCALEMM_MAGICTEST}
Assert(pnext.Magic1 = 0); //must be free
pnext.Magic2 := 495;
{$ENDIF}
newsize := newsize + (pnext.Size and -2); //remove lowest bit
pheaderremainder.Size := newsize or 1;
if newsize < C_MAX_MEDIUMMEM_SIZE then //smaller than 1mb?
begin
iRemainder := BitScanLast( //get highest bit
newsize shr 4); //div 16 so 1mb fits in 16bit
//same index? then reposition
if (NativeUint(iRemainder) = pnext.ArrayPosition) and
(pnext.BlockMask <> 0) then
begin
pheaderremainder.BlockMask := pnext.BlockMask;
pheaderremainder.ArrayPosition := pnext.ArrayPosition;
pheaderremainder.NextFreeItem := pnext.NextFreeItem;
if pnext.NextFreeItem <> nil then
pnext.NextFreeItem.PrevFreeItem := pheaderremainder;
pheaderremainder.PrevFreeItem := pnext.PrevFreeItem;
Assert( (pheaderremainder.PrevFreeItem=nil) or (pheaderremainder.PrevFreeItem.OwnerThread.FThreadId = GetCurrentThreadId));
Assert( (pheaderremainder.NextFreeItem=nil) or (pheaderremainder.NextFreeItem.OwnerThread.FThreadId = GetCurrentThreadId));
if pnext.PrevFreeItem = nil then {first?}
begin
Assert(FFreeMem[iRemainder] = pnext);
FFreeMem[iRemainder] := pheaderremainder;
{$IFDEF SCALEMM_MAGICTEST}
Assert(pheaderremainder.Magic1 = 0); //must be free
{$ENDIF}
//pheaderremainder.PrevFreeItem := nil;
//pheaderremainder.NextMem := pnext.NextMem; done below
//pheaderremainder.PrevMem := pnext.PrevMem; done by caller
//pheaderremainder.Size := pnext.Size; done above
//pheaderremainder.OwnerBlock := pnext.OwnerBlock; done by caller
end
else
pnext.PrevFreeItem.NextFreeItem := pheaderremainder;
Assert( (pnext.PrevFreeItem=nil) or (pnext.PrevFreeItem.OwnerThread.FThreadId = GetCurrentThreadId));
Assert( (pnext.NextFreeItem=nil) or (pnext.NextFreeItem.OwnerThread.FThreadId = GetCurrentThreadId));
{$IFDEF SCALEMM_DEBUG}
CheckSingleSizeFreeMemList(pnext);
{$ENDIF}
{$IFDEF SCALEMM_MAGICTEST}
Assert(pnext.Magic1 = 0); //must be free
pnext.Magic2 := 609;
{$ENDIF}
pnext := PMediumHeaderExt(pnext.NextMem);
pnext.PrevMem := PMediumHeader(pheaderremainder);
pheaderremainder.NextMem := PMediumHeader(pnext);
{$IFDEF SCALEMM_MAGICTEST}
//Assert(pnext.Magic1 = 0); //must be free
pnext.Magic2 := 616;
{$ENDIF}
{$ifdef SCALEMM_DEBUG}
pheaderremainder.CheckMem(sdNone);
Self.CheckMem(nil);
{$ENDIF}
Exit;
end;
end;
//remove old size
with pnext^ do
if BlockMask <> 0 then
begin
{$IFDEF SCALEMM_MAGICTEST}
Assert( (NextFreeItem = nil) or (NextFreeItem.Magic1 = 0) ); //must be free
Assert(pnext.OwnerThread.FThreadId = GetCurrentThreadId);
{$ENDIF}
if {next.}PrevFreeItem = nil then {first?}
begin
Assert(FFreeMem[{next.}ArrayPosition] = pnext);
//replace first item with next
FFreeMem[{next.}ArrayPosition] := {next.}NextFreeItem;
if {next.}NextFreeItem = nil then
FFreeMask := FFreeMask xor {next.}BlockMask
else
{next.}NextFreeItem.PrevFreeItem := nil;
end
else
begin
//remove from linked list
{next.}PrevFreeItem.NextFreeItem := {next.}NextFreeItem;
if {next.}NextFreeItem <> nil then
{next.}NextFreeItem.PrevFreeItem := {next.}PrevFreeItem;
Assert( (pnext.PrevFreeItem=nil) or (pnext.PrevFreeItem.OwnerThread.FThreadId = GetCurrentThreadId));
Assert( (pnext.NextFreeItem=nil) or (pnext.NextFreeItem.OwnerThread.FThreadId = GetCurrentThreadId));
end;
{$ifdef SCALEMM_DEBUG}
CheckSingleSizeFreeMemList(pnext, False);
{$ENDIF}
end;
//reset
{$IFDEF SCALEMM_MAGICTEST}
pnext.Magic1 := -1;
pnext.Size := MaxInt;
pnext.Magic2 := 667;
{$ENDIF}
pnext := PMediumHeaderExt(pnext.NextMem);
pnext.PrevMem := PMediumHeader(pheaderremainder);
pheaderremainder.NextMem := PMediumHeader(pnext);
end
else
begin
pnext.PrevMem := PMediumHeader(pheaderremainder);
with pheaderremainder^ do
begin
{pheaderremainder.}NextMem := PMediumHeader(pnext);
{pheaderremainder.}Size := newsize or 1;
{$IFDEF SCALEMM_MAGICTEST}
Magic2 := 594;
{$ENDIF}
end;
end;
{$ifdef SCALEMM_DEBUG}
Assert(newsize >= SizeOf(TMediumHeaderExt));
Assert(newsize <= C_MAX_MEDIUMMEM_SIZE);
{$ENDIF}
{$IFDEF SCALEMM_MAGICTEST}
pheaderremainder.Magic1 := 0;//free
{$ENDIF}
if newsize < C_MAX_MEDIUMMEM_SIZE then //smaller than 1mb?
begin
iRemainder := BitScanLast( //get highest bit
newsize shr 4); //div 16 so 1mb fits in 16bit
end
else
begin
iRemainder := 16;
if (pheaderremainder.NextMem.NextMem = nil) and //end?
(pheaderremainder.PrevMem = nil) then //begin? -> fully free
//block complete free: release mem back to Windows? {or global manager}
//we keep one block in cache
if FFreeMem[iRemainder] <> nil then
begin
pheaderremainder.ArrayPosition := iRemainder;
//FreeBlock(pheaderremainder.OwnerBlock);
FreeBlock( PMediumBlockMemory(nativeuint(pheaderremainder) - SizeOf(TMediumBlockMemory)) );
{$IFDEF SCALEMM_DEBUG}
CheckMem();
{$ENDIF}
Exit;
end;
end;
//set mask
FFreeMask := FFreeMask or (1 shl iRemainder);
//get first
pnext := FFreeMem[iRemainder];
//replace first
FFreeMem[iRemainder] := pheaderremainder;
//set previous of the next
if pnext <> nil then
begin
pnext.PrevFreeItem := pheaderremainder;
{$IFDEF SCALEMM_MAGICTEST}
assert(pnext.Magic1 = 0); //must be free
{$ENDIF}
end;
with pheaderremainder^ do
begin
{pheaderremainder.}NextFreeItem := pnext;
{pheaderremainder.}ArrayPosition := iRemainder;
{pheaderremainder.}BlockMask := (1 shl iRemainder);
{pheaderremainder.}PrevFreeItem := nil; //we are the first
end;
Assert( (pheaderremainder.NextFreeItem=nil) or (pheaderremainder.NextFreeItem.OwnerThread.FThreadId = GetCurrentThreadId));
{$ifdef SCALEMM_DEBUG}
CheckMemArray;
CheckSingleSizeFreeMemList(pheaderremainder);
pheaderremainder.CheckMem(sdNone);
CheckMem();
{$ENDIF}
end;
const
{The granularity of medium blocks. Newly allocated medium blocks are
a multiple of this size plus MediumBlockSizeOffset, to avoid cache line
conflicts}
//MediumBlockGranularity = 256;
C_MediumBlockSizeOffset = 48;
(*
function TMediumThreadManager.GetFilledMem(aSize: NativeUInt): Pointer;
var
header: PMediumHeader;
begin
Result := GetMem(aSize);
header := PMediumHeader(NativeUInt(Result) - SizeOf(TMediumHeader));
FillChar(Result^, header.Size - SizeOf(TMediumHeader), 80);
end;
*)
function TMediumThreadManager.GetMem(aSize: NativeUInt): Pointer;
var
iWordRemainderSize: NativeUInt;
iMSB: NativeUInt;
iFreeMemIndex,
iRemainder: NativeUInt;
remaindersize, allocsize,
tempmask: NativeUInt;
pheader : PMediumHeaderExt;
pnext,
pheaderremainder: PMediumHeaderExt;
loopcount: Integer;
begin
{$ifdef SCALEMM_DEBUG}
Assert(Self.OwnerThread.FThreadId > 1);
Assert(Self.OwnerThread.FThreadId = GetCurrentThreadID);
Result := nil;
Self.CheckMem(nil);
try
{$ENDIF}
allocsize := ( aSize +
SizeOf(TMediumHeader) + //alloc extra for header
//8 ); //8byte aligned: add 8 and remove lowest bits (later)
C_MediumBlockSizeOffset );
if allocsize < SizeOf(TMediumHeaderExt) then
allocsize := SizeOf(TMediumHeaderExt);
if allocsize > C_MAX_MEDIUMMEM_SIZE then
allocsize := C_MAX_MEDIUMMEM_SIZE;
Assert(allocsize <= C_MAX_MEDIUMMEM_SIZE);
{$IFDEF Align16Bytes}
allocsize := allocsize and -16;
{$ELSE}
allocsize := allocsize and -8; //8byte aligned: we've add 8 before and remove lowest bits now
{$ENDIF}
iMSB := BitScanLast( //get highest bit
allocsize shr 4); //div 16 so 1mb fits in 16bit
Assert(allocsize >= aSize);
iFreeMemIndex := 0;
//first without +1 and check if size is OK (otherwise alloc of same size after alloc + free will fail)
pheader := FFreeMem[iMSB];
if (pheader <> nil) then
begin
loopcount := 0;
//check all sizes
repeat
if (pheader.Size >= allocsize) then
begin
iFreeMemIndex := iMSB;
Break;
end;
inc(loopcount);
if loopcount > 100 then //no endless scan, can make it slower and slower...
begin
pheader := nil;
Break;
end;
pheader := pheader.NextFreeItem;
until pheader = nil;
end;
if (pheader = nil) then
begin
inc(iMSB); //+1 for max size
tempmask := FFreeMask shr iMSB shl iMSB; //reset lowest bits (shr + shl)
if tempmask > 0 then
//get available mem
begin
iFreeMemIndex := BitScanFirst(tempmask); //get lowest bit (smallest size)
//Assert(iFreeMemIndex >= 0);
Assert(iFreeMemIndex <= High(FFreeMem));
pheader := FFreeMem[iFreeMemIndex];
Assert(pheader <> nil);
end;
//alloc new mem (for biggest block)
if pheader = nil then //extra check
begin
iFreeMemIndex := 16;
pheader := AllocBlock(allocsize);
end;
end;
{$IFDEF SCALEMM_DEBUG}
Assert(pheader <> nil);
Assert(pheader.Size >= allocsize);
Assert( pheader.Size and 1 <> 0); //lowest bit (free mark)
pheader.CheckMem(sdNone);
{$ENDIF}
{$IFDEF SCALEMM_MAGICTEST}
Assert(pheader.Magic1 = 0); //not in use!
pheader.Magic1 := 123456789; //mark in use
Assert(pheader.OwnerThread.FThreadId = GetCurrentThreadId);
{$ENDIF}
//remainder
remaindersize := (pheader.Size and -2) - allocsize;
//if remaindersize > SizeOf(TMediumHeaderExt) then
//less then "small" mem? then do not free remaining (will never be used)
if remaindersize > C_MAX_SMALLMEM_SIZE then
begin
iWordRemainderSize := remaindersize shr 4; //div 16 so 1mb fits in 16bit
iRemainder := BitScanLast(iWordRemainderSize); //get highest bit
end
else
begin
allocsize := allocsize + remaindersize; //use all remaining mem too
remaindersize := 0;
iRemainder := MaxInt;
end;
{$IFDEF SCALEMM_DEBUG}
assert(allocsize >= aSize);
{$ENDIF}
//same block left?
if iFreeMemIndex = iRemainder then
begin
Assert(pheader.PrevFreeItem = nil);
pheaderremainder := PMediumHeaderExt(NativeUInt(pheader) + allocsize);
//copy header
with pheaderremainder^ do
begin
{pheaderremainder.}OwnerThread := pheader.OwnerThread;
{pheaderremainder.}Size := remaindersize or 1;
{$IFDEF SCALEMM_MAGICTEST}
pheaderremainder.Magic1 := 0; //mark as free
pheaderremainder.Magic2 := 796;
{$ENDIF}