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
|
{
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 cmem;
interface
Const
{$if defined(go32v2) or defined(wii)}
{$define USE_STATIC_LIBC}
{$endif}
{$if defined(win32)}
LibName = 'msvcrt';
{$elseif defined(win64)}
LibName = 'msvcrt';
{$elseif defined(wince)}
LibName = 'coredll';
{$elseif defined(netware)}
LibName = 'clib';
{$elseif defined(netwlibc)}
LibName = 'libc';
{$elseif defined(macos)}
LibName = 'StdCLib';
{$elseif defined(beos)}
LibName = 'root';
{$else}
LibName = 'c';
{$endif}
{$ifdef USE_STATIC_LIBC}
{$linklib c}
Function malloc (Size : ptruint) : Pointer;cdecl; external;
Procedure free (P : pointer); cdecl; external;
function realloc (P : Pointer; Size : ptruint) : pointer;cdecl; external;
Function calloc (unitSize,UnitCount : ptruint) : pointer;cdecl; external;
{$else not USE_STATIC_LIBC}
Function Malloc (Size : ptruint) : Pointer; cdecl; external LibName name 'malloc';
Procedure Free (P : pointer); cdecl; external LibName name 'free';
function ReAlloc (P : Pointer; Size : ptruint) : pointer; cdecl; external LibName name 'realloc';
Function CAlloc (unitSize,UnitCount : ptruint) : pointer; cdecl; external LibName name 'calloc';
{$endif not USE_STATIC_LIBC}
implementation
Function CGetMem (Size : ptruint) : Pointer;
begin
CGetMem:=Malloc(Size+sizeof(ptruint));
if (CGetMem <> nil) then
begin
Pptruint(CGetMem)^ := size;
inc(CGetMem,sizeof(ptruint));
end;
end;
Function CFreeMem (P : pointer) : ptruint;
begin
if (p <> nil) then
dec(p,sizeof(ptruint));
Free(P);
CFreeMem:=0;
end;
Function CFreeMemSize(p:pointer;Size:ptruint):ptruint;
begin
if size<=0 then
exit;
if (p <> nil) then
begin
if (size <> Pptruint(p-sizeof(ptruint))^) then
runerror(204);
end;
CFreeMemSize:=CFreeMem(P);
end;
Function CAllocMem(Size : ptruint) : Pointer;
begin
CAllocMem:=calloc(Size+sizeof(ptruint),1);
if (CAllocMem <> nil) then
begin
Pptruint(CAllocMem)^ := size;
inc(CAllocMem,sizeof(ptruint));
end;
end;
Function CReAllocMem (var p:pointer;Size:ptruint):Pointer;
begin
if size=0 then
begin
if p<>nil then
begin
dec(p,sizeof(ptruint));
free(p);
p:=nil;
end;
end
else
begin
inc(size,sizeof(ptruint));
if p=nil then
p:=malloc(Size)
else
begin
dec(p,sizeof(ptruint));
p:=realloc(p,size);
end;
if (p <> nil) then
begin
Pptruint(p)^ := size-sizeof(ptruint);
inc(p,sizeof(ptruint));
end;
end;
CReAllocMem:=p;
end;
Function CMemSize (p:pointer): ptruint;
begin
CMemSize:=Pptruint(p-sizeof(ptruint))^;
end;
function CGetHeapStatus:THeapStatus;
var res: THeapStatus;
begin
fillchar(res,sizeof(res),0);
CGetHeapStatus:=res;
end;
function CGetFPCHeapStatus:TFPCHeapStatus;
begin
fillchar(CGetFPCHeapStatus,sizeof(CGetFPCHeapStatus),0);
end;
Const
CMemoryManager : TMemoryManager =
(
NeedLock : false;
GetMem : @CGetmem;
FreeMem : @CFreeMem;
FreememSize : @CFreememSize;
AllocMem : @CAllocMem;
ReallocMem : @CReAllocMem;
MemSize : @CMemSize;
InitThread : nil;
DoneThread : nil;
RelocateHeap : nil;
GetHeapStatus : @CGetHeapStatus;
GetFPCHeapStatus: @CGetFPCHeapStatus;
);
Var
OldMemoryManager : TMemoryManager;
Initialization
GetMemoryManager (OldMemoryManager);
SetMemoryManager (CmemoryManager);
Finalization
SetMemoryManager (OldMemoryManager);
end.
|