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

  1. /*
  2.  * reserved Lock flushing routines.
  3.  */
  4.  
  5. #include "vm.h"
  6.  
  7. /*
  8.  * Flush the lock. This involves removing it from the reserved list (if it is
  9.  * on it) locating the file position (extending the file if needed) and writing
  10.  * the memory segment to the file.
  11.  *
  12.  * This action is altered only if the lock is marked READ_ONLY, in which case
  13.  * the file write is skipped.
  14.  *
  15.  */
  16. ULONG
  17. VM_FlushLock (struct VM_Lock *VM_L) {
  18.    BPTR VFile = VM_L->VM_C->VM_File ;
  19.    ULONG Diff ;
  20.    ULONG Err ;
  21.    char *VBuf ;
  22.  
  23.    if (!(VM_L->VM_Flags & VM_READONLY)) {
  24.       if (VM_L->VM_C->VM_Length >= VM_L->VM_FL->VM_Offset) {
  25.          Err = Seek(VFile,VM_L->VM_FL->VM_Offset, OFFSET_BEGINNING) ;
  26.          if (Err)
  27.             return -1 ;
  28.          }
  29.       else {
  30.          Err = Seek(VFile,VM_L->VM_C->VM_Length, OFFSET_BEGINNING) ;
  31.          if (Err)
  32.             return -1 ;
  33.  
  34.          Diff = VM_L->VM_FL->VM_Offset - VM_L->VM_C->VM_Length ;
  35.          if (Diff > 0) {
  36.             VBuf = (char *) TMemAlloc( Diff, MEMF_PUBLIC | MEMF_CLEAR ) ;
  37.  
  38.             if (!VBuf)
  39.                return -1 ;
  40.  
  41.             Err = Write(VFile,VBuf,Diff) ;
  42.             FreeTrackedItem(VBuf) ;
  43.  
  44.             if (Err < 0)
  45.                return -1 ;
  46.  
  47.             VM_L->VM_C->VM_Length += Diff + VM_L->VM_FL->VM_Length ;
  48.             }
  49.          }
  50.  
  51.       Err = Write(VFile,VM_L->VM_Addr, VM_L->VM_FL->VM_Length) ;
  52.  
  53.       if (Err < 0)
  54.          return -1 ;
  55.       }
  56.  
  57.    if (VM_L->VM_Flags & VM_RESERVED) {
  58.       VM_L->VM_C->VM_NumLocks -- ;
  59.       Remove( (struct Node *) VM_L );
  60.       }
  61.  
  62.    VM_L->VM_Flags &= ~(VM_INUSE | VM_RESERVED) ;
  63.    FreeTrackedItem(VM_L->VM_Addr) ;
  64.    return 0 ;
  65.    }
  66.  
  67. ULONG
  68. VM_FlushLockList(struct VM_Construct *VM_C) {
  69.  
  70.    ULONG Err ;
  71.  
  72.    while ( VM_C->VM_NumLocks ) {
  73.       Err = VM_FlushLock((struct VM_Lock *)(VM_C->VM_LockList.mlh_TailPred)) ;
  74.       if (Err)
  75.          return -1 ;
  76.       }
  77.  
  78.    return 0 ;
  79.    }
  80.  
  81. ULONG
  82. VM_ReadLock(struct VM_Lock *VM_L) {
  83.    ULONG Err ;
  84.    BPTR VFile = VM_L->VM_C->VM_File ;
  85.  
  86.    /*
  87.     * Allocate the new memory and store the pointer in the lock.
  88.     */
  89.    VM_L->VM_Addr = (void *) TMemAlloc(VM_L->VM_FL->VM_Length,
  90.                                          MEMF_PUBLIC|MEMF_CLEAR) ;
  91.    if (VM_L->VM_Addr) {
  92.       /*
  93.        * If the file segment exists (Ie, file length > File Offset) then seek
  94.        * to the correct place, and read the file.
  95.        */
  96.       if (VM_L->VM_C->VM_Length >= VM_L->VM_FL->VM_Offset) {
  97.          Err = Seek(VFile,VM_L->VM_FL->VM_Offset, OFFSET_BEGINNING) ;
  98.          if (Err) {
  99.             FreeTrackedItem(VM_L->VM_Addr) ;
  100.             return NULL ;
  101.             }
  102.          Err = Read(VFile,VM_L->VM_Addr, VM_L->VM_FL->VM_Length) ;
  103.          if (Err < 0) {
  104.             FreeTrackedItem(VM_L->VM_Addr) ;
  105.             return NULL ;
  106.             }
  107.          }
  108.       /*
  109.        * Flags == IN_USE if the lock is in use.
  110.        */
  111.       VM_L->VM_Flags |= VM_INUSE ;
  112.  
  113.       return 0 ;
  114.       }
  115.    else
  116.       return -1 ;
  117.    }
  118.