home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / mslang / vm / src / lock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-15  |  3.6 KB  |  167 lines

  1. /***
  2. *lock.c - Lock/Unlock space
  3. *
  4. *       Copyright (c) 1989-1992, Microsoft Corporation.  All rights reserved.
  5. *
  6. *******************************************************************************/
  7.  
  8. #include <version.h>
  9. #include <vmassert.h>
  10. #include <stdlib.h>
  11. #include <vmm.h>
  12. #include <vmbm.h>
  13.  
  14. /*** PVmLockHbk
  15. *
  16. * Purpose:  Lock in the allocated space
  17. *
  18. * Input:
  19. *   hbk     The handle to lock down.
  20. *
  21. * Output:
  22. *  Returns:
  23. *           A physical pointer to the memory, or a NULL on error.
  24. *
  25. * Exceptions:
  26. *
  27. * Notes:
  28. *
  29. *   There is an implicit assumption that if the memory is already locked,
  30. *   a load will take you to the same physical place!
  31. *
  32. *   It is assumed that any allocation over a page is allocated by page
  33. *
  34. *************************************************************************/
  35. PVOID   VMFUNC __PVmLockHbk(
  36. HBK     hbk
  37. ) {
  38.     PHDB    phdb;
  39.  
  40.     Assert( hbk != NULL);
  41.  
  42.    if (!_fVmInit || (unsigned long)hbk < (unsigned long)_hbkMin || (unsigned long)hbk >= (unsigned long)_hbkMax)
  43.       return NULL;
  44.  
  45.     phdb = __PVmLoadVp((VPVOID) hbk, FALSE);
  46.  
  47.    Assert( phdb != NULL );
  48.  
  49.    if (!phdb || phdb->fFree)
  50.       return FALSE;
  51.    
  52.     // if it has never been locked, bring it into memory
  53.     if( !phdb->cLock )
  54.     {
  55.         if(phdb->fByPage)
  56.             phdb = __PVmLockVpCb((VPVOID) hbk, phdb->cPage * (unsigned long) cbVmPage);
  57.         else
  58.         {
  59.             Assert(CbGetSize(phdb->cbSize) < cbVmPage);
  60.             phdb = __PVmLockVp((VPVOID) hbk);
  61.         }
  62.     }
  63.  
  64.     // if the pointer was obtained, then up the lock count
  65.     if( phdb )
  66.     {
  67.         if (phdb->cLock == cMaxLock)
  68.             return(NULL);
  69.         phdb->cLock++;
  70.     }
  71.  
  72.     // return the vm pointer
  73.     return((PVOID) ((phdb != NULL) ? ++phdb : NULL));
  74. }
  75.  
  76.  
  77. /*** PVmUnlockHbk
  78. *
  79. * Purpose:  Unlock the locked page
  80. *
  81. * Input:
  82. *   hbk     The handle to lock down.
  83. *
  84. * Output:
  85. *  Returns:
  86. *
  87. * Exceptions:
  88. *
  89. * Notes:
  90. *   It is assumed that any allocation over a page is allocated by page
  91. *
  92. *************************************************************************/
  93. void VMFUNC __VmUnlockHbk(
  94. HBK     hbk,
  95. int     fDirty
  96. ) {
  97.     PHDB    phdb;
  98.  
  99.     Assert( hbk != NULL );
  100.  
  101.    if (!_fVmInit || (unsigned long)hbk < (unsigned long)_hbkMin || (unsigned long)hbk >= (unsigned long)_hbkMax)
  102.       return;
  103.     
  104.     // if the page is locked, I don't care that the lock count gets
  105.     // saved because it is locked in memory and can't be swapped out
  106.     // once the lock count goes to zero, I can swap out, but the swapped
  107.     // out copy has zero too.
  108.     phdb = __PVmLoadVp((VPVOID) hbk, FALSE);
  109.  
  110.     if ( !phdb || phdb->fFree || !phdb->cLock )
  111.         return;
  112.  
  113.     Assert( phdb->fFree == FALSE );
  114.  
  115.     phdb->cLock--;
  116.  
  117.     // If this is the last lock, then unlock the page
  118.     if( !phdb->cLock )
  119.     {
  120.         if(phdb->fByPage)
  121.             __VmUnlockVpCb((VPVOID) hbk, phdb->cPage * (unsigned long) cbVmPage, fDirty);
  122.         else
  123.         {
  124.             Assert(CbGetSize(phdb->cbSize) < cbVmPage);
  125.             __VmUnlockVp((VPVOID) hbk, fDirty);
  126.         }
  127.     }
  128. }
  129.  
  130. /*** CVmLockHbk
  131. *
  132. * Purpose:  Get the current HBK lock count
  133. *
  134. * Input:
  135. *   hbk     The handle to get the count of.
  136. *
  137. * Output:
  138. *  Returns:
  139. *
  140. * Exceptions:
  141. *
  142. * Notes:
  143. *       PVmLockMember is not included in this count
  144. *
  145. *
  146. *************************************************************************/
  147. unsigned VMFUNC __CVmLockHbk(
  148. HBK hbk
  149. ) {
  150. PHDB    phdb;
  151.  
  152.    Assert( hbk != NULL);
  153.  
  154.    if (!_fVmInit || (unsigned long)hbk < (unsigned long)_hbkMin || (unsigned long)hbk >= (unsigned long)_hbkMax)
  155.       return 0;
  156.  
  157.    phdb = __PVmLoadVp((VPVOID) hbk, FALSE);
  158.  
  159.    Assert( phdb != NULL );
  160.    Assert( phdb->fFree == FALSE );
  161.  
  162.    if (!phdb || phdb->fFree)
  163.        return 0;
  164.  
  165.    return(phdb->cLock);
  166. }
  167.