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

  1. /*
  2.  * Memory allocation and freeing functions.
  3.  * VM_AllocMem :
  4.  *   Call the VM_Allocation() function to get a piece of file.
  5.  *   Check if the file requires extension.
  6.  *   Extend the file.
  7.  *   Fill in a lock for the file, and return it.
  8.  *
  9.  * VM_FreeMem :
  10.  *   VM_Free the file record.
  11.  *   Dispose the lock.
  12.  */
  13.  
  14. #include "vm.h"
  15.  
  16. struct VM_Lock *
  17. VM_AllocMem(struct VM_Construct *VM_C, LONG Size) {
  18.    struct VM_Lock *VM_L ;
  19.    struct VM_FreeList *VM_N ;
  20.  
  21.    VM_L = (struct VM_Lock *) TMemAlloc( sizeof(struct VM_Lock), MEMF_PUBLIC) ;
  22.  
  23.    if (!VM_L)
  24.       return NULL ;
  25.  
  26.    VM_N = VM_Allocate(&(VM_C->VM_FreeList),Size) ;
  27.  
  28.    if (!VM_N) {
  29.       FreeTrackedItem(VM_L) ;
  30.       return NULL ;
  31.       }
  32.  
  33.    VM_L->VM_C = VM_C ;
  34.    VM_L->VM_FL = VM_N ;
  35.    VM_L->VM_Flags = 0 ;
  36.  
  37.    return VM_L ;
  38.    }
  39.  
  40. ULONG
  41. VM_FreeMem(struct VM_Lock *VM_L) {
  42.  
  43.    ULONG Err ;
  44.  
  45.    if (VM_L->VM_Flags & VM_INUSE) {
  46.       Err = VM_UnLock(VM_L) ;
  47.       if (Err)
  48.          return -1 ;
  49.       }
  50.  
  51.    if (VM_L->VM_Flags & VM_RESERVED) {
  52.       Err = VM_FlushLock(VM_L) ;
  53.       if (Err)
  54.          return -1 ;
  55.       }
  56.     
  57.    VM_Free(&(VM_L->VM_C->VM_FreeList),VM_L->VM_FL) ;
  58.    FreeTrackedItem(VM_L) ;
  59.  
  60.    return 0;
  61.    }
  62.