home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 534.lha / vmalloc_v1.2 / vlock.c < prev    next >
C/C++ Source or Header  |  1991-08-08  |  2KB  |  81 lines

  1. /*
  2.  * Memory Locking and unlocking functions.
  3.  * VM_Lock :
  4.  *   Check if the lock is 'valid'
  5.  *   'Remove()' it from the reserve list if needed
  6.  *   If the lock is invalid, allocate memory and read the
  7.  *   section from the file.
  8.  *
  9.  * VM_UnLock :
  10.  *   If the lock list contained 10 items, then free the tail.
  11.  *   Move the lock to the reserved list.
  12.  */
  13.  
  14. #include "vm.h"
  15.  
  16. /*
  17.  * To 'Lock' an area of VMEM, you must allocate room for the memory, read the
  18.  * file segment into memory, and return the address of the physical memory.
  19.  *
  20.  * This is further confused by the fact that the lock COULD be part of the 
  21.  * reserved list. If so, then it is removed from the list, and does not need
  22.  * to be processed.
  23.  */
  24. void *
  25. VM_Lock(struct VM_Lock *VM_L) {
  26.    ULONG Err ;
  27.  
  28.    /*
  29.     * Flags == 0 if the lock is flushed.
  30.     */
  31.    if (!( VM_L->VM_Flags & (VM_INUSE|VM_RESERVED)) ) {
  32.       Err = VM_ReadLock(VM_L) ;
  33.       if (Err)
  34.          return NULL ;
  35.       }
  36.    else 
  37.       /*
  38.        * Flags & VM_RESERVED If the lock is in the reserved list.
  39.        */
  40.       if (VM_L->VM_Flags & VM_RESERVED) {
  41.      /*
  42.       * Remove the node from the reserved list.
  43.       */
  44.      Remove((struct Node *) VM_L) ;
  45.      /*
  46.       * Mark the lock as IN_USE.
  47.       */
  48.      VM_L->VM_Flags ^= (VM_INUSE|VM_RESERVED) ;
  49.          }
  50.  
  51.    VM_L->VM_Flags &= ~(VM_READONLY) ;
  52.    return VM_L->VM_Addr ;
  53.    }
  54.  
  55. ULONG
  56. VM_UnLock(struct VM_Lock *VM_L) {
  57.  
  58.    ULONG Err ;
  59.  
  60.    if (VM_L->VM_Flags & VM_INUSE) {
  61.       AddHead( (struct List *) &(VM_L->VM_C->VM_LockList) , (struct Node *) VM_L );
  62.       VM_L->VM_Flags ^= (VM_RESERVED|VM_INUSE) ;
  63.       VM_L->VM_C->VM_NumLocks ++ ;
  64.  
  65.       if (VM_L->VM_C->VM_NumLocks > 10) {
  66.          Err = VM_FlushLock((struct VM_Lock *)
  67.                         (VM_L->VM_C->VM_LockList.mlh_TailPred)) ;
  68.          return Err ;
  69.          }
  70.       }
  71.  
  72.    return 0 ;
  73.    }
  74.  
  75. void
  76. VM_ReadOnly(struct VM_Lock *VM_L) {
  77.  
  78.    if (VM_L->VM_Flags & VM_INUSE)
  79.       VM_L->VM_Flags |= VM_READONLY ;
  80.    }
  81.