Subversion Repositories SvarDOS

Rev

Rev 537 | Rev 957 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 537 Rev 949
Line 1... Line 1...
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 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,
Line 223... Line 223...
223
    *comspecptr = FP_OFF(comspecfp);
223
    *comspecptr = FP_OFF(comspecfp);
224
  } else {
224
  } else {
225
    *comspecptr = 0;
225
    *comspecptr = 0;
226
  }
226
  }
227
}
227
}
-
 
228
 
-
 
229
 
-
 
230
/* allocates bytes of far memory, flags it as belonging to rmod
-
 
231
 * the new block can be optionally flagged as 'ident' (if not null)
-
 
232
 * returns a far ptr to the allocated block, or NULL on error */
-
 
233
void far *rmod_fmalloc(unsigned short bytes, unsigned short rmod_seg, char *ident) {
-
 
234
  unsigned short far *owner;
-
 
235
  unsigned short newseg = 0;
-
 
236
 
-
 
237
  /* ask DOS for a memory block (as high as possible) */
-
 
238
  _asm {
-
 
239
    push bx /* save initial value in BX so I can restore it later */
-
 
240
 
-
 
241
    /* get current allocation strategy and save it on stack */
-
 
242
    mov ax, 0x5800
-
 
243
    int 0x21
-
 
244
    push ax
-
 
245
 
-
 
246
    /* set strategy to 'last fit, try high then low memory' */
-
 
247
    mov ax, 0x5801
-
 
248
    mov bx, 0x0082
-
 
249
    int 0x21
-
 
250
 
-
 
251
    /* ask for a memory block and save the given segment to rmodseg */
-
 
252
    mov ah, 0x48  /* Allocate Memory */
-
 
253
    mov bx, bytes
-
 
254
    add bx, 15    /* convert bytes to paragraphs */
-
 
255
    shr bx, 1     /* bx /= 16 */
-
 
256
    shr bx, 1
-
 
257
    shr bx, 1
-
 
258
    shr bx, 1
-
 
259
    int 0x21
-
 
260
 
-
 
261
    /* error handling */
-
 
262
    jc FAIL
-
 
263
 
-
 
264
    /* save newly allocated segment to newseg */
-
 
265
    mov newseg, ax
-
 
266
 
-
 
267
    FAIL:
-
 
268
    /* restore initial allocation strategy */
-
 
269
    mov ax, 0x5801
-
 
270
    pop bx
-
 
271
    int 0x21
-
 
272
 
-
 
273
    pop bx /* restore BX to its initial value */
-
 
274
  }
-
 
275
 
-
 
276
  if (newseg == 0) return(NULL);
-
 
277
 
-
 
278
  /* mark memory as "owned by rmod" */
-
 
279
  owner = (void far *)(MK_FP(newseg - 1, 1));
-
 
280
  *owner = rmod_seg;
-
 
281
 
-
 
282
  /* set the MCB description to ident, if provided */
-
 
283
  if (ident) {
-
 
284
    char far *mcbdesc = MK_FP(newseg - 1, 8);
-
 
285
    int i;
-
 
286
    _fmemset(mcbdesc, 0, 8);
-
 
287
    for (i = 0; (i < 8) && (ident[i] != 0); i++) { /* field's length is limited to 8 bytes max */
-
 
288
      mcbdesc[i] = ident[i];
-
 
289
    }
-
 
290
  }
-
 
291
 
-
 
292
  return(MK_FP(newseg, 0));
-
 
293
}
-
 
294
 
-
 
295
 
-
 
296
/* free memory previously allocated by rmod_ffmalloc() */
-
 
297
void rmod_ffree(void far *ptr) {
-
 
298
  unsigned short ptrseg;
-
 
299
  unsigned short myseg = 0;
-
 
300
  unsigned short far *owner;
-
 
301
  if (ptr == NULL) return;
-
 
302
  ptrseg = FP_SEG(ptr);
-
 
303
 
-
 
304
  /* get my own segment */
-
 
305
  _asm {
-
 
306
    mov myseg, cs
-
 
307
  }
-
 
308
 
-
 
309
  /* mark memory in MCB as my own, otherwise DOS might refuse to free it */
-
 
310
  owner = MK_FP(ptrseg - 1, 1);
-
 
311
  *owner = myseg;
-
 
312
 
-
 
313
  /* free the memory block */
-
 
314
  _asm {
-
 
315
    push es
-
 
316
    mov ah, 0x49  /* Free Memory Block */
-
 
317
    mov es, ptrseg
-
 
318
    int 0x21
-
 
319
    pop es
-
 
320
  }
-
 
321
}