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

  1. /*
  2.  * Code to initialise the VMarray. 
  3.  * 
  4.  * 1) Open an empty disk file.
  5.  * 2) Initialise a structure holding a pointer to the free list, and
  6.  *    the file handle.
  7.  * 3) Initialise the free list with the special dummy entry containing a
  8.  *    pointer to the end of the file, and a maximum size of zero.
  9.  */
  10.  
  11. #include "vm.h"
  12.  
  13. struct VM_Construct *
  14. VM_Open(char *VM_Filename) {
  15.    BPTR VM_File ;
  16.    struct VM_Construct *VM_C ;
  17.    struct VM_FreeList *VM_F ;
  18.  
  19.    VM_File = TOpen(VM_Filename,MODE_NEWFILE) ;
  20.    if (!VM_File) 
  21.       return NULL ;
  22.  
  23.    VM_C = (struct VM_Construct *)
  24.           TMemAlloc( sizeof(struct VM_Construct), MEMF_PUBLIC) ;
  25.    if (!VM_C) {
  26.       FreeTrackedItem((void *)VM_File) ;
  27.       return NULL ;
  28.       }
  29.  
  30.    VM_C->VM_File = VM_File ;
  31.    VM_C->VM_FileName = VM_Filename ;
  32.    VM_C->VM_Length = 0 ;
  33.  
  34.    VM_F = (struct VM_FreeList *)
  35.           TMemAlloc( sizeof(struct VM_FreeList), MEMF_PUBLIC) ;
  36.    if (!VM_F) {
  37.       FreeTrackedItem((void *)VM_File) ;
  38.       FreeTrackedItem(VM_C) ;
  39.       return NULL ;
  40.       }
  41.  
  42.    VM_F->VM_Next = NULL ;
  43.    VM_F->VM_Offset = 0 ;
  44.    VM_F->VM_Length = 0 ;
  45.  
  46.    VM_C->VM_FreeList = VM_F ;
  47.    VM_C->VM_NumLocks = 0 ;
  48.    NewList((struct List *) &(VM_C->VM_LockList)) ;
  49.  
  50.    return VM_C ;
  51.    }
  52.  
  53. ULONG
  54. VM_Close(struct VM_Construct *VM_C) {
  55.  
  56.    ULONG Err ;
  57.  
  58.    if (VM_C) {
  59.       if (VM_C->VM_NumLocks) {
  60.      Err = VM_FlushLockList(VM_C) ;
  61.      if (Err)
  62.         return -1 ;
  63.          }
  64.  
  65.       VM_FlushFreeList(&(VM_C->VM_FreeList)) ;
  66.       FreeTrackedItem((void *)VM_C->VM_File) ;
  67.       DeleteFile(VM_C->VM_FileName) ;
  68.       VM_C->VM_File = NULL ;
  69.       FreeTrackedItem(VM_C) ;
  70.       }
  71.    }
  72.