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

  1. /*
  2.  * Code to Manage the FreeList.
  3.  * 
  4.  * a) Allocate
  5.  *     1) Scan for the smallest block larger than the request.
  6.  *     2) Fix or delete the FreeList node.
  7.  *     3) Create an Allocation node.
  8.  *     4) Return the allocation node.
  9.  * b) Free
  10.  *     1) Scan the free list for an adjacent node.
  11.  *     2) If found
  12.  *          i) Adjust adjacent node offset/length
  13.  *     3) If NOT found
  14.  *          i) Create a FreeList entry
  15.  *         ii) Link the FreeList entry into the FreeList in a position
  16.  *             relative to its address in the file.
  17.  */
  18.  
  19. #include "vm.h"
  20.  
  21. struct VM_FreeList *
  22. VM_Allocate(struct VM_FreeList **VM_F, long Size) {
  23.    struct VM_FreeList **VM_P = NULL ;
  24.    struct VM_FreeList *VM_N = NULL ;
  25.    ULONG largest = 0xFFFFFFFF ;
  26.    ULONG len ;
  27.  
  28.    while (*VM_F) {
  29.       len = (*VM_F)->VM_Length ;
  30.       if (!len) {
  31.      VM_N = (struct VM_FreeList *)
  32.             TMemAlloc(sizeof(struct VM_FreeList),MEMF_PUBLIC) ;
  33.      if (!VM_N)
  34.         return NULL ;
  35.      if (VM_P) {
  36.         VM_N->VM_Offset = (*VM_P)->VM_Offset ;
  37.         VM_N->VM_Length = Size ;
  38.         (*VM_P)->VM_Offset += Size ;
  39.         (*VM_P)->VM_Length -= Size ;
  40.         }
  41.      else {
  42.         VM_N->VM_Offset = (*VM_F)->VM_Offset ;
  43.         VM_N->VM_Length = Size ;
  44.         (*VM_F)->VM_Offset += Size ;
  45.         }
  46.      return VM_N ;
  47.          }
  48.       if (len == Size) {
  49.          VM_N = *VM_F ;
  50.      *VM_F = VM_N->VM_Next ;
  51.      return VM_N ;
  52.          }
  53.       if (len < largest) {
  54.      VM_P = VM_F ;
  55.      largest = len ;
  56.          }
  57.       VM_F = &((*VM_F)->VM_Next) ;
  58.       }
  59.    }
  60.  
  61. void
  62. VM_Free(struct VM_FreeList **VM_F, struct VM_FreeList *VM_A) {
  63.    struct VM_FreeList *VM_N ;
  64.    ULONG offset ;
  65.    ULONG end ;
  66.    ULONG here = VM_A->VM_Offset ;
  67.    ULONG len = VM_A->VM_Length ;
  68.    ULONG there = here + len ;
  69.  
  70.    while (*VM_F) {
  71.  
  72.       VM_N = (*VM_F)->VM_Next ;
  73.  
  74.       offset = (*VM_F)->VM_Offset ;
  75.       end = offset + (*VM_F)->VM_Length ;
  76.  
  77.       if (end == here) {
  78.      if (VM_N && (VM_N->VM_Offset == there)) {
  79.         if (VM_N->VM_Length)
  80.            (*VM_F)->VM_Length += (len + VM_N->VM_Length );
  81.         (*VM_F)->VM_Next = VM_N->VM_Next ;
  82.         FreeTrackedItem(VM_N) ;
  83.         }
  84.      else {
  85.         if (VM_N)
  86.            (*VM_F)->VM_Length += len ;
  87.         }
  88.      FreeTrackedItem(VM_A) ;
  89.      return ;
  90.          }
  91.       if (offset == there) {
  92.      (*VM_F)->VM_Offset -= len ;
  93.      if (VM_N)
  94.         (*VM_F)->VM_Length += len ;
  95.      FreeTrackedItem(VM_A) ;
  96.      return ;
  97.          }
  98.       if (offset > here) {
  99.          VM_A->VM_Next = *VM_F ;
  100.      *VM_F = VM_A ;
  101.      return ;
  102.          }
  103.       VM_F = &((*VM_F)->VM_Next) ;
  104.       }
  105.    }
  106.  
  107. void
  108. VM_FlushFreeList(struct VM_FreeList **VM_F) {
  109.  
  110.    /*
  111.     * Dispose of the free list attached to the VM_Construct structure.
  112.     */
  113.  
  114.    struct VM_FreeList *VM_A ;
  115.  
  116.    while (*VM_F) {
  117.  
  118.       /*
  119.        * Unlink the first node.
  120.        */
  121.       VM_A = *VM_F ;
  122.       *VM_F = VM_A->VM_Next ;
  123.  
  124.       /*
  125.        * Free It.
  126.        */
  127.       FreeTrackedItem(VM_A) ;
  128.       }
  129.  
  130.    }
  131.