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

  1. ;****************************************************************************
  2. ; Filename: MALLOC.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: PVOID @malloc(ULONG blocksize);
  12. ;    Input: Eax, blocksize - size of block to allocate
  13. ;   Output: pointer to the allocated memory or NULL
  14. ;  Comment: PALallocate allocates a memory block from the global heap memory.
  15. ;           It returns a pointer to the allocated memory block or NULL if not
  16. ;           enough memory is available.
  17. ;****************************************************************************
  18.  
  19.     Include    STDDEF.INC
  20.     Include    "MEMORY.INC"
  21.  
  22.     Codeseg
  23.  
  24. Proc    malloc ,1
  25.         TestZ    Eax                            ; If zero size is requested then exit
  26.         Jz    @@Exit01
  27.         Push    Ebx
  28.         Add    Eax,Size AllocBlock                    ; Add the header size
  29.         Adjust    Eax,MEMALIGN                        ; Align the requested block size
  30.         GetSize Edx,Eax
  31. @@Next01:    Mov    Ebx,[Edx+FreeBlock.NextFree]                ; Get the bit block pointer
  32.         Mov    Ecx,[Ebx+FreeBlock.BlockSize]                ; Get the block size
  33.         Jecxz    @@Next03                        ; If the block size is zero then abort the loop
  34.         Sub    Ecx,Eax                            ; Check the block size and
  35.         Jae    @@Next06                        ; jump if it's big enough
  36. @@Next02:    Mov    Ebx,[Ebx+FreeBlock.NextFree]                ; Get the next free pointer
  37.         Mov    Ecx,[Ebx+FreeBlock.BlockSize]                ; Get the block size
  38.         Jecxz    @@Next03                        ; If the block size is zero then abort loop
  39.         Sub    Ecx,Eax                            ; Check the block size and
  40.         Jae    @@Next06                        ; jump if it's big enough
  41.         Mov    Ebx,[Ebx+FreeBlock.NextFree]                ; Get the next free pointer
  42.         Mov    Ecx,[Ebx+FreeBlock.BlockSize]                ; Get the block size
  43.         Jecxz    @@Next03                        ; If the block size is zero then abort loop
  44.         Sub    Ecx,Eax                            ; Check the size and
  45.         Jb    @@Next02                        ; don't jump if it's big enough
  46.         Jmp    @@Next06
  47.     Align    4
  48. @@Next03:    Add    Edx,Size FreeBlock                    ; Get next pointer from the free pointer list
  49.         Cmp    Edx,Offset BlockTableEnd                ; Reached the end?
  50.         Jne    @@Next01                        ; No, jump and do another lap
  51.         Pop    Ebx
  52.         Clear    Eax                            ; Return a NULL pointer
  53.         Ret
  54.     Align    4
  55. @@Next06:    Cmp    Ecx,Size FreeBlock+MEMALIGN                ; Jump if it can can't split the block
  56.         Jb    @@Next08
  57.         Mov    [Ebx+AllocBlock.BlockSize],Eax                ; Store the new block size
  58.         Add    Eax,Ebx                            ; Calculate a new pointer to the free block
  59.         Mov    Edx,[Ebx+FreeBlock.NextFree]                ; Save the old NextFree pointer
  60.         Mov    [Eax+FreeBlock.BlockSize],Ecx                ; Fix the free header
  61.         Mov    [Eax+FreeBlock.PrevBlock],Ebx                ; Make the free header
  62.         Add    Ecx,Eax                            ; Get pointer to the next block
  63.         Mov    [Ecx+AllocBlock.PrevBlock],Eax
  64.         Mov    Ecx,[Ebx+FreeBlock.PrevFree]
  65.         Mov    [Edx+FreeBlock.PrevFree],Ecx
  66.         Mov    [Ecx+FreeBlock.NextFree],Edx
  67.         Mov    Ecx,[Eax+FreeBlock.BlockSize]
  68.         GetSize    Edx,Ecx
  69.         Mov    Ecx,[Edx+FreeBlock.NextFree]                ; Get the old pointer and insert it into
  70.         Mov    [Eax+FreeBlock.PrevFree],Edx                ; the new free block list
  71.         Mov    [Ecx+FreeBlock.PrevFree],Eax
  72.         Mov    [Ebx+AllocBlock.Ident],ALLOCID                ; Mark the block as allocated
  73.         Mov    [Edx+FreeBlock.NextFree],Eax
  74.         Mov    [Eax+FreeBlock.NextFree],Ecx
  75.         Lea    Eax,[Ebx+Size AllocBlock]                ; Return pointer to to the allocated block
  76.         Pop    Ebx
  77.         Ret
  78.     Align    4
  79. @@Next08:    Mov    Ecx,[Ebx+FreeBlock.NextFree]                ; Remove block from the free pointer list
  80.         Mov    Edx,[Ebx+FreeBlock.PrevFree]
  81.         Mov    [Ebx+AllocBlock.Ident],ALLOCID                ; Mark the block as allocated
  82.         Mov    [Edx+FreeBlock.PrevFree],Ecx
  83.         Mov    [Ecx+FreeBlock.NextFree],Edx
  84.         Lea    Eax,[Ebx+Size AllocBlock]                ; Return pointer to the allocated block
  85.         Pop    Ebx
  86. @@Exit01:    Ret
  87. Endp
  88.  
  89.     End
  90.