forked from yavfast/dbg-spider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectList.inc
83 lines (67 loc) · 1.57 KB
/
CollectList.inc
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
{ TCollectList<T> }
function TCollectList<T>.Add: PData;
var
Idx: Cardinal;
Seg, Offset: Integer;
begin
Idx := Count;
IndexToSegment(Idx, Seg, Offset);
CheckSeg(Seg);
FCount := Idx + 1;
Result := @FSegList[Seg][Offset];
FillChar(Result^, SizeOf(T), 0);
end;
procedure TCollectList<T>.CheckSeg(const Seg: Integer);
begin
if Length(FSegList) <= Seg then
begin
SetLength(FSegList, Seg + 1);
SetLength(FSegList[Seg], FSegSize);
end;
end;
procedure TCollectList<T>.Clear;
begin
FCount := 0;
SetLength(FSegList, 0);
end;
constructor TCollectList<T>.Create;
begin
inherited;
FCount := 0;
FLock := TCriticalSection.Create;
FSegSize := _SEGMENT_SIZE div SizeOf(T);
SetLength(FSegList, 0);
end;
destructor TCollectList<T>.Destroy;
begin
Clear;
FreeAndNil(FLock);
inherited;
end;
function TCollectList<T>.GetItem(const Index: Cardinal): PData;
var
Seg, Offset: Integer;
begin
if IndexToSegment(Index, Seg, Offset) then
Result := @FSegList[Seg][Offset]
else
RaiseError(@EIndexError, [Index]);
end;
function TCollectList<T>.IndexToSegment(const Index: Cardinal; var Seg, Offset: Integer): Boolean;
begin
Result := Index < Count;
Seg := Index div FSegSize;
Offset := Index mod FSegSize;
end;
procedure TCollectList<T>.RaiseError(Msg: PString; const Args: Array of const);
begin
raise TCollectListError.CreateFmt(Msg^, Args);
end;
procedure TCollectList<T>.Lock;
begin
FLock.Enter;
end;
procedure TCollectList<T>.UnLock;
begin
FLock.Leave;
end;