Debian Codegraph @

src/base/castleutils_pointers.inc

raw
1 {%MainUnit castleutils.pas}
2 {
3 Copyright 2002-2018 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { Some helpers for pointer arithmetic, Get/FreeMem
18 and for "raw memory" scanning (IsMemCharFilled). }
19
20 {$ifdef read_interface}
21
22 { Pointer arithmetic. Will wrap over if you add too much. Add may be negative.
23 This is just a shortcut for @code(PtrInt(Result) := PtrInt(P)+Add),
24 without any range / overflow checks. }
25 function PointerAdd(p: pointer; add: integer): pointer;
26
27 function GetClearMem(Size: integer; ClearValue: byte = 0): pointer; overload;
28
29 { Safer version of FreeMem, checks is parameter @nil, and sets it to @nil
30 afterwards. }
31 procedure FreeMemNiling(var p: pointer);
32
33 { Check is memory filled with the given character.
34 Returns -1 is @true, or the number (between 0 .. Size - 1)
35 or the first character different than AChar. }
36 function CheckIsMemCharFilled(const Data; Size: Integer; AChar: AnsiChar): Integer;
37
38 { Check is memory filled with the given character.
39 Returns simple boolean, use CheckIsMemCharFilled to get more informative
40 result. }
41 function IsMemCharFilled(const Data; Size: Integer; AChar: AnsiChar): boolean;
42
43 { Check is memory filled with the Word (2 byte sequence).
44 Size if the number of words (not bytes), this is consequent
45 with Size parameter from FillWord from FPC's RTL. }
46 function IsMemWordFilled(const Data; Size: Integer; Value: Word): boolean;
47
48 { Check is memory filled with the DWord (4 byte sequence).
49 Size if the number of dwords (not bytes), this is consequent
50 with Size parameter from FillDWord from FPC's RTL. }
51 function IsMemDWordFilled(const Data; Size: Integer; Value: DWord): boolean;
52
53 { Calculate shift between A and B addresses (in bytes).
54 Useful to pass as offsets to methods like TGLSLAttribute.EnableArray
55 that wrap OpenGL vertex-array API.
56 This is simply Result := A - B, except we do some typecasting. }
57 function OffsetUInt(var A, B): PtrUInt;
58 function Offset(var A, B): Pointer;
59
60 { Compare two methods. }
61 function CompareMethods(const AMethod1, AMethod2: TMethod): boolean; deprecated 'use SameMethods';
62 function SameMethods(const AMethod1, AMethod2: TMethod): boolean;
63
64 {$endif read_interface}
65
66 {$ifdef read_implementation}
67
68 {$include norqcheckbegin.inc}
69 function PointerAdd(p: pointer; Add: integer): pointer;
70 { Ten kod moze powodowac falszywe alarmy co do rangeChecking / arith. overflow,
71 dlatego powinien dzialac z R-,Q- }
72 var
73 { PtrInt aliases for pointer types, to allow for
74 addition on them with Add (signed) without any warnings. }
75 ResultInt: PtrInt absolute Result;
76 PInt: PtrInt absolute P;
77 begin
78 ResultInt := PInt + Add;
79 end;
80 {$include norqcheckend.inc}
81
82 function GetClearMem(Size: integer; clearValue: byte{=0}): pointer;
83 begin result := GetMem(Size); FillChar(result^,Size, clearvalue) end;
84
85 procedure FreeMemNiling(var p: pointer);
86 begin
87 {$ifdef VER3_3} try {$endif}
88 if p <> nil then begin FreeMem(p); p := nil end;
89 {$ifdef VER3_3}
90 except
91 on E: EAccessViolation do
92 begin
93 if IsConsole then
94 Writeln(ErrOutput, 'WARNING: FreeMem failed with EAccessViolation, observed bug of FPC 3.3.1 when reading some (but not all) OggVorbis files. TODO: reproduce in simple testcase (right now: play_sounds on Linux/x86_64) and report. As a workaround, we just continue.');
95 Exit;
96 end;
97 end;
98 {$endif}
99 end;
100
101 function CheckIsMemCharFilled(const Data; Size: Integer; AChar: AnsiChar): Integer;
102 var i: Integer;
103 p: PAnsiChar;
104 begin
105 { TODO: hmm, taka optymalizacje jak w IsMemCharFilled moznaby tu zrobic,
106 idac po 32 lub 16.}
107 p := @Data;
108 for i := 0 to Size-1 do
109 begin
110 if p^<>AChar then begin result := i; Exit end;
111 Inc(p);
112 end;
113 result := -1;
114 end;
115
116 function IsMemCharFilled(const Data; Size: Integer; AChar: AnsiChar): boolean;
117 var p: Pointer;
118 p32: PUInt32 absolute p;
119 p16: PWord absolute p;
120 p8: PAnsiChar absolute p;
121 Good32: UInt32;
122 Good16: Word;
123 begin
124 { prosto moznaby ta funkcje zrealizowac jako
125 result := CheckIsMemCharFilled(Data, Size, AChar) = -1;
126 Ale jest ona dosc niskopoziomowa i w zwiazku z tym chce zeby dzialala
127 blyskawicznie nawet dla naprawde duzych Size.
128 Patrz time testy w TestCastleUtils - zysk czasowy okolo 4x dla Size
129 rzedu 10^2, 3,4 (naturalnie w pesymistycznym przypadku, dla danych
130 np. losowych kazdy algorytm bardzo szybko znajduje niezgodnosc...) }
131
132 p := @Data;
133
134 if Size >= 4 then
135 begin
136 FillChar(Good32, SizeOf(Good32), AChar);
137 repeat
138 if p32^<>Good32 then begin result := false; Exit end;
139 Dec(Size, 4);
140 Inc(p32);
141 until Size < 4;
142 end;
143
144 { teraz Size moze byc tylko 0, 1,2 lub 3. Czyli moze sie tam zmiescic tylko
145 raz liczba 2 bajtowa, Word. }
146 if Size >= 2 then
147 begin
148 FillChar(Good16, SizeOf(Good16), AChar);
149 if p16^<>Good16 then begin result := false; Exit end;
150 Dec(Size, 2);
151 Inc(p16);
152 end;
153
154 { teraz Size moze byc tylko 0 lub 1 }
155 result:=(Size = 0) or (p8^=AChar);
156 end;
157
158 function IsMemWordFilled(const Data; Size: Integer; Value: Word): boolean;
159 var i: Integer;
160 P: PWord;
161 begin
162 { TODO -- optimize this by using IsMemDWordFilled with
163 Value = Value or (Value shl 16). }
164 P := @Data;
165 for i := 1 to Size do
166 begin
167 if P^ <> Value then
168 begin
169 Result := false;
170 Exit;
171 end;
172 Inc(P);
173 end;
174 Result := true;
175 end;
176
177 function IsMemDWordFilled(const Data; Size: Integer; Value: DWord): boolean;
178 var i: Integer;
179 P: PDWord;
180 begin
181 P := @Data;
182 for i := 1 to Size do
183 begin
184 if P^ <> Value then
185 begin
186 Result := false;
187 Exit;
188 end;
189 Inc(P);
190 end;
191 Result := true;
192 end;
193
194 function OffsetUInt(var A, B): PtrUInt;
195 begin
196 Result := PtrUInt(@A) - PtrUInt(@B);
197 end;
198
199 function Offset(var A, B): Pointer;
200 begin
201 { additional PtrUInt typecast before Pointer, to avoid warning. }
202 Result := Pointer(PtrUInt( PtrUInt(@A) - PtrUInt(@B) ));
203 end;
204
205 function CompareMethods(const AMethod1, AMethod2: TMethod): boolean;
206 begin
207 Result := SameMethods(AMethod1, AMethod2);
208 end;
209
210 function SameMethods(const AMethod1, AMethod2: TMethod): boolean;
211 begin
212 Result := (AMethod1.Code = AMethod2.Code) and
213 (AMethod1.Data = AMethod2.Data);
214 end;
215
216 {$endif read_implementation}