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

  1. ;****************************************************************************
  2. ; Filename: RESIZE.ASM
  3. ;   Author: Peter Andersson
  4. ;  Version: 0.0
  5. ;  Created: 1995.01.26
  6. ;  Updated: -
  7. ;****************************************************************************
  8. ; Copyright Peter Andersson, 1994-1995.
  9. ; All rights reserved.
  10. ;****************************************************************************
  11. ; Function: PVOID @resize(PVOID blockptr,ULONG blocksize);
  12. ;    Input: Eax, blockptr - pointer to the block to be resized
  13. ;           Edx, blocksize - new size of the block
  14. ;   Output: pointer to resized block or NULL if it failed
  15. ;  Comment: PALresize tries to resize a previously allocated block. If it
  16. ;           fails you must allocate a new block and copy the block to the
  17. ;           new block. OBSERVE: The new block size can't be zero, use
  18. ;           @PALfree to release a block!
  19. ;****************************************************************************
  20.  
  21.     Include    STDDEF.INC
  22.     Include    "MEMORY.INC"
  23.  
  24.     Codeseg
  25.  
  26. Proc    resize ,2
  27.         TestZ    Eax                            ; Do not allow any NULL pointers...
  28.         Jz    @@Exit01
  29.         TestZ    Edx                            ; ...and do not allow any zero length requests
  30.         Jz    @@Exit01
  31.         Push    Ebx
  32.         Lea    Ebx,[Eax-Size AllocBlock]
  33.         Cmp    [Ebx+AllocBlock.Ident],ALLOCID                ; Check the allocation block identifier
  34.         Jne    @@Exit02
  35.         Adjust    Edx,MEMALIGN                        ; Align    the request length
  36.         Add    Edx,Size AllocBlock                    ; Add the block header size to the AllocBlock
  37.         Mov    Ecx,[Ebx+AllocBlock.BlockSize]                ; Get the current block size
  38.         Sub    Edx,Ecx                            ; and check if it is big enough
  39.         Ja    @@Next01                        ; Jump if too small
  40.         Neg    Edx
  41.         Cmp    Edx,Size FreeBlock+MINBLOCK                ; Check if it can be split?
  42.         Jb    @@Next02                        ; Jump if can't be split
  43.         Add    Ecx,Ebx                            ; Get the next block
  44.         Cmp    [Ecx+AllocBlock.Ident],ALLOCID                ; Check if the next block is allocated
  45.         Je    @@Next03                        ; Jump if it is allocated
  46.         Mov    Eax,[Ecx+FreeBlock.NextFree]                ; Remove the block from the free list
  47.         Mov    Ebx,[Ecx+FreeBlock.PrevFree]
  48.         Mov    [Eax+FreeBlock.PrevFree],Ebx
  49.         Mov    [Ebx+FreeBlock.NextFree],Eax
  50.         Mov    Ebx,[Ecx+FreeBlock.PrevBlock]
  51.         Mov    Eax,[Ebx+AllocBlock.BlockSize]
  52.         Mov    Ecx,[Ecx+FreeBlock.BlockSize]
  53.         Sub    Eax,Edx
  54.         Add    Ecx,Edx
  55.         Jmp    @@Next04
  56.     Align    4
  57. @@Next03:    Mov    Eax,[Ebx+FreeBlock.BlockSize]
  58.         Sub    Eax,Edx
  59.         Mov    Ecx,Edx
  60. @@Next04:    Mov    [Ebx+AllocBlock.BlockSize],Eax
  61.         Add    Eax,Ebx
  62.         Mov    [Eax+FreeBlock.PrevBlock],Ebx
  63.         Mov    [Eax+Ecx+AllocBlock.PrevBlock],Eax
  64.         Mov    [Eax+FreeBlock.BlockSize],Ecx                ; Fix header
  65.         GetSize    Edx,Ecx
  66.         Mov    Ecx,[Edx+FreeBlock.NextFree]                ; Add it to the free list
  67.         Mov    [Edx+FreeBlock.NextFree],Eax
  68.         Mov    [Eax+FreeBlock.PrevFree],Edx
  69.         Mov    [Eax+FreeBlock.NextFree],Ecx
  70.         Mov    [Ecx+FreeBlock.PrevFree],Eax
  71. @@Next02:    Lea    Eax,[Ebx+Size AllocBlock]
  72.         Pop    Ebx
  73.         Ret
  74.     Align    4
  75. @@Next01:    Add    Ecx,Ebx
  76.         Cmp    [Ecx+AllocBlock.Ident],ALLOCID                ; Check the next block's identifier
  77.         Je    @@Exit02                        ; Jump if it is allocated
  78.         Cmp    Edx,[Ecx+AllocBlock.BlockSize]                ; Check the free block's size
  79.         Jb    @@Exit02                        ; Jump if the block wasn't big enough
  80.         Mov    Ebx,[Ecx+FreeBlock.NextFree]                ; Remove the block from the free list
  81.         Mov    Eax,[Ecx+FreeBlock.PrevFree]
  82.         Mov    [Ebx+FreeBlock.PrevFree],Eax
  83.         Mov    [Eax+FreeBlock.NextFree],Ebx
  84.         Mov    Ebx,[Ecx+FreeBlock.PrevBlock]
  85.         Mov    Ecx,[Ecx+FreeBlock.BlockSize]
  86.         Mov    Eax,[Ebx+AllocBlock.BlockSize]
  87.         Add    Eax,Edx
  88.         Sub    Ecx,Edx
  89.         Jmp    @@Next04
  90.     Align    4
  91. @@Exit02:    Clear    Eax
  92.         Pop    Ebx
  93. @@Exit01:    Ret
  94. Endp
  95.  
  96.     End
  97.  
  98.