
freeroutine:    tst.l    D0
            beq.b      .exit
           movem.l    D3/A2,-(A7)
           move.l     A1,D1
           moveq.l    #-8,D3
           and.l      D3,D1
           exg.l      D1,A1
           sub.l      A1,D1
           add.l      D1,D0
           addq.l     #7,D0
           and.l      D3,D0
            beq.b      9$
           lea        ML_ME(A0),A2
           move.l     (A2),D3     ; mh_Node
            beq.b      5$
2$:        cmpa.l     D3,A1
            bls.b      3$
           movea.l    D3,A2
           move.l     (A2),D3
            bne.b      2$
          bra.b      4$

3$:         beq.b      .alreadyfree
4$:        moveq.l    #16,D1
           add.l      A0,D1
           cmp.l      A2,D1
            beq.b      5$
           move.l     4(A2),D3
           add.l      A2,D3
           cmp.l      A1,D3
            beq.b      6$
            bhi.b      .corrupt
5$:        move.l     (A2),(A1)
           move.l     A1,(A2)
           move.l     D0,4(A1)
          bra.b      7$

6$:        add.l      D0,4(A2)
           movea.l    A2,A1
7$:        tst.l      (A1)
            beq.b      8$
           move.l     4(A1),D3
           add.l      A1,D3
           cmp.l      (A1),D3
            bhi.b      .corrupt
            bne.b      8$
           movea.l    (A1),A2
           move.l     (A2),(A1)
           move.l     4(A2),D3
           add.l      D3,4(A1)
8$:        add.l      D0,mh_Free(A0)
9$:        movem.l    (A7)+,D3/A2
.exit:      rtS
; display alert -> Freeing memory that is already free
.alreadyfree:       movem.l    (A7)+,D3/A2
           move.l     D7,-(A7)
           move.l     #$1000009,D7
            jsr        _LVOAlert(A6)
           move.l     (A7)+,D7
          rtS
; display alert -> Corrupt memory list detected in FreeMem
; strange routine: display alert and continue freeing memory, changing byteSize
.corrupt:       move.l     #$81000005,D7
            jsr        _LVOAlert(A6)
           move.l     A1,D1
            beq.b      exitfreemem
           move.l     -(A1),D0     ; why this?
; FreeMem(memoryBlock,byteSize)(a1,d0)
FreeMem:    move.l     A1,D1
            beq.b      exitfreemem
           lea        MemList(A6),A0
           addq.b     #1,TDNestCNT(A6)    ; -> forbid
.C:    movea.l    (A0),A0              ; points to memlist node
           tst.l      (A0)             ; test MemHeader
            beq.b      .invalid        ; if null invalid
           cmpa.l     mh_Lower(A0),A1  ; here we will search in the list for the wanted MemHeader
            bcs.b      .C              ; this should be equal to -> bgt.b .c
           cmpa.l     mh_Upper(A0),A1       ;
            bcc.b      .C              ; -> ble.b .c
            bsr.w      freeroutine     ;
           jmp      _LVOPermit(a6)

; display alert -> Memory header not located. [ Usually an invalid address passed to FreeMem() ]
.invalid:    jsr      _LVOPermit(a6)
           move.l     D7,-(A7)
           move.l     #$100000F,D7
            jsr        _LVOAlert(A6)
           move.l     (A7)+,D7
exitfreemem:  rtS

