-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrtlutils.pas
More file actions
146 lines (131 loc) · 3.06 KB
/
rtlutils.pas
File metadata and controls
146 lines (131 loc) · 3.06 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
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit rtlutils;
interface
function GetRefCount(constref S: UTF8String): SizeInt; inline;
procedure IncRefCount(var S: UTF8String); inline;
procedure DecRefCount(var S: UTF8String); inline;
function IsStringConstant(constref S: UTF8String): Boolean; inline;
{$IFOPT C+} procedure AssertStringIsConstant(constref S: UTF8String); {$ENDIF}
{$IFOPT C+} procedure AssertStringIsReffed(constref S: UTF8String; const MinRef: Cardinal); {$ENDIF}
type
{$IFNDEF OPT}
TDebugObject = class(TObject)
procedure FreeInstance(); override;
end;
{$ELSE}
TDebugObject = TObject;
{$ENDIF}
implementation
type
PAnsiRec = ^TAnsiRec;
TAnsiRec = record
// based on TAnsiRec in astrings.inc
CodePage: TSystemCodePage;
ElementSize: Word;
{$IF NOT DEFINED(VER3_2)}
{$IFDEF CPU64}
RefCount: Longint;
{$ELSE}
RefCount: SizeInt;
{$ENDIF}
{$ELSE}
{$IFDEF CPU64}
Dummy: DWord;
{$ENDIF CPU64}
RefCount: SizeInt;
{$ENDIF}
Length: SizeInt;
Data: record end;
end;
function GetRefCount(constref S: UTF8String): SizeInt;
var
StringStart: PAnsiRec;
begin
if (S <> '') then
begin
StringStart := PAnsiRec(Pointer(S)-SizeOf(TAnsiRec));
Result := StringStart^.RefCount;
end
else
Result := 1;
end;
procedure IncRefCount(var S: UTF8String);
var
StringStart: PAnsiRec;
begin
if (S <> '') then
begin
StringStart := PAnsiRec(Pointer(S)-SizeOf(TAnsiRec));
if (StringStart^.RefCount <> -1) then
begin
Inc(StringStart^.RefCount);
end;
end;
end;
procedure DecRefCount(var S: UTF8String);
var
StringStart: PAnsiRec;
begin
if (S <> '') then
begin
StringStart := PAnsiRec(Pointer(S)-SizeOf(TAnsiRec));
if (StringStart^.RefCount <> -1) then
begin
Dec(StringStart^.RefCount);
end;
end;
end;
function IsStringConstant(constref S: UTF8String): Boolean;
var
StringStart: PAnsiRec;
begin
if (S <> '') then
begin
StringStart := PAnsiRec(Pointer(S)-SizeOf(TAnsiRec));
Result := StringStart^.RefCount = -1;
end
else
Result := True;
end;
{$IFOPT C+}
procedure AssertStringIsConstant(constref S: UTF8String);
var
StringStart: PAnsiRec;
begin
if (S <> '') then
begin
StringStart := PAnsiRec(Pointer(S)-SizeOf(TAnsiRec));
Assert(StringStart^.RefCount = -1);
end;
end;
{$ENDIF}
{$IFOPT C+}
procedure AssertStringIsReffed(constref S: UTF8String; const MinRef: Cardinal);
var
StringStart: PAnsiRec;
begin
if (S <> '') then
begin
StringStart := PAnsiRec(Pointer(S)-SizeOf(TAnsiRec));
Assert(StringStart^.RefCount >= MinRef);
end;
end;
{$ENDIF}
procedure TDebugObject.FreeInstance();
var
Instance: Pointer;
begin
CleanupInstance;
{$PUSH}
{$POINTERMATH ON}
Instance := Self;
FillChar(Instance^, InstanceSize, $66);
{$POP}
FreeMem(Pointer(Self));
end;
initialization
{$IFOPT C+}
AssertStringIsConstant('rtlutils.pas test');
{$ENDIF}
end.