Debian Codegraph @

components/kascrypt/Hashes/Private/memh.pas

raw
1 unit memh;
2
3 {Basic portable heap memory allocation functions}
4
5 interface
6
7 {$i STD.INC}
8
9 (*************************************************************************
10
11 DESCRIPTION : Basic portable heap memory allocation functions
12
13 REQUIREMENTS : TP5-7, D1-D7/D9-D10/D12/D17-D18, FPC, VP, WDOSX
14
15 EXTERNAL DATA : ---
16
17 MEMORY USAGE : ---
18
19 DISPLAY MODE : ---
20
21 REMARK : 16-bit compilers and sizes >= $10000: alloc functions
22 return nil, free procedures do nothing!
23
24
25 Version Date Author Modification
26 ------- -------- ------- ------------------------------------------
27 0.10 15.04.14 W.Ehrhardt Initial version with malloc/mfree
28 0.11 16.04.14 we calloc/cfree
29 0.12 16.04.14 we use untyped var p in free routines
30 0.13 17.04.14 we ialloc with longint size
31 0.14 17.04.14 we long versions
32 0.15 18.04.14 we remove word versions, rename long versions
33 **************************************************************************)
34
35 (*-------------------------------------------------------------------------
36 (C) Copyright 2014 Wolfgang Ehrhardt
37
38 This software is provided 'as-is', without any express or implied warranty.
39 In no event will the authors be held liable for any damages arising from
40 the use of this software.
41
42 Permission is granted to anyone to use this software for any purpose,
43 including commercial applications, and to alter it and redistribute it
44 freely, subject to the following restrictions:
45
46 1. The origin of this software must not be misrepresented; you must not
47 claim that you wrote the original software. If you use this software in
48 a product, an acknowledgment in the product documentation would be
49 appreciated but is not required.
50
51 2. Altered source versions must be plainly marked as such, and must not be
52 misrepresented as being the original software.
53
54 3. This notice may not be removed or altered from any source distribution.
55 ----------------------------------------------------------------------------*)
56
57
58 function malloc(size: longint): pointer;
59 {-Allocate heap, return nil if error}
60
61 function calloc(size: longint): pointer;
62 {-Allocate heap, return nil if error, clear allocated memory to 0}
63
64 procedure mfree(var p; size: longint);
65 {-Deallocate heap if p<>nil, p will be set to nil}
66
67 procedure cfree(var p; size: longint);
68 {-Deallocate heap if p<>nil, p will be set to nil, memory set to 0}
69
70
71 implementation
72
73
74 {$ifdef BIT16}
75 {$F+}
76 {--------------------------------------------------------------------------}
77 function HeapFunc(Size: word): integer;
78 {-Forces nil return values instead of runtime error if out of memory}
79 begin
80 if size>0 then HeapFunc := 1;
81 end;
82
83
84 {---------------------------------------------------------------------------}
85 function ialloc(size: longint; set0: boolean): pointer;
86 {-Allocate heap, return nil if error, clear allocated memory to 0 if set0}
87 var
88 p, SaveHeapError : pointer;
89 wsize: word absolute size;
90 type
91 LH = packed record L,H: word; end;
92 begin
93 if LH(size).H<>0 then ialloc := nil
94 else begin
95 SaveHeapError := HeapError;
96 HeapError := @HeapFunc;
97 getmem(p, wsize);
98 HeapError := SaveHeapError;
99 if (p<>nil) and set0 then fillchar(p^,wsize,0);
100 ialloc := p;
101 end;
102 end;
103
104
105 {---------------------------------------------------------------------------}
106 procedure ifree(var p; size: longint; set0: boolean);
107 {-Dellocate heap if p<>nil, set p=nil, clear allocated memory to 0 if set0}
108 var
109 pp: pointer absolute p;
110 wsize: word absolute size;
111 type
112 LH = packed record L,H: word; end;
113 begin
114 if (pp<>nil) and (LH(size).H=0) then begin
115 if set0 then fillchar(pp^, wsize, 0);
116 freemem(pp, wsize);
117 pp := nil;
118 end;
119 end;
120
121 {$else}
122
123
124 {---------------------------------------------------------------------------}
125 procedure ifree(var p; size: longint; set0: boolean);
126 {-Dellocate heap if p<>nil, set p=nil, clear allocated memory to 0 if set0}
127 var
128 pp: pointer absolute p;
129 begin
130 if pp<>nil then begin
131 if set0 then fillchar(pp^, size, 0);
132 freemem(pp, size);
133 pp := nil;
134 end;
135 end;
136
137
138 {$ifdef FPC}
139
140 {---------------------------------------------------------------------------}
141 function ialloc(size: longint; set0: boolean): pointer;
142 {-Allocate heap, return nil if error, clear allocated memory to 0 if set0}
143 var
144 p: pointer;
145 sh: boolean;
146 begin
147 sh := ReturnNilIfGrowHeapFails;
148 ReturnNilIfGrowHeapFails := true;
149 getmem(p, size);
150 ReturnNilIfGrowHeapFails := sh;
151 if (p<>nil) and set0 then fillchar(p^,size,0);
152 ialloc := p;
153 end;
154
155 {$else}
156
157 {---------------------------------------------------------------------------}
158 function ialloc(size: longint; set0: boolean): pointer;
159 {-Allocate heap, return nil if error, clear allocated memory to 0 if set0}
160 var
161 p: pointer;
162 begin
163 try
164 getmem(p, size);
165 except
166 p := nil;
167 end;
168 if (p<>nil) and set0 then fillchar(p^,size,0);
169 ialloc := p;
170 end;
171
172 {$endif}
173
174 {$endif}
175
176
177 {---------------------------------------------------------------------------}
178 function malloc(size: longint): pointer;
179 {-Allocate heap, return nil if error}
180 begin
181 malloc := ialloc(size,false);
182 end;
183
184
185 {---------------------------------------------------------------------------}
186 function calloc(size: longint): pointer;
187 {-Allocate heap, return nil if error, clear allocated memory to 0}
188 begin
189 calloc := ialloc(size,true);
190 end;
191
192
193 {---------------------------------------------------------------------------}
194 procedure mfree(var p; size: longint);
195 {-Deallocate heap if p<>nil, p will be set to nil}
196 begin
197 ifree(p,size,false);
198 end;
199
200
201 {---------------------------------------------------------------------------}
202 procedure cfree(var p; size: longint);
203 {-Deallocate heap if p<>nil, p will be set to nil, memory set to 0}
204 begin
205 ifree(p,size,true);
206 end;
207
208
209 end.