Advertisement
ISSOtm

malloc for Game Boy

Jul 1st, 2018
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Chunk header:
  2. ; word - Size of chunk
  3. ; word - Ptr to next chunk (NULL means last chunk). Bit 7 indicates if chunk is in use (set if so)
  4. HEADER_SIZE equ 4
  5.  
  6. ; Allocates a block of memory with the demanded size, using a best-fit strategy.
  7. ; @param bc The size of the block to be allocated.
  8. ; @return C Set if an error occurred.
  9. ; @return hl A pointer to the allocated data. Undefined if C is set
  10. malloc:
  11.     ; Temporarily add header size to requested size
  12.     ld a, c
  13.     add a, HEADER_SIZE
  14.     ld c, a
  15.     adc a, b
  16.     sub c
  17.     ld b, a
  18.  
  19.     ; Find block to split
  20.     ld hl, Heap
  21.     ld de, 0 ; No chunk found
  22. .searchHeap
  23.     ld a, [hli]
  24.     ldh [hChunkSizeLow], a
  25.     ld a, [hli]
  26.     bit 7, a
  27.     jr nz, .skip ; Used chunk
  28.     ; Check if chunk size is sufficient
  29.     cp b
  30.     jr c, .skip
  31.     jr nz, .suitable
  32.     ldh a, [hChunkSizeLow]
  33.     cp c
  34.     jr c, .skip
  35.     jr z, .perfectFit ; Just mark the chunk as used and return
  36.  .suitable
  37.     ; This chunk works, but is it better than the current one?
  38.     dec hl
  39.     dec hl
  40.     ld a, d
  41.     or e
  42.     jr z, .firstChunk ; Anything is better than nothing
  43.     ; Check if this chunk is smaller than the candidate one
  44.     ; Compare high bytes
  45.     inc de
  46.     inc hl
  47.     ld a, [de]
  48.     dec de
  49.     cp [hl]
  50.     dec hl
  51.     jr c, .better
  52.     jr nz, .incThenSkip
  53.     ; Compare low bytes
  54.     ld a, [de]
  55.     cp [hl]
  56.     jr nc, .incThenSkip
  57. .better
  58. .firstChunk
  59.     ld d, h
  60.     ld e, l
  61. .incThenSkip
  62.     inc hl
  63.     inc hl
  64. .skip
  65.     ld a, [hli]
  66.     ld h, [hl]
  67.     ld l, a
  68.     or h
  69.     jr nz, .searchHeap
  70.  
  71.     ; Check if a chunk has been found
  72.     ld a, d
  73.     or e
  74.     jr nz, .chunkFound
  75.     ld a, ENOMEM ; Or whatever else
  76.     ld [wErrno], a
  77.     scf
  78.     ret
  79.  
  80. .perfectFit
  81.     dec hl
  82.     set 7, [hl]
  83.     inc hl
  84.     inc hl
  85.     inc hl
  86.     xor a
  87.     ld [wErrno], a
  88.     ret
  89.  
  90. .chunkFound
  91.  
  92.     ; Set pointer to new block to current block pointer + size
  93.     ld h, d
  94.     ld l, e
  95.  
  96.     ld a, e
  97.     add a, c
  98.     ld e, a
  99.     ld a, d
  100.     adc a, b
  101.     ld d, a
  102.  
  103.     ; Set size of new block to current block size - size; set it to be free
  104.     ld a, [hli]
  105.     sub c
  106.     ld [de], a
  107.     inc de
  108.     ld a, [hld]
  109.     sbc b
  110.     ; bit 7 should be reset, normally...
  111.     ld [de], a
  112.     inc de
  113.  
  114.     ; Set size of the current block to requested size (subtracting header size), and mark it as in use
  115.     ld a, c
  116.     sub HEADER_SIZE
  117.     ld [hli], a
  118.     ld a, b
  119.     sbc 0
  120.     or $80
  121.     ld [hli], a
  122.    
  123.     ; Set next pointer of new block to next pointer of current block
  124.     ld a, [hli]
  125.     ld [de], a
  126.     inc de
  127.     ld a, [hld]
  128.     ld [de], a
  129.  
  130.     ; Set next pointer of current block to point to new block
  131.     ld a, e
  132.     sub HEADER_SIZE - 1
  133.     ld [hli], a
  134.     ld a, d
  135.     sbc 0
  136.     ld [hli], a
  137.  
  138.     xor a
  139.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement