home *** CD-ROM | disk | FTP | other *** search
/ Groovy Bytes: Behind the Moon / groovybytes.iso / GROOVY / SND_TOOL / FUNK108A.ZIP / DOS32V30.ZIP / PAL / MALLOC / FREE.ASM < prev    next >
Encoding:
Assembly Source File  |  1995-05-20  |  2.8 KB  |  69 lines

  1. ;****************************************************************************
  2. ; Filename: FREE.ASM
  3. ;   Author: Peter Andersson
  4. ;  Version: 0.0
  5. ;  Created: 1994.12.25
  6. ;  Updated: -
  7. ;****************************************************************************
  8. ; Copyright Peter Andersson, 1994-1995.
  9. ; All rights reserved.
  10. ;****************************************************************************
  11. ; Function: VOID @free(PVOID blockptr);
  12. ;    Input: Eax, blockptr - pointer to the allocated block which is to be
  13. ;           returned to the heap.
  14. ;   Output: nothing
  15. ;  Comment: PALfree removes an added block from the global allocation heap.
  16. ;****************************************************************************
  17.  
  18.     Include    STDDEF.INC
  19.     Include    "MEMORY.INC"
  20.  
  21.     Codeseg
  22.  
  23. Proc    free ,1
  24.         TestZ    Eax                            ; Do not allow any NULL pointers
  25.         Jz    @@Exit01
  26.         Sub    Eax,Size AllocBlock
  27.         Cmp    [Eax+AllocBlock.Ident],ALLOCID                ; Check allocation identifier
  28.         Jne    @@Exit01
  29.         Push    Ebx
  30.         Mov    Ebx,[Eax+AllocBlock.PrevBlock]                ; Get previous block pointer
  31.         Add    Eax,[Eax+AllocBlock.BlockSize]                ; Get next block pointer
  32.         Cmp    [Ebx+AllocBlock.Ident],ALLOCID                ; Test if the block is allocated
  33.         Je    @@Next01                        ; Yes, jump and check the next block
  34.         Mov    Ecx,[Ebx+FreeBlock.PrevFree]                ; Remove the FreeBlock from the free list
  35.         Mov    Edx,[Ebx+FreeBlock.NextFree]
  36.         Cmp    [Eax+AllocBlock.Ident],ALLOCID                ; Test if the next block is allocated
  37.         Mov    [Edx+FreeBlock.PrevFree],Ecx
  38.         Mov    [Ecx+FreeBlock.NextFree],Edx
  39.         Je    @@Next03                        ; Yes, jump and add the block to the free list
  40.         Mov    Ecx,[Eax+FreeBlock.PrevFree]                ; Remove the FreeBlock from the free list
  41.         Mov    Edx,[Eax+FreeBlock.NextFree]
  42.         Add    Eax,[Eax+FreeBlock.BlockSize]                ; Get the next block (should be allocated)
  43.         Mov    [Edx+FreeBlock.PrevFree],Ecx
  44.         Mov    [Ecx+FreeBlock.NextFree],Edx
  45.         Jmp    @@Next03
  46.     Align    4
  47. @@Next01:    Cmp    [Eax+AllocBlock.Ident],ALLOCID                ; Test if the block is allocated
  48.         Mov    Ebx,[Eax+AllocBlock.PrevBlock]                ; Get the current AllocBlock
  49.         Je    @@Next03                        ; Yes, jump and add the block to the free list
  50.         Mov    Ecx,[Eax+FreeBlock.PrevFree]                ; Remove the FreeBlock from the free list
  51.         Mov    Edx,[Eax+FreeBlock.NextFree]
  52.         Add    Eax,[Eax+FreeBlock.BlockSize]                ; Go to the next block (should be allocate)
  53.         Mov    [Edx+FreeBlock.PrevFree],Ecx
  54.         Mov    [Ecx+FreeBlock.NextFree],Edx
  55. @@Next03:    Mov    [Eax+AllocBlock.PrevBlock],Ebx                ; Store pointer to previous block
  56.         Sub    Eax,Ebx                            ; Get total number of free bytes
  57.         Mov    [Ebx+FreeBlock.BlockSize],Eax                ; Store it in the header
  58.         GetSize    Edx,Eax
  59.         Mov    Eax,[Edx+FreeBlock.NextFree]                ; Add it to the free list
  60.         Mov    [Edx+FreeBlock.NextFree],Ebx
  61.         Mov    [Eax+FreeBlock.PrevFree],Ebx
  62.         Mov    [Ebx+FreeBlock.NextFree],Eax
  63.         Mov    [Ebx+FreeBlock.PrevFree],Edx
  64.         Pop    Ebx
  65. @@Exit01:    Ret
  66. Endp
  67.  
  68.     End
  69.