-
Notifications
You must be signed in to change notification settings - Fork 3
/
JUDASMEM.C
239 lines (203 loc) · 6.89 KB
/
JUDASMEM.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
/*
* JUDAS DPMI memory locking functions. Includes also functions to
* allocate/free locked memory. Feel free to use in your own proggys.
*/
#define FALSE 0
#define TRUE 1
#define NULL 0
/*
* Borland / Watcom REGS structure compatibility
*/
#ifdef __DJGPP__
#define _BORLAND_DOS_REGS
#endif
#include <stdlib.h>
#include <mem.h>
#include <dos.h>
#include <stdio.h> // temporary
#include <string.h>
#ifdef __DJGPP__
#include <dpmi.h>
#include <go32.h>
#include <sys/nearptr.h>
#include <sys/farptr.h>
#endif
int judas_memlock(void *start, unsigned size);
int judas_memunlock(void *start, unsigned size);
void *locked_malloc(int size);
void locked_free(void *address);
void *dos_malloc(int size);
void dos_free(void *address);
int DPMI_MapMemory (unsigned long *physaddress, unsigned long *linaddress, unsigned long size);
int DPMI_UnmapMemory (unsigned long *linaddress);
/*
* We allocate 4 bytes more and store the block size before the actual block
* passed to user. Afterwards, when freeing, we know the size (needed in
* unlocking!)
*/
void *locked_malloc(int size)
{
unsigned *block = malloc(size + sizeof(unsigned));
if (!block) return NULL;
*block = size;
if (!judas_memlock(block, size + sizeof(unsigned)))
{
free(block);
return NULL;
}
block++;
return block;
}
void locked_free(void *address)
{
unsigned *block;
if (!address) return;
block = (unsigned *)address - 1;
judas_memunlock(block, *block);
free(block);
}
int judas_memlock(void *start, unsigned size)
{
unsigned *address;
#ifdef __DJGPP__
__dpmi_meminfo dpmimeminfo;
__dpmi_get_segment_base_address(_my_ds(), (unsigned long *)&address);
address = (unsigned *)((unsigned char*)address + (unsigned)start);
dpmimeminfo.address = (unsigned long)address;
dpmimeminfo.size = (unsigned long)size;
if (__dpmi_lock_linear_region(&dpmimeminfo) == -1) return 0;
else return 1;
#else
union REGS glenregs;
address = (unsigned *)start;
memset(&glenregs, 0, sizeof(glenregs));
glenregs.w.ax = 0x600;
glenregs.w.bx = (unsigned)address >> 16;
glenregs.w.cx = (unsigned)address & 0xffff;
glenregs.w.si = size >> 16;
glenregs.w.di = size & 0xffff;
int386(0x31, &glenregs, &glenregs);
if (glenregs.w.cflag) return 0;
return 1;
#endif
}
int judas_memunlock(void *start, unsigned size)
{
unsigned *address;
#ifdef __DJGPP__
__dpmi_meminfo dpmimeminfo;
__dpmi_get_segment_base_address(_my_ds(), (unsigned long *)&address);
address = (unsigned *)((unsigned char*)address + (unsigned)start);
dpmimeminfo.address = (unsigned long)address;
dpmimeminfo.size = (unsigned long)size;
if (__dpmi_unlock_linear_region(&dpmimeminfo) == -1) return 0;
else return 1;
#else
union REGS glenregs;
address = (unsigned *)start;
memset(&glenregs, 0, sizeof(glenregs));
glenregs.w.ax = 0x601;
glenregs.w.bx = (unsigned)address >> 16;
glenregs.w.cx = (unsigned)address & 0xffff;
glenregs.w.si = size >> 16;
glenregs.w.di = size & 0xffff;
int386(0x31, &glenregs, &glenregs);
if (glenregs.w.cflag) return 0;
return 1;
#endif
}
void *dos_malloc(int size)
{
unsigned *address;
#ifdef __DJGPP__
int selector;
/* allocate 16 bytes more to store selector of allocated block and align black on para */
if (__dpmi_allocate_dos_memory(((size + 16) >> 4) + 1, &selector) == -1) return 0;
__dpmi_get_segment_base_address(selector, (unsigned long *)&address);
_farpokel (_dos_ds, (unsigned) address, selector);
address += 4;
return address;
#else
union REGS glenregs;
/* allocate 16 bytes more to store selector of allocated block and align block on parag */
memset(&glenregs, 0, sizeof(glenregs));
glenregs.w.ax = 0x100;
glenregs.w.bx = (unsigned) (((size + 16) >> 4) + 1);
int386(0x31, &glenregs, &glenregs);
if (glenregs.w.cflag) return 0;
address = (unsigned *) (glenregs.w.ax << 4);
*address = (unsigned) glenregs.w.dx;
address += 4;
return address;
#endif
}
void dos_free(void *address)
{
#ifdef __DJGPP__
__dpmi_free_dos_memory (_farpeekl (_dos_ds, (unsigned) ((unsigned *)address - 4) ) );
#else
union REGS glenregs;
memset(&glenregs, 0, sizeof(glenregs));
glenregs.w.ax = 0x101;
glenregs.w.dx = (unsigned) *((unsigned *)address - 4);
int386(0x31, &glenregs, &glenregs);
#endif
}
int DPMI_MapMemory (unsigned long *physaddress, unsigned long *linaddress, unsigned long size)
{
#ifdef __DJGPP__
__dpmi_meminfo dpmimeminfo;
unsigned *address;
__dpmi_get_segment_base_address(_my_ds(), (unsigned long *)&address);
// printf ("DS base Address = %#06x\n", (unsigned int)address);
dpmimeminfo.address = (unsigned long)*physaddress;
dpmimeminfo.size = (unsigned long)size;
// printf ("Physical Address = %#06x\n", (unsigned int)dpmimeminfo.address);
if (__dpmi_physical_address_mapping (&dpmimeminfo) == -1) {
*linaddress = 0;
return FALSE;
}
// printf ("Linear Address = %#06x\n", (unsigned int)dpmimeminfo.address);
dpmimeminfo.address -= (unsigned long)address;
// printf ("Final Address = %#06x\n", (unsigned int)dpmimeminfo.address);
*linaddress = (unsigned long)dpmimeminfo.address;
return TRUE;
#else
union REGS r;
memset(&r, 0, sizeof(r));
r.x.ebx = (*physaddress) >> 16;
r.x.ecx = (*physaddress);
r.x.esi = size >> 16;
r.x.edi = size;
r.x.eax = 0x800;
int386 (0x31, &r, &r);
if (r.x.cflag) {
*linaddress = 0;
return FALSE;
}
*linaddress = (r.w.bx << 16) + r.w.cx;
return TRUE;
#endif
}
int DPMI_UnmapMemory (unsigned long *linaddress)
{
#ifdef __DJGPP__
__dpmi_meminfo dpmimeminfo;
unsigned *address;
__dpmi_get_segment_base_address(_my_ds(), (unsigned long *)&address);
memset(&dpmimeminfo, 0, sizeof(dpmimeminfo));
dpmimeminfo.address = (unsigned long)*linaddress;
dpmimeminfo.address += (unsigned long)address;
if (__dpmi_free_physical_address_mapping (&dpmimeminfo) == -1) return FALSE;
return TRUE;
#else
union REGS r;
memset(&r, 0, sizeof(r));
r.x.ebx = (*linaddress) >> 16;
r.x.ecx = (*linaddress);
r.x.eax = 0x801;
int386 (0x31, &r, &r);
if (r.x.cflag) return FALSE;
return TRUE;
#endif
}