home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / nasm20b / nasm_src / nmalloc.s < prev    next >
Text File  |  1993-01-19  |  6KB  |  167 lines

  1. ; ***** DON'T KNOW WHETHER THIS WORKS. WAS NEVER USED!! *****
  2. ; ----------------------------------------------------------------------
  3. ;                   Copyright (C) 1991 by Natürlich!
  4. ;                      This file is copyrighted!
  5. ;                Refer to the documentation for details.
  6. ; ----------------------------------------------------------------------
  7. ;                 Nothing beats a selfwritten malloc....
  8. ; HEAD -> [used.w][size.w][next.l] -> ....
  9. ; This mallocer doesn't really free anything back to the OS. Why should
  10. ; we ? For all programs that are expected to run in a matter of seconds
  11. ; not hours, the overhead isn't justified.
  12. ; Leaves in the worst case MANY bytes of fragmented OS space unused
  13. ;    This is only a crutch for GEMDOSes fucked up memory manager
  14. ; ----------------------------------------------------------------------
  15.          .export  malloc
  16.          .export  free
  17.  
  18. BLOCKSIZE   equ   $6008                ;; must be within 15 bits
  19. GBLOCK      equ   $03
  20.  
  21.  
  22.          .code
  23. failed:
  24.          sub.l    a0,a0
  25.          bra      done
  26.  
  27. malloc:
  28.          move.w   d0,1
  29.          move.l   a3,-(a7)             ;; save two regs
  30.          move.l   d3,-(a7)
  31.  
  32.          move.l   d0,d3                ;; try to alloc 0 ??
  33.          beq.b    failed               ;; that's stupid
  34.          bmi.b    failed               ;; negative is wrong also
  35.          
  36.          cmpi.l   #BLOCKSIZE-$100,d0   ;; more than we can handle
  37.          ble.b    goodenough           ;; then give it to GEMDOS
  38.  
  39.          addq.l   #8,d0                ;; so this is too big for us
  40.          move.l   d0,-(a7)             ;; use MALLOC from GEMDOS
  41.          move.w   #$48,-(a7)
  42.          trap     #1
  43.          addq.l   #6,a7
  44.          tst.l    d0                   ;; Enough memory ??
  45.          beq.b    failed               ;; njet ->
  46.          move.l   d0,a0                ;; else mark it as
  47.          move.w   #GBLOCK,(a0)         ;; being a GEMDOS block
  48.          addq.l   #8,a0                ;; skip header
  49.          bra.b    done                 ;; and return
  50.  
  51. goodenough:
  52.          lea      head,a1              ;; get head pointer
  53.          bra.b    next
  54.  
  55. loop:
  56.          tst.w    (a1)                 ;; block in USE ?
  57.          bne.b    next                 ;; yes ->next
  58.          cmp.l    (a1),d0              ;; size OK ?  (First fit)
  59.          ble.b    ok
  60. next:
  61.          lea      4(a1),a3             ;; save address of NEXT pointer
  62.          move.l   (a3),a1              ;; get NEXT pointer
  63.          move.l   a1,d1                ;; end of list
  64.          bne.b    loop                 ;; da nada ---^
  65.  
  66. alloc:
  67.          pea      BLOCKSIZE
  68.          move.w   #$48,-(a7)
  69.          trap     #1
  70.          addq.l   #6,a7
  71.          tst.l    d0
  72.          beq.b    failed               ;; not enough room ---V
  73.  
  74.          move.l   d0,(a3)              ;; initialize header
  75.          move.l   d0,a1                ;; of memory block
  76.          move.l   #BLOCKSIZE-8,(a1)    ;; actual block - header
  77.          clr.l    4(a1)                ;; clear next pointer
  78.  
  79. ok:
  80.          lea      8(a1),a0             ;; get memory area anyway
  81.          move.l   (a1),d1              ;; get block size
  82.          sub.l    d3,d1                ;; subtract #bytes
  83.          subq.l   #8,d1                ;; less the header
  84.          cmpi.w   #4,d1                ;; no split for less than 4
  85.          blt.b    success              ;; then we are thru!!
  86.  
  87.          move.l   d3,(a1)+             ;; mosey on up to NEXT field
  88.          lea      4(d3,a1),a3          ;;       DO THE SPLIT
  89.          move.l   (a1),4(a3)           ;; save old NEXT in new
  90.          move.l   a3,(a1)              ;; save new as NEXT in old
  91.          move.l   d1,(a3)              ;; save SIZE and clear USED
  92.  
  93. success:
  94.          st       -8(a0)               ;; mark as used
  95. done:
  96.          move.l   (a7)+,d3
  97. fdone:
  98.          move.l   (a7)+,a3
  99.          rts
  100.  
  101.  
  102. free:
  103.          move.l   a3,-(a7)             ;; save a reg
  104.  
  105.          move.l   a0,d0                ;; 0 pointer (MAYBE WE SHOULD CRASH)
  106.          beq.b    fdone                ;; but we don't
  107.          
  108.          lea      -8(a0),a3            ;; get header
  109.          move.w   (a3),d0
  110.          beq.b    fdone                ;; LUSER tried to free twice
  111.          subq.w   #GBLOCK,d0           ;; Was it a bona fide GEMDOS block ?
  112.          beq.b    yup                  ;; then we DO free ---V
  113.  
  114.          clr.w    (a3)                 ;; mark as unused
  115.          move.l   (a3),d1              ;; get size in D1
  116.          add.l    a3,d1                ;; add with current pointer
  117.          addq.l   #8,d1                ;; add header len
  118.          move.l   4(a3),a1             ;; get NEXT pointer
  119.          cmpa.l   d1,a1                ;; curr + #header + size == NEXT ??
  120.          bne.b    nojoin               ;; NO join impossible
  121.          tst.w    (a1)                 ;; next block free  ?
  122.          bne.b    nojoin               ;; no --V
  123.          bsr.b    join_adjacent
  124.  
  125. nojoin:
  126.          lea      head,a1              ;; get head (CHK LFT NEIGHBOR)
  127.  
  128. loop2a:
  129.          move.l   4(a1),a1             ;; get next pointer address
  130.          move.l   a1,d1                ;; end of list
  131.          beq.b    fdone
  132.  
  133.          move.l   (a1),d1              ;; get size in A2
  134.          add.l    a1,d1                ;; add with current pointer
  135.          addq.l   #8,d1                ;; add header len
  136.          cmpa.l   d1,a3                ;; points to us ??
  137.          bne.b    loop2a               ;; yes -> done
  138.  
  139.          bsr.b    join_adjacent        ;; USED must have been CLEAR 
  140.          bra.b    fdone                ;; for this to work
  141.  
  142. yup:
  143.          move.l   a0,-(a7)             ;; free that sucker
  144.          move.w   #$49,-(a7)
  145.          trap     #1
  146.          addq.l   #6,a7
  147.          bra.b    fdone
  148.  
  149.  
  150.  
  151. join_adjacent:
  152.          move.l   (a3),d0              ;; get SIZE of LEFT guy
  153.          add.l    (a1),d0              ;; add sizes of RITE to it
  154.          addq.l   #8,d0                ;; don't forget the header
  155.          move.l   d0,(a3)              ;; write in LEFT
  156.          move.l   4(a1),4(a3)          ;; copy RITE NEXT pointer to LEFT
  157.          rts
  158.  
  159.  
  160.          .data
  161. head:
  162.          .dc.w    $FFFF                ;; used internally flag
  163.          .dc.w    $0000                ;; no size
  164. headnext:
  165.          .dc.l    0                    ;; points nowhere
  166.  
  167.