-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrtlutils.pas
109 lines (98 loc) · 2.35 KB
/
rtlutils.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
{$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;
{$IFOPT C+} procedure AssertStringIsConstant(constref S: UTF8String); {$ENDIF}
{$IFOPT C+} procedure AssertStringIsReffed(constref S: UTF8String; const MinRef: Cardinal); {$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;
{$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}
initialization
{$IFOPT C+}
AssertStringIsConstant('rtlutils.pas test');
{$ENDIF}
end.