home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / mslang / vm / src / vmutil.asm < prev    next >
Encoding:
Assembly Source File  |  1993-12-15  |  13.4 KB  |  531 lines

  1.     page    ,132
  2.     title    vmutil - VM util routines
  3. ;***
  4. ;vmutil.asm - VM util routines
  5. ;
  6. ;       Copyright (c) 1989-1992, Microsoft Corporation.  All rights reserved.
  7. ;
  8. ;*******************************************************************************
  9.  
  10.     include version.inc
  11.     .model  large,pascal
  12.  
  13.     .data?
  14.  
  15.     extrn   C _rghpgdHash:word
  16.     extrn   C _segVmDos:word
  17.  
  18.     extrn   __VmTerminate:far
  19.  
  20. szFilename      db      128 dup(?)
  21.  
  22.     .code   VM_TEXT
  23.  
  24. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  25. ;*
  26. ;*                       FVmWin30StandardMode
  27. ;*
  28. ;*     This function returns true if running in Windows 3.0 standard
  29. ;*   mode.
  30. ;*
  31. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  32. ;*
  33. ;* Entry Conditions:
  34. ;*
  35. ;*     DOS only.
  36. ;*
  37. ;* Exit Conditions:
  38. ;*
  39. ;*     None.
  40. ;*
  41. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  42.  
  43.  
  44. __FVmWin30StandardMode  proc    near uses di
  45.  
  46.     ;If running in Windows 3.1 standard mode a task switcher is
  47.     ;active.  Windows 3.0 standard mode doesn't support the task
  48.     ;switcher API.  There is a loophole.  The DOS 5.0 shell with
  49.     ;task switcher enabled can be run in a DOS session under
  50.     ;Windows 3.0 standard mode.  This will result in this function
  51.     ;reporting that Windows 3.0 standard mode is not active.
  52.  
  53.     mov     ax,4B02h                ;SWTCH_DETECT_SWITCHER API
  54.     xor     bx,bx
  55.     mov     di,bx
  56.     mov     es,bx
  57.     int     2Fh                     ;AX = 0 if task switcher is active
  58.     or      ax,ax                   ;Is a switcher active?
  59.     jz      Exit                    ;Brif so.
  60.  
  61.     mov     ax,4680h                ;IS_WINOLDAP_ACTIVE
  62.     int     2Fh                     ;AX = 0 if winoldap is active
  63.     or      ax,ax                   ;Is Windows 3.0 winoldap active?
  64.     jnz     Not30StandardMode       ;Brif not.
  65.  
  66.     mov     ax,sp                   ;We are in Win 3.0 standard mode.
  67.     jmp     Exit
  68.  
  69. Not30StandardMode:
  70.     xor     ax,ax                   ;We are not in Win 3.0 standard mode
  71.  
  72. Exit:
  73.     ret
  74.  
  75. __FVmWin30StandardMode  endp
  76.  
  77.     page
  78.  
  79. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  80. ;*
  81. ;*                       CParaVmDosAvail
  82. ;*
  83. ;*     This function returns the number of paragraphs in the largest
  84. ;*   memory block available in the DOS memory heap.
  85. ;*
  86. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  87. ;*
  88. ;* Entry Conditions:
  89. ;*
  90. ;*     DOS only.
  91. ;*
  92. ;* Exit Conditions:
  93. ;*
  94. ;*     None.
  95. ;*
  96. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  97.  
  98.  
  99. __CParaVmDosAvail proc  near
  100.  
  101.     mov     ah,48h                  ;Allocate Memory Block
  102.     mov     bx,0FFFFh               ;Try for all available memory
  103.     int     21h                     ;Do it
  104.     xchg    ax,bx                   ;Return size of available block
  105.     ret
  106.  
  107. __CParaVmDosAvail endp
  108.  
  109.     page
  110.  
  111. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  112. ;*
  113. ;*                       SegVmDosAllocate
  114. ;*
  115. ;*     This function allocates the requested number of paragraphs
  116. ;*   from the DOS heap and returns the segment value of the block.
  117. ;*
  118. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  119. ;*
  120. ;* Entry Conditions:
  121. ;*
  122. ;*     DOS only.
  123. ;*
  124. ;* Exit Conditions:
  125. ;*
  126. ;*     Returns the segment if the allocation succeeds or _NULLSEG if
  127. ;*   the allocation fails.
  128. ;*
  129. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  130.  
  131.  
  132. __SegVmDosAllocate      proc    near cPara:word
  133.  
  134.     mov     ah,48h                  ;Allocate Memory Block
  135.     mov     bx,[cPara]
  136.     int     21h                     ;Do it
  137.     jnc     @F                      ;Brif allocation succeeded
  138.     xor     ax,ax                   ;Return 0 == _NULLSEG
  139. @@:
  140.     ret
  141.  
  142. __SegVmDosAllocate      endp
  143.  
  144.     page
  145.  
  146. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  147. ;*
  148. ;*                       VmDosFreeSeg
  149. ;*
  150. ;*     This function releases the specified DOS memory block.
  151. ;*
  152. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  153. ;*
  154. ;* Entry Conditions:
  155. ;*
  156. ;*     DOS only.
  157. ;*
  158. ;* Exit Conditions:
  159. ;*
  160. ;*     None.
  161. ;*
  162. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  163.  
  164. __VmDosFreeSeg  proc    near segDos:word
  165.  
  166.     mov     ah,49h                  ;Release Memory Block
  167.     mov     es,[segDos]
  168.     int     21h                     ;Do it
  169.     ret
  170.  
  171. __VmDosFreeSeg  endp
  172.  
  173.     page
  174.  
  175. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  176. ;*
  177. ;*                       VmCreateTemp
  178. ;*
  179. ;*     This function creates a file for disk swapping.
  180. ;*
  181. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  182. ;*
  183. ;* Entry Conditions:
  184. ;*
  185. ;*     DOS only.
  186. ;*
  187. ;* Exit Conditions:
  188. ;*
  189. ;*     None.
  190. ;*
  191. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  192.  
  193. __VmCreateTemp  proc    near uses si di
  194.  
  195.     mov     ah,62h
  196.     int     21h
  197.     jc      UseCurrentDirectory
  198.     mov     ds,bx                   ;DS:0 = Psp
  199.     assume  ds:nothing
  200.     mov     ds,ds:[2Ch]
  201.     xor     si,si                   ;DS:SI -> Environment
  202.  
  203. CheckEnvironmentVar:
  204.     lodsw
  205.     or      al,al                   ;End of enviroment?
  206.     jz      UseCurrentDirectory     ;Brif so, use current directory
  207.  
  208.     cmp     ax,'MT'                 ;Look for TMP=
  209.     jne     @F
  210.     lodsw
  211.     cmp     ax,'=P'
  212.     je      CopyTMP
  213. @@:
  214.     lodsb
  215.     or      al,al
  216.     jnz     @B
  217.     jmp     CheckEnvironmentVar
  218.  
  219. CopyTMP:
  220.     mov     cx,SIZE szFilename-13
  221.     mov     di,OFFSET DGROUP:szFilename
  222.     push    ss
  223.     pop     es                      ;ES:DI = pszFilename
  224.  
  225. @@:
  226.     lodsb
  227.     stosb
  228.     or      al,al                   ;End of environment string?
  229.     jz      CreateFile              ;Brif so
  230.     dec     cx                      ;Is buffer full?
  231.     jz      EndOfTMP                ;Brif so.  Truncate it.
  232.     cmp     al,';'                  ;Path seperator?
  233.     jne     @B                      ;Brif not.
  234.  
  235. EndOfTMP:
  236.     dec     di                      ;Backup over last character
  237.     mov     al,0
  238.     stosb                           ;Zero terminate
  239.     jmp     short CreateFile
  240.  
  241. UseCurrentDirectory:
  242.     mov     word ptr [szFilename],'.'
  243.  
  244. CreateFile:
  245.     push    ss                      ;Restore DS == DGROUP
  246.     pop     ds
  247.     assume  ds:DGROUP
  248.  
  249.     mov     ah,5Ah                  ;Create temporary file
  250.     xor     cx,cx                   ;Normal file
  251.     mov     dx,OFFSET ds:szFilename
  252.     int     21h
  253.     jnc     @F
  254.     cmp     word ptr [szFilename],'.'
  255.     jne     UseCurrentDirectory     ;Retry create in current directory
  256.     mov     ax,0FFFFh
  257. @@:
  258.     ret
  259.  
  260. __VmCreateTemp  endp
  261.  
  262.     page
  263.  
  264. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  265. ;*
  266. ;*                       VmCloseTemp
  267. ;*
  268. ;*     This function closes and deletes the disk swap file.
  269. ;*
  270. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  271. ;*
  272. ;* Entry Conditions:
  273. ;*
  274. ;*     The fh parameter is the DOS file handle of the swap file.
  275. ;*
  276. ;* Exit Conditions:
  277. ;*
  278. ;*     None.
  279. ;*
  280. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  281.  
  282. __VmCloseTemp   proc    near fh:word
  283.  
  284.     mov     ah,3Eh                  ;Close file
  285.     mov     bx,[fh]
  286.     int     21h
  287.  
  288.     mov     ah,41h                  ;Delete file
  289.     mov     dx,OFFSET ds:szFilename
  290.     int     21h
  291.  
  292.     ret
  293.  
  294. __VmCloseTemp   endp
  295.  
  296.     page
  297.  
  298. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  299. ;*
  300. ;*                       FVmExtendTemp
  301. ;*
  302. ;*     This function extends the size of the swap file.
  303. ;*
  304. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  305. ;*
  306. ;* Entry Conditions:
  307. ;*
  308. ;*     The fh parameter is the DOS file handle of the swap file.  The
  309. ;*   lfa parameter is the new size of the file.
  310. ;*
  311. ;* Exit Conditions:
  312. ;*
  313. ;*     If the disk swap file can be grown, the function returns TRUE.
  314. ;*   If the disk allocation fails, the function retunrs FALSE.
  315. ;*
  316. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  317.  
  318. __FVmExtendTemp proc    near            \
  319.             fh:word,        \
  320.             lfa:dword
  321.  
  322.     push    [fh]
  323.     push    word ptr [lfa+2]
  324.     push    word ptr [lfa+0]
  325.     push    ds                      ;Dummy far pointer
  326.     push    dx
  327.     xor     ax,ax
  328.     push    ax
  329.     call    near ptr __FVmWriteTemp ;Write 0 bytes
  330.  
  331.     mov     ax,4202h                ;Seek relative to end
  332.     mov     bx,[fh]
  333.     xor     cx,cx
  334.     xor     dx,dx
  335.     int     21h
  336.  
  337.     jc      @F                      ;Brif seek failed.  AX != 0
  338.     xor     dx,word ptr [lfa+2]     ;Is current size = requested size?
  339.     xor     ax,word ptr [lfa+0]
  340.     or      ax,dx                   ;AX != 0 if size mismatch
  341. @@:
  342.     ret
  343.  
  344. __FVmExtendTemp endp
  345.  
  346.     page
  347.  
  348. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  349. ;*
  350. ;*                          FVmReadTemp
  351. ;*
  352. ;*     This function reads a page from the disk swap file.
  353. ;*
  354. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  355. ;*
  356. ;* Entry Conditions:
  357. ;*
  358. ;*     The fh parameter is the DOS file handle of the swap file.  The
  359. ;*   lfa parameter is the file offset of the page to be read.  The pv
  360. ;*   parameter is the address into which the page is to be read.  The
  361. ;*   cb parameter is the number of bytes to be read.
  362. ;*
  363. ;* Exit Conditions:
  364. ;*
  365. ;*     If the read succeeds the function returns TRUE.  If the read
  366. ;*   fails, the function returns false.
  367. ;*
  368. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  369.  
  370. __FVmReadTemp   proc    near uses ds,   \
  371.             fh:word,        \
  372.             lfa:dword,      \
  373.             pv:far ptr,     \
  374.             cb:word
  375.  
  376.     mov     ax,4200h                ;Seek absolute
  377.     mov     bx,[fh]
  378.     mov     cx,word ptr [lfa+2]
  379.     mov     dx,word ptr [lfa+0]
  380.     int     21h
  381.     jc      CheckForFailure         ;Brif Int 21 Failed
  382.  
  383.     mov     ah,3Fh
  384.     mov     cx,[cb]
  385.     lds     dx,[pv]
  386.     assume  ds:nothing
  387.     int     21h
  388.  
  389. CheckForFailure:
  390.     mov     ax,0                    ;Assume failure. Return FALSE
  391.     jc      @F                      ;Brif Int 21 Failed
  392.     mov     ax,sp                   ;Return TRUE for success
  393. @@:
  394.     ret
  395.  
  396. __FVmReadTemp   endp
  397.  
  398.     assume  ds:DGROUP
  399.  
  400.     page
  401.  
  402. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  403. ;*
  404. ;*                          FVmWriteTemp
  405. ;*
  406. ;*     This function writes a page to the disk swap file.
  407. ;*
  408. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  409. ;*
  410. ;* Entry Conditions:
  411. ;*
  412. ;*     The fh parameter is the DOS file handle of the swap file.  The
  413. ;*   lfa parameter is the file offset of the page to be write.  The pv
  414. ;*   parameter is the address into which the page is to be write.  The
  415. ;*   cb parameter is the number of bytes to be write.
  416. ;*
  417. ;* Exit Conditions:
  418. ;*
  419. ;*     If the write succeeds the function returns TRUE. If the write
  420. ;*   fails, the function returns false.
  421. ;*
  422. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  423.  
  424. __FVmWriteTemp  proc    near uses ds,   \
  425.             fh:word,        \
  426.             lfa:dword,      \
  427.             pv:far ptr,     \
  428.             cb:word
  429.  
  430.     mov     ax,4200h                ;Seek absolute
  431.     mov     bx,[fh]
  432.     mov     cx,word ptr [lfa+2]
  433.     mov     dx,word ptr [lfa+0]
  434.     int     21h
  435.     jc      CheckForFailure         ;Brif Int 21 Failed
  436.  
  437.     mov     ah,40h
  438.     mov     cx,[cb]
  439.     lds     dx,[pv]
  440.     assume  ds:nothing
  441.     int     21h
  442.  
  443. CheckForFailure:
  444.     mov     ax,0                    ;Assume failure. Return FALSE
  445.     jc      @F                      ;Brif Int 21 Failed
  446.     mov     ax,sp                   ;Return TRUE for success
  447. @@:
  448.  
  449.     ret
  450.  
  451. __FVmWriteTemp  endp
  452.  
  453.     assume  ds:DGROUP
  454.  
  455.     page
  456.  
  457. __HpgdSearchCache proc  near    \
  458.             vp:dword
  459.  
  460.     mov     ax,word ptr [vp]        ;DX:AX = vp
  461.     mov     dx,word ptr [vp+2]
  462.  
  463.     and     ax,0F800h               ;DX:AX = VpPageOfVp(vp)
  464.  
  465.     and     dx,03Fh                 ;DX:AX = vp & ipgdHashMask
  466.  
  467.     mov     bx,ax                   ;Save for comparison below
  468.  
  469.     mov     cx,79
  470.     div     cx                      ;DX = ipgd = IpgdHashOfVp(vp)
  471.  
  472.     mov     ax,bx                   ;AX = Low word of VpPageOfVp(vp)
  473.  
  474.     mov     bx,dx                   ;BX = ipgd
  475.     add     bx,bx                   ;Scale to index into word array
  476.     mov     bx,_rghpgdHash[bx]      ;BX = hpgd = _rghpgdHash[ipgd]
  477.  
  478.     cmp     bx,0FFFFh               ;Is this hpgdNil
  479.     je      Exit                    ;Brif so, page is not resident
  480.  
  481.     mov     es,[_segVmDos]
  482.     mov     dx,word ptr [vp+2]      ;DX:AX = VpPageOfVp(vp)
  483.  
  484. CheckPage:
  485.     cmp     es:[bx],ax              ;Compare low word of VP
  486.     jne     @F                      ;No match
  487.     cmp     es:[bx+2],dx            ;Compare high word of VP
  488.     je      Exit                    ;There is a match
  489. @@:
  490.     mov     bx,es:[bx+12]           ;BX = PpgdOfHpgd(hpgd)->hpgdHashNext
  491.     cmp     bx,0FFFFh               ;Is this hpgdNil
  492.     jne     CheckPage               ;Brif not, compare address
  493.  
  494. Exit:
  495.     xchg    ax,bx                   ;AX = hpgd
  496.     ret
  497.  
  498. __HpgdSearchCache endp
  499.  
  500.  
  501. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  502. ;*
  503. ;*      C Runtime Termination Entry
  504. ;*
  505. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  506. ;*
  507. ;* Place the address of the VM termination routine in the C runtime
  508. ;* far terminator table.  The C runtime will then call this routine
  509. ;* at termination so that VM resources can be cleaned up (in the case
  510. ;* where the user didn't previously do so).
  511. ;*
  512. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  513.  
  514. XCFB    segment  word public 'DATA'
  515. XCFB    ends
  516. XCF     segment  word public 'DATA'
  517. XCF     ends
  518. XCFE    segment  word public 'DATA'
  519. XCFE    ends
  520.  
  521. DGROUP  group XCFB, XCF, XCFE
  522.  
  523. XCF     segment
  524.  
  525.     dd      __VmTerminate
  526.  
  527. XCF     ends
  528.  
  529.  
  530.     end
  531.