home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / UTILITY / RAMDISK / SRDSK141.ZIP / DISKIO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-06  |  3.5 KB  |  151 lines

  1. /* ReSizeable RAMDisk - disk I/O
  2. ** Copyright (c) 1992 Marko Kohtala
  3. */
  4.  
  5. #include "srdisk.h"
  6. #include <assert.h>
  7. #include <dos.h>
  8. #include <string.h>
  9.  
  10. /*
  11. **  Read/Write sector from/to disk
  12. **
  13. **  Return 0 for failure, transferred sector count otherwise
  14. */
  15.  
  16. static int xfer_sector(char rw, int count, dword start, void far *buffer)
  17. {
  18.   struct config_s far *subconf = conf;
  19.   int sectors = count;
  20.  
  21.   while(count) {
  22.     asm {
  23.       mov ax,word ptr start
  24.       mov dx,word ptr start+2
  25.       mov cx,count
  26.       mov bh,rw
  27.       les di,buffer
  28.       push ds
  29.       lds si,subconf
  30.       call dword ptr [si+2]   /* !!!! Configuration format dependent */
  31.       pop ds
  32.       jc fail
  33.       sub count,ax
  34.       jbe done
  35.     }
  36.     start -= subconf->sectors;
  37.     if ( !(subconf = conf_ptr(subconf->next)) )
  38.       return 0;
  39.   }
  40.  done:
  41.   return sectors;
  42.  fail:
  43.   fatal("Cannot access disk");
  44.   return 0;
  45. }
  46.  
  47. /*
  48. **  Read sector from disk
  49. **
  50. **  Return 0 for failure, transferred sector count otherwise
  51. */
  52.  
  53. int read_sector(int count, dword start, void *buffer)
  54. {
  55.   return xfer_sector(0, count, start, buffer);
  56. }
  57.  
  58. /*
  59. **  Write sector to disk
  60. **
  61. **  Return 0 for failure, transferred sector count otherwise
  62. */
  63.  
  64. int write_sector(int count, dword start, void *buffer)
  65. {
  66.   return xfer_sector(1, count, start, buffer);
  67. }
  68.  
  69.  
  70. /*
  71. **  Allocate memory on a part of the disk
  72. */
  73.  
  74. dword disk_alloc(struct config_s far *conf, dword size)
  75. {
  76.   struct dev_hdr _seg *dev = (struct dev_hdr _seg *)FP_SEG(conf);
  77.   byte far *alloc = MK_FP(dev, conf->malloc_off);
  78.  
  79.   if (!(conf->flags & C_NOALLOC))
  80.     return ((dword (far *)(dword))alloc)(size);
  81.  
  82.   if (_fstrncmp("XMS ", dev->u.s.memory, 4) == 0) {
  83.     if (size >= 0x10000L) /* Can not allocate over 0xFFFF K */
  84.      fail:
  85.       return conf->size;
  86.  
  87.     #define XMS_handle 0    /* Dependant on the XMS_alloc structure format */
  88.     #define XMS_entry 2
  89.  
  90.     if (*(word far *)(alloc+XMS_handle) != 0) {
  91.       /* Has already a handle - reallocate to new size */
  92.       asm {
  93.         les si,alloc
  94.         mov dx,es:[si+XMS_handle]
  95.         mov bx,word ptr size
  96.         mov ah,0xF      /* Reallocate */
  97.         call dword ptr es:[si+XMS_entry]
  98.         or ax,ax
  99.         jnz realloc_ok
  100.  
  101.         /* Failed to allocate, check if there is memory anyway */
  102.         /* This kludge hopefully fixes some problems with DesqView */
  103.         mov ah,8    /* Query free extended memory */
  104.         call dword ptr es:[si+XMS_entry]
  105.         push ax
  106.         mov dx,es:[si+XMS_handle]
  107.         mov ah,0xE  /* Get handle information */
  108.         call dword ptr es:[si+XMS_entry]
  109.         or ax,ax
  110.         pop ax
  111.         jz fail
  112.  
  113.         add ax,dx   /* AX = free memory + current block */
  114.         cmp ax,word ptr size
  115.         jb fail
  116.  
  117.         mov dx,es:[si+XMS_handle]
  118.         mov ah,0xA  /* Free extended memory block */
  119.         call dword ptr es:[si+XMS_entry]
  120.         or ax,ax
  121.         jz fail
  122.         jmp alloc_handle
  123.       }
  124.      realloc_ok:;
  125.     }
  126.     else {
  127.       /* Handle 0 is no handle - must allocate */
  128.      alloc_handle:
  129.       asm {
  130.         les si,alloc
  131.         mov dx,word ptr size
  132.         mov ah,0x9      /* Allocate */
  133.         call dword ptr es:[si+XMS_entry]
  134.         or ax,ax
  135.         jz alloc_fail
  136.         mov es:[si+XMS_handle],dx
  137.         jmp alloc_ok
  138.       }
  139.      alloc_fail:
  140.       error("Can not allocate XMS memory");
  141.       return 0;
  142.      alloc_ok:;
  143.     }
  144.   }
  145.   else
  146.     fatal("Don't know how to allocate memory");
  147.   return size;
  148. }
  149.  
  150.  
  151.