-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompheap.pas
235 lines (198 loc) · 6.04 KB
/
compheap.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
{
$Id$
This file is part of the Free Pascal run time library.
Copyright (c) 1999 by Michael Van Canneyt, member of the
Free Pascal development team
Implements a memory manager that uses the C memory management.
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
unit compheap;
{$mode objfpc}
interface
implementation
const
blocksize = 16; { at least size of freerecord }
blockshr = 4; { shr value for blocksize=2^blockshr}
maxblocksize = 512+blocksize; { 1024+8 needed for heaprecord }
maxblock = maxblocksize div blocksize;
maxreusebigger = 8; { max reuse bigger tries }
usedmask = 1; { flag if the block is used or not }
beforeheapendmask = 2; { flag if the block is just before a heapptr }
sizemask = not(blocksize-1);
type
pfreerecord = ^tfreerecord;
tfreerecord = record
size : longint;
next,
prev : pfreerecord;
end; { 12 bytes }
pheaprecord = ^theaprecord;
theaprecord = record
{ this should overlap with tfreerecord }
size : longint;
end; { 4 bytes }
{$IFDEF Win32 }
Function Malloc (Size : Longint) : Pointer;cdecl; external 'msvcrt' name 'malloc';
Procedure Free (P : pointer); cdecl; external 'msvcrt' name 'free';
Procedure FreeMem (P : Pointer); cdecl; external 'msvcrt' name 'free';
function ReAlloc (P : Pointer; Size : longint) : pointer; cdecl; external 'msvcrt' name 'realloc';
Function CAlloc (unitSize,UnitCount : Longint) : pointer;cdecl;external 'msvcrt' name 'calloc';
{$ELSE }
Function Malloc (Size : Longint) : Pointer;cdecl; external 'c' name 'malloc';
Procedure Free (P : pointer); cdecl; external 'c' name 'free';
Procedure FreeMem (P : Pointer); cdecl; external 'c' name 'free';
function ReAlloc (P : Pointer; Size : longint) : pointer; cdecl; external 'c' name 'realloc';
Function CAlloc (unitSize,UnitCount : Longint) : pointer;cdecl;external 'c' name 'calloc';
{$ENDIF }
Var
OldMemoryManager : TMemoryManager;
Function CompGetMem (Size : Longint) : Pointer;
var
s: cardinal;
newsize: longint;
begin
newsize := size;
if newsize = 0 then
newsize := 1;
newsize:=(newsize+sizeof(theaprecord)+(blocksize-1)) and (not (blocksize-1));
s := newsize shr blockshr;
if s > maxblock then
begin
CompGetMem:=malloc(newsize);
pheaprecord(CompGetMem)^.size := newsize;
inc(CompGetMem,sizeof(theaprecord));
end
else
CompGetMem := OldMemoryManager.GetMem(size);
end;
Function CompFreeMemSize(var p:pointer;Size:Longint):Longint;
var
pcurr: pfreerecord;
pcurrsize,s: longint;
begin
pcurr:=pfreerecord(pointer(p)-sizeof(theaprecord));
pcurrsize:=pcurr^.size and sizemask;
s:=pcurrsize shr blockshr;
if s>maxblock then
begin
size:=(size+sizeof(theaprecord)+(blocksize-1)) and (not (blocksize-1));
if size<>pcurrsize then
RunError(204);
dec(p,sizeof(theaprecord));
free(p);
CompFreeMemSize := pcurrsize;
end
else
CompFreeMemSize := OldMemoryManager.FreeMemSize(p,size);
end;
Function CompFreeMem (Var P : pointer) : Longint;
var
pcurr: pfreerecord;
pcurrsize, s,size: longint;
begin
pcurr:=pfreerecord(pointer(p)-sizeof(theaprecord));
pcurrsize:=pcurr^.size and sizemask;
s:=pcurrsize shr blockshr;
if s>maxblock then
begin
size:=(pcurrsize+sizeof(theaprecord)+(blocksize-1)) and (not (blocksize-1));
dec(p,sizeof(theaprecord));
free(p);
p := nil;
CompFreeMem := pcurrsize;
end
else
CompFreeMem := OldMemoryManager.FreeMem(p);
end;
Function CompReAllocMem (var p:pointer;Size:longint):Pointer;
var
pcurr: pfreerecord;
pcurrsize,newsize: longint;
newp: pointer;
begin
if p = nil then
begin
CompReAllocMem := CompGetMem(size);
p := result;
exit;
end;
pcurr:=pfreerecord(pointer(p)-sizeof(theaprecord));
pcurrsize:=pcurr^.size and sizemask;
newsize:=(size+sizeof(theaprecord)+(blocksize-1)) and (not (blocksize-1));
if pcurrsize <= maxblocksize then
if newsize <= maxblocksize then
OldMemoryManager.ReallocMem(p,size)
else
begin
newp := CompGetMem(size);
move(p^,newp^,MemSize(p));
CompFreeMem(p);
p := newp
end
else
if newsize <= maxblocksize then
begin
newp := CompGetMem(size);
move(p^,newp^,size);
CompFreeMem(p);
p := newp;
end
else
begin
dec(p,sizeof(theaprecord));
p := Realloc(p,newsize);
pheaprecord(p)^.size := newsize;
inc(p,sizeof(theaprecord));
end;
CompReallocMem := p;
end;
Function CMemSize (p:pointer): Longint;
begin
Result:=OldMemoryManager.MemSize(p);
end;
Function CMemAvail : Longint;
begin
Result:=0;
end;
Function CMaxAvail: Longint;
begin
Result:=0;
end;
Function CHeapSize : Longint;
begin
Result:=0;
end;
Const
CMemoryManager : TMemoryManager =
(
GetMem : {$ifdef fpc}@{$endif}CompGetmem;
FreeMem : {$ifdef fpc}@{$endif}CompFreeMem;
FreememSize : {$ifdef fpc}@{$endif}CompFreememSize;
AllocMem : {$ifdef fpc}@{$endif}CompGetMem;
ReallocMem : {$ifdef fpc}@{$endif}CompReAllocMem;
MemSize : {$ifdef fpc}@{$endif}CMemSize;
MemAvail : {$ifdef fpc}@{$endif fpc}CMemAvail;
MaxAvail : {$ifdef fpc}@{$endif}MaxAvail;
HeapSize : {$ifdef fpc}@{$endif}CHeapSize;
);
Initialization
GetMemoryManager (OldMemoryManager);
SetMemoryManager (CmemoryManager);
Finalization
SetMemoryManager (OldMemoryManager);
end.
{
$Log: compheap.pas,v $
Revision 1.1 2002/06/06 20:05:37 mk
- added compheap as memory manager (disabled)
Revision 1.4 2001/06/07 16:34:41 jonas
* added ifdef fpc round @ for procvars
Revision 1.3 2001/06/07 16:14:48 marco
* Fixed @ procvar
Revision 1.2 2000/07/13 11:33:10 michael
+ removed logs
}