Subversion Repositories SvarDOS

Rev

Rev 987 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 987 Rev 1101
1
/* This file is part of the SvarCOM project and is published under the terms
1
/* This file is part of the SvarCOM project and is published under the terms
2
 * of the MIT license.
2
 * of the MIT license.
3
 *
3
 *
4
 * Copyright (C) 2021-2022 Mateusz Viste
4
 * Copyright (C) 2021-2022 Mateusz Viste
5
 *
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * Permission is hereby granted, free of charge, to any person obtaining a
7
 * copy of this software and associated documentation files (the "Software"),
7
 * copy of this software and associated documentation files (the "Software"),
8
 * to deal in the Software without restriction, including without limitation
8
 * to deal in the Software without restriction, including without limitation
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * and/or sell copies of the Software, and to permit persons to whom the
11
 * Software is furnished to do so, subject to the following conditions:
11
 * Software is furnished to do so, subject to the following conditions:
12
 *
12
 *
13
 * The above copyright notice and this permission notice shall be included in
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
14
 * all copies or substantial portions of the Software.
15
 *
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
 * DEALINGS IN THE SOFTWARE.
22
 * DEALINGS IN THE SOFTWARE.
23
 */
23
 */
24
 
24
 
25
#include <i86.h>
25
#include <i86.h>
26
#include <string.h>
26
#include <string.h>
27
 
27
 
28
#include "env.h"
28
#include "env.h"
29
#include "helpers.h"
29
#include "helpers.h"
30
 
30
 
31
#include "rmodinit.h"
31
#include "rmodinit.h"
32
 
32
 
33
 
33
 
34
/* returns far pointer to rmod's settings block on success */
34
/* returns far pointer to rmod's settings block on success */
35
struct rmod_props far *rmod_install(unsigned short envsize, unsigned char *rmodcore, unsigned short rmodcore_len) {
35
struct rmod_props far *rmod_install(unsigned short envsize, unsigned char *rmodcore, unsigned short rmodcore_len) {
36
  char far *myptr, far *mcb;
36
  char far *myptr, far *mcb;
37
  unsigned short far *owner;
37
  unsigned short far *owner;
38
  const unsigned short sizeof_rmodandprops_paras = (0x100 + rmodcore_len + sizeof(struct rmod_props) + 15) / 16;
38
  const unsigned short sizeof_rmodandprops_paras = (0x100 + rmodcore_len + sizeof(struct rmod_props) + 15) / 16;
39
  unsigned short rmodseg = 0xffff;
39
  unsigned short rmodseg = 0xffff;
40
  unsigned short envseg, origenvseg;
40
  unsigned short envseg, origenvseg;
41
  struct rmod_props far *res;
41
  struct rmod_props far *res;
42
 
42
 
43
  /* read my current env segment from PSP and save it */
43
  /* read my current env segment from PSP and save it */
44
  envseg = *((unsigned short *)0x2c);
44
  envseg = *((unsigned short *)0x2c);
45
  origenvseg = envseg;
45
  origenvseg = envseg;
46
 
46
 
47
  /* printf("original (PSP) env buffer at %04X\r\n", envseg); */
47
  /* printf("original (PSP) env buffer at %04X\r\n", envseg); */
48
 
48
 
49
  /* if envseg is zero, then enforce our own one (MSDOS 5 does not provide a default env) */
49
  /* if envseg is zero, then enforce our own one (MSDOS 5 does not provide a default env) */
50
  if ((envseg == 0) && (envsize == 0)) envsize = 256;
50
  if ((envseg == 0) && (envsize == 0)) envsize = 256;
51
 
51
 
52
  /* if custom envsize requested, convert it to number of paragraphs */
52
  /* if custom envsize requested, convert it to number of paragraphs */
53
  if (envsize != 0) {
53
  if (envsize != 0) {
54
    envsize += 15;
54
    envsize += 15;
55
    envsize /= 16;
55
    envsize /= 16;
56
  }
56
  }
57
 
57
 
58
  _asm {
58
  _asm {
59
    /* link in the UMB memory chain for enabling high-memory allocation (and save initial status on stack) */
59
    /* link in the UMB memory chain for enabling high-memory allocation (and save initial status on stack) */
60
    mov ax, 0x5802  /* GET UMB LINK STATE */
60
    mov ax, 0x5802  /* GET UMB LINK STATE */
61
    int 0x21
61
    int 0x21
62
    xor ah, ah
62
    xor ah, ah
63
    push ax         /* save link state on stack */
63
    push ax         /* save link state on stack */
64
    mov ax, 0x5803  /* SET UMB LINK STATE */
64
    mov ax, 0x5803  /* SET UMB LINK STATE */
65
    mov bx, 1
65
    mov bx, 1
66
    int 0x21
66
    int 0x21
67
    /* get current allocation strategy and save it in DX */
67
    /* get current allocation strategy and save it in DX */
68
    mov ax, 0x5800
68
    mov ax, 0x5800
69
    int 0x21
69
    int 0x21
70
    push ax
70
    push ax
71
    pop dx
71
    pop dx
72
    /* set strategy to 'last fit, try high then low memory' */
72
    /* set strategy to 'last fit, try high then low memory' */
73
    mov ax, 0x5801
73
    mov ax, 0x5801
74
    mov bx, 0x0082
74
    mov bx, 0x0082
75
    int 0x21
75
    int 0x21
76
    /* ask for a memory block and save the given segment to rmodseg */
76
    /* ask for a memory block and save the given segment to rmodseg */
77
    mov ah, 0x48
77
    mov ah, 0x48
78
    mov bx, sizeof_rmodandprops_paras
78
    mov bx, sizeof_rmodandprops_paras
79
    int 0x21
79
    int 0x21
80
    jc ALLOC_FAIL
80
    jc ALLOC_FAIL
81
    mov rmodseg, ax
81
    mov rmodseg, ax
82
    /* ask for a memory block for the environment and save it to envseg (only if custom size requested) */
82
    /* ask for a memory block for the environment and save it to envseg (only if custom size requested) */
83
    mov bx, envsize
83
    mov bx, envsize
84
    test bx, bx
84
    test bx, bx
85
    jz ALLOC_FAIL
85
    jz ALLOC_FAIL
86
    mov ah, 0x48
86
    mov ah, 0x48
87
    int 0x21
87
    int 0x21
88
    jc ALLOC_FAIL
88
    jc ALLOC_FAIL
89
    mov envseg, ax
89
    mov envseg, ax
90
 
90
 
91
    ALLOC_FAIL:
91
    ALLOC_FAIL:
92
    /* restore initial allocation strategy */
92
    /* restore initial allocation strategy */
93
    mov ax, 0x5801
93
    mov ax, 0x5801
94
    mov bx, dx
94
    mov bx, dx
95
    int 0x21
95
    int 0x21
96
    /* restore initial UMB memory link state */
96
    /* restore initial UMB memory link state */
97
    mov ax, 0x5803
97
    mov ax, 0x5803
98
    pop bx       /* pop initial UMB link state from stack */
98
    pop bx       /* pop initial UMB link state from stack */
99
    int 0x21
99
    int 0x21
100
  }
100
  }
101
 
101
 
102
  if (rmodseg == 0xffff) {
102
  if (rmodseg == 0xffff) {
103
    outputnl("malloc error");
103
    outputnl("malloc error");
104
    return(NULL);
104
    return(NULL);
105
  }
105
  }
106
 
106
 
107
  /* copy rmod to its destination, prefixed with a copy of my own PSP */
107
  /* copy rmod to its destination, prefixed with a copy of my own PSP */
108
  myptr = MK_FP(rmodseg, 0);
108
  myptr = MK_FP(rmodseg, 0);
109
  {
109
  {
110
    unsigned short i;
110
    unsigned short i;
111
    char *mypsp = (void *)0;
111
    char *mypsp = (void *)0;
112
    for (i = 0; i < 0x100; i++) myptr[i] = mypsp[i];
112
    for (i = 0; i < 0x100; i++) myptr[i] = mypsp[i];
113
  }
113
  }
114
  myptr = MK_FP(rmodseg, 0x100);
114
  myptr = MK_FP(rmodseg, 0x100);
115
  _fmemcpy(myptr, rmodcore, rmodcore_len);
115
  _fmemcpy(myptr, rmodcore, rmodcore_len);
116
 
116
 
117
  /* mark rmod memory as "self owned" */
117
  /* mark rmod memory as "self owned" */
118
  mcb = MK_FP(rmodseg - 1, 0);
118
  mcb = MK_FP(rmodseg - 1, 0);
119
  owner = (void far *)(mcb + 1);
119
  owner = (void far *)(mcb + 1);
120
  *owner = rmodseg;
120
  *owner = rmodseg;
121
  _fmemcpy(mcb + 8, "SVARCOM", 8);
121
  _fmemcpy(mcb + 8, "SVARCOM", 8);
122
 
122
 
123
  /* mark env memory as "owned by rmod" */
123
  /* mark env memory as "owned by rmod" */
124
  mcb = MK_FP(envseg - 1, 0);
124
  mcb = MK_FP(envseg - 1, 0);
125
  owner = (void far *)(mcb + 1);
125
  owner = (void far *)(mcb + 1);
126
  *owner = rmodseg;
126
  *owner = rmodseg;
127
  _fmemcpy(mcb + 8, "SVARENV", 8);
127
  _fmemcpy(mcb + 8, "SVARENV", 8);
128
 
128
 
129
  /* if env block is newly allocated, fill it with a few NULLs */
129
  /* if env block is newly allocated, fill it with a few NULLs */
130
  if (envsize != 0) {
130
  if (envsize != 0) {
131
    owner = MK_FP(envseg, 0);
131
    owner = MK_FP(envseg, 0);
132
    owner[0] = 0;
132
    owner[0] = 0;
133
    owner[1] = 0;
133
    owner[1] = 0;
134
  }
134
  }
135
 
135
 
136
  /* set CTRL+BREAK handler to rmod */
136
  /* set CTRL+BREAK handler to rmod */
137
  _asm {
137
  _asm {
138
    push ax
138
    push ax
139
    push dx
139
    push dx
140
    push ds
140
    push ds
141
    mov ax, 0x2523
141
    mov ax, 0x2523
142
    mov ds, rmodseg
142
    mov ds, rmodseg
143
    mov dx, RMOD_OFFSET_BRKHANDLER
143
    mov dx, RMOD_OFFSET_BRKHANDLER
144
    int 0x21
144
    int 0x21
145
    pop ds
145
    pop ds
146
    pop dx
146
    pop dx
147
    pop ax
147
    pop ax
148
  }
148
  }
149
 
149
 
150
  /* mark the input buffer as empty */
150
  /* mark the input buffer as empty */
151
  myptr = MK_FP(rmodseg, RMOD_OFFSET_INPUTBUF);
151
  myptr = MK_FP(rmodseg, RMOD_OFFSET_INPUTBUF);
152
  myptr[0] = 128;  /* max acceptable length */
152
  myptr[0] = 128;  /* max acceptable length */
153
  myptr[1] = 0;    /* len of currently stored history string */
153
  myptr[1] = 0;    /* len of currently stored history string */
154
  myptr[2] = '\r'; /* string terminator */
154
  myptr[2] = '\r'; /* string terminator */
155
  myptr[3] = 0xCA; /* signature to detect stack overflow damaging the buffer */
155
  myptr[3] = 0xCA; /* signature to detect stack overflow damaging the buffer */
156
  myptr[4] = 0xFE; /* 2nd byte of the signature */
156
  myptr[4] = 0xFE; /* 2nd byte of the signature */
157
 
157
 
158
  /* prepare result (rmod props) */
158
  /* prepare result (rmod props) */
159
  res = MK_FP(rmodseg, 0x100 + rmodcore_len);
159
  res = MK_FP(rmodseg, 0x100 + rmodcore_len);
160
  _fmemset(res, 0, sizeof(*res));  /* zero out */
160
  _fmemset(res, 0, sizeof(*res));  /* zero out */
161
  res->rmodseg = rmodseg;          /* rmod segment */
161
  res->rmodseg = rmodseg;          /* rmod segment */
162
  res->origenvseg = origenvseg;    /* original environment segment */
162
  res->origenvseg = origenvseg;    /* original environment segment */
163
 
163
 
164
  /* write env segment to rmod's PSP */
164
  /* write env segment to rmod's PSP */
165
  owner = MK_FP(rmodseg, RMOD_OFFSET_ENVSEG);
165
  owner = MK_FP(rmodseg, RMOD_OFFSET_ENVSEG);
166
  *owner = envseg;
166
  *owner = envseg;
167
 
167
 
168
  /* write boot drive to rmod bootdrive field */
168
  /* write boot drive to rmod bootdrive field */
169
  _asm {
169
  _asm {
170
    push ax
170
    push ax
171
    push bx
171
    push bx
172
    push dx
172
    push dx
173
    push ds
173
    push ds
174
    mov ax, 0x3305 /* DOS 4.0+ - GET BOOT DRIVE */
174
    mov ax, 0x3305 /* DOS 4.0+ - GET BOOT DRIVE */
175
    int 0x21 /* boot drive is in DL now (1=A:, 2=B:, etc) */
175
    int 0x21 /* boot drive is in DL now (1=A:, 2=B:, etc) */
176
    add dl, 'A'-1 /* convert to a proper ASCII letter */
176
    add dl, 'A'-1 /* convert to a proper ASCII letter */
177
    /* set DS to rmodseg */
177
    /* set DS to rmodseg */
178
    mov ax, rmodseg
178
    mov ax, rmodseg
179
    mov ds, ax
179
    mov ds, ax
180
    /* write boot drive to rmod bootdrive field */
180
    /* write boot drive to rmod bootdrive field */
181
    mov bx, RMOD_OFFSET_BOOTDRIVE
181
    mov bx, RMOD_OFFSET_BOOTDRIVE
182
    mov [bx], dl
182
    mov [bx], dl
183
    pop ds
183
    pop ds
184
    pop dx
184
    pop dx
185
    pop bx
185
    pop bx
186
    pop ax
186
    pop ax
187
  }
187
  }
188
 
188
 
189
  /* save my original parent in rmod's memory */
189
  /* save my original parent in rmod's memory */
190
  res->origparent = *((unsigned long *)0x0a); /* original parent seg:off is at 0x0a of my PSP */
190
  res->origparent = *((unsigned long *)0x0a); /* original parent seg:off is at 0x0a of my PSP */
191
 
191
 
192
  /* set the int22 handler in my PSP to rmod so DOS jumps to rmod after I
192
  /* set the int22 handler in my PSP to rmod so DOS jumps to rmod after I
193
   * terminate and save the original handler in rmod's memory */
193
   * terminate and save the original handler in rmod's memory */
194
  {
194
  {
195
    unsigned short *ptr = (void *)0x0a; /* int22 handler is at 0x0A of the PSP */
195
    unsigned short *ptr = (void *)0x0a; /* int22 handler is at 0x0A of the PSP */
196
    ptr[0] = RMOD_OFFSET_ROUTINE;
196
    ptr[0] = RMOD_OFFSET_ROUTINE;
197
    ptr[1] = rmodseg;
197
    ptr[1] = rmodseg;
198
  }
198
  }
199
 
199
 
200
  return(res);
200
  return(res);
201
}
201
}
202
 
202
 
203
 
203
 
204
/* look up my parent: if it's rmod then return a ptr to its props struct,
204
/* look up my parent: if it's rmod then return a ptr to its props struct,
205
 * otherwise return NULL
205
 * otherwise return NULL
206
 * I look at PSP[Ch] to locate RMOD (ie. the "terminate address") */
206
 * I look at PSP[Ch] to locate RMOD (ie. the "terminate address") */
207
struct rmod_props far *rmod_find(unsigned short rmodcore_len) {
207
struct rmod_props far *rmod_find(unsigned short rmodcore_len) {
208
  unsigned short *parent = (void *)0x0C;
208
  unsigned short *parent = (void *)0x0C;
209
  unsigned short far *ptr;
209
  unsigned short far *ptr;
210
  const unsigned short sig[] = {0x1983, 0x1985, 0x2017, 0x2019};
210
  const unsigned short sig[] = {0x1983, 0x1985, 0x2017, 0x2019};
211
  unsigned char *cmdtail = (void *)0x80;
211
  unsigned char *cmdtail = (void *)0x80;
212
  unsigned char i;
212
  unsigned char i;
213
  /* is it rmod? */
213
  /* is it rmod? */
214
  ptr = MK_FP(*parent, 0x100);
214
  ptr = MK_FP(*parent, 0x100);
215
  for (i = 0; i < 4; i++) if (ptr[i] != sig[i]) return(NULL);
215
  for (i = 0; i < 4; i++) if (ptr[i] != sig[i]) return(NULL);
216
  /* match successfull (rmod is my parent) - but is it really a respawn?
216
  /* match successfull (rmod is my parent) - but is it really a respawn?
217
   * command-line tail should contain a single character '\r' */
217
   * command-line tail should contain a single character '\r' */
218
  if ((cmdtail[0] != 1) || (cmdtail[1] != '\n')) return(NULL);
218
  if ((cmdtail[0] != 1) || (cmdtail[1] != '\n')) return(NULL);
219
  cmdtail[0] = 0;
219
  cmdtail[0] = 0;
220
  cmdtail[1] = '\r';
220
  cmdtail[1] = '\r';
221
  /* */
221
  /* */
222
  return(MK_FP(*parent, 0x100 + rmodcore_len));
222
  return(MK_FP(*parent, 0x100 + rmodcore_len));
223
}
223
}
224
 
224
 
225
 
225
 
226
/* update rmod's pointer to comspec */
226
/* update rmod's pointer to comspec */
227
void rmod_updatecomspecptr(unsigned short rmod_seg, unsigned short env_seg) {
227
void rmod_updatecomspecptr(unsigned short rmod_seg, unsigned short env_seg) {
228
  unsigned short far *comspecptr = MK_FP(rmod_seg, RMOD_OFFSET_COMSPECPTR);
228
  unsigned short far *comspecptr = MK_FP(rmod_seg, RMOD_OFFSET_COMSPECPTR);
229
  char far *comspecfp = env_lookup_val(env_seg, "COMSPEC");
229
  char far *comspecfp = env_lookup_val(env_seg, "COMSPEC");
230
  if (comspecfp != NULL) {
230
  if (comspecfp != NULL) {
231
    *comspecptr = FP_OFF(comspecfp);
231
    *comspecptr = FP_OFF(comspecfp);
232
  } else {
232
  } else {
233
    *comspecptr = 0;
233
    *comspecptr = 0;
234
  }
234
  }
235
}
235
}
236
 
236
 
237
 
237
 
238
/* allocates bytes of far memory, flags it as belonging to rmod
238
/* allocates bytes of far memory, flags it as belonging to rmod
239
 * the new block can be optionally flagged as 'ident' (if not null) and zero
239
 * the new block can be optionally flagged as 'ident' (if not null) and zero
240
 * out the newly allocated memory.
240
 * out the newly allocated memory.
241
 * returns a far ptr to the allocated block, or NULL on error */
241
 * returns a far ptr to the allocated block, or NULL on error */
242
void far *rmod_fcalloc(unsigned short bytes, unsigned short rmod_seg, char *ident) {
242
void far *rmod_fcalloc(unsigned short bytes, unsigned short rmod_seg, char *ident) {
243
  unsigned short far *owner;
243
  unsigned short far *owner;
244
  unsigned short newseg = 0;
244
  unsigned short newseg = 0;
245
 
245
 
246
  /* ask DOS for a memory block (as high as possible) */
246
  /* ask DOS for a memory block (as high as possible) */
247
  _asm {
247
  _asm {
248
    push bx /* save initial value in BX so I can restore it later */
248
    push bx /* save initial value in BX so I can restore it later */
249
 
249
 
250
    /* get current allocation strategy and save it on stack */
250
    /* get current allocation strategy and save it on stack */
251
    mov ax, 0x5800
251
    mov ax, 0x5800
252
    int 0x21
252
    int 0x21
253
    push ax
253
    push ax
254
 
254
 
255
    /* set strategy to 'last fit, try high then low memory' */
255
    /* set strategy to 'last fit, try high then low memory' */
256
    mov ax, 0x5801
256
    mov ax, 0x5801
257
    mov bx, 0x0082
257
    mov bx, 0x0082
258
    int 0x21
258
    int 0x21
259
 
259
 
260
    /* ask for a memory block and save the given segment to rmodseg */
260
    /* ask for a memory block and save the given segment to rmodseg */
261
    mov ah, 0x48  /* Allocate Memory */
261
    mov ah, 0x48  /* Allocate Memory */
262
    mov bx, bytes
262
    mov bx, bytes
263
    add bx, 15    /* convert bytes to paragraphs */
263
    add bx, 15    /* convert bytes to paragraphs */
264
    shr bx, 1     /* bx /= 16 */
264
    shr bx, 1     /* bx /= 16 */
265
    shr bx, 1
265
    shr bx, 1
266
    shr bx, 1
266
    shr bx, 1
267
    shr bx, 1
267
    shr bx, 1
268
    int 0x21
268
    int 0x21
269
 
269
 
270
    /* error handling */
270
    /* error handling */
271
    jc FAIL
271
    jc FAIL
272
 
272
 
273
    /* save newly allocated segment to newseg */
273
    /* save newly allocated segment to newseg */
274
    mov newseg, ax
274
    mov newseg, ax
275
 
275
 
276
    FAIL:
276
    FAIL:
277
    /* restore initial allocation strategy */
277
    /* restore initial allocation strategy */
278
    mov ax, 0x5801
278
    mov ax, 0x5801
279
    pop bx
279
    pop bx
280
    int 0x21
280
    int 0x21
281
 
281
 
282
    pop bx /* restore BX to its initial value */
282
    pop bx /* restore BX to its initial value */
283
  }
283
  }
284
 
284
 
285
  if (newseg == 0) return(NULL);
285
  if (newseg == 0) return(NULL);
286
 
286
 
287
  /* mark memory as "owned by rmod" */
287
  /* mark memory as "owned by rmod" */
288
  owner = (void far *)(MK_FP(newseg - 1, 1));
288
  owner = (void far *)(MK_FP(newseg - 1, 1));
289
  *owner = rmod_seg;
289
  *owner = rmod_seg;
290
 
290
 
291
  /* set the MCB description to ident, if provided */
291
  /* set the MCB description to ident, if provided */
292
  if (ident) {
292
  if (ident) {
293
    char far *mcbdesc = MK_FP(newseg - 1, 8);
293
    char far *mcbdesc = MK_FP(newseg - 1, 8);
294
    int i;
294
    int i;
295
    _fmemset(mcbdesc, 0, 8);
295
    _fmemset(mcbdesc, 0, 8);
296
    for (i = 0; (i < 8) && (ident[i] != 0); i++) { /* field's length is limited to 8 bytes max */
296
    for (i = 0; (i < 8) && (ident[i] != 0); i++) { /* field's length is limited to 8 bytes max */
297
      mcbdesc[i] = ident[i];
297
      mcbdesc[i] = ident[i];
298
    }
298
    }
299
  }
299
  }
300
 
300
 
301
  /* zero out the memory before handing it out */
301
  /* zero out the memory before handing it out */
302
  _fmemset(MK_FP(newseg, 0), 0, bytes);
302
  _fmemset(MK_FP(newseg, 0), 0, bytes);
303
 
303
 
304
  return(MK_FP(newseg, 0));
304
  return(MK_FP(newseg, 0));
305
}
305
}
306
 
306
 
307
 
307
 
308
/* free memory previously allocated by rmod_fcalloc() */
308
/* free memory previously allocated by rmod_fcalloc() */
309
void rmod_ffree(void far *ptr) {
309
void rmod_ffree(void far *ptr) {
310
  unsigned short ptrseg;
310
  unsigned short ptrseg;
311
  unsigned short myseg = 0;
311
  unsigned short myseg = 0;
312
  unsigned short far *owner;
312
  unsigned short far *owner;
313
  if (ptr == NULL) return;
313
  if (ptr == NULL) return;
314
  ptrseg = FP_SEG(ptr);
314
  ptrseg = FP_SEG(ptr);
315
 
315
 
316
  /* get my own segment */
316
  /* get my own segment */
317
  _asm {
317
  _asm {
318
    mov myseg, cs
318
    mov myseg, cs
319
  }
319
  }
320
 
320
 
321
  /* mark memory in MCB as my own, otherwise DOS might refuse to free it */
321
  /* mark memory in MCB as my own, otherwise DOS might refuse to free it */
322
  owner = MK_FP(ptrseg - 1, 1);
322
  owner = MK_FP(ptrseg - 1, 1);
323
  *owner = myseg;
323
  *owner = myseg;
324
 
324
 
325
  /* free the memory block */
325
  /* free the memory block */
326
  _asm {
326
  _asm {
327
    push es
327
    push es
328
    mov ah, 0x49  /* Free Memory Block */
328
    mov ah, 0x49  /* Free Memory Block */
329
    mov es, ptrseg
329
    mov es, ptrseg
330
    int 0x21
330
    int 0x21
331
    pop es
331
    pop es
332
  }
332
  }
333
}
333
}
334
 
334
 
335
 
335
 
336
/* free the entire linked list of bat ctx nodes (and set its rmod ptr to NULL) */
336
/* free the entire linked list of bat ctx nodes (and set its rmod ptr to NULL) */
337
void rmod_free_bat_llist(struct rmod_props far *rmod) {
337
void rmod_free_bat_llist(struct rmod_props far *rmod) {
338
  while (rmod->bat != NULL) {
338
  while (rmod->bat != NULL) {
339
    struct batctx far *victim = rmod->bat;
339
    struct batctx far *victim = rmod->bat;
340
    rmod->bat = rmod->bat->parent;
340
    rmod->bat = rmod->bat->parent;
341
    rmod_ffree(victim);
341
    rmod_ffree(victim);
342
  }
342
  }
343
}
343
}
344
 
344