home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / SOFTWARE / LIBS / PMC101.ZIP / LIBSRC.ZIP / BMSQSH.ASM (.txt) < prev    next >
Assembly Source File  |  1994-06-24  |  4KB  |  136 lines

  1. ;──────────────────────────────────────────────────────────────────────────────
  2. ;                               MEMBLOCK Squish
  3. ;──────────────────────────────────────────────────────────────────────────────
  4. ;
  5. ; Borland C++ 4.0 for WIN32 prototype:
  6. ; DWORD __pascal mbsquish(MEMBLOCK *mb);
  7. ;
  8. ; Returns # bytes removed, and modifies MEMBLOCK struct
  9. ;
  10. ; version 0.1
  11. ; - White Shadow -
  12. ;
  13. .386p
  14. Ideal
  15. include "bmmalloc.inc"
  16.  
  17. Public MBSQUISH
  18.  
  19. ;──────────────────────────────────────────────────────────────────────────────
  20. Segment _TEXT byte public use32 'CODE'
  21. Assume  cs:_TEXT, ds:DGROUP
  22.  
  23. ; -- argument stack offsets
  24. arg1 = 0                ; -> MEMBLOCK
  25.  
  26. MBSQUISH:       push ebx
  27. pct = (4)+(1*4)         ; # bytes pushed on stack after last argument
  28.  
  29.                 ;-- load MEMBLOCK info
  30.                 mov  ecx, [esp+pct+arg1]        ; -> MEMBLOCK
  31.                 mov  ebx, [ecx+MEMBLOCK.base]   ; linear adx of MEMBLOCK
  32.                 sub  ebx, [_database]           ; relative ofs to DGROUP
  33.                 mov  edx, [ecx+MEMBLOCK.size]   ; size of memblock
  34.                 add  edx, ebx                   ; relative ofs + size
  35.  
  36.                 ;-- MEMBLOCK uninialized?
  37.                 cmp  edx, ebx
  38.                 je   ExitNull
  39.  
  40. If DebugMode    ;-- check MBSig
  41.                 cmp  [dword ebx], MBSig         ; 4 byte signature
  42.                 jne  BadMBSig                   ; is it there?
  43.                 add  ebx, MBSigSize             ; -> first node
  44. EndIf
  45.  
  46. ;---------------
  47.  
  48. FindLastNode:   ;-- Get Node Size
  49.                 mov  eax, [ebx+MemNode.size]
  50. If DebugMode
  51.                 xor  eax, NodeSigKey
  52.                 cmp  [ebx+MemNode.sig], eax
  53.                 jne  CorruptMB
  54.                 xor  eax, NodeSigKey
  55. EndIf
  56.                 and  eax, 7fffffffh
  57.  
  58.                 ;-- Last Node?
  59.                 add  eax, ebx
  60.                 add  eax, size MemNode
  61.                 cmp  eax, edx
  62.                 je   FoundLastNode
  63.                 ja   CorruptMB
  64.  
  65.                 ;-- No, continue
  66.                 mov  ebx, eax
  67.                 jmp  short FindLastNode
  68.  
  69. ;---------------
  70.  
  71. FoundLastNode:
  72. ; ebx -> last MemNode
  73.                 ;-- Is last node allocated?
  74.                 mov  eax, [ebx+MemNode.size]
  75.                 or   eax, eax
  76.                 js   ExitNull
  77.  
  78.                 ;-- Is the last node also the first node?
  79.                 mov  edx, [ecx+MEMBLOCK.base]   ; linear adx of MEMBLOCK
  80.                 sub  edx, [_database]           ; relative ofs to DGROUP
  81. If DebugMode
  82.                 add  edx, MBSigSize
  83. EndIf
  84.                 cmp  ebx, edx
  85.                 jne  NotFirstNode
  86.  
  87. ;---------------
  88.  
  89.                 ;-- Make only (and free) MemNode's size = 4
  90.                 mov  [ebx+MemNode.size], 4
  91. If DebugMode
  92.                 mov  [ebx+MemNode.sig], NodeSigKey XOR 4
  93. EndIf
  94.  
  95.                 ;-- Get size freed  (eax >= 4)
  96.                 sub  eax, 4
  97.                 jmp  short ReduceMB
  98.  
  99. ;---------------
  100.  
  101. NotFirstNode:   ;-- Kill last MemNode
  102.                 mov  [ebx+MemNode.size], 0
  103.                 add  eax, size MemNode
  104.  
  105. ;---------------
  106.  
  107. ReduceMB:       ;-- Reduce MEMBLOCK size
  108.                 sub  [ecx+MEMBLOCK.size], eax
  109.  
  110. ;---------------
  111.  
  112. Exit:           pop  ebx
  113.                 ret  4                          ;1 arg
  114.  
  115. ;---------------
  116.  
  117. BadMBSig:
  118. ExitNull:       xor  eax, eax
  119.                 jmp  short Exit
  120.  
  121. ;---------------
  122.  
  123. CorruptMB:
  124. If DebugMode    ;-- Invalidate MEMBLOCK
  125.                 mov  ecx, [esp+pct+arg1]        ; ->MEMBLOCK
  126.                 mov  ecx, [ecx+MEMBLOCK.base]   ; linear adx of MEMBLOCK
  127.                 sub  ecx, [_database]           ; realtive ofs to DGROUP
  128.                 mov  [dword ecx], MBSigInvl
  129. EndIf
  130.                 jmp  short ExitNull
  131.  
  132.  
  133. ;──────────────────────────────────────────────────────────────────────────────
  134. EndS            _TEXT
  135. End
  136.