-
Notifications
You must be signed in to change notification settings - Fork 1
/
rtlutils.pas
65 lines (56 loc) · 1.38 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
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit rtlutils;
interface
function GetRefCount(constref S: UTF8String): SizeInt; 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;
Dummy: DWord;
RefCount: SizeInt;
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;
{$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}
end.