forked from adaloveless/commonx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigBrainUltra2020.pas
More file actions
4536 lines (3721 loc) · 113 KB
/
BigBrainUltra2020.pas
File metadata and controls
4536 lines (3721 loc) · 113 KB
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
{$IFNDEF DISABLE_MASTER_KEYWORDS}
unit BigBrainUltra2020;
{x$INLINE OFF}
interface
{$ENDIF}
{$DEFINE FIB}
{$IFNDEF DISABLE_INTERFACE}
{$DEFINE SIMPLEHEAPS}
{$IFDEF BBDEBUG}
{$D+}
{$ENDIF}
{$D-}
var
bb_initialized: boolean = false;
type
{$IFDEF USE_LARGER_BOOLEANS}
BBBool = LongBool;
{$ELSE}
BBBool = boolean;
{$ENDIF}
type
DWORD = cardinal;
BOOL = LONGBOOL;
type
{$IFDEF CPUx86}
SIZE_T = cardinal;
{$else}
SIZE_T = uint64;
{$Endif}
THeapObject=class
public
class function NewInstance:TObject; override;
procedure FreeInstance; override;
end;
{$IFNDEF DELPHI64}
NativeInt = integer;
NativeUInt = cardinal;
PNativeInt = ^integer;
PNativeUInt = ^cardinal;
{$ENDIF}
{$IFDEF USE_SPINLOCK}
type
PLockMap = ^TLockMap;
TLockMap = record
Lock: Cardinal;
LockCount: Cardinal;
end;
const
PageSize = 65536;
MaxLocks = PageSize div SizeOf(TLockMap);
TSLOwnerOffset = SizeOf(TLockMap);
TSLNextOffset = TSLOwnerOffset + SizeOf(Word);
{$IFDEF CPUX64}
kernel64 = 'kernel32.dll';
{$ELSE}
kernel32 = 'kernel32.dll';
{$ENDIF}
type
TSpinLock = class(THeapObject)
protected
FNoSleepCount: Cardinal;
FOwner: PLockMap;
FSleepAmount: Cardinal;
procedure InitLockMap; {$IFNDEF NOINLINE}inline;{$ENDIF}
procedure InitNoSleepCount(const NoSleepCount: Cardinal); {$IFNDEF NOINLINE}inline;{$ENDIF}
procedure ReleaseLockMap; {$IFNDEF NOINLINE}inline;{$ENDIF}
public
constructor Create(const SleepAmount: Cardinal = 0;
const NoSleepCount: Cardinal = 0); virtual;
destructor Destroy; override;
procedure Lock;{$IFNDEF ASM}{$IFNDEF NOINLINE}inline;{$ENDIF}{$ENDIF}
function Trylock(TryCount: Cardinal): BBBool;{$IFNDEF ASM}{$IFNDEF NOINLINE}inline;{$ENDIF}{$ENDIF}
procedure Unlock;{$IFNDEF ASM}{$IFNDEF NOINLINE}inline;{$ENDIF}{$ENDIF}
end;
TSpinIndex = 0..MaxLocks-1;
function ProcessIdToSessionId(const dwProcessId: DWORD; out pSessionId: DWORD): BOOL; stdcall; external kernel32;
function GetProcessAffinityMask(hProcess: THandle;
var lpProcessAffinityMask, lpSystemAffinityMask: DWORD): BOOL; stdcall;
{$EXTERNALSYM GetProcessAffinityMask}
function GetProcessAffinityMask; external kernel32 name 'GetProcessAffinityMask';
//function GetCurrentProcess: THandle; stdcall;
//{$EXTERNALSYM GetCurrentProcess}
//function GetCurrentProcess; external kernel32 name 'GetCurrentProcess';
{$ENDIF}
//uses CLX_Windows, Libc;
//----------------------------KERNEL--------------------------------------------
const
SPIN_COUNT = 0;
OS_BLOCK_HOLD_TIME = 8000;
{$IFNDEF XE2}
MAXSIZEINDEX = 63;
{$ELSE}
{$IFDEF CPUX86}
MAXSIZEINDEX = 63;
{$ELSE}
MAXSIZEINDEX = 63;
{$ENDIF}
{$ENDIF}
{$IFDEF USE_MULTIPLE_THREAD_BLOCK_HOLD_TIMES}
THREAD_BLOCK_HOLD_TIME: array[0..56] of cardinal =
(4000,4000,4000,4000,4000,4000,4000,4000,4000,4000,4000,4000,4000,4000,4000,
4000,8000,8000,8000,8000,8000,8000,8000,8000,8000, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100);
{$ELSE}
THREAD_BLOCK_HOLD_TIME = 8000;
{$ENDIF}
LITE_HEADER_SIZE = sizeof(nativeint);
MANAGER_TIMEOUT = 10000;
const
LARGE_BLOCK_INDEX = MAXSIZEINDEX;
FLAG_AVAILABLE = 1;
{$IFDEF DONT_CACHE_LARGE_BLOCKS}
FLAG_LARGE = 2;
{$ENDIF}
{$IFNDEF LINUX}
{$IFDEF CPUX64}
kern = 'kernel32.dll';
{$ELSE}
kern = 'kernel32.dll';
{$ENDIF}
const
// PAGE_NOACCESS = 1;
PAGE_READWRITE = 4;
LMEM_FIXED = 0;
LMEM_MOVEABLE = 2;
LMEM_ZEROINIT = $40;
MEM_COMMIT = $1000;
MEM_RESERVE = $2000;
MEM_DECOMMIT = $4000;
MEM_RELEASE = $8000;
const
STATUS_NO_MEMORY = pointer($C0000017);
STATUS_ACCESS_VIOLATION = pointer($C0000005);
{$IFDEF FIB}
FIBS : array of int64 = [
1,
2,
4,
8,
16,
32,
64,
128,
192,
256,
512,
610,
987,
1597,
2584,
4181,
6765,
10946,
17711,
28657,
46368,
75025,
121393,
196418,
317811,
514229,
832040,
1346269,
2178309,
3524578,
5702887,
9227465,
14930352,
24157817,
39088169,
63245986,
102334155,
165580141,
267914296,
433494437,
701408733,
1134903170,
1836311903,
2971215073,
4807526976,
7778742049,
12586269025,
20365011074,
32951280099,
53316291173,
86267571272,
139583862445,
225851433717,
365435296162,
591286729879,
956722026041,
1548008755920,
2504730781961,
4052739537881,
6557470319842,
65574703198420,
655747031984200,
6557470319842000,
65574703198420000
];
{$ENDIF}
procedure Sleep(dwMilliseconds: DWORD); stdcall;
{$EXTERNALSYM Sleep}
procedure Sleep; external kern name 'Sleep';
{$ENDIF}
type
TSystemInfo = record
case Integer of
0: (
dwOemId: DWORD);
1: (
wProcessorArchitecture: Word;
wReserved: Word;
dwPageSize: DWORD;
lpMinimumApplicationAddress: Pointer;
lpMaximumApplicationAddress: Pointer;
dwActiveProcessorMask: DWORD;
dwNumberOfProcessors: DWORD;
dwProcessorType: DWORD;
dwAllocationGranularity: DWORD;
wProcessorLevel: Word;
wProcessorRevision: Word);
end;
{$IFNDEF LINUX}
procedure GetSystemInfo(var lpSystemInfo: TSystemInfo); stdcall;
{$EXTERNALSYM GetSystemInfo}
procedure GetSystemInfo; external kern name 'GetSystemInfo';
{$ELSE}
procedure GetSystemInfo(var lpSystemInfo: TsystemInfo); stdcall;
{$ENDIF}
{$IFDEF REALLINUX}
const
libcmodulename = 'libc.so.6';
libcryptmodulename = 'libcrypt.so.1';
libdlmodulename = 'libdl.so.2';
libmmodulename = 'libm.so.6';
libpthreadmodulename = 'libpthread.so.0';
libresolvmodulename = 'libresolv.so.2';
librtmodulename = 'librt.so.1';
libutilmodulename = 'libutil.so.1';
{$ENDIF}
type
THandle = NativeUInt;
_RTL_CRITICAL_SECTION = record
DebugInfo: Pointer;
LockCount: Longint;
RecursionCount: Longint;
OwningThread: THandle;
LockSemaphore: THandle;
Reserved: PNativeUInt;
end;
TAtomicLock = record
lock: integer;
handle: NativeInt;
end;
{$IFNDEF ATOMICLOCKS}
{$IFDEF MACOS}
{$ELSE}
TCLXCriticalSection = _RTL_CRITICAL_SECTION;
{$ENDIF}
{$ELSE}
TCLXCriticalSection = TAtomicLock;xxx - no, just no
{$ENDIF}
{$IFDEF LINUX}
{$IFDEF MACOS}
{$I BBOSX.inc}
type
TCLXCriticalSection = pthread_mutex_t;
{$ENDIF}
{L}
{L}const
{L} EBUSY = 16; { Device or resource busy }
{L} PTHREAD_MUTEX_RECURSIVE_NP = 1;
{L} //PTHREAD_MUTEX_RECURSIVE = PTHREAD_MUTEX_RECURSIVE_NP;
{L}type
{L} _pthread_descr = Pointer;
{L} _pthread_fastlock = {packed} record
{L} __status: Longint; { "Free" or "taken" or head of waiting list }
{L} __spinlock: Integer; { For compare-and-swap emulation }
{L} end;
{L} TPthreadFastlock = _pthread_fastlock;
{L} PPthreadFastlock = ^TPthreadFastlock;
{L}
{L} TPthreadMutex = {packed} record
{L} __m_reserved: Integer; { Reserved for future use }
{L} __m_count: Integer; { Depth of recursive locking }
{L} __m_owner: _pthread_descr; { Owner thread (if recursive or errcheck) }
{L} __m_kind: Integer; { Mutex kind: fast, recursive or errcheck }
{L} __m_lock: _pthread_fastlock; { Underlying fast lock }
{L} end;
{L}
//{L} {$EXTERNALSYM TPthreadMutex}
//{L} pthread_mutex_t = TPthreadMutex;
{L} {$EXTERNALSYM pthread_mutex_t}
{L} TRTLCriticalSection = TPthreadMutex;
{L} PRTLCriticalSection = ^TCLXCriticalSection;
{L} {$EXTERNALSYM PRTLCriticalSection}
{L}
{L}{ Attribute for mutex. }
//{L} pthread_mutexattr_t = {packed} record
//{L} __mutexkind: Integer;
//{L} end;
{L} {$EXTERNALSYM pthread_mutexattr_t}
{L} TMutexAttribute = pthread_mutexattr_t;
{L} {$EXTERNALSYM TMutexAttribute}
{L} PMutexAttribute = ^TMutexAttribute;
{L} {$EXTERNALSYM PMutexAttribute}
{L}
{L} __time_t = type Longint;
{L} __useconds_t = Cardinal;
{L} __suseconds_t = Longint;
{L}
{L} timeval = {packed} record
{L} tv_sec: __time_t; { Seconds. }
{L} tv_usec: __suseconds_t; { Microseconds. }
{L}
{L} end;
{L}
//{L} timespec = {packed} record
//{L} tv_sec: Longint; { Seconds. }
//{L} tv_nsec: Longint; { Nanoseconds. }
//{L} end;
//{L} {$EXTERNALSYM timespec}
//{L} TTimeSpec = timespec;
//{L} {$EXTERNALSYM TTimeSpec}
//{L} PTimeSpec = ^TTimeSpec;
{L} {$EXTERNALSYM PTimeSpec}
{L} timezone = {packed} record
{L} tz_minuteswest: Integer; { Minutes west of GMT. }
{L} tz_dsttime: Integer; { Nonzero if DST is ever in effect. }
{L} end;
{L}
{L}
{L} TTimeZone = timezone;
{L} PTimeZone = ^TTimeZone;
{L}
{L}
{L} TTimeVal = timeval;
{L} PTimeVal = ^TTimeVal;
{L}
{L}
{L}{ Attributes for threads. }
//{L} pthread_attr_t = TThreadAttr; // TThreadAttr in System.pas
{L} {$EXTERNALSYM pthread_attr_t}
{L}
{L}
//{L} TCLXCriticalSection = TCLXCriticalSection;
{L}
{L}
{L}
{L}//function gettimeofday(var timeval: TTimeVal; var timezone: TTimeZone): Integer; external libcmodulename name 'gettimeofday';
{L}//function gettimeofday(var timeval: TTimeVal; timezone: PTimeZone): Integer; external libcmodulename name 'gettimeofday';
{L}
{L}function DeleteCriticalSection(var lpCriticalSection: TCLXCriticalSection): Integer; cdecl;
//{L}function pthread_mutex_trylock(var Mutex: TCLXCriticalSection): Integer; cdecl;
{L}
//{L}function pthread_mutex_init(var Mutex: TCLXCriticalSection;
//{L} var Attr: TMutexAttribute): Integer; cdecl; overload;
//{L}function pthread_mutex_init(var Mutex: TCLXCriticalSection;
//{L} Attr: PMutexAttribute): Integer; cdecl; overload;
//{L}function pthread_mutex_destroy(var Mutex: TCLXCriticalSection): Integer; cdecl;
//{L}function pthread_mutex_timedlock(var Mutex: TCLXCriticalSection; const AbsTime: timespec): Integer; cdecl;
//{L}function pthread_mutex_unlock(var Mutex: TCLXCriticalSection): Integer; cdecl;
//{L}function pthread_mutexattr_init(var Attr: TMutexAttribute): Integer; cdecl;
//{L}function pthread_mutexattr_destroy(var Attr: TMutexAttribute): Integer; cdecl;
//{L}function pthread_mutexattr_getpshared(var Attr: TMutexAttribute; // Actually __const pthread_mutexattr_t *
//{L} var ProcessShared: Integer): Integer; cdecl;
//{L}function pthread_mutexattr_setpshared(var Attr: TMutexAttribute;
//{L} ProcessShared: Integer): Integer; cdecl;
//{L}function pthread_mutexattr_settype(var Attr: TMutexAttribute; Kind: Integer): Integer; cdecl;
//{L}function pthread_mutexattr_gettype(var Attr: TMutexAttribute; var Kind: Integer): Integer; cdecl;
{L}
{L}
//{L}function pthread_mutex_destroy; external libpthreadmodulename name 'pthread_mutex_destroy';
//{L}function pthread_mutex_init(var Mutex: TCLXCriticalSection; var Attr: TMutexAttribute): Integer; external libpthreadmodulename name 'pthread_mutex_init';
//{L}function pthread_mutex_init(var Mutex: TCLXCriticalSection; Attr: PMutexAttribute): Integer; external libpthreadmodulename name 'pthread_mutex_init';
//{L}function pthread_mutex_timedlock; external libpthreadmodulename name 'pthread_mutex_timedlock';
{L}//function pthread_mutex_trylock; external libpthreadmodulename name 'pthread_mutex_trylock';
//{L}function pthread_mutex_unlock; external libpthreadmodulename name 'pthread_mutex_unlock';
//{L}function pthread_mutexattr_destroy; external libpthreadmodulename name 'pthread_mutexattr_destroy';
//{L}function pthread_mutexattr_getpshared; external libpthreadmodulename name 'pthread_mutexattr_getpshared';
//{L}function pthread_mutexattr_setpshared; external libpthreadmodulename name 'pthread_mutexattr_setpshared';
//{L}function pthread_mutexattr_gettype; external libpthreadmodulename name 'pthread_mutexattr_gettype';
//{L}function pthread_mutexattr_init; external libpthreadmodulename name 'pthread_mutexattr_init';
//{L}function pthread_mutexattr_settype; external libpthreadmodulename name 'pthread_mutexattr_settype';
//{L}function pthread_mutex_trylock; external libpthreadmodulename name 'pthread_mutex_trylock';
{L}
{L}function InitializeCriticalSection(var lpCriticalSection: TCLXCriticalSection): Integer; cdecl;
{L}function InitializeCriticalSectionAndSpinCount(var lpCriticalSection: TCLXCriticalSection; dwSpinCount: DWORD): BOOL; cdecl;
{L}function EnterCriticalSection(var lpCriticalSection: TCLXCriticalSection): Integer; cdecl;
{L}function LeaveCriticalSection(var lpCriticalSection: TCLXCriticalSection): Integer; cdecl;
//{L}function DeleteCriticalSection(var lpCriticalSection: TCLXCriticalSection): Integer; cdecl;
{L}function TryEnterCriticalSection(var lpCriticalSection: TCLXCriticalSection): LongBool; cdecl;
{L}{$IFNDEF ATOMICLOCKS}
function DeleteCriticalSection(var lpCriticalSection: TCLXCriticalSection): Integer; cdecl; external libpthread name _PU + 'pthread_mutex_destroy';
{$EXTERNALSYM pthread_mutex_destroy}
{ Try to lock MUTEX. }
function TryEnterCriticalSection(var lpCriticalSection: TCLXCriticalSection): LongBool; cdecl; external libpthread name _PU + 'pthread_mutex_trylock';
{$EXTERNALSYM pthread_mutex_trylock}
{ Wait until lock for MUTEX becomes available and lock it. }
function EnterCriticalSection(var lpCriticalSection: TCLXCriticalSection): Integer; cdecl; external libpthread name _PU + 'pthread_mutex_lock';
{$EXTERNALSYM pthread_mutex_lock}
function LeaveCriticalSection(var lpCriticalSection: TCLXCriticalSection): Integer; cdecl;
external libpthread name _PU + 'pthread_mutex_unlock';
{$EXTERNALSYM pthread_mutex_unlock}
{L}{$ENDIF}
{L}
//{L}procedure pthread_exit(pexit: pointer); external libpthreadmodulename name 'pthread_exit';
{L}procedure Beep(freq, duration: nativeint);
{L}function GetTickCount: Cardinal;
{L}
{L}
function GetCurrentThreadID: TThreadID; cdecl; external libpthread name _PU + 'pthread_self';
{$ENDIF}
{$IFNDEF LINUX}
// TCLXCriticalSection = TRTLCriticalSection;
{$IFDEF MESSAGES}
procedure OutputDebugString(lpOutputString: PWideChar); stdcall;
{$EXTERNALSYM OutputDebugString}
{$IFDEF UNICODE}
procedure OutputDebugString; external kern name 'OutputDebugStringW';
procedure OutputDebugStringW(lpOutputString: PWideChar); stdcall;
{$EXTERNALSYM OutputDebugStringW}
procedure OutputDebugStringW; external kern name 'OutputDebugStringW';
{$ELSE}
procedure OutputDebugString; external kern name 'OutputDebugStringA';
procedure OutputDebugStringA(lpOutputString: PAnsiChar); stdcall;
{$EXTERNALSYM OutputDebugStringA}
procedure OutputDebugStringA; external kern name 'OutputDebugStringA';
{$ENDIF}
{$ENDIF}
function FlushInstructionCache(hProcess: THandle; const lpBaseAddress:
Pointer; dwSize: DWORD): BOOL; stdcall;
external kern name 'FlushInstructionCache';
function GetCurrentProcess: THandle; stdcall;
external kern name 'GetCurrentProcess';
function GetCurrentThreadId: integer; external kern name 'GetCurrentThreadId';
function VirtualProtect(lpAddress:pointer; dwSize, flNewProtect: DWORD;
var lpflOldProtect:DWord):BOOL; stdcall;
external kern name 'VirtualProtect';
{$IFNDEF ATOMICLOCKS}
function InitializeCriticalSectionAndSpinCount(var lpCriticalSection: TCLXCriticalSection; dwSpinCount: DWORD): BOOL; stdcall;
external kern name 'InitializeCriticalSectionAndSpinCount';
procedure InitializeCriticalSection(var lpCriticalSection: TCLXCriticalSection); stdcall;
external kern name 'InitializeCriticalSection';
procedure EnterCriticalSection(var lpCriticalSection: TCLXCriticalSection); stdcall;
external kern name 'EnterCriticalSection';
function TryEnterCriticalSection(var lpCriticalSection: TCLXCriticalSection): BOOL; stdcall;
external kern name 'TryEnterCriticalSection';
procedure LeaveCriticalSection(var lpCriticalSection: TCLXCriticalSection); stdcall;
external kern name 'LeaveCriticalSection';
procedure DeleteCriticalSection(var lpCriticalSection: TCLXCriticalSection); stdcall;
external kern name 'DeleteCriticalSection';
{$ELSE}
procedure InitializeCriticalSection(var lpCriticalSection: TCLXCriticalSection); stdcall;
procedure EnterCriticalSection(var lpCriticalSection: TCLXCriticalSection); {$IFNDEF ATOMICLOCKS} stdcall;{$ELSE}{$IFDEF ALLOW_INLINE}inline;{$ENDIF}{$ENDIF}
function TryEnterCriticalSection(var lpCriticalSection: TCLXCriticalSection): BOOL; {$IFNDEF ATOMICLOCKS} stdcall;{$ELSE}{$IFDEF ALLOW_INLINE}inline;{$ENDIF}{$ENDIF}
procedure LeaveCriticalSection(var lpCriticalSection: TCLXCriticalSection); {$IFNDEF ATOMICLOCKS} stdcall;{$ELSE}{$IFDEF ALLOW_INLINE}inline;{$ENDIF}{$ENDIF}
procedure DeleteCriticalSection(var lpCriticalSection: TCLXCriticalSection); {$IFNDEF ATOMICLOCKS} stdcall;{$ELSE}{$IFDEF ALLOW_INLINE}inline;{$ENDIF}{$ENDIF}
function TryLockAtomic(var lock: TAtomicLock):BBBool; {$IFNDEF ATOMICLOCKS} stdcall;{$ELSE}{$IFDEF ALLOW_INLINE}inline;{$ENDIF}{$ENDIF}
procedure LockAtomic(var lock: TAtomicLock); {$IFNDEF ATOMICLOCKS} stdcall;{$ELSE}{$IFDEF ALLOW_INLINE}inline;{$ENDIF}{$ENDIF}
procedure UnLockAtomic(var lock: TAtomicLock); {$IFNDEF ATOMICLOCKS} stdcall;{$ELSE}{$IFDEF ALLOW_INLINE}inline;{$ENDIF}{$ENDIF}
{$ENDIF}
procedure ExitThread(ExitCode: Integer); stdcall;
external kern name 'ExitThread';
function LocalAlloc(flags, size: Integer): Pointer; stdcall;
external kern name 'LocalAlloc';
function HeapCreate(flOptions, dwInitialSize, dwMaximumSize: DWORD): THandle; stdcall;
{$EXTERNALSYM HeapCreate}
function HeapDestroy(hHeap: THandle): BOOL; stdcall;
{$EXTERNALSYM HeapDestroy}
function HeapAlloc(hHeap: THandle; dwFlags, dwBytes: DWORD): Pointer; stdcall;
{$EXTERNALSYM HeapAlloc}
function HeapReAlloc(hHeap: THandle; dwFlags: DWORD; lpMem: Pointer; dwBytes: DWORD): Pointer; stdcall;
{$EXTERNALSYM HeapReAlloc}
function HeapFree(hHeap: THandle; dwFlags: DWORD; lpMem: Pointer): BOOL; stdcall;
{$EXTERNALSYM HeapFree}
function HeapSize(hHeap: THandle; dwFlags: DWORD; lpMem: Pointer): DWORD; stdcall;
{$EXTERNALSYM HeapSize}
function HeapValidate(hHeap: THandle; dwFlags: DWORD; lpMem: Pointer): BOOL; stdcall;
{$EXTERNALSYM HeapValidate}
function HeapSetInformation(hHeap: THandle; heapinfo: pointer; var HeapFragValue; ValueSize: integer): BOOL; stdcall;
{$IFNDEF WINDOWS2000_COMPATIBLE}
{$EXTERNALSYM HeapSetInformation}
{$ENDIF}
function HeapCompact(hHeap: THandle; dwFlags: DWORD): integer; stdcall;
{$EXTERNALSYM HeapCompact}
function GetProcessHeap: THandle; stdcall;
{$EXTERNALSYM GetProcessHeap}
function LocalReAlloc(addr:Pointer; size,flags : Integer): Pointer; stdcall;
external kern name 'LocalReAlloc';
function LocalFree(addr: Pointer): Pointer; stdcall;
external kern name 'LocalFree';
function VirtualAlloc(lpAddress: Pointer;
dwSize, flAllocationType, flProtect: DWORD): Pointer; stdcall;
external kern name 'VirtualAlloc';
function VirtualFree(lpAddress: Pointer; dwSize, dwFreeType: DWORD): BOOL; stdcall;
external kern name 'VirtualFree';
{$IFNDEF LINUX}
function Beep(dwFreq, dwDuration: DWORD): BOOL; stdcall; external kern name 'Beep';
{$ENDIF}
function GetTickCount: cardinal; stdcall; external kern name 'GetTickCount';
{$IFNDEF DELPHI64}
{$IFNDEF USE_ASM_INTERLOCKED_FUNCTIONS}
function InterlockedIncrement(var Addend: Integer): Integer; stdcall; external kern name 'InterlockedIncrement';
function InterlockedCompareExchangePointer(var destination: pointer; var Exchange: integer; var Comperand: integer): pointer; stdcall; external kern name 'InterlockedCompareExhangePointer';
function InterlockedDecrement(var Addend: Integer): Integer; stdcall; external kern name 'InterlockedDecrement';
{$ENDIF}
{$ELSE}
{$IFDEF CPUX86}
function InterlockedAdd(var Addend: Integer; Increment: Integer): Integer;
function InterlockedCompareExchange(var Target: Integer; Exchange: Integer; Comparand: Integer): Integer;
function InterlockedCompareExchangePointer(var Target: Pointer; Exchange: Pointer; Comparand: Pointer): Pointer;
function InterlockedDecrement(var Addend: Integer): Integer;
function InterlockedExchange(var Target: Integer; Value: Integer): Integer;
function InterlockedExchangePointer(var Target: Pointer; Value: Pointer): Pointer;
function InterlockedIncrement(var Addend: Integer): Integer;
{$ENDIF CPUX86}
{$IFDEF CPUX64}
function InterlockedExchangeAdd(var Addend: Integer; Value: Integer): Integer;
function InterlockedDecrement(var Addend: LongInt): LongInt;
function InterlockedIncrement(var Addend: LongInt): LongInt;
function InterlockedCompareExchange(var Destination: Integer; Exchange: Integer; Comparand: Integer): Integer;
function InterlockedCompareExchange64(var Destination: Int64; Exchange: Int64; Comparand: Int64): Int64; overload;
function InterlockedCompareExchangePointer(var Destination: Pointer; Exchange: Pointer; Comparand: Pointer): Pointer;
function InterlockedExchangePointer(var Target: Pointer; Value: Pointer): Pointer;
function InterlockedExchange(var Target: Integer; Value: Integer): Integer;// inline;
{$ENDIF CPUX64}
{$ENDIF}
function HeapLock(hHeap: THandle): BOOL; stdcall;
{$EXTERNALSYM HeapLock}
function HeapUnlock(hHeap: THandle): BOOL; stdcall;
{$EXTERNALSYM HeapUnlock}
function HeapAlloc; external kern name 'HeapAlloc';
function HeapCompact; external kern name 'HeapCompact';
function HeapCreate; external kern name 'HeapCreate';
function HeapDestroy; external kern name 'HeapDestroy';
function HeapFree; external kern name 'HeapFree';
function HeapLock; external kern name 'HeapLock';
function HeapReAlloc; external kern name 'HeapReAlloc';
{$IFNDEF WINDOWS2000_COMPATIBLE}
function HeapSetInformation; external kern name 'HeapSetInformation';
{$ENDIF}
function HeapSize; external kern name 'HeapSize';
function HeapUnlock; external kern name 'HeapUnlock';
function HeapValidate; external kern name 'HeapValidate';
function GetProcessHeap; external kern name 'GetProcessHeap';
function VirtualLock(lpAddress: Pointer; dwSize: SIZE_T): BOOL; stdcall;
{$EXTERNALSYM VirtualLock}
function VirtualLock; external kern name 'VirtualLock';
{$EXTERNALSYM VirtualLock}
{$ENDIF}
//----------------------------KERNEL--------------------------------------------
const
SUPER_MEM_BLOCK_CORE_SIZE =
{$IFDEF STATS}SizeOf(NativeInt)*2+{$ENDIF}
SizeOf(pointer)*3+
{$IFDEF PACK_HEADERS}
SizeOf(byte)*2;
{$ELSE}
SizeOf(nativeInt)*2;
{$ENDIF}
{$IFDEF CPUX64}
{$IFDEF STATS}
INFLATE_CEILING = 48;
{$ELSE}
INFLATE_CEILING = 32;
{$ENDIF}
{$ENDIF}
type
PSuperMemBlock = ^TSuperMemBlock;
TSuperMemBlock = {$IFDEF PACK_HEADERS}packed{$ENDIF} record
FPreviousBlock: pointer;
FNextBlock: pointer;
ManagedBy: pointer;
{$IFDEF STATS}
UsedBytes: NativeInt;
ReportedUsedBytes: NativeInt;
{$ENDIF STATS}
Flags: byte;
SizeIndex: byte;
{$IFDEF CPUX86}
{$IFDEF INFLATE_TO_24}
junk: array[1..28-SUPER_MEM_BLOCK_CORE_SIZE] of byte;
{$ENDIF}
{$IFDEF INFLATE_TO_32}
junk: array[1..32-SUPER_MEM_BLOCK_CORE_SIZE] of byte;
{$ENDIF}
{$ENDIF}
{$IFDEF CPUX64}
{$IFDEF INFLATE_TO_24}
junk: array[1..INFLATE_CEILING-SUPER_MEM_BLOCK_CORE_SIZE] of byte;//24 is not possible
{$ENDIF}
{$IFDEF INFLATE_TO_32}
junk: array[1..INFLATE_CEILING-SUPER_MEM_BLOCK_CORE_SIZE] of byte;
{$ENDIF}
{$ENDIF}
end;
procedure smb_SetNextBlock(const self: PSuperMemBlock; const Value: pointer);{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure smb_SetPreviousBlock(const self: PSuperMemBlock; const Value: pointer);{$IFNDEF NOINLINE}inline;{$ENDIF}
function smb_GetisLinked(const self: PSuperMemBlock): BBBool;{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure smb_Init(const self: PSuperMemBlock; const UserSize: NativeInt);{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure smb_SetAvailable(const self: PSuperMemBlock; const b: BBBool);{$IFNDEF NOINLINE}inline;{$ENDIF}
// function smb_GetSizeIndexSize(self: PSuperMemBlock): NativeInt;
function smb_GetUserDataArea(const self: PSuperMemBlock): pointer;{$IFNDEF NOINLINE}inline;{$ENDIF}
function smb_GetHeaderSize(const self: PSuperMemBlock): NativeInt;{$IFNDEF NOINLINE}inline;{$ENDIF}
function smb_GetUserAreaSize(const self: PSuperMemBlock): NativeInt;
function smb_GetDelphiAllocSize(const self: PSuperMemblock): NativeInt;
function smb_GetWasteBytes(const self: PSuperMemBlock): NativeInt;{$IFNDEF NOINLINE}inline;{$ENDIF}
function smb_GetAvailable(const self: PSuperMemBlock): BBBool;{$IFNDEF NOINLINE}inline;{$ENDIF}
// property Available:BBBool read GetAvailable write SetAvailable;
// property NextBlock: pointer read FNextBlock write SetNextBlock;
// property PreviousBlock: pointer read FPreviousBlock write SetPreviousBlock;
// property IsLinked: BBBool read GetisLinked;
type
TBlockManager = class;
TlockedBlockList = class;
PLockedBlockList = {$IFNDEF USELISTCLASS}^{$ENDIF}TLockedBlockList;
TLockedBlockList = {$IFDEF USELISTCLASS}class(THeapObject){$ELSE}object{$ENDIF}
private
FWasteBytes: NativeInt;
FBlockCount: NativeInt;
FUsedBytes: NativeInt;
FFreeBytes: NativeInt;
{$IFDEF USE_SPINLOCK}
sl: TSpinLock;
{$ELSE}
sect: TCLXCriticalSection;
{$ENDIF}
tip: PSuperMemBlock;
FFreePool: BBBool;
Fowner: TBlockManager;
FAllocTo: TLockedBlockList;
FFreeTo: TLockedBlockList;
FFinalized: BBBool;
FlastUsed: cardinal;
function DiscreetExtract: PSuperMemBlock;{$IFNDEF NOINLINE}inline;{$ENDIF}
function Extract: PSuperMemBlock;{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure TallyStats(const block: PSuperMemBlock);{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure UnTallyStats(const block: PSuperMemBlock);{$IFNDEF NOINLINE}inline;{$ENDIF}
function GEtFreeBytes: NativeInt;{$IFNDEF NOINLINE}inline;{$ENDIF}
function GetAllocBytes: NativeInt;{$IFNDEF NOINLINE}inline;{$ENDIF}
function GEtBlockCount: NativeInt;{$IFNDEF NOINLINE}inline;{$ENDIF}
function GetUsedBytes: NativeInt;{$IFNDEF NOINLINE}inline;{$ENDIF}
function GetWasteBytes: NativeInt;{$IFNDEF NOINLINE}inline;{$ENDIF}
public
{$IFDEF USELISTCLASS}
constructor Create(Aowner: pointer); reintroduce; virtual;
destructor Destroy; override;
{$ENDIF}
procedure Lock;{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure Unlock;{$IFNDEF NOINLINE}inline;{$ENDIF}
function TryLock: BBBool;{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure Link(const block: PSuperMemBlock);//{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure Unlink(const block: PSuperMemBlock);//{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure DiscreetLink(const block: PSuperMemBlock);{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure DiscreetUnlink(const block: PSuperMemBlock);{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure LockedUnlink(const block: PSuperMemBlock);{$IFNDEF NOINLINE}inline;{$ENDIF}
function LockedExtract: PSuperMemBlock;{$IFNDEF NOINLINE}inline;{$ENDIF}
function TryLockedExtract: PSuperMemBlock;{$IFNDEF NOINLINE}inline;{$ENDIF}
function TryDiscreetLockedExtract: PSuperMemBlock;{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure LockedLInk(const block: PSuperMemBlock);{$IFNDEF NOINLINE}inline;{$ENDIF}
property FreeBytes: NativeInt read GEtFreeBytes;
property AllocBytes: NativeInt read GetAllocBytes;
property BlockCount: NativeInt read GEtBlockCount;
property UsedBytes: NativeInt read GetUsedBytes;
property WasteBytes: NativeInt read GetWasteBytes;
property FreePool: BBBool read FFreePool write FFreePool;
procedure Init(AOwner: TBlockManager);{$IFNDEF NOINLINE}inline;{$ENDIF}
function Finalize: BBBool;{$IFNDEF NOINLINE}inline;{$ENDIF}
property Owner: TBlockMAnager read FOwner;
property AllocTo: TLockedBlockList read FallocTo write FAllocTo;
property FreeTo: TLockedBlocklist read FFreeTo write FFreeTo;
{$IFDEF HOLD_MEMORY}
function TimeSinceUsed: cardinal;
{$ENDIF}
end;
//PLockedBlockList = {$IFNDEF USELISTCLASS}^{$ENDIF}TLockedBlockList;
TBlockManager=class(THeapObject)
private
function GetFree: BBBool;{$IFNDEF NOINLINE}inline;{$ENDIF}
function GetAllocBytes: NativeInt;{$IFNDEF NOINLINE}inline;{$ENDIF}
function GetBlockCount: NativeInt;{$IFNDEF NOINLINE}inline;{$ENDIF}
function GetUsedBlockCount: NativeInt;{$IFNDEF NOINLINE}inline;{$ENDIF}
function GetFreeBlockCount: NativeInt;{$IFNDEF NOINLINE}inline;{$ENDIF}
function GetFreeBytes: NativeInt;{$IFNDEF NOINLINE}inline;{$ENDIF}
function GetUsedBytes: NativeInt;{$IFNDEF NOINLINE}inline;{$ENDIF}
function GetWasteBytes: NativeInt;{$IFNDEF NOINLINE}inline;{$ENDIF}
protected
//!Free flags/variables
Fblocks: array[0..MAXSIZEINDEX] of TLockedBlockList;
FFreeBlocks: array[0..MAXSIZEINDEX] of TLockedBlockList;
FFree: BBBool;
FFreeTime: cardinal;
FFreeing: BBBool;
FDestroyCountdown: integer;
FTimeOfLastClean: cardinal;
{$IFDEF LOCK_BLOCK_MAN} sect: TCLXCriticalSection;{$ENDIF}
//!Statistics
//!Key Block Management functions
function ResizeBlock(block: pSuperMEmBlock;
Size: NativeInt): PSuperMemBlock;{$IFNDEF NOINLINE}inline;{$ENDIF}
function NeedBlock(UserSize: NativeInt): PSuperMemBlock;{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure PrefetchExtraBlock(UserSize: NativeInt);{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure NoNeedBlock(const block: pSuperMemBlock);{$IFNDEF NOINLINE}inline;{$ENDIF}
//!Key Block Management functions for talking to OS
function AllocateNewBlock(const UserSize: NativeInt): PSuperMemBlock; virtual;
procedure FreeBlockToOS(const block:PSuperMemBlock); {$IFNDEF NOINLINE}inline;{$ENDIF}
//!Key Link list management functions
procedure LinkAllocated(const block: PSuperMemBlock);{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure UnlinkAllocated(const block: PSuperMemBlock);{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure LinkFreed(const block: PSuperMemBlock);{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure UnlinkFreed(const block: PSuperMemBlock);{$IFNDEF NOINLINE}inline;{$ENDIF}
//!Debug
function CheckAlloc: BBBool;{$IFNDEF NOINLINE}inline;{$ENDIF}
function CheckFree: BBBool;{$IFNDEF NOINLINE}inline;{$ENDIF}
//!Construction
procedure InitBlocks;{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure FinalizeBlocks;virtual;
//!Getters/Setters
function GEtBlockHeaderOverhead: NativeInt;{$IFNDEF NOINLINE}inline;{$ENDIF}
// function GetFreeBlockCount(idx: NativeInt): NativeInt;
// function GetRealWasteBytes: NativeInt;
procedure SetFree(const Value: BBBool);{$IFNDEF NOINLINE}inline;{$ENDIF}
function GetDestroyCount: NativeInt;
function GetEmptying: BBBool;{$IFNDEF NOINLINE}inline;{$ENDIF}
public
//create/destroy
constructor Create; reintroduce; virtual;
destructor Destroy; override;
//!Newmem, FreeMem, Reallocmem handlers
function ResizeMemory(p: pointer; Size: NativeInt): pointer;virtual;
function NeedMemory(const UserSize: NativeInt): pointer;virtual;
procedure NoNeedMemory(const p: pointer);virtual;
//!Managers talking to other managers
function ExtractBlock(const UserSize: NativeInt): PSuperMemBlock;{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure InjectBlock(const block: PSuperMemBlock);{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure RemanageBlock(const block: PSuperMemBlock);{$IFNDEF NOINLINE}inline;{$ENDIF}
//!Instrumentation/Statistics
property BlockHeaderOverhead: NativeInt read GEtBlockHeaderOverhead;
//!Flags
property Freeing: BBBool read FFreeing write FFreeing;
property Emptying: BBBool read GEtEmptying;
property IsFree: BBBool read GetFree write SetFree;
property DestroyCount: NativeInt read GetDestroycount;
procedure DestroyCountdown;
procedure ResetCountdown;{$IFNDEF NOINLINE}inline;{$ENDIF}
property FreeTime: cardinal read FFreeTime;
//!Locks
function TryLock: BBBool;{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure Lock;{$IFNDEF NOINLINE}inline;{$ENDIF}
Procedure Unlock;{$IFNDEF NOINLINE}inline;{$ENDIF}
//!Garbage collection
function Clean: nativeint; virtual;
//!debug
function CheckMem: BBBool;{$IFNDEF NOINLINE}inline;{$ENDIF}
// property RealWasteBytes: NativeInt read GetRealWasteBytes;
// property FreeBlockCount[idx: NativeInt]: NativeInt read GetFreeBlockCount;
property FreeBytes: NativeInt read GetFreeBytes;
property AllocBytes: NativeInt read GetAllocBytes;
property BlockCount: NativeInt read GetBlockCount;
property UsedBlockCount: NativeInt read GetUsedBlockCount;
property FreeBlockCount: NativeInt read GetFreeBlockCount;
property UsedBytes: NativeInt read GetUsedBytes;
property WasteBytes: NativeInt read GetWasteBytes;
end;
TThreadBlockManager = class(TBlockManager)
//Manager for a specific thread
private
FMainMan: TBlockManager;
FLastOwnedThreadID: cardinal;
protected
procedure FinalizeBlocks;override;
public
//!Construction/destruction
constructor Create;override;
destructor Destroy;override;
//!New Properties/Methods
function EmptyBlocksToMain: nativeint;
property MainMan: TBlockManager read FMainMan write FMainMan;
//!Overrides
function AllocateNewBlock(const Size: NativeInt): PSuperMemBlock; override;
procedure NoNeedBlock(const block: pSuperMemBlock);{$IFNDEF NOINLINE}inline;{$ENDIF}
function Clean: nativeint; override;
property LastOwnedThreadID: cardinal read FLastOwnedThreadID write FLastOwnedThreadID;
end;
TBlockManagerManager=class(THeapObject)
//Manages block managers
private
list: array [0..15000] of TBlockManager;
// reserve: array[0..30000] of TBlockManager;
deadlist: array[0..15000] of TBlockManager;
sect: TCLXCriticalSection;
dangerlock: TCLXCriticalSection;
Fcount: NativeInt;
Fdanger: integer;
FDeadCount: NativeInt;
//!Getters/setters
function GetBlockManager(idx: NativeInt): TBlockManager;{$IFNDEF NOINLINE}inline;{$ENDIF}
function GetManagerCount: NativeInt;{$IFNDEF NOINLINE}inline;{$ENDIF}
function GetDeadCount: NativeInt;{$IFNDEF NOINLINE}inline;{$ENDIF}
public
//!Construction/destruction
constructor create;reintroduce; virtual;
destructor Destroy; override;
//!Managers
property Managers[idx: NativeInt]: TBlockManager read GetBlockManager;
property ManagerCount: NativeInt read GetManagerCount;
property DeadCount: NativeInt read GetDeadCount;
function ExtractBlockFromAny(iSize: nativeint): PSuperMemBlock;
function GetDeadMan: TBlockManager;
function NewManager: TThreadBlockManager;{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure FreeManager(man: TBlockManager);{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure RegisterManager(bm: TBlockManager);{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure DeregisterManager(bm: TblockManager);{$IFNDEF NOINLINE}inline;{$ENDIF}
function IndexOf(bm: TblockManager): NativeInt;{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure AddMan(bm: TBlockManager);{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure DeleteMan(idx: NativeInt);{$IFNDEF NOINLINE}inline;{$ENDIF}
//!Danger zone
procedure EnterDangerZone;{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure LeaveDangerZone;{$IFNDEF NOINLINE}inline;{$ENDIF}
//!Locks
procedure Lock;{$IFNDEF NOINLINE}inline;{$ENDIF}
procedure Unlock;{$IFNDEF NOINLINE}inline;{$ENDIF}
function TryLock: BBBool;{$IFNDEF NOINLINE}inline;{$ENDIF}
//!Garbage collection
function Clean: nativeint;{$IFNDEF NOINLINE}inline;{$ENDIF}
//!Experimental
function LockFirstAvailable: TThreadBlockManager;{$IFNDEF NOINLINE}inline;{$ENDIF}
function TotalBlockCount: NativeInt;{$IFNDEF NOINLINE}inline;{$ENDIF}
function TotalFreeBytes: NativeInt;{$IFNDEF NOINLINE}inline;{$ENDIF}
function TotalBytes: NativeInt;{$IFNDEF NOINLINE}inline;{$ENDIF}
end;
//!This is crazy stuff for hooking into when a thread ends
type
PJump=^TJump;
TJump=packed record
OpCode:byte;
Distance:NativeInt;
end;
var
OldCode:TJump;
NewCode:TJump=(OpCode:$E9; Distance:0);
{$IFDEF SIMPLEHEAPS}
FHeap: NativeInt;
FHeaps: array[0..MAXSIZEINDEX] of NativeInt;
{$ENDIF}
{$IFDEF USE_SPINLOCKS}