/
ifsmem.c
332 lines (276 loc) · 7.33 KB
/
ifsmem.c
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <dos.h>
#define INCL_DOS
#define INCL_DOSDEVIOCTL
#define INCL_DOSDEVICES
#define INCL_DOSERRORS
#include "os2.h"
#include "portable.h"
#include "fat32ifs.h"
#define HEAP_SIZE (0xFFF0)
#define MAX_SELECTORS 100
#define RESERVED_SEGMENT (PVOID)0x00000001
static BOOL IsBlockFree(PBYTE pMCB);
static ULONG BlockSize(PBYTE pMCB);
static VOID SetFree(PBYTE pMCB);
static VOID SetInUse(PBYTE pMCB);
static VOID SetBlockSize(PBYTE pMCB, ULONG ulSize);
static void * FindFreeSpace(void * pStart, size_t tSize);
static VOID GetMemAccess(VOID);
static VOID ReleaseMemAccess(VOID);
static void * rgpSegment[MAX_SELECTORS] = {0};
static BOOL fLocked = FALSE;
static ULONG ulMemSem = 0UL;
VOID CheckHeap(VOID)
{
USHORT usSel;
BYTE _huge * pHeapStart;
BYTE _huge * pHeapEnd;
BYTE _huge * pWork;
USHORT rc;
for (usSel = 0; usSel < MAX_SELECTORS; usSel++)
{
pHeapStart = rgpSegment[usSel];
if (!pHeapStart || pHeapStart == RESERVED_SEGMENT)
continue;
rc = MY_PROBEBUF(PB_OPREAD, (PBYTE)pHeapStart, HEAP_SIZE);
if (rc)
{
CritMessage("FAT32: Protection VIOLATION in CheckHeap (SYS%d)", rc);
Message("FAT32: Protection VIOLATION in CheckHeap (SYS%d)", rc);
return;
}
pHeapEnd = pHeapStart + HEAP_SIZE;
pWork = pHeapStart;
while (pWork < pHeapEnd)
pWork += BlockSize(pWork) + sizeof (ULONG);
if (pWork != pHeapEnd)
CritMessage("FAT32: Heap corruption found!");
}
}
/*********************************************************************
* malloc
*********************************************************************/
#ifdef __WATCOM
_WCRTLINK void * malloc(size_t tSize)
#else
void * cdecl malloc(size_t tSize)
#endif
{
USHORT usSel;
void * pRet;
/* CheckHeap();*/
GetMemAccess();
if (tSize % 2)
tSize++;
if( tSize > 0 )
{
for (usSel = 0; usSel < MAX_SELECTORS; usSel++)
{
if (rgpSegment[usSel] && rgpSegment[usSel] != RESERVED_SEGMENT)
{
pRet = FindFreeSpace(rgpSegment[usSel], tSize);
if (pRet)
goto malloc_exit;
}
}
for (usSel = 0; usSel < MAX_SELECTORS; usSel++)
{
if (!rgpSegment[usSel])
{
rgpSegment[usSel] = RESERVED_SEGMENT;
rgpSegment[usSel] = gdtAlloc(HEAP_SIZE, TRUE);
if (!rgpSegment[usSel])
{
CritMessage("FAT32: No gdtSelector for heap!");
pRet = NULL;
goto malloc_exit;
}
SetBlockSize(rgpSegment[usSel], HEAP_SIZE - sizeof (ULONG));
SetFree(rgpSegment[usSel]);
pRet = FindFreeSpace(rgpSegment[usSel], tSize);
if (pRet)
goto malloc_exit;
}
}
}
if (f32Parms.fMessageActive & LOG_MEM)
Message("Malloc failed, calling gdtAlloc");
pRet = gdtAlloc(tSize ? ( ULONG )tSize : 65536L, TRUE);
malloc_exit:
if (f32Parms.fMessageActive & LOG_MEM)
Message("malloc %lu bytes at %lX", tSize ? ( ULONG )tSize : 65536L, pRet);
ReleaseMemAccess();
return pRet;
}
/*********************************************************************
* FindFreeSpace
*********************************************************************/
void * FindFreeSpace(void * pStart, size_t tSize)
{
BYTE _huge * pHeapStart;
BYTE _huge * pHeapEnd;
BYTE _huge * pWork;
BYTE _huge * pNext;
USHORT rc;
pHeapStart = pStart;
pHeapEnd = pHeapStart + HEAP_SIZE;
rc = MY_PROBEBUF(PB_OPREAD, (PBYTE)pHeapStart, HEAP_SIZE);
if (rc)
{
CritMessage("FAT32: Protection VIOLATION in FindFreeSpace (SYS%d)", rc);
Message("FAT32: Protection VIOLATION in FindFreeSpace (SYS%d)", rc);
return NULL;
}
pWork = pHeapStart;
while (pWork < pHeapEnd)
{
if (BlockSize(pWork) >= tSize && IsBlockFree(pWork))
{
ULONG ulRemaining = BlockSize(pWork) - tSize;
if (ulRemaining > sizeof (ULONG))
{
pNext = pWork + sizeof (ULONG) + tSize;
SetBlockSize(pNext, BlockSize(pWork) - tSize - sizeof (ULONG));
SetFree(pNext);
SetBlockSize(pWork, tSize);
}
SetInUse(pWork);
return pWork + sizeof (ULONG);
}
pWork += BlockSize(pWork) + sizeof (ULONG);
}
return NULL;
}
#ifdef __WATCOM
_WCRTLINK void free(void * pntr)
#else
void cdecl free(void * pntr)
#endif
{
USHORT usSel;
BYTE _huge * pHeapStart;
BYTE _huge * pHeapEnd;
BYTE _huge * pWork;
BYTE _huge * pToFree = pntr;
BYTE _huge * pPrev;
BYTE _huge * pNext;
USHORT rc;
if (f32Parms.fMessageActive & LOG_MEM)
Message("free %lX", pntr);
/* CheckHeap();*/
if (OFFSETOF(pntr) == 0)
{
freeseg(pntr);
return;
}
GetMemAccess();
for (usSel = 0; usSel < MAX_SELECTORS; usSel++)
{
if (SELECTOROF(pntr) == SELECTOROF(rgpSegment[usSel]))
break;
}
if (usSel == MAX_SELECTORS)
{
CritMessage("FAT32: %lX not found in free!", pntr);
Message("FAT32: %lX not found in free!", pntr);
ReleaseMemAccess();
return;
}
pHeapStart = rgpSegment[usSel];
pHeapEnd = pHeapStart + HEAP_SIZE;
rc = MY_PROBEBUF(PB_OPREAD, (PBYTE)pHeapStart, HEAP_SIZE);
if (rc)
{
CritMessage("FAT32: Protection VIOLATION in free (SYS%d)", rc);
Message("FAT32: Protection VIOLATION in free (SYS%d)", rc);
ReleaseMemAccess();
return;
}
pWork = pHeapStart;
pPrev = NULL;
while (pWork < pHeapEnd)
{
if (pWork + sizeof (ULONG) == pToFree)
{
if (pPrev && IsBlockFree(pPrev))
{
SetBlockSize(pPrev,
BlockSize(pPrev) + BlockSize(pWork) + sizeof (ULONG));
pWork = pPrev;
}
pNext = pWork + BlockSize(pWork) + sizeof (ULONG);
if (pNext < pHeapEnd && IsBlockFree(pNext))
SetBlockSize(pWork, BlockSize(pWork) + BlockSize(pNext) + sizeof (ULONG));
SetFree(pWork);
break;
}
else
pPrev = pWork;
pWork += BlockSize(pWork) + sizeof (ULONG);
}
if (pWork >= pHeapEnd)
{
CritMessage("FAT32: ERROR: Address not found in free");
Message("ERROR: Address not found in free");
ReleaseMemAccess();
return;
}
/*
free selector if no longer needed
*/
if (usSel > 0 &&
BlockSize(rgpSegment[usSel]) == (HEAP_SIZE - sizeof (ULONG)) &&
IsBlockFree(rgpSegment[usSel]))
{
PBYTE p = rgpSegment[usSel];
rgpSegment[usSel] = NULL;
freeseg(p);
}
ReleaseMemAccess();
}
BOOL IsBlockFree(PBYTE pMCB)
{
return (BOOL)(*((PULONG)pMCB) & 1L);
}
ULONG BlockSize(PBYTE pMCB)
{
return *((PULONG)pMCB) & ~1L;
}
VOID SetFree(PBYTE pMCB)
{
*((PULONG)pMCB) |= 1L;
}
VOID SetInUse(PBYTE pMCB)
{
*((PULONG)pMCB) &= ~1L;
}
VOID SetBlockSize(PBYTE pMCB, ULONG ulSize)
{
BOOL fFree = IsBlockFree(pMCB);
*((PULONG)pMCB) = ulSize;
if (fFree)
SetFree(pMCB);
}
VOID GetMemAccess(VOID)
{
APIRET rc;
if (f32Parms.fMessageActive & LOG_WAIT)
Message("Waiting for a heap access");
rc = FSH_SEMREQUEST(&ulMemSem,-1);
if (rc == NO_ERROR)
{
fLocked = TRUE;
}
}
VOID ReleaseMemAccess(VOID)
{
APIRET rc;
if (fLocked)
{
rc = FSH_SEMCLEAR(&ulMemSem);
}
}